public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
added merge_block
tsaleh (author)
Tue Jul 15 13:31:09 -0700 2008
commit  cc61627a5260270c84cb9c520314fd56dd7cd0b6
tree    2a3921332505b05348734c8dc0e5f8020121bfb8
parent  11cad2c0a15d1f8ea66b0c2458e30444a5c0b4e0
...
2
3
4
 
 
5
6
7
 
 
 
 
8
9
 
 
 
 
 
 
 
 
 
 
 
 
10
11
12
...
58
59
60
61
62
63
64
...
136
137
138
139
140
 
 
141
142
143
144
145
 
146
147
148
...
151
152
153
 
 
 
 
 
154
155
156
157
158
159
160
 
161
162
163
...
2
3
4
5
6
7
 
 
8
9
10
11
12
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
73
74
75
 
76
77
78
...
150
151
152
 
 
153
154
155
156
157
158
 
159
160
161
162
...
165
166
167
168
169
170
171
172
173
 
174
175
176
 
 
177
178
179
180
0
@@ -2,11 +2,26 @@ require File.join(File.dirname(__FILE__), 'proc_extensions')
0
 
0
 module Thoughtbot
0
   module Shoulda
0
+ VERSION = '1.1.1'
0
+
0
     class << self
0
- attr_accessor :current_context
0
- end
0
+ attr_accessor :contexts
0
+ def contexts
0
+ @contexts ||= []
0
+ end
0
 
0
- VERSION = '1.1.1'
0
+ def current_context
0
+ self.contexts.last
0
+ end
0
+
0
+ def add_context(context)
0
+ self.contexts.push(context)
0
+ end
0
+
0
+ def remove_context
0
+ self.contexts.pop
0
+ end
0
+ end
0
 
0
     # == Should statements
0
     #
0
@@ -58,7 +73,6 @@ module Thoughtbot
0
       end
0
     end
0
 
0
-
0
     # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
0
     def should_eventually(name, options = {}, &blk)
0
       context_name = self.name.gsub(/Test/, "")
0
@@ -136,13 +150,13 @@ module Thoughtbot
0
       attr_accessor :name # my name
0
       attr_accessor :parent # may be another context, or the original test::unit class.
0
       attr_accessor :subcontexts # array of contexts nested under myself
0
- attr_accessor :setup_blocks # block given via a setup method
0
- attr_accessor :teardown_blocks # block given via a teardown method
0
+ attr_accessor :setup_blocks # blocks given via setup methods
0
+ attr_accessor :teardown_blocks # blocks given via teardown methods
0
       attr_accessor :shoulds # array of hashes representing the should statements
0
       attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
0
 
0
       def initialize(name, parent, &blk)
0
- Shoulda.current_context = self
0
+ Shoulda.add_context(self)
0
         self.name = name
0
         self.parent = parent
0
         self.setup_blocks = []
0
@@ -151,13 +165,16 @@ module Thoughtbot
0
         self.should_eventuallys = []
0
         self.subcontexts = []
0
 
0
+ merge_block(&blk)
0
+ Shoulda.remove_context
0
+ end
0
+
0
+ def merge_block(&blk)
0
         blk.bind(self).call
0
- Shoulda.current_context = nil
0
       end
0
 
0
       def context(name, &blk)
0
- subcontexts << Context.new(name, self, &blk)
0
- Shoulda.current_context = self
0
+ self.subcontexts << Context.new(name, self, &blk)
0
       end
0
 
0
       def setup(&blk)
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
6
7
8
9
10
 
11
12
13
...
24
25
26
27
 
28
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
33
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
25
26
27
28
...
39
40
41
 
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
0
@@ -2,12 +2,27 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
0
 
0
 class ContextTest < Test::Unit::TestCase # :nodoc:
0
   
0
+ def self.context_macro(&blk)
0
+ context "with a subcontext made by a macro" do
0
+ setup { @context_macro = :foo }
0
+
0
+ merge_block &blk
0
+ end
0
+ end
0
+
0
+ # def self.context_macro(&blk)
0
+ # context "with a subcontext made by a macro" do
0
+ # setup { @context_macro = :foo }
0
+ # yield # <- this doesn't work.
0
+ # end
0
+ # end
0
+
0
   context "context with setup block" do
