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.
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/shoulda/context.rb', line 284
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
408
409
410
|
# File 'lib/shoulda/context.rb', line 408
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
|
Instance Attribute Details
#name ⇒ Object
275
276
277
|
# File 'lib/shoulda/context.rb', line 275
def name
@name
end
|
#parent ⇒ Object
may be another context, or the original test::unit class.
276
277
278
|
# File 'lib/shoulda/context.rb', line 276
def parent
@parent
end
|
#setup_blocks ⇒ Object
blocks given via setup methods
278
279
280
|
# File 'lib/shoulda/context.rb', line 278
def setup_blocks
@setup_blocks
end
|
#should_eventuallys ⇒ Object
array of hashes representing the should eventually statements
281
282
283
|
# File 'lib/shoulda/context.rb', line 281
def should_eventuallys
@should_eventuallys
end
|
#shoulds ⇒ Object
array of hashes representing the should statements
280
281
282
|
# File 'lib/shoulda/context.rb', line 280
def shoulds
@shoulds
end
|
#subcontexts ⇒ Object
array of contexts nested under myself
277
278
279
|
# File 'lib/shoulda/context.rb', line 277
def subcontexts
@subcontexts
end
|
#subject_block ⇒ Object
Returns the value of attribute subject_block.
282
283
284
|
# File 'lib/shoulda/context.rb', line 282
def subject_block
@subject_block
end
|
#teardown_blocks ⇒ Object
blocks given via teardown methods
279
280
281
|
# File 'lib/shoulda/context.rb', line 279
def teardown_blocks
@teardown_blocks
end
|
Instance Method Details
#am_subcontext? ⇒ Boolean
340
341
342
|
# File 'lib/shoulda/context.rb', line 340
def am_subcontext?
parent.is_a?(self.class) end
|
#build ⇒ Object
398
399
400
401
402
403
404
405
406
|
# File 'lib/shoulda/context.rb', line 398
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
302
303
304
|
# File 'lib/shoulda/context.rb', line 302
def context(name, &blk)
self.subcontexts << Context.new(name, self, &blk)
end
|
#create_test_from_should_hash(should) ⇒ Object
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
# File 'lib/shoulda/context.rb', line 348
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
335
336
337
338
|
# File 'lib/shoulda/context.rb', line 335
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
|
#merge_block(&blk) ⇒ Object
298
299
300
|
# File 'lib/shoulda/context.rb', line 298
def merge_block(&blk)
blk.bind(self).call
end
|
#print_should_eventuallys ⇒ Object
391
392
393
394
395
396
|
# File 'lib/shoulda/context.rb', line 391
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
369
370
371
372
|
# File 'lib/shoulda/context.rb', line 369
def run_all_setup_blocks(binding)
run_parent_setup_blocks(binding)
run_current_setup_blocks(binding)
end
|
#run_all_teardown_blocks(binding) ⇒ Object
384
385
386
387
388
389
|
# File 'lib/shoulda/context.rb', line 384
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
378
379
380
381
382
|
# File 'lib/shoulda/context.rb', line 378
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
374
375
376
|
# File 'lib/shoulda/context.rb', line 374
def run_parent_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
end
|
#setup(&blk) ⇒ Object
306
307
308
|
# File 'lib/shoulda/context.rb', line 306
def setup(&blk)
self.setup_blocks << blk
end
|
#should(name, options = {}, &blk) ⇒ Object
314
315
316
317
318
319
320
|
# File 'lib/shoulda/context.rb', line 314
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
322
323
324
|
# File 'lib/shoulda/context.rb', line 322
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
|
#subject(&block) ⇒ Object
326
327
328
|
# File 'lib/shoulda/context.rb', line 326
def subject(&block)
self.subject_block = block
end
|
#teardown(&blk) ⇒ Object
310
311
312
|
# File 'lib/shoulda/context.rb', line 310
def teardown(&blk)
self.teardown_blocks << blk
end
|
#test_unit_class ⇒ Object
344
345
346
|
# File 'lib/shoulda/context.rb', line 344
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
|