citrusbyte-contest 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +6 -3
- data/lib/contest.rb +24 -9
- data/test/all_test.rb +4 -0
- metadata +1 -1
data/README.markdown
CHANGED
@@ -6,8 +6,7 @@ Contexts for Test::Unit.
|
|
6
6
|
Description
|
7
7
|
-----------
|
8
8
|
|
9
|
-
Write declarative tests
|
10
|
-
penalties. Contest is less than 100 lines of code and gets the job done.
|
9
|
+
Write declarative tests using nested contexts without performance penalties. Contest is less than 100 lines of code and gets the job done.
|
11
10
|
|
12
11
|
Usage
|
13
12
|
-----
|
@@ -20,6 +19,10 @@ Declare your tests as you would in RSpec or Shoulda:
|
|
20
19
|
setup do
|
21
20
|
@value = 1
|
22
21
|
end
|
22
|
+
|
23
|
+
teardown do
|
24
|
+
@value = nil
|
25
|
+
end
|
23
26
|
|
24
27
|
test "sample test" do
|
25
28
|
assert_equal 1, @value
|
@@ -64,7 +67,7 @@ For your convenience, `context` is aliased as `describe` and `test` is aliased a
|
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
67
|
-
You can run it
|
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:
|
68
71
|
|
69
72
|
$ testrb my_test.rb -n test_yet_more_tests
|
70
73
|
|
data/lib/contest.rb
CHANGED
@@ -1,34 +1,45 @@
|
|
1
1
|
require "test/unit"
|
2
2
|
|
3
|
-
# Test::Unit loads a default test if the suite is empty,
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# counter-intuitive.
|
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.
|
8
7
|
class Test::Unit::TestSuite
|
9
8
|
def empty?
|
10
9
|
false
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# blocks
|
17
|
-
#
|
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
18
|
class Test::Unit::TestCase
|
19
19
|
def self.setup(&block)
|
20
20
|
setup_blocks << block
|
21
21
|
end
|
22
22
|
|
23
|
+
def self.teardown(&block)
|
24
|
+
teardown_blocks << block
|
25
|
+
end
|
26
|
+
|
23
27
|
def setup
|
24
28
|
self.class.setup_blocks.each do |block|
|
25
29
|
instance_eval(&block)
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
33
|
+
def teardown
|
34
|
+
self.class.teardown_blocks.each do |block|
|
35
|
+
instance_eval(&block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
29
39
|
def self.context(name, &block)
|
30
40
|
subclass = Class.new(self.superclass)
|
31
41
|
subclass.setup_blocks.unshift(*setup_blocks)
|
42
|
+
subclass.teardown_blocks.unshift(*teardown_blocks)
|
32
43
|
subclass.class_eval(&block)
|
33
44
|
const_set(context_name(name), subclass)
|
34
45
|
end
|
@@ -48,6 +59,10 @@ private
|
|
48
59
|
@setup_blocks ||= []
|
49
60
|
end
|
50
61
|
|
62
|
+
def self.teardown_blocks
|
63
|
+
@teardown_blocks ||= []
|
64
|
+
end
|
65
|
+
|
51
66
|
def self.context_name(name)
|
52
67
|
"Test#{sanitize_name(name).gsub(/(^| )(\w)/) { $2.upcase }}".to_sym
|
53
68
|
end
|
data/test/all_test.rb
CHANGED