testa 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODMxYTU4YjliMDE5YjFiNzI5Nzc3NDU0YzAyMGExZDZjNmE3NGRkNA==
5
+ data.tar.gz: !binary |-
6
+ ZmJmYjFmMTIzN2MxYmYyZTYwNWJlZDA2NTBlNjkwYWIzYmQ5ZWNkOQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OWYxMDgwOTMyNDVlOTU4OGM3NGQ0NDYxZTNlN2NiZmEyZmM4ZWQwZjAyZDg5
10
+ ODA0ZWZkMDkzN2QwMGQzMjU0N2M5YWRkNDE4NzMyN2UxYmYwODA2NDFhOWI5
11
+ ZDljZGU1NWQ2MWFmMGU0NzNhZThkZDlkYmQwYWI5ZDgyMTQxZDA=
12
+ data.tar.gz: !binary |-
13
+ NjA5YjM3MTdmYjM2YjhlODg5ZjNiN2ZhMWVkOWQ1M2ViNTNhOTMyZmNlMmFk
14
+ NTJjZWU1MjE0Yjg4MWU1NGFlMzhiZTUwYjRjNmRmNjA0MjBjOTcwYmRhMWNm
15
+ NDBkOTJjMjBmOWQyM2FmYjg0MmRkNTIxY2U2MjQ0YjZiN2YyZWQ=
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in testa.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 wenjun.yan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Testa
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'testa'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install testa
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/testa/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new {|t|
5
+ # t.libs << 'lib'
6
+ t.test_files = FileList['tests/**/*']
7
+ }
8
+
9
+ desc 'Run tests'
10
+ task :default => :test
@@ -0,0 +1,183 @@
1
+ require "testa/version"
2
+
3
+ module Testa
4
+
5
+ # Create a test
6
+ #
7
+ # description - {String} test's description string. Default is nil
8
+ # options - {Hash} meta data
9
+ # block - {Block} test code
10
+ def test description=nil, options={}, &block
11
+ location = caller(0)[1].split(":").tap(&:pop).join ":"
12
+ Testa.tests << Test.new(location, description, options, &block)
13
+ end
14
+
15
+ class << self
16
+
17
+ # Configuration
18
+ #
19
+ # :matchers - {Array of Module} module(s) containing assertion helpers
20
+ # :reporter - {Object} whose class inherits ReporterBase
21
+ # :filters - {Array of Callable} filter out some tests
22
+ def config
23
+ @config ||= {:matchers => [Matcher],
24
+ :reporter => Reporter.new,
25
+ :filters => []}
26
+ end
27
+
28
+ def run
29
+ Testa::Context.send(:include, *config[:matchers])
30
+ reporter = config[:reporter]
31
+ runnable.each { |t| reporter.after_each(t.call) }
32
+ reporter.after_all(results)
33
+ results.none? { |r| [:error, :failed].include? r.status }
34
+ end
35
+
36
+ def run!
37
+ exit(run ? 0 : 1)
38
+ end
39
+
40
+ def tests
41
+ @tests ||= []
42
+ end
43
+
44
+ protected
45
+
46
+ def runnable
47
+ @_tests ||= config[:filters].inject(tests) {|ts, f| f.call ts}
48
+ end
49
+
50
+ def results
51
+ runnable.map(&:result).compact
52
+ end
53
+ end
54
+
55
+ class Failure < StandardError; end
56
+ class Context < Object; end
57
+ class Result < Struct.new(:test, :status, :exception); end
58
+
59
+ class Test
60
+ attr_accessor :description, :result, :location, :options
61
+
62
+ def initialize location, description, options={}, &block
63
+ @location = location
64
+ @description = description
65
+ @options = options
66
+ @block = block
67
+ @result = nil
68
+ end
69
+
70
+ def call
71
+ @result ||= Result.new self, *_call
72
+ end
73
+
74
+ def in_context &block
75
+ (@context ||= Context.new).instance_eval &block
76
+ end
77
+
78
+ private
79
+
80
+ def _call
81
+ unless @block
82
+ :todo
83
+ else
84
+ begin
85
+ in_context &@block
86
+ rescue Failure => e
87
+ [:failed, e]
88
+ rescue => e
89
+ [:error, e]
90
+ else
91
+ :passed
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ # Reporter object print out test result.
98
+ # It should have the following methods:
99
+ #
100
+ # #after_each(result)
101
+ # #after_all(results)
102
+ class Reporter
103
+ CHARS = {:passed => ".",
104
+ :failed => "F",
105
+ :error => "E",
106
+ :todo => "*"}
107
+
108
+ def initialize(out=nil)
109
+ @out = out || $stdout
110
+ @stat = {:passed => 0,
111
+ :failed => 0,
112
+ :error => 0,
113
+ :todo => 0}
114
+ end
115
+
116
+ def after_each(result)
117
+ return unless result
118
+ @stat[result.status] += 1
119
+ @out.print CHARS[result.status]
120
+ end
121
+
122
+ def after_all(results)
123
+ @out.puts
124
+
125
+ results.each {|result|
126
+ case result.status
127
+ when :failed, :error
128
+ @out.puts
129
+ @out.puts "[#{result.status.upcase}] #{result.test.location}",
130
+ (result.test.description || "*NO DESCRIPTION*")
131
+ @out.puts "\t#{result.exception.message}"
132
+ @out.puts result.exception.backtrace.reject {|m| m[__FILE__] }
133
+
134
+ when :todo
135
+ @out.puts
136
+ @out.puts "[#{result.status.upcase}] #{result.test.location}",
137
+ result.test.description || "*NO DESCRIPTION*"
138
+ end
139
+ }
140
+
141
+ @out.puts
142
+ @out.puts " PASSED: #{@stat[:passed]}"
143
+ @out.puts " FAILED: #{@stat[:failed]}"
144
+ @out.puts " ERROR: #{@stat[:error]}"
145
+ @out.puts " TODO: #{@stat[:todo]}"
146
+ @out.puts " TOTAL: #{@stat.values.inject(:+)}"
147
+ @out.puts
148
+ end
149
+ end
150
+
151
+ # Assertion methods
152
+ #
153
+ # Assertion method should raise Testa::Failure if assertion failed
154
+ module Matcher
155
+ def ok
156
+ yield or fail!
157
+ end
158
+
159
+ def error(class_or_message=nil, message=nil)
160
+ ok {
161
+ begin
162
+ yield
163
+ rescue => e
164
+ return true unless class_or_message
165
+ if message
166
+ e.class == class_or_message && e.message[message]
167
+ else
168
+ class_or_message.is_a?(Class) ?
169
+ e.class == class_or_message :
170
+ e.message[class_or_message]
171
+ end
172
+ else
173
+ false
174
+ end
175
+ }
176
+ end
177
+
178
+ def fail!
179
+ raise Failure
180
+ end
181
+ end
182
+
183
+ end
@@ -0,0 +1,3 @@
1
+ module Testa
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,61 @@
1
+ include Testa
2
+
3
+ class MyError < StandardError; end
4
+
5
+ test("matcher#ok pass") {
6
+ ok { true }
7
+ }
8
+
9
+ test("matcher#ok fail when falsy value returned") {
10
+ ok { false }
11
+ }
12
+
13
+ test("matcher#error pass 1") {
14
+ error { raise MyError, "error message!" }
15
+ }
16
+
17
+ test("matcher#error fail when no error occur") {
18
+ error { true }
19
+ }
20
+
21
+ test("matcher#error pass 2") {
22
+ error(MyError) { raise MyError, "error message!" }
23
+ }
24
+
25
+ test("matcher#error fail when error classes do not match") {
26
+ error(StandardError) { raise MyError, "error message!" }
27
+ }
28
+
29
+ test("matcher#error pass 3") {
30
+ error(/message/) { raise MyError, "error message!" }
31
+ }
32
+
33
+ test("matcher#error fail when error messages do not match") {
34
+ error(/wenjun/) { raise MyError, "error message!" }
35
+ }
36
+
37
+ test("matcher#error pass 4") {
38
+ error(MyError, /message/) { raise MyError, "error message!" }
39
+ }
40
+
41
+ test("matcher#error fail when error messages/classes do not match") {
42
+ error(StandardError, /message/) { raise MyError, "error message!" }
43
+ }
44
+
45
+
46
+ class User < Struct.new(:name); end
47
+
48
+ def setup_user
49
+ @user = User.new("default-user")
50
+ end
51
+
52
+ test("user have a default name") {
53
+ setup_user
54
+ ok { @user.name == "default-user" }
55
+ }
56
+
57
+ test("[NO SETUP] user have a default name ") {
58
+ ok { @user.name == "default-user" }
59
+ }
60
+
61
+ Testa.run
@@ -0,0 +1,17 @@
1
+ include Testa
2
+
3
+ test("failed") {
4
+ ok { 1 == 2 }
5
+ }
6
+
7
+ test("passed") {
8
+ ok { 1 == 1 }
9
+ }
10
+
11
+ test "todo"
12
+
13
+ test("error") {
14
+ undeinfed_vars
15
+ }
16
+
17
+ Testa.run
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'testa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "testa"
8
+ spec.version = Testa::VERSION
9
+ spec.authors = ["wenjun.yan"]
10
+ spec.email = ["mylastnameisyan@gmail.com"]
11
+ spec.summary = %q{Simple test framework}
12
+ spec.description = %q{Simple test framework}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,70 @@
1
+ require 'minitest/autorun'
2
+ require 'Testa'
3
+
4
+ class TestMatchers < MiniTest::Unit::TestCase
5
+ include Testa::Matcher
6
+ class MyError < StandardError; end
7
+
8
+ def test_fail_raise_failure
9
+ assert_raises Testa::Failure do
10
+ fail!
11
+ end
12
+ end
13
+
14
+ def test_ok_not_raise_failure_for_truthy_value
15
+ assert(ok { true })
16
+ assert(ok { 0 })
17
+ assert(ok { "" })
18
+ end
19
+
20
+ def test_ok_raise_failure_for_falsy_value
21
+ assert_raises Testa::Failure do
22
+ ok { false }
23
+ end
24
+
25
+ assert_raises Testa::Failure do
26
+ ok { nil }
27
+ end
28
+ end
29
+
30
+ def test_error_raise_failure_when_no_error_in_given_block
31
+ assert_raises Testa::Failure do
32
+ error { nil }
33
+ end
34
+
35
+ assert_raises Testa::Failure do
36
+ error { false }
37
+ end
38
+
39
+ assert_raises Testa::Failure do
40
+ error { true }
41
+ end
42
+ end
43
+
44
+ def test_error_raise_failure_when_expected_error_class_not_match
45
+ assert_raises Testa::Failure do
46
+ error(MyError) { raise }
47
+ end
48
+ end
49
+
50
+ def test_error_raise_failure_when_expected_error_message_not_match
51
+ assert_raises Testa::Failure do
52
+ error("hello") { raise "world" }
53
+ end
54
+ end
55
+
56
+ def test_error_raise_failure_when_expected_error_message_or_class_not_match
57
+ assert_raises Testa::Failure do
58
+ error(MyError, "hello") { raise "hello" }
59
+ end
60
+
61
+ assert_raises Testa::Failure do
62
+ error(MyError, "hello") { raise MyError }
63
+ end
64
+
65
+ assert_raises Testa::Failure do
66
+ error(MyError, "hello") { raise MyError, "world" }
67
+ end
68
+ end
69
+
70
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: testa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - wenjun.yan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple test framework
42
+ email:
43
+ - mylastnameisyan@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/testa.rb
54
+ - lib/testa/version.rb
55
+ - sample/matcher.rb
56
+ - sample/status.rb
57
+ - testa.gemspec
58
+ - tests/test_matchers.rb
59
+ homepage: ''
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Simple test framework
83
+ test_files: []