Class: Shoulda::Context
- Inherits:
-
Object
show all
- Defined in:
- lib/shoulda/context.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, parent, &blk) ⇒ Context
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/shoulda/context.rb', line 185
def initialize(name, parent, &blk)
Shoulda.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.remove_context
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
299
300
301
|
# File 'lib/shoulda/context.rb', line 299
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
|
Instance Attribute Details
#name ⇒ Object
177
178
179
|
# File 'lib/shoulda/context.rb', line 177
def name
@name
end
|
#parent ⇒ Object
may be another context, or the original test::unit class.
178
179
180
|
# File 'lib/shoulda/context.rb', line 178
def parent
@parent
end
|
#setup_blocks ⇒ Object
blocks given via setup methods
180
181
182
|
# File 'lib/shoulda/context.rb', line 180
def setup_blocks
@setup_blocks
end
|
#should_eventuallys ⇒ Object
array of hashes representing the should eventually statements
183
184
185
|
# File 'lib/shoulda/context.rb', line 183
def should_eventuallys
@should_eventuallys
end
|
#shoulds ⇒ Object
array of hashes representing the should statements
182
183
184
|
# File 'lib/shoulda/context.rb', line 182
def shoulds
@shoulds
end
|
#subcontexts ⇒ Object
array of contexts nested under myself
179
180
181
|
# File 'lib/shoulda/context.rb', line 179
def subcontexts
@subcontexts
end
|
#teardown_blocks ⇒ Object
blocks given via teardown methods
181
182
183
|
# File 'lib/shoulda/context.rb', line 181
def teardown_blocks
@teardown_blocks
end
|
Instance Method Details
#am_subcontext? ⇒ Boolean
232
233
234
|
# File 'lib/shoulda/context.rb', line 232
def am_subcontext?
parent.is_a?(self.class)
end
|
#build ⇒ Object
289
290
291
292
293
294
295
296
297
|
# File 'lib/shoulda/context.rb', line 289
def build
shoulds.each do |should|
create_test_from_should_hash(should)
end
subcontexts.each { |context| context.build }
print_should_eventuallys
end
|
#context(name, &blk) ⇒ Object
203
204
205
|
# File 'lib/shoulda/context.rb', line 203
def context(name, &blk)
self.subcontexts << Context.new(name, self, &blk)
end
|
#create_test_from_should_hash(should) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
# File 'lib/shoulda/context.rb', line 240
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_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
|
#full_name ⇒ Object
227
228
229
230
|
# File 'lib/shoulda/context.rb', line 227
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
|
#merge_block(&blk) ⇒ Object
199
200
201
|
# File 'lib/shoulda/context.rb', line 199
def merge_block(&blk)
blk.bind(self).call
end
|
#print_should_eventuallys ⇒ Object
282
283
284
285
286
287
|
# File 'lib/shoulda/context.rb', line 282
def print_should_eventuallys
should_eventuallys.each do |should|
test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
puts " * DEFERRED: " + test_name
end
end
|
#run_all_setup_blocks(binding) ⇒ Object
260
261
262
263
|
# File 'lib/shoulda/context.rb', line 260
def run_all_setup_blocks(binding)
run_parent_setup_blocks(binding)
run_current_setup_blocks(binding)
end
|
#run_all_teardown_blocks(binding) ⇒ Object
275
276
277
278
279
280
|
# File 'lib/shoulda/context.rb', line 275
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
|
#run_current_setup_blocks(binding) ⇒ Object
269
270
271
272
273
|
# File 'lib/shoulda/context.rb', line 269
def run_current_setup_blocks(binding)
setup_blocks.each do |setup_block|
setup_block.bind(binding).call
end
end
|
#run_parent_setup_blocks(binding) ⇒ Object
265
266
267
|
# File 'lib/shoulda/context.rb', line 265
def run_parent_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
end
|
#setup(&blk) ⇒ Object
207
208
209
|
# File 'lib/shoulda/context.rb', line 207
def setup(&blk)
self.setup_blocks << blk
end
|
#should(name, options = {}, &blk) ⇒ Object
215
216
217
218
219
220
221
|
# File 'lib/shoulda/context.rb', line 215
def should(name, options = {}, &blk)
if block_given?
self.shoulds << { :name => name, :before => options[:before], :block => blk }
else
self.should_eventuallys << { :name => name }
end
end
|
#should_eventually(name, &blk) ⇒ Object
223
224
225
|
# File 'lib/shoulda/context.rb', line 223
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
|
#teardown(&blk) ⇒ Object
211
212
213
|
# File 'lib/shoulda/context.rb', line 211
def teardown(&blk)
self.teardown_blocks << blk
end
|
#test_unit_class ⇒ Object
236
237
238
|
# File 'lib/shoulda/context.rb', line 236
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
|