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.
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/shoulda/context.rb', line 278
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
397
398
399
|
# File 'lib/shoulda/context.rb', line 397
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
|
Instance Attribute Details
#name ⇒ Object
269
270
271
|
# File 'lib/shoulda/context.rb', line 269
def name
@name
end
|
#parent ⇒ Object
may be another context, or the original test::unit class.
270
271
272
|
# File 'lib/shoulda/context.rb', line 270
def parent
@parent
end
|
#setup_blocks ⇒ Object
blocks given via setup methods
272
273
274
|
# File 'lib/shoulda/context.rb', line 272
def setup_blocks
@setup_blocks
end
|
#should_eventuallys ⇒ Object
array of hashes representing the should eventually statements
275
276
277
|
# File 'lib/shoulda/context.rb', line 275
def should_eventuallys
@should_eventuallys
end
|
#shoulds ⇒ Object
array of hashes representing the should statements
274
275
276
|
# File 'lib/shoulda/context.rb', line 274
def shoulds
@shoulds
end
|
#subcontexts ⇒ Object
array of contexts nested under myself
271
272
273
|
# File 'lib/shoulda/context.rb', line 271
def subcontexts
@subcontexts
end
|
#subject_block ⇒ Object
Returns the value of attribute subject_block.
276
277
278
|
# File 'lib/shoulda/context.rb', line 276
def subject_block
@subject_block
end
|
#teardown_blocks ⇒ Object
blocks given via teardown methods
273
274
275
|
# File 'lib/shoulda/context.rb', line 273
def teardown_blocks
@teardown_blocks
end
|
Instance Method Details
#am_subcontext? ⇒ Boolean
329
330
331
|
# File 'lib/shoulda/context.rb', line 329
def am_subcontext?
parent.is_a?(self.class) end
|
#build ⇒ Object
387
388
389
390
391
392
393
394
395
|
# File 'lib/shoulda/context.rb', line 387
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
296
297
298
|
# File 'lib/shoulda/context.rb', line 296
def context(name, &blk)
self.subcontexts << Context.new(name, self, &blk)
end
|
#create_test_from_should_hash(should) ⇒ Object
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# File 'lib/shoulda/context.rb', line 337
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
324
325
326
327
|
# File 'lib/shoulda/context.rb', line 324
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
|
#merge_block(&blk) ⇒ Object
292
293
294
|
# File 'lib/shoulda/context.rb', line 292
def merge_block(&blk)
blk.bind(self).call
end
|
#print_should_eventuallys ⇒ Object
380
381
382
383
384
385
|
# File 'lib/shoulda/context.rb', line 380
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
358
359
360
361
|
# File 'lib/shoulda/context.rb', line 358
def run_all_setup_blocks(binding)
run_parent_setup_blocks(binding)
run_current_setup_blocks(binding)
end
|
#run_all_teardown_blocks(binding) ⇒ Object
373
374
375
376
377
378
|
# File 'lib/shoulda/context.rb', line 373
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
367
368
369
370
371
|
# File 'lib/shoulda/context.rb', line 367
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
363
364
365
|
# File 'lib/shoulda/context.rb', line 363
def run_parent_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
end
|
#setup(&blk) ⇒ Object
300
301
302
|
# File 'lib/shoulda/context.rb', line 300
def setup(&blk)
self.setup_blocks << blk
end
|
#should(name, options = {}, &blk) ⇒ Object
308
309
310
311
312
313
314
|
# File 'lib/shoulda/context.rb', line 308
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
316
317
318
|
# File 'lib/shoulda/context.rb', line 316
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
|
#subject(&block) ⇒ Object
320
321
322
|
# File 'lib/shoulda/context.rb', line 320
def subject(&block)
self.subject_block = block
end
|
#teardown(&blk) ⇒ Object
304
305
306
|
# File 'lib/shoulda/context.rb', line 304
def teardown(&blk)
self.teardown_blocks << blk
end
|
#test_unit_class ⇒ Object
333
334
335
|
# File 'lib/shoulda/context.rb', line 333
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
|