0
     setup do
0
       @blah = "blah"
0
     end
0
     
0
- should "have @blah == 'blah'" do
0
+ should "run the setup block" do
0
       assert_equal "blah", @blah
0
     end
0
     
0
@@ -24,10 +39,25 @@ class ContextTest < Test::Unit::TestCase # :nodoc:
0
         assert_match(/^test: context with setup block and a subcontext should be named correctly/, self.to_s)
0
       end
0
       
0
- should "run the setup methods in order" do
0
+ should "run the setup blocks in order" do
0
         assert_equal @blah, "blah twice"
0
       end
0
     end
0
+
0
+ context_macro do
0
+ should "have name set right" do
0
+ assert_match(/^test: context with setup block with a subcontext made by a macro should have name set right/, self.to_s)
0
+ end
0
+
0
+ should "run the setup block of that context macro" do
0
+ assert_equal :foo, @context_macro
0
+ end
0
+
0
+ should "run the setup block of the main context" do
0
+ assert_equal "blah", @blah
0
+ end
0
+ end
0
+
0
   end
0
 
0
   context "another context with setup block" do
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
0
@@ -1,6 +1,225 @@
0
 require File.join(File.dirname(__FILE__), '..', 'test_helper')
0
 
0
 class ShouldTest < Test::Unit::TestCase # :nodoc:
