Class: Shoulda::Context
- Inherits:
-
Object
show all
- Defined in:
- lib/shoulda/context.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#am_subcontext? ⇒ Boolean
-
#build ⇒ Object
-
#context(name, &blk) ⇒ Object
-
#create_test_from_should_hash(should) ⇒ Object
-
#fast_context(name, &blk) ⇒ Object
-
#full_name ⇒ Object
-
#initialize(name, parent, &blk) ⇒ Context
constructor
A new instance of Context.
-
#merge_block(&blk) ⇒ Object
-
#method_missing(method, *args, &blk) ⇒ Object
-
#print_should_eventuallys ⇒ Object
-
#run_all_setup_blocks(binding) ⇒ Object
-
#run_all_teardown_blocks(binding) ⇒ Object
-
#run_current_setup_blocks(binding) ⇒ Object
-
#run_parent_setup_blocks(binding) ⇒ Object
-
#setup(count = :each, &blk) ⇒ Object
-
#should(name_or_matcher, options = {}, &blk) ⇒ Object
-
#should_eventually(name, &blk) ⇒ Object
-
#should_not(matcher) ⇒ Object
-
#subject(&block) ⇒ Object
-
#teardown(&blk) ⇒ Object
-
#test_methods ⇒ Object
-
#test_unit_class ⇒ Object
Constructor Details
#initialize(name, parent, &blk) ⇒ Context
Returns a new instance of Context.
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# File 'lib/shoulda/context.rb', line 341
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
491
492
493
|
# File 'lib/shoulda/context.rb', line 491
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
|
Instance Attribute Details
#name ⇒ Object
332
333
334
|
# File 'lib/shoulda/context.rb', line 332
def name
@name
end
|
#parent ⇒ Object
may be another context, or the original test::unit class.
333
334
335
|
# File 'lib/shoulda/context.rb', line 333
def parent
@parent
end
|
#setup_blocks ⇒ Object
blocks given via setup methods
335
336
337
|
# File 'lib/shoulda/context.rb', line 335
def setup_blocks
@setup_blocks
end
|
#should_eventuallys ⇒ Object
array of hashes representing the should eventually statements
338
339
340
|
# File 'lib/shoulda/context.rb', line 338
def should_eventuallys
@should_eventuallys
end
|
#shoulds ⇒ Object
array of hashes representing the should statements
337
338
339
|
# File 'lib/shoulda/context.rb', line 337
def shoulds
@shoulds
end
|
#subcontexts ⇒ Object
array of contexts nested under myself
334
335
336
|
# File 'lib/shoulda/context.rb', line 334
def subcontexts
@subcontexts
end
|
#subject_block ⇒ Object
Returns the value of attribute subject_block.
339
340
341
|
# File 'lib/shoulda/context.rb', line 339
def subject_block
@subject_block
end
|
#teardown_blocks ⇒ Object
blocks given via teardown methods
336
337
338
|
# File 'lib/shoulda/context.rb', line 336
def teardown_blocks
@teardown_blocks
end
|
Instance Method Details
#am_subcontext? ⇒ Boolean
414
415
416
|
# File 'lib/shoulda/context.rb', line 414
def am_subcontext?
parent.is_a?(self.class) || self.class < parent.class end
|
#build ⇒ Object
481
482
483
484
485
486
487
488
489
|
# File 'lib/shoulda/context.rb', line 481
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
359
360
361
|
# File 'lib/shoulda/context.rb', line 359
def context(name, &blk)
self.subcontexts << Context.new(name, self, &blk)
end
|
#create_test_from_should_hash(should) ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/shoulda/context.rb', line 428
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
|
#fast_context(name, &blk) ⇒ Object
363
364
365
|
# File 'lib/shoulda/context.rb', line 363
def fast_context(name,&blk)
self.subcontexts << FastContext.new(name,self,&blk)
end
|
#full_name ⇒ Object
409
410
411
412
|
# File 'lib/shoulda/context.rb', line 409
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
|
#merge_block(&blk) ⇒ Object
355
356
357
|
# File 'lib/shoulda/context.rb', line 355
def merge_block(&blk)
blk.bind(self).call
end
|
#print_should_eventuallys ⇒ Object
474
475
476
477
478
479
|
# File 'lib/shoulda/context.rb', line 474
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
451
452
453
454
|
# File 'lib/shoulda/context.rb', line 451
def run_all_setup_blocks(binding)
run_parent_setup_blocks(binding)
run_current_setup_blocks(binding)
end
|
#run_all_teardown_blocks(binding) ⇒ Object
467
468
469
470
471
472
|
# File 'lib/shoulda/context.rb', line 467
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
460
461
462
463
464
465
|
# File 'lib/shoulda/context.rb', line 460
def run_current_setup_blocks(binding)
setup_blocks.each do |setup_block|
setup_block.run(binding)
end
end
|
#run_parent_setup_blocks(binding) ⇒ Object
456
457
458
|
# File 'lib/shoulda/context.rb', line 456
def run_parent_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
end
|
#setup(count = :each, &blk) ⇒ Object
367
368
369
|
# File 'lib/shoulda/context.rb', line 367
def setup(count = :each,&blk)
self.setup_blocks << Setup.new(count,&blk)
end
|
#should(name_or_matcher, options = {}, &blk) ⇒ Object
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
# File 'lib/shoulda/context.rb', line 375
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
396
397
398
|
# File 'lib/shoulda/context.rb', line 396
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
|
#should_not(matcher) ⇒ Object
390
391
392
393
394
|
# File 'lib/shoulda/context.rb', line 390
def should_not(matcher)
name = matcher.description
blk = lambda { assert_rejects matcher, subject }
self.shoulds << { :name => "not #{name}", :block => blk }
end
|
#subject(&block) ⇒ Object
400
401
402
|
# File 'lib/shoulda/context.rb', line 400
def subject(&block)
self.subject_block = block
end
|
#teardown(&blk) ⇒ Object
371
372
373
|
# File 'lib/shoulda/context.rb', line 371
def teardown(&blk)
self.teardown_blocks << blk
end
|
#test_methods ⇒ Object
422
423
424
425
426
|
# File 'lib/shoulda/context.rb', line 422
def test_methods
@test_methods ||= Hash.new { |h,k|
h[k] = Hash[k.instance_methods.map { |n| [n, true] }]
}
end
|
#test_unit_class ⇒ Object
418
419
420
|
# File 'lib/shoulda/context.rb', line 418
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
|