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
shoulda / lib / shoulda / gem / shoulda.rb
100644 247 lines (214 sloc) 7.711 kb
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
require File.join(File.dirname(__FILE__), 'proc_extensions')
 
module Thoughtbot
  module Shoulda
    class << self
      attr_accessor :current_context
    end
 
    VERSION = '1.1.1'
 
    # = 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.
 
    def should(name, &blk)
      if Shoulda.current_context
        block_given? ? Shoulda.current_context.should(name, &blk) : Should.current_context.should_eventually(name)
      else
        context_name = self.name.gsub(/Test/, "")
        context = Thoughtbot::Shoulda::Context.new(context_name, self) do
          block_given? ? should(name, &blk) : should_eventually(name)
        end
        context.build
      end
    end
 
    # Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
    def should_eventually(name, &blk)
      context_name = self.name.gsub(/Test/, "")
      context = Thoughtbot::Shoulda::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.current_context
        Shoulda.current_context.context(name, &blk)
      else
        context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
        context.build
      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 # block given via a setup method
      attr_accessor :teardown_blocks # block given via a teardown method
      attr_accessor :shoulds # array of hashes representing the should statements
      attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
 
      def initialize(name, parent, &blk)
        Shoulda.current_context = self
        self.name = name
        self.parent = parent
        self.setup_blocks = []
        self.teardown_blocks = []
        self.shoulds = []
        self.should_eventuallys = []
        self.subcontexts = []
 
        blk.bind(self).call
        Shoulda.current_context = nil
      end
 
      def context(name, &blk)
        subcontexts << Context.new(name, self, &blk)
        Shoulda.current_context = self
      end
 
      def setup(&blk)
        self.setup_blocks << blk
      end
 
      def teardown(&blk)
        self.teardown_blocks << blk
      end
 
      def should(name, &blk)
        if block_given?
          self.shoulds << { :name => name, :block => blk }
        else
         self.should_eventuallys << { :name => name }
       end
      end
 
      def should_eventually(name, &blk)
        self.should_eventuallys << { :name => name, :block => blk }
      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 create_test_from_should_hash(should)
        test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
 
        if test_unit_class.instance_methods.include?(test_name.to_s)
          warn " * WARNING: '#{test_name}' is already defined"
        end
        
        context = self
        test_unit_class.send(:define_method, test_name) do
          begin
            context.run_all_setup_blocks(self)
            should[:block].bind(self).call
          ensure
            context.run_all_teardown_blocks(self)
          end
        end
      end
 
      def run_all_setup_blocks(binding)
        self.parent.run_all_setup_blocks(binding) if am_subcontext?
        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
 
module Test # :nodoc: all
  module Unit
    class TestCase
      extend Thoughtbot::Shoulda
    end
  end
end