oktobertest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2766bd05820c2140c972c6cb72d2a9bf08f0034f
4
+ data.tar.gz: 536b310425a0d6b4b845c36ee2500a1720044313
5
+ SHA512:
6
+ metadata.gz: dac4ac71b09de1b9c7a4b59416bb374ef6f0bc4951c13f3a93c63e58fc93e22ea32489c1805cf0d160b68960e0c2c54e5a44976f0e479b20d4fec17275ff8612
7
+ data.tar.gz: f565aab1347b5fa094d0a3e1efa251bae18e2a76f2f4aa633eb3cef45b86a5c6c5a7a208ba0b73edc9975bcf3b6ab76bdd980522d45225ab3414fd18f6a5b6fb
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Patricio Mac Adden <patriciomacadden@gmail.com>
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.
data/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # Oktobertest
2
+
3
+ Small test library.
4
+
5
+ ## Why?
6
+
7
+ This is a project I started over the weekend. I was looking at [Cutest](https://github.com/djanowski/cutest)
8
+ and I thought: "It shouldn't be so difficult to make a test library".
9
+
10
+ ### Features
11
+
12
+ * Setup - Test - Teardown model.
13
+ * Each `test` is an instance of `Oktobertest::Test`, which means that each test
14
+ runs its own context.
15
+ * Each `scope` is an instance of `Oktobertest::Scope`. Methods defined in a
16
+ scope are copied into each `test` it contains.
17
+ * Extensible: it's easy to add more assertions.
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'oktobertest', group: :test
25
+ # or this if you want to use oktobertest master
26
+ # gem 'oktobertest', group: :test, github: 'patriciomacadden/oktobertest'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ ```bash
32
+ $ bundle
33
+ ```
34
+
35
+ Or install it yourself as:
36
+
37
+ ```bash
38
+ $ gem install oktobertest
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ### Example
44
+
45
+ ```ruby
46
+ scope do
47
+ setup do
48
+ @foo = 'foo'
49
+ end
50
+
51
+ test 'this test will pass' do
52
+ assert @foo == 'foo'
53
+ end
54
+
55
+ test 'this test will pass too' do
56
+ assert_raises RuntimeError do
57
+ raise RuntimeError
58
+ end
59
+ end
60
+
61
+ test 'this test will fail' do
62
+ flunk 'fail!'
63
+ end
64
+
65
+ test 'skip this one!' do
66
+ skip
67
+ end
68
+ end
69
+ ```
70
+
71
+ ### Assertions
72
+
73
+ For now there are 2 assertions:
74
+
75
+ * `assert`: fails unless the given condition is true.
76
+ * `assert_raises`: fails unless the given block raises the given exception.
77
+
78
+ And there is a way of making tests fail (`flunk`) and skipping them (`skip`).
79
+
80
+ #### How to add assertions?
81
+
82
+ That's quite simple. Just define them in the `Oktobertest::Assertions` module:
83
+
84
+ ```ruby
85
+ module Oktobertest
86
+ module Assertions
87
+ def assert_equal(expected, actual, message = nil)
88
+ message ||= "#{expected.inspect} is not equal to #{actual.inspect}"
89
+ assert expected == actual, message
90
+ end
91
+ end
92
+ end
93
+ ```
94
+
95
+ ### Running the test suite
96
+
97
+ You can run the test suite using `rake` or the `ok` command.
98
+
99
+ Using `rake`:
100
+
101
+ ```ruby
102
+ require 'oktobertest'
103
+
104
+ task :test do
105
+ Oktobertest.run Dir['test/*_test.rb']
106
+ end
107
+
108
+ task default: :test
109
+ ```
110
+
111
+ Using `ok`:
112
+
113
+ ```bash
114
+ $ ok test/*_test.rb
115
+ ```
116
+
117
+ Please check the default options by running `ok`.
118
+
119
+ ## Contributing
120
+
121
+ 1. Fork it
122
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
123
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
124
+ 4. Push to the branch (`git push origin my-new-feature`)
125
+ 5. Create new Pull Request
126
+
127
+ ## License
128
+
129
+ See the [LICENSE](https://github.com/patriciomacadden/oktobertest/blob/master/LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'oktobertest'
4
+
5
+ desc 'Run the tests'
6
+ task :test do
7
+ Oktobertest.run Dir['test/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/ok ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts 'usage: ok [-r lib] [-s scope] [-t test] [-v] file ...' if ARGV.empty?
4
+
5
+ require 'clap'
6
+ require_relative '../lib/oktobertest'
7
+
8
+ files = Clap.run ARGV,
9
+ '-r' => ->(file) { require file },
10
+ '-s' => ->(name) { Oktobertest.run_scope = name },
11
+ '-t' => ->(name) { Oktobertest.run_test = name },
12
+ '-v' => -> { puts Oktobertest::VERSION }
13
+
14
+ Oktobertest.run files if files.any?
@@ -0,0 +1,144 @@
1
+ module Oktobertest
2
+ VERSION = '0.0.1'
3
+
4
+ TestFailed = Class.new StandardError
5
+ TestSkipped = Class.new StandardError
6
+
7
+ def self.run(files)
8
+ files.each { |file| load file }
9
+ puts
10
+ errors.each do |error|
11
+ case error
12
+ when TestFailed
13
+ file, line, _ = error.backtrace[2].split(':')
14
+ print "\nerror: #{error.message}"
15
+ when TestSkipped
16
+ file, line, _ = error.backtrace[1].split(':')
17
+ print "\nskip"
18
+ print ": #{error.message}" unless error.message == error.class.name
19
+ else
20
+ file, line, _ = error.backtrace[0].split(':')
21
+ print "\nerror: #{error.message}"
22
+ end
23
+ print "\nfile: #{file}\nline: #{line}\n"
24
+ end
25
+ end
26
+
27
+ def self.run_test=(name)
28
+ @run_test = name
29
+ end
30
+
31
+ def self.run_test
32
+ @run_test
33
+ end
34
+
35
+ def self.run_scope=(name)
36
+ @run_scope = name
37
+ end
38
+
39
+ def self.run_scope
40
+ @run_scope
41
+ end
42
+
43
+ module Assertions
44
+ def assert(value, message = nil)
45
+ message ||= "condition is not true: #{value.inspect}"
46
+ flunk message unless !!value
47
+ end
48
+
49
+ def assert_raises(exception, message = nil)
50
+ begin
51
+ yield
52
+ rescue => error
53
+ ensure
54
+ message ||= "block doesn't raise #{exception}"
55
+ flunk message unless error.kind_of? exception
56
+ end
57
+ end
58
+
59
+ def flunk(message = nil)
60
+ raise TestFailed, message
61
+ end
62
+
63
+ def skip(message = nil)
64
+ raise TestSkipped, message
65
+ end
66
+ end
67
+
68
+ class Scope
69
+ def initialize(name = nil, &block)
70
+ @name, @block = name, block
71
+ @setup, @teardown = [], []
72
+ end
73
+
74
+ def run
75
+ instance_eval &@block
76
+ end
77
+
78
+ protected
79
+
80
+ def test(name = nil, &block)
81
+ if !Oktobertest.run_test || Oktobertest.run_test == name
82
+ test = Test.new(name, &block)
83
+ singleton_methods.each { |m| test.define_singleton_method m, &method(m) }
84
+ @setup.each { |b| test.instance_eval &b }
85
+ test.run
86
+ @teardown.each { |b| test.instance_eval &b }
87
+ end
88
+ end
89
+
90
+ def setup(&block)
91
+ @setup << block
92
+ end
93
+
94
+ def teardown(&block)
95
+ @teardown << block
96
+ end
97
+
98
+ def scope(name = nil, &block)
99
+ if !Oktobertest.run_scope || Oktobertest.run_scope == name
100
+ scope = Scope.new(name, &block)
101
+ singleton_methods.each { |m| scope.define_singleton_method m, &method(m) }
102
+ @setup.each { |b| scope.setup &b }
103
+ @teardown.each { |b| scope.teardown &b }
104
+ scope.run
105
+ end
106
+ end
107
+ end
108
+
109
+ class Test
110
+ include Assertions
111
+
112
+ def initialize(name = nil, &block)
113
+ @name, @block = name, block
114
+ end
115
+
116
+ def run
117
+ instance_eval &@block
118
+ print '.'
119
+ rescue TestFailed => error
120
+ Oktobertest.errors << error
121
+ print 'F'
122
+ rescue TestSkipped => error
123
+ Oktobertest.errors << error
124
+ print 'S'
125
+ rescue StandardError => error
126
+ Oktobertest.errors << error
127
+ print 'E'
128
+ end
129
+ end
130
+
131
+ private
132
+
133
+ def self.errors
134
+ @errors ||= []
135
+ end
136
+ end
137
+
138
+ module Kernel
139
+ def scope(name = nil, &block)
140
+ if !Oktobertest.run_scope || Oktobertest.run_scope == name
141
+ Oktobertest::Scope.new(name, &block).run
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'oktobertest'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'oktobertest'
8
+ spec.version = Oktobertest::VERSION
9
+ spec.authors = ['Patricio Mac Adden']
10
+ spec.email = ['patriciomacadden@gmail.com']
11
+ spec.summary = %q{Small test library}
12
+ spec.description = %q{Small test library}
13
+ spec.homepage = 'https://github.com/patriciomacadden/oktobertest'
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.5'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_runtime_dependency 'clap', '~> 1.0.0'
25
+ end
@@ -0,0 +1,43 @@
1
+ scope 'assert' do
2
+ test 'passes if the value is true' do
3
+ assert true
4
+ end
5
+
6
+ test 'fails if the value is false' do
7
+ assert_raises Oktobertest::TestFailed do
8
+ assert false
9
+ end
10
+ end
11
+ end
12
+
13
+ scope 'assert_raises' do
14
+ test 'passes if the block raises the given exception' do
15
+ assert_raises RuntimeError do
16
+ raise RuntimeError
17
+ end
18
+ end
19
+
20
+ test 'fails if the block does not raise the given exception' do
21
+ assert_raises Oktobertest::TestFailed do
22
+ assert_raises RuntimeError do
23
+ raise ArgumentError
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ scope 'flunk' do
30
+ test 'raises Oktobertest::TestFailed exception' do
31
+ assert_raises Oktobertest::TestFailed do
32
+ flunk
33
+ end
34
+ end
35
+ end
36
+
37
+ scope 'skip' do
38
+ test 'raises Oktobertest::TestSkipped exception' do
39
+ assert_raises Oktobertest::TestSkipped do
40
+ skip
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ scope do
2
+ test { raise RuntimeError }
3
+ end
@@ -0,0 +1,3 @@
1
+ scope do
2
+ test { assert false }
3
+ end
@@ -0,0 +1,19 @@
1
+ scope 'run this' do
2
+ test 'run this' do
3
+ assert true
4
+ end
5
+
6
+ test 'and this' do
7
+ assert true
8
+ end
9
+ end
10
+
11
+ scope 'and not this' do
12
+ test 'run this' do
13
+ assert false
14
+ end
15
+
16
+ test 'and this' do
17
+ assert false
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ scope do
2
+ test 'run this' do
3
+ assert true
4
+ end
5
+
6
+ test 'and not this' do
7
+ assert false
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ scope do
2
+ test { skip }
3
+ end
@@ -0,0 +1,3 @@
1
+ scope do
2
+ test { assert true }
3
+ end
@@ -0,0 +1,55 @@
1
+ scope do
2
+ test 'successful run' do
3
+ expected = ".\n"
4
+ output = %x(bin/ok test/fixtures/successful_test.rb)
5
+ assert expected == output
6
+ end
7
+
8
+ test 'errored run' do
9
+ expected = <<EOS
10
+ E
11
+
12
+ error: RuntimeError
13
+ file: test/fixtures/error_test.rb
14
+ line: 2
15
+ EOS
16
+ output = %x(bin/ok test/fixtures/error_test.rb)
17
+ assert expected == output
18
+ end
19
+
20
+ test 'failed run' do
21
+ expected = <<EOS
22
+ F
23
+
24
+ error: condition is not true: false
25
+ file: test/fixtures/failing_test.rb
26
+ line: 2
27
+ EOS
28
+ output = %x(bin/ok test/fixtures/failing_test.rb)
29
+ assert expected == output
30
+ end
31
+
32
+ test 'run with skipped tests' do
33
+ expected = <<EOS
34
+ S
35
+
36
+ skip
37
+ file: test/fixtures/skipped_test.rb
38
+ line: 2
39
+ EOS
40
+ output = %x(bin/ok test/fixtures/skipped_test.rb)
41
+ assert expected == output
42
+ end
43
+
44
+ test 'run only one scope' do
45
+ expected = "..\n"
46
+ output = %x(bin/ok -s 'run this' test/fixtures/run_scope_test.rb)
47
+ assert expected == output
48
+ end
49
+
50
+ test 'run only one test' do
51
+ expected = ".\n"
52
+ output = %x(bin/ok -t 'run this' test/fixtures/run_test_test.rb)
53
+ assert expected == output
54
+ end
55
+ end
@@ -0,0 +1,37 @@
1
+ scope do
2
+ def foo
3
+ 'foo'
4
+ end
5
+
6
+ test 'responds to foo' do
7
+ assert foo == 'foo'
8
+ end
9
+
10
+ scope do
11
+ test 'responds to foo too' do
12
+ assert foo == 'foo'
13
+ end
14
+ end
15
+ end
16
+
17
+ scope do
18
+ setup do
19
+ @foo = 'foo'
20
+ end
21
+
22
+ scope do
23
+ setup do
24
+ @bar = 'bar'
25
+ end
26
+
27
+ test 'foo and bar must be defined' do
28
+ assert @foo == 'foo'
29
+ assert @bar == 'bar'
30
+ end
31
+ end
32
+
33
+ test 'foo must be defined and bar wont be' do
34
+ assert @foo == 'foo'
35
+ assert @bar.nil?
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ scope do
2
+ setup do
3
+ @foo = 'foo'
4
+ end
5
+
6
+ test 'foo is defined' do
7
+ assert @foo == 'foo'
8
+ end
9
+
10
+ scope do
11
+ test 'foo is defined too' do
12
+ assert @foo == 'foo'
13
+ end
14
+ end
15
+ end
16
+
17
+ scope do
18
+ setup do
19
+ @foo = 'foo'
20
+ end
21
+
22
+ test 'foo is defined' do
23
+ assert @foo == 'foo'
24
+ @foo = 'bar'
25
+ end
26
+
27
+ test 'foo is not changed' do
28
+ assert @foo == 'foo'
29
+ end
30
+ end
31
+
32
+ scope do
33
+ setup do
34
+ @foo = 'foo'
35
+ end
36
+
37
+ setup do
38
+ @bar = 'bar'
39
+ end
40
+
41
+ test 'both foo and bar are defined, but baz is not' do
42
+ assert @foo == 'foo'
43
+ assert @bar == 'bar'
44
+ assert @baz.nil?
45
+ end
46
+
47
+ setup do
48
+ @baz = 'baz'
49
+ end
50
+
51
+ test 'both foo, bar and baz are defined' do
52
+ assert @foo == 'foo'
53
+ assert @bar == 'bar'
54
+ assert @baz == 'baz'
55
+ end
56
+ end
@@ -0,0 +1,13 @@
1
+ scope do
2
+ setup do
3
+ @foo = 'foo'
4
+ end
5
+
6
+ test 'after this test @foo is changed' do
7
+ @foo = 'bar'
8
+ end
9
+
10
+ teardown do
11
+ assert @foo == 'bar'
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oktobertest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patricio Mac Adden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-25 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
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
+ - !ruby/object:Gem::Dependency
42
+ name: clap
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ description: Small test library
56
+ email:
57
+ - patriciomacadden@gmail.com
58
+ executables:
59
+ - ok
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/ok
69
+ - lib/oktobertest.rb
70
+ - oktobertest.gemspec
71
+ - test/assertions_test.rb
72
+ - test/fixtures/error_test.rb
73
+ - test/fixtures/failing_test.rb
74
+ - test/fixtures/run_scope_test.rb
75
+ - test/fixtures/run_test_test.rb
76
+ - test/fixtures/skipped_test.rb
77
+ - test/fixtures/successful_test.rb
78
+ - test/oktobertest_test.rb
79
+ - test/scope_test.rb
80
+ - test/setup_test.rb
81
+ - test/teardown_test.rb
82
+ homepage: https://github.com/patriciomacadden/oktobertest
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Small test library
106
+ test_files:
107
+ - test/assertions_test.rb
108
+ - test/fixtures/error_test.rb
109
+ - test/fixtures/failing_test.rb
110
+ - test/fixtures/run_scope_test.rb
111
+ - test/fixtures/run_test_test.rb
112
+ - test/fixtures/skipped_test.rb
113
+ - test/fixtures/successful_test.rb
114
+ - test/oktobertest_test.rb
115
+ - test/scope_test.rb
116
+ - test/setup_test.rb
117
+ - test/teardown_test.rb