File: context.rb

package info (click to toggle)
ruby-shoulda-context 1.0.0~beta1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 220 kB
  • sloc: ruby: 1,050; makefile: 3
file content (446 lines) | stat: -rw-r--r-- 13,858 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
module Shoulda
  module Context
    class << self
      attr_accessor :contexts
      def contexts # :nodoc:
        @contexts ||= []
      end

      def current_context # :nodoc:
        self.contexts.last
      end

      def add_context(context) # :nodoc:
        self.contexts.push(context)
      end

      def remove_context # :nodoc:
        self.contexts.pop
      end
    end

    module ClassMethods
      # == Should statements
      #
      # Should statements are just syntactic sugar over normal Test::Unit test
      # methods.  A should block contains all the normal code and assertions
      # you're used to seeing, with the added benefit that they can be wrapped
      # inside context blocks (see below).
      #
      # === Example:
      #
      #  class UserTest < Test::Unit::TestCase
      #
      #    def setup
      #      @user = User.new("John", "Doe")
      #    end
      #
      #    should "return its full name"
      #      assert_equal 'John Doe', @user.full_name
      #    end
      #
      #  end
      #
      # ...will produce the following test:
      # * <tt>"test: User should return its full name. "</tt>
      #
      # Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
      #
      # Should statements can also take a Proc as a <tt>:before </tt>option.  This proc runs after any
      # parent context's setups but before the current context's setup.
      #
      # === Example:
      #
      #  context "Some context" do
      #    setup { puts("I run after the :before proc") }
      #
      #    should "run a :before proc", :before => lambda { puts("I run before the setup") }  do
      #      assert true
      #    end
      #  end
      #
      # Should statements can also wrap matchers, making virtually any matcher
      # usable in a macro style. The matcher's description is used to generate a
      # test name and failure message, and the test will pass if the matcher
      # matches the subject.
      #
      # === Example:
      #
      #   should validate_presence_of(:first_name).with_message(/gotta be there/)
      #

      def should(name_or_matcher, options = {}, &blk)
        if Shoulda::Context.current_context
          Shoulda::Context.current_context.should(name_or_matcher, options, &blk)
        else
          context_name = self.name.gsub(/Test/, "")
          context = Shoulda::Context::Context.new(context_name, self) do
            should(name_or_matcher, options, &blk)
          end
          context.build
        end
      end

      # Allows negative tests using matchers. The matcher's description is used
      # to generate a test name and negative failure message, and the test will
      # pass unless the matcher matches the subject.
      #
      # === Example:
      #
      #   should_not set_the_flash
      def should_not(matcher)
        if Shoulda::Context.current_context
          Shoulda::Context.current_context.should_not(matcher)
        else
          context_name = self.name.gsub(/Test/, "")
          context = Shoulda::Context::Context.new(context_name, self) do
            should_not(matcher)
          end
          context.build
        end
      end

      # == Before statements
      #
      # Before statements are should statements that run before the current
      # context's setup. These are especially useful when setting expectations.
      #
      # === Example:
      #
      #  class UserControllerTest < Test::Unit::TestCase
      #    context "the index action" do
      #      setup do
      #        @users = [Factory(:user)]
      #        User.stubs(:find).returns(@users)
      #      end
      #
      #      context "on GET" do
      #        setup { get :index }
      #
      #        should respond_with(:success)
      #
      #        # runs before "get :index"
      #        before_should "find all users" do
      #          User.expects(:find).with(:all).returns(@users)
      #        end
      #      end
      #    end
      #  end
      def before_should(name, &blk)
        should(name, :before => blk) { assert true }
      end

      # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
      def should_eventually(name, options = {}, &blk)
        context_name = self.name.gsub(/Test/, "")
        context = Shoulda::Context::Context.new(context_name, self) do
          should_eventually(name, &blk)
        end
        context.build
      end

      # == Contexts
      #
      # A context block groups should statements under a common set of setup/teardown methods.
      # Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
      # and readability of your test code.
      #
      # A context block can contain setup, should, should_eventually, and teardown blocks.
      #
      #  class UserTest < Test::Unit::TestCase
      #    context "A User instance" do
      #      setup do
      #        @user = User.find(:first)
      #      end
      #
      #      should "return its full name"
      #        assert_equal 'John Doe', @user.full_name
      #      end
      #    end
      #  end
      #
      # This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
      #
      # Contexts may be nested.  Nested contexts run their setup blocks from out to in before each
      # should statement.  They then run their teardown blocks from in to out after each should statement.
      #
      #  class UserTest < Test::Unit::TestCase
      #    context "A User instance" do
      #      setup do
      #        @user = User.find(:first)
      #      end
      #
      #      should "return its full name"
      #        assert_equal 'John Doe', @user.full_name
      #      end
      #
      #      context "with a profile" do
      #        setup do
      #          @user.profile = Profile.find(:first)
      #        end
      #
      #        should "return true when sent :has_profile?"
      #          assert @user.has_profile?
      #        end
      #      end
      #    end
      #  end
      #
      # This code will produce the following methods
      # * <tt>"test: A User instance should return its full name. "</tt>
      # * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
      #
      # <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
      # tests</b>.  This means you do not have to fully commit to the context/should syntax in a test file.

      def context(name, &blk)
        if Shoulda::Context.current_context
          Shoulda::Context.current_context.context(name, &blk)
        else
          context = Shoulda::Context::Context.new(name, self, &blk)
          context.build
        end
      end

      # Returns the class being tested, as determined by the test class name.
      #
      #   class UserTest; described_type; end
      #   # => User
      def described_type
        @described_type ||= self.name.
          gsub(/Test$/, '').
          split('::').
          inject(Object) { |parent, local_name| parent.const_get(local_name) }
      end

      # Sets the return value of the subject instance method:
      #
      #   class UserTest < Test::Unit::TestCase
      #     subject { User.first }
      #
      #     # uses the existing user
      #     should validate_uniqueness_of(:email)
      #   end
      def subject(&block)
        @subject_block = block
      end

      def subject_block # :nodoc:
        @subject_block
      end
    end

    module InstanceMethods
      # Returns an instance of the class under test.
      #
      #   class UserTest
      #     should "be a user" do
      #       assert_kind_of User, subject # passes
      #     end
      #   end
      #
      # The subject can be explicitly set using the subject class method:
      #
      #   class UserTest
      #     subject { User.first }
      #     should "be an existing user" do
      #       assert !subject.new_record? # uses the first user
      #     end
      #   end
      #
      # The subject is used by all macros that require an instance of the class
      # being tested.
      def subject
        @shoulda_subject ||= construct_subject
      end

      def subject_block # :nodoc:
        (@shoulda_context && @shoulda_context.subject_block) || self.class.subject_block
      end

      def get_instance_of(object_or_klass) # :nodoc:
        if object_or_klass.is_a?(Class)
          object_or_klass.new
        else
          object_or_klass
        end
      end

      def instance_variable_name_for(klass) # :nodoc:
        klass.to_s.split('::').last.underscore
      end

      private

      def construct_subject
        if subject_block
          instance_eval(&subject_block)
        else
          get_instance_of(self.class.described_type)
        end
      end
    end

    class Context # :nodoc:

      attr_accessor :name               # my name
      attr_accessor :parent             # may be another context, or the original test::unit class.
      attr_accessor :subcontexts        # array of contexts nested under myself
      attr_accessor :setup_blocks       # blocks given via setup methods
      attr_accessor :teardown_blocks    # blocks given via teardown methods
      attr_accessor :shoulds            # array of hashes representing the should statements
      attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
      attr_accessor :subject_block

      def initialize(name, parent, &blk)
        Shoulda::Context.add_context(self)
        self.name               = name
        self.parent             = parent
        self.setup_blocks       = []
        self.teardown_blocks    = []
        self.shoulds            = []
        self.should_eventuallys = []
        self.subcontexts        = []

        merge_block(&blk)
        Shoulda::Context.remove_context
      end

      def merge_block(&blk)
        blk.bind(self).call
      end

      def context(name, &blk)
        self.subcontexts << Context.new(name, self, &blk)
      end

      def setup(&blk)
        self.setup_blocks << blk
      end

      def teardown(&blk)
        self.teardown_blocks << blk
      end

      def should(name_or_matcher, options = {}, &blk)
        if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
          name = name_or_matcher.description
          blk = lambda { assert_accepts name_or_matcher, subject }
        else
          name = name_or_matcher
        end

        if blk
          self.shoulds << { :name => name, :before => options[:before], :block => blk }
        else
         self.should_eventuallys << { :name => name }
       end
      end

      def should_not(matcher)
        name = matcher.description
        blk = lambda { assert_rejects matcher, subject }
        self.shoulds << { :name => "not #{name}", :block => blk }
      end

      def should_eventually(name, &blk)
        self.should_eventuallys << { :name => name, :block => blk }
      end

      def subject(&block)
        self.subject_block = block
      end

      def subject_block
        return @subject_block if @subject_block
        parent.subject_block
      end

      def full_name
        parent_name = parent.full_name if am_subcontext?
        return [parent_name, name].join(" ").strip
      end

      def am_subcontext?
        parent.is_a?(self.class) # my parent is the same class as myself.
      end

      def test_unit_class
        am_subcontext? ? parent.test_unit_class : parent
      end

      def test_methods
        @test_methods ||= Hash.new { |h,k|
          h[k] = Hash[k.instance_methods.map { |n| [n, true] }]
        }
      end

      def create_test_from_should_hash(should)
        test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym

        if test_methods[test_unit_class][test_name.to_s] then
          warn "  * WARNING: '#{test_name}' is already defined"
        end

        test_methods[test_unit_class][test_name.to_s] = true

        context = self
        test_unit_class.send(:define_method, test_name) do
          @shoulda_context = context
          begin
            context.run_parent_setup_blocks(self)
            should[:before].bind(self).call if should[:before]
            context.run_current_setup_blocks(self)
            should[:block].bind(self).call
          ensure
            context.run_all_teardown_blocks(self)
          end
        end
      end

      def run_all_setup_blocks(binding)
        run_parent_setup_blocks(binding)
        run_current_setup_blocks(binding)
      end

      def run_parent_setup_blocks(binding)
        self.parent.run_all_setup_blocks(binding) if am_subcontext?
      end

      def run_current_setup_blocks(binding)
        setup_blocks.each do |setup_block|
          setup_block.bind(binding).call
        end
      end

      def run_all_teardown_blocks(binding)
        teardown_blocks.reverse.each do |teardown_block|
          teardown_block.bind(binding).call
        end
        self.parent.run_all_teardown_blocks(binding) if am_subcontext?
      end

      def print_should_eventuallys
        should_eventuallys.each do |should|
          test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
          puts "  * DEFERRED: " + test_name
        end
      end

      def build
        shoulds.each do |should|
          create_test_from_should_hash(should)
        end

        subcontexts.each { |context| context.build }

        print_should_eventuallys
      end

      def method_missing(method, *args, &blk)
        test_unit_class.send(method, *args, &blk)
      end

    end
  end
end