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
Returns a new instance of Context.
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/shoulda/context.rb', line 291
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
428
429
430
|
# File 'lib/shoulda/context.rb', line 428
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
|
Instance Attribute Details
#name ⇒ Object
282
283
284
|
# File 'lib/shoulda/context.rb', line 282
def name
@name
end
|
#parent ⇒ Object
may be another context, or the original test::unit class.
283
284
285
|
# File 'lib/shoulda/context.rb', line 283
def parent
@parent
end
|
#setup_blocks ⇒ Object
blocks given via setup methods
285
286
287
|
# File 'lib/shoulda/context.rb', line 285
def setup_blocks
@setup_blocks
end
|
#should_eventuallys ⇒ Object
array of hashes representing the should eventually statements
288
289
290
|
# File 'lib/shoulda/context.rb', line 288
def should_eventuallys
@should_eventuallys
end
|
#shoulds ⇒ Object
array of hashes representing the should statements
287
288
289
|
# File 'lib/shoulda/context.rb', line 287
def shoulds
@shoulds
end
|
#subcontexts ⇒ Object
array of contexts nested under myself
284
285
286
|
# File 'lib/shoulda/context.rb', line 284
def subcontexts
@subcontexts
end
|
#subject_block ⇒ Object
Returns the value of attribute subject_block.
289
290
291
|
# File 'lib/shoulda/context.rb', line 289
def subject_block
@subject_block
end
|
#teardown_blocks ⇒ Object
blocks given via teardown methods
286
287
288
|
# File 'lib/shoulda/context.rb', line 286
def teardown_blocks
@teardown_blocks
end
|
Instance Method Details
#am_subcontext? ⇒ Boolean
360
361
362
|
# File 'lib/shoulda/context.rb', line 360
def am_subcontext?
parent.is_a?(self.class) end
|
#build ⇒ Object
418
419
420
421
422
423
424
425
426
|
# File 'lib/shoulda/context.rb', line 418
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
309
310
311
|
# File 'lib/shoulda/context.rb', line 309
def context(name, &blk)
self.subcontexts << Context.new(name, self, &blk)
end
|
#create_test_from_should_hash(should) ⇒ Object
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/shoulda/context.rb', line 368
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
@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
|
#full_name ⇒ Object
355
356
357
358
|
# File 'lib/shoulda/context.rb', line 355
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
|
#merge_block(&blk) ⇒ Object
305
306
307
|
# File 'lib/shoulda/context.rb', line 305
def merge_block(&blk)
blk.bind(self).call
end
|
#print_should_eventuallys ⇒ Object
411
412
413
414
415
416
|
# File 'lib/shoulda/context.rb', line 411
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
389
390
391
392
|
# File 'lib/shoulda/context.rb', line 389
def run_all_setup_blocks(binding)
run_parent_setup_blocks(binding)
run_current_setup_blocks(binding)
end
|
#run_all_teardown_blocks(binding) ⇒ Object
404
405
406
407
408
409
|
# File 'lib/shoulda/context.rb', line 404
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
398
399
400
401
402
|
# File 'lib/shoulda/context.rb', line 398
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
394
395
396
|
# File 'lib/shoulda/context.rb', line 394
def run_parent_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
end
|
#setup(&blk) ⇒ Object
313
314
315
|
# File 'lib/shoulda/context.rb', line 313
def setup(&blk)
self.setup_blocks << blk
end
|
#should(name_or_matcher, options = {}, &blk) ⇒ Object
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/shoulda/context.rb', line 321
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
|
#should_eventually(name, &blk) ⇒ Object
342
343
344
|
# File 'lib/shoulda/context.rb', line 342
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
|
#should_not(matcher) ⇒ Object
336
337
338
339
340
|
# File 'lib/shoulda/context.rb', line 336
def should_not(matcher)
name = matcher.description
blk = lambda { assert_rejects matcher, subject }
self.shoulds << { :name => "not #{name}", :block => blk }
end
|
#subject(&block) ⇒ Object
346
347
348
|
# File 'lib/shoulda/context.rb', line 346
def subject(&block)
self.subject_block = block
end
|
#teardown(&blk) ⇒ Object
317
318
319
|
# File 'lib/shoulda/context.rb', line 317
def teardown(&blk)
self.teardown_blocks << blk
end
|
#test_unit_class ⇒ Object
364
365
366
|
# File 'lib/shoulda/context.rb', line 364
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
|