0
+ should "be able to define a should statement outside of a context" do
0
+ assert true
0
+ end
0
+
0
+ should "see the name of my class as ShouldTest" do
0
+ assert_equal "ShouldTest", self.class.name
0
+ end
0
+
0
+ def self.should_see_class_methods
0
+ should "be able to see class methods" do
0
+ assert true
0
+ end
0
+ end
0
+
0
+ def self.should_see_a_context_block_like_a_Test_Unit_class
0
+ should "see a context block as a Test::Unit class" do
0
+ assert_equal "ShouldTest", self.class.name
0
+ end
0
+ end
0
+
0
+ def self.should_see_blah
0
+ should "see @blah through a macro" do
0
+ assert @blah
0
+ end
0
+ end
0
+
0
+ def self.should_not_see_blah
0
+ should "not see @blah through a macro" do
0
+ assert_nil @blah
0
+ end
0
+ end
0
+
0
+ def self.should_be_able_to_make_context_macros(prefix = nil)
0
+ context "a macro" do
0
+ should "have the tests named correctly" do
0
+ assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
0
+ end
0
+ end
0
+ end
0
+
0
+ context "Context" do
0
+
0
+ should_see_class_methods
0
+ should_see_a_context_block_like_a_Test_Unit_class
0
+ should_be_able_to_make_context_macros("Context ")
0
+
0
+ should "not define @blah" do
0
+ assert ! self.instance_variables.include?("@blah")
0
+ end
0
+
0
+ should_not_see_blah
0
+
0
+ should "be able to define a should statement" do
0
+ assert true
0
+ end
0
+
0
+ should "see the name of my class as ShouldTest" do
0
+ assert_equal "ShouldTest", self.class.name
0
+ end
0
+
0
+ context "with a subcontext" do
0
+ should_be_able_to_make_context_macros("Context with a subcontext ")
0
+ end
0
+ end
0
+
0
+ context "Context with setup block" do
0
+ setup do
0
+ @blah = "blah"
0
+ end
0
+
0
+ should "have @blah == 'blah'" do
0
+ assert_equal "blah", @blah
0
+ end
0
+ should_see_blah
0
+
0
+ should "have name set right" do
0
+ assert_match(/^test: Context with setup block/, self.to_s)
0
+ end
0
+
0
+ context "and a subcontext" do
0
+ setup do
0
+ @blah = "#{@blah} twice"
0
+ end
0
+
0
+ should "be named correctly" do
0
+ assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
0
+ end
0
+
0
+ should "run the setup methods in order" do
0
+ assert_equal @blah, "blah twice"
0
+ end
0
+ should_see_blah
0
+ end
0
+ end
0
+
0
+ context "Another context with setup block" do
0
+ setup do
0
+ @blah = "foo"
0
+ end
0
+
0
+ should "have @blah == 'foo'" do
0
+ assert_equal "foo", @blah
0
+ end
0
+
0
+ should "have name set right" do
0
+ assert_match(/^test: Another context with setup block/, self.to_s)
0
+ end
0
+ should_see_blah
0
+ end
0
+
0
+ should_eventually "pass, since it's a should_eventually" do
0
+ flunk "what?"
0
+ end
0
+
0
+ # Context creation and naming
0
+
0
+ def test_should_create_a_new_context
0
+ assert_nothing_raised do
0
+ Thoughtbot::Shoulda::Context.new("context name", self) do; end
0
+ end
0
+ end
0
+
0
+ def test_should_create_a_nested_context
0
+ assert_nothing_raised do
0
+ parent = Thoughtbot::Shoulda::Context.new("Parent", self) do; end
0
+ child = Thoughtbot::Shoulda::Context.new("Child", parent) do; end
0
+ end
0
+ end
0
+
0
+ def test_should_name_a_contexts_correctly
0
+ parent = Thoughtbot::Shoulda::Context.new("Parent", self) do; end
0
+ child = Thoughtbot::Shoulda::Context.new("Child", parent) do; end
0
+ grandchild = Thoughtbot::Shoulda::Context.new("GrandChild", child) do; end
0
+
0
+ assert_equal "Parent", parent.full_name
0
+ assert_equal "Parent Child", child.full_name
0
+ assert_equal "Parent Child GrandChild", grandchild.full_name
0
+ end
0
+
0
+ # Should statements
0
+
0
+ def test_should_have_should_hashes_when_given_should_statements
0
+ context = Thoughtbot::Shoulda::Context.new("name", self) do
0
+ should "be good" do; end
0
+ should "another" do; end
0
+ end
0
+
0
+ names = context.shoulds.map {|s| s[:name]}
0
+ assert_equal ["another", "be good"], names.sort
0
+ end
0
+
0
+ # setup and teardown
0
+
0
+ def test_should_capture_setup_and_teardown_blocks
0
+ context = Thoughtbot::Shoulda::Context.new("name", self) do
0
+ setup do; "setup"; end
0
+ teardown do; "teardown"; end
0
+ end
0
+
0
+ assert_equal "setup", context.setup_blocks.first.call
0
+ assert_equal "teardown", context.teardown_blocks.first.call
0
+ end
0
+
0
+ # building
0
+
0
+ def test_should_create_shoulda_test_for_each_should_on_build
0
+ context = Thoughtbot::Shoulda::Context.new("name", self) do
0
+ should "one" do; end
0
+ should "two" do; end
0
+ end
0
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
0
+ context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
0
+ context.build
0
+ end
0
+
0
+ def test_should_create_test_methods_on_build
0
+ tu_class = Test::Unit::TestCase
0
+ context = Thoughtbot::Shoulda::Context.new("A Context", tu_class) do
0
+ should "define the test" do; end
0
+ end
0
+
0
+ tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
0
+ context.build
0
+ end
0
+
0
+ def test_should_create_test_methods_on_build_when_subcontext
0
+ tu_class = Test::Unit::TestCase
0
+ context = Thoughtbot::Shoulda::Context.new("A Context", tu_class) do
0
+ context "with a child" do
0
+ should "define the test" do; end
0
+ end
0
+ end
0
+
0
+ tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
0
+ context.build
0
+ end
0
+
0
+ # Test::Unit integration
0
+
0
+ def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
0
+ c = mock("context")
0
+ c.expects(:build)
0
+ Thoughtbot::Shoulda::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
0
+ self.class.context "foo" do; end
0
+ end
0
+
0
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
0
+ s = mock("test")
0
+ Thoughtbot::Shoulda::Context.any_instance.expects(:should).with("rock", {}).returns(s)
0
+ Thoughtbot::Shoulda::Context.any_instance.expects(:build)
0
+ self.class.should "rock" do; end
0
+ end
0
+
0
+ def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
0
+ s = mock("test")
0
+ Thoughtbot::Shoulda::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
0
+ Thoughtbot::Shoulda::Context.any_instance.expects(:build)
0
+ self.class.should_eventually "rock" do; end
0
+ end
0
 
0
   should "run a :before proc", :before => lambda { @value = "before" } do
0
     assert_equal "before", @value

Comments

    No one has commented yet.