public
Fork of thoughtbot/shoulda
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/rmm5t/shoulda.git
Added support for passing a :before Proc option to a should statement
Ryan McGeary (author)
Tue Jul 08 17:58:05 -0700 2008
commit  e6e2f6b906edc4ecd6d7f8a77046216e308d1cf0
tree    d920d6adc4224db5e95d97cfaadf1bb720e015b7
parent  9f8c29edf3e632cdbc9756f6c5792bdc8cc9494c
...
8
9
10
11
 
12
13
14
15
16
17
 
18
19
20
...
32
33
34
 
 
 
 
 
 
 
 
 
 
 
 
 
35
36
 
37
38
 
39
40
41
42
 
43
44
45
46
47
 
48
49
 
50
51
52
...
54
55
56
57
 
58
59
60
...
154
155
156
157
 
158
159
 
160
161
162
...
189
190
191
192
 
 
 
193
194
195
...
198
199
200
 
 
 
 
 
201
 
 
 
202
203
204
...
8
9
10
 
11
12
13
14
15
16
 
17
18
19
20
...
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
...
68
69
70
 
71
72
73
74
...
168
169
170
 
171
172
 
173
174
175
176
...
203
204
205
 
206
207
208
209
210
211
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
0
@@ -8,13 +8,13 @@ module Thoughtbot
0
 
0
     VERSION = '1.1.1'
0
 
0
-    # = Should statements
0
+    # == Should statements
0
     #
0
     # Should statements are just syntactic sugar over normal Test::Unit test methods.  A should block 
0
     # contains all the normal code and assertions you're used to seeing, with the added benefit that 
0
     # they can be wrapped inside context blocks (see below).
0
     #
0
-    # == Example:
0
+    # === Example:
0
     #
0
     #  class UserTest << Test::Unit::TestCase
0
     #    
0
@@ -32,21 +32,35 @@ module Thoughtbot
0
     # * <tt>"test: User should return its full name. "</tt>
0
     #
0
     # Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
0
+    #
0
+    # Should statements can also take a Proc as a <tt>:before </tt>option.  This proc runs after any
0
+    # parent context's setups but before the current context's setup.
0
+    #
0
+    # === Example:
0
+    #
0
+    #  context "Some context" do
0
+    #    setup { puts("I run after the :before proc") }
0
+    #
0
+    #    should "run a :before proc", :before => lambda { puts("I run before the setup") }  do
0
+    #      assert true
0
+    #    end
0
+    #  end
0
 
0
-    def should(name, &blk)
0
+    def should(name, options = {}, &blk)
0
       if Shoulda.current_context
0
-        block_given? ? Shoulda.current_context.should(name, &blk) : Should.current_context.should_eventually(name)
0
+        block_given? ? Shoulda.current_context.should(name, options, &blk) : Should.current_context.should_eventually(name)
0
       else
0
         context_name = self.name.gsub(/Test/, "")
0
         context = Thoughtbot::Shoulda::Context.new(context_name, self) do
0
-          block_given? ? should(name, &blk) : should_eventually(name)
0
+          block_given? ? should(name, options, &blk) : should_eventually(name)
0
         end
0
         context.build
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, &blk)
0
+    def should_eventually(name, options = {}, &blk)
0
       context_name = self.name.gsub(/Test/, "")
0
       context = Thoughtbot::Shoulda::Context.new(context_name, self) do
0
         should_eventually(name, &blk)
0
@@ -54,7 +68,7 @@ module Thoughtbot
0
       context.build
0
     end
0
 
0
-    # = Contexts
0
+    # == Contexts
0
     # 
0
     # A context block groups should statements under a common set of setup/teardown methods.  
0
     # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
0
@@ -154,9 +168,9 @@ module Thoughtbot
0
         self.teardown_blocks << blk
0
       end
0
 
0
-      def should(name, &blk)
0
+      def should(name, options = {}, &blk)
0
         if block_given?
0
-          self.shoulds << { :name => name, :block => blk }
0
+          self.shoulds << { :name => name, :before => options[:before], :block => blk }
0
         else
0
          self.should_eventuallys << { :name => name }
0
        end
0
@@ -189,7 +203,9 @@ module Thoughtbot
0
         context = self
0
         test_unit_class.send(:define_method, test_name) do
0
           begin
0
-            context.run_all_setup_blocks(self)
0
+            context.run_parent_setup_blocks(self)
0
+            should[:before].bind(self).call if should[:before]
0
+            context.run_current_setup_blocks(self)
0
             should[:block].bind(self).call
0
           ensure
0
             context.run_all_teardown_blocks(self)
0
@@ -198,7 +214,15 @@ module Thoughtbot
0
       end
0
 
0
       def run_all_setup_blocks(binding)
0
+        run_parent_setup_blocks(binding)
0
+        run_current_setup_blocks(binding)
0
+      end
0
+
0
+      def run_parent_setup_blocks(binding)
0
         self.parent.run_all_setup_blocks(binding) if am_subcontext?
0
+      end
0
+
0
+      def run_current_setup_blocks(binding)
0
         setup_blocks.each do |setup_block|
0
           setup_block.bind(binding).call
0
         end

Comments