mcmire-contest 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/LICENSE +19 -0
- data/README.markdown +116 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/contest.rb +79 -0
- data/rails/init.rb +27 -0
- data/test/all_test.rb +145 -0
- data/test/setup_and_teardown_order_test.rb +28 -0
- metadata +68 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
Contest
|
2
|
+
=======
|
3
|
+
|
4
|
+
Contexts for Test::Unit.
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
Declare your tests as you would in RSpec or Shoulda:
|
15
|
+
|
16
|
+
require 'contest'
|
17
|
+
|
18
|
+
class SomeTest < Test::Unit::TestCase
|
19
|
+
setup do
|
20
|
+
@value = 1
|
21
|
+
end
|
22
|
+
|
23
|
+
teardown do
|
24
|
+
@value = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
test "sample test" do
|
28
|
+
assert_equal 1, @value
|
29
|
+
end
|
30
|
+
|
31
|
+
context "a context" do
|
32
|
+
setup do
|
33
|
+
@value += 1
|
34
|
+
end
|
35
|
+
|
36
|
+
test "more tests" do
|
37
|
+
assert_equal 2, @value
|
38
|
+
end
|
39
|
+
|
40
|
+
context "a nested context" do
|
41
|
+
setup do
|
42
|
+
@value += 1
|
43
|
+
end
|
44
|
+
|
45
|
+
test "yet more tests" do
|
46
|
+
assert_equal 3, @value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
For your convenience, `context` is aliased as `describe` and `test` is aliased as `should`, so this is valid:
|
53
|
+
|
54
|
+
class SomeTest < Test::Unit::TestCase
|
55
|
+
setup do
|
56
|
+
@value = 1
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "something" do
|
60
|
+
setup do
|
61
|
+
@value += 1
|
62
|
+
end
|
63
|
+
|
64
|
+
should "equal 2" do
|
65
|
+
assert_equal 2, @value
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
You can run it normally, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:
|
71
|
+
|
72
|
+
$ testrb my_test.rb -n test_yet_more_tests
|
73
|
+
|
74
|
+
Or with a regular expression:
|
75
|
+
|
76
|
+
$ testrb my_test.rb -n /yet_more_tests/
|
77
|
+
|
78
|
+
Installation
|
79
|
+
------------
|
80
|
+
|
81
|
+
$ sudo gem install contest
|
82
|
+
|
83
|
+
If you want to use it with Rails, add this to config/environment.rb:
|
84
|
+
|
85
|
+
config.gem "contest"
|
86
|
+
|
87
|
+
Then you can vendor the gem:
|
88
|
+
|
89
|
+
rake gems:install
|
90
|
+
rake gems:unpack
|
91
|
+
|
92
|
+
License
|
93
|
+
-------
|
94
|
+
|
95
|
+
Copyright (c) 2009 Damian Janowski and Michel Martens for Citrusbyte
|
96
|
+
|
97
|
+
Permission is hereby granted, free of charge, to any person
|
98
|
+
obtaining a copy of this software and associated documentation
|
99
|
+
files (the "Software"), to deal in the Software without
|
100
|
+
restriction, including without limitation the rights to use,
|
101
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
102
|
+
copies of the Software, and to permit persons to whom the
|
103
|
+
Software is furnished to do so, subject to the following
|
104
|
+
conditions:
|
105
|
+
|
106
|
+
The above copyright notice and this permission notice shall be
|
107
|
+
included in all copies or substantial portions of the Software.
|
108
|
+
|
109
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
110
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
111
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
112
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
113
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
114
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
115
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
116
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "mcmire-contest"
|
8
|
+
gem.summary = %Q{Write more readable tests in Test::Unit with this tiny script.}
|
9
|
+
gem.description = %Q{Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.}
|
10
|
+
gem.email = ["djanowski@dimaion.com", "michel@soveran.com"]
|
11
|
+
gem.homepage = "http://github.com/mcmire/contest"
|
12
|
+
gem.authors = ["Damian Janowski", "Michel Martens"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "mcmire-contest #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.3
|
data/lib/contest.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
# Test::Unit loads a default test if the suite is empty, whose purpose is to
|
4
|
+
# fail. Since having empty contexts is a common practice, we decided to
|
5
|
+
# overwrite TestSuite#empty? in order to allow them. Having a failure when no
|
6
|
+
# tests have been defined seems counter-intuitive.
|
7
|
+
class Test::Unit::TestSuite
|
8
|
+
def empty?
|
9
|
+
false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Contest adds +teardown+, +test+ and +context+ as class methods, and the
|
14
|
+
# instance methods +setup+ and +teardown+ now iterate on the corresponding
|
15
|
+
# blocks. Note that all setup and teardown blocks must be defined with the
|
16
|
+
# block syntax. Adding setup or teardown instance methods defeats the purpose
|
17
|
+
# of this library.
|
18
|
+
class Test::Unit::TestCase
|
19
|
+
def self.setup(&block)
|
20
|
+
define_method :setup do
|
21
|
+
super(&block)
|
22
|
+
instance_eval(&block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.teardown(&block)
|
27
|
+
define_method :teardown do
|
28
|
+
instance_eval(&block)
|
29
|
+
super(&block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.context(*name, &block)
|
34
|
+
subclass = Class.new(self)
|
35
|
+
remove_tests(subclass)
|
36
|
+
subclass.class_eval(&block) if block_given?
|
37
|
+
const_set(context_name(name.join(" ")), subclass)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.test(name, &block)
|
41
|
+
define_method(test_name(name), &block) if block_given?
|
42
|
+
end
|
43
|
+
|
44
|
+
class << self
|
45
|
+
alias_method :should, :test
|
46
|
+
alias_method :describe, :context
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.context_name(name)
|
52
|
+
"Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.test_name(name)
|
56
|
+
# don't sanitize name if we don't have to
|
57
|
+
"test: #{name}".to_sym
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.sanitize_name(name)
|
61
|
+
# convert #, !, and ? to their words
|
62
|
+
# so for instance context "#foo!" is different from context "#foo?"
|
63
|
+
name.
|
64
|
+
downcase.
|
65
|
+
gsub('#', ' hash ').
|
66
|
+
gsub('!', ' bang ').
|
67
|
+
gsub('?', ' query ').
|
68
|
+
gsub(/[^a-z]+/, ' ').
|
69
|
+
gsub(/([a-z])_([a-z])/) { $1.downcase + $2.upcase }.
|
70
|
+
strip.
|
71
|
+
squeeze(" ")
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.remove_tests(subclass)
|
75
|
+
subclass.public_instance_methods.grep(/^test:/).each do |meth|
|
76
|
+
subclass.send(:undef_method, meth.to_sym)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# ActiveStupor defines its own idiom for the class-level setup method
|
2
|
+
# (using callback chains). This hack is to ensure that Contest users can
|
3
|
+
# still call the setup method with a block.
|
4
|
+
if RAILS_ENV == 'test'
|
5
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'contest')
|
6
|
+
require "active_support/test_case"
|
7
|
+
|
8
|
+
class Test::Unit::TestCase
|
9
|
+
class << self
|
10
|
+
alias contest_setup setup
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ActiveSupport::TestCase
|
15
|
+
class << self
|
16
|
+
alias activesupport_setup setup
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.setup(*args, &block)
|
20
|
+
if args.empty?
|
21
|
+
contest_setup(&block)
|
22
|
+
else
|
23
|
+
activesupport_setup(*args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/all_test.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/contest"
|
2
|
+
|
3
|
+
class FooTest < Test::Unit::TestCase
|
4
|
+
setup do
|
5
|
+
@value = 1
|
6
|
+
end
|
7
|
+
|
8
|
+
teardown do
|
9
|
+
@value = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
test "truth" do
|
13
|
+
assert_equal 1, @value
|
14
|
+
end
|
15
|
+
|
16
|
+
test "pending test"
|
17
|
+
should "another pending test"
|
18
|
+
|
19
|
+
context '#foo?' do
|
20
|
+
test "truth" do
|
21
|
+
assert true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context '#foo!' do
|
26
|
+
test "truth" do
|
27
|
+
assert true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "context's non-word characters " do
|
32
|
+
should "run the test inside" do
|
33
|
+
assert_equal 1, @value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "context", "with multiple", "arguments", 123, FooTest do
|
38
|
+
should "run the test inside" do
|
39
|
+
assert_equal 1, @value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "some context" do
|
44
|
+
setup do
|
45
|
+
@value += 1
|
46
|
+
end
|
47
|
+
|
48
|
+
test "another truth" do
|
49
|
+
assert_equal 2, @value
|
50
|
+
end
|
51
|
+
|
52
|
+
context "and a nested context" do
|
53
|
+
setup do
|
54
|
+
@value += 1
|
55
|
+
end
|
56
|
+
|
57
|
+
test "more" do
|
58
|
+
assert_equal 3, @value
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "some other context" do
|
64
|
+
setup do
|
65
|
+
@value += 1
|
66
|
+
end
|
67
|
+
|
68
|
+
test "yet another truth" do
|
69
|
+
assert_equal 2, @value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "context with should" do
|
74
|
+
setup do
|
75
|
+
@value += 1
|
76
|
+
end
|
77
|
+
|
78
|
+
should "yet another truth" do
|
79
|
+
assert_equal 2, @value
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class BarTest < Test::Unit::TestCase
|
85
|
+
setup do
|
86
|
+
@value = 1
|
87
|
+
end
|
88
|
+
|
89
|
+
context "some context" do
|
90
|
+
setup do
|
91
|
+
@value += 1
|
92
|
+
end
|
93
|
+
|
94
|
+
test "another truth" do
|
95
|
+
assert_equal 2, @value
|
96
|
+
end
|
97
|
+
|
98
|
+
test "yet another truth" do
|
99
|
+
assert_equal 2, @value
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class TestBaz < Test::Unit::TestCase
|
105
|
+
def foo
|
106
|
+
42
|
107
|
+
end
|
108
|
+
|
109
|
+
def setup
|
110
|
+
@value = 1
|
111
|
+
super
|
112
|
+
end
|
113
|
+
|
114
|
+
context "some context" do
|
115
|
+
def setup
|
116
|
+
super
|
117
|
+
@value += 2
|
118
|
+
end
|
119
|
+
|
120
|
+
def bar
|
121
|
+
foo + 1
|
122
|
+
end
|
123
|
+
|
124
|
+
test "a helper" do
|
125
|
+
assert_equal 42, foo
|
126
|
+
assert_equal 3, @value
|
127
|
+
end
|
128
|
+
|
129
|
+
test "another helper" do
|
130
|
+
assert_equal 43, bar
|
131
|
+
end
|
132
|
+
|
133
|
+
context "another context" do
|
134
|
+
setup do
|
135
|
+
@value += 3
|
136
|
+
end
|
137
|
+
|
138
|
+
test "blah" do
|
139
|
+
assert_equal 6, @value
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context "empty context"
|
145
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/contest"
|
2
|
+
|
3
|
+
class BaseTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@order = []
|
6
|
+
@order << "Grandparent Setup"
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
@order << "Grandparent Teardown"
|
11
|
+
|
12
|
+
assert_equal ["Grandparent Setup", "Parent Setup", "Child Setup", "Test Case", "Child Teardown", "Parent Teardown", "Grandparent Teardown"], @order
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class MidLayerTest < BaseTest
|
17
|
+
setup { @order << "Parent Setup" }
|
18
|
+
teardown { @order << "Parent Teardown" }
|
19
|
+
end
|
20
|
+
|
21
|
+
class LeafTest < MidLayerTest
|
22
|
+
setup { @order << "Child Setup" }
|
23
|
+
teardown { @order << "Child Teardown" }
|
24
|
+
|
25
|
+
test "my actual test" do
|
26
|
+
@order << "Test Case"
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mcmire-contest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damian Janowski
|
8
|
+
- Michel Martens
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2010-01-01 00:00:00 -06:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.
|
18
|
+
email:
|
19
|
+
- djanowski@dimaion.com
|
20
|
+
- michel@soveran.com
|
21
|
+
executables: []
|
22
|
+
|
23
|
+
extensions: []
|
24
|
+
|
25
|
+
extra_rdoc_files:
|
26
|
+
- LICENSE
|
27
|
+
- README.markdown
|
28
|
+
files:
|
29
|
+
- .gitignore
|
30
|
+
- LICENSE
|
31
|
+
- README.markdown
|
32
|
+
- Rakefile
|
33
|
+
- VERSION
|
34
|
+
- lib/contest.rb
|
35
|
+
- rails/init.rb
|
36
|
+
- test/all_test.rb
|
37
|
+
- test/setup_and_teardown_order_test.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/mcmire/contest
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.5
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Write more readable tests in Test::Unit with this tiny script.
|
66
|
+
test_files:
|
67
|
+
- test/all_test.rb
|
68
|
+
- test/setup_and_teardown_order_test.rb
|