Class: Shoulda::Context::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/context/context.rb

Overview

:nodoc:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Context) initialize(name, parent, &blk)

A new instance of Context



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/shoulda/context/context.rb', line 295

def initialize(name, parent, &blk)
  Shoulda::Context.add_context(self)
  self.name               = name
  self.parent             = parent
  self.setup_blocks       = []
  self.teardown_blocks    = []
  self.shoulds            = []
  self.should_eventuallys = []
  self.subcontexts        = []

  if block_given?
    merge_block(&blk)
  else
    merge_block { warn "  * WARNING: Block missing for context '#{full_name}'" }
  end
  Shoulda::Context.remove_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &blk)



444
445
446
# File 'lib/shoulda/context/context.rb', line 444

def method_missing(method, *args, &blk)
  test_unit_class.send(method, *args, &blk)
end

Instance Attribute Details

- (Object) name

Returns the value of attribute name



286
287
288
# File 'lib/shoulda/context/context.rb', line 286

def name
  @name
end

- (Object) parent

Returns the value of attribute parent



287
288
289
# File 'lib/shoulda/context/context.rb', line 287

def parent
  @parent
end

- (Object) setup_blocks

Returns the value of attribute setup_blocks



289
290
291
# File 'lib/shoulda/context/context.rb', line 289

def setup_blocks
  @setup_blocks
end

- (Object) should_eventuallys

my name may be another context, or the original test::unit class. array of contexts nested under myself blocks given via setup methods blocks given via teardown methods array of hashes representing the should statements array of hashes representing the should eventually statements



292
293
294
# File 'lib/shoulda/context/context.rb', line 292

def should_eventuallys
  @should_eventuallys
end

- (Object) shoulds

Returns the value of attribute shoulds



291
292
293
# File 'lib/shoulda/context/context.rb', line 291

def shoulds
  @shoulds
end

- (Object) subcontexts

Returns the value of attribute subcontexts



288
289
290
# File 'lib/shoulda/context/context.rb', line 288

def subcontexts
  @subcontexts
end

- (Object) subject_block

Returns the value of attribute subject_block



293
294
295
# File 'lib/shoulda/context/context.rb', line 293

def subject_block
  @subject_block
end

- (Object) teardown_blocks

Returns the value of attribute teardown_blocks



290
291
292
# File 'lib/shoulda/context/context.rb', line 290

def teardown_blocks
  @teardown_blocks
end

Instance Method Details

- (Boolean) am_subcontext?

Returns:

  • (Boolean)


368
369
370
# File 'lib/shoulda/context/context.rb', line 368

def am_subcontext?
  parent.is_a?(self.class) # my parent is the same class as myself.
end

- (Object) build



434
435
436
437
438
439
440
441
442
# File 'lib/shoulda/context/context.rb', line 434

def build
  shoulds.each do |should|
    create_test_from_should_hash(should)
  end

  subcontexts.each { |context| context.build }

  print_should_eventuallys
end

- (Object) context(name, &blk)



317
318
319
# File 'lib/shoulda/context/context.rb', line 317

def context(name, &blk)
  self.subcontexts << Context.new(name, self, &blk)
end

- (Object) create_test_from_should_hash(should)



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/shoulda/context/context.rb', line 382

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

- (Object) full_name



363
364
365
366
# File 'lib/shoulda/context/context.rb', line 363

def full_name
  parent_name = parent.full_name if am_subcontext?
  return [parent_name, name].join(" ").strip
end

- (Object) merge_block(&blk)



313
314
315
# File 'lib/shoulda/context/context.rb', line 313

def merge_block(&blk)
  blk.bind(self).call
end


427
428
429
430
431
432
# File 'lib/shoulda/context/context.rb', line 427

def print_should_eventuallys
  should_eventuallys.each do |should|
    test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
    puts "  * DEFERRED: " + test_name
  end
end

- (Object) run_all_setup_blocks(binding)



405
406
407
408
# File 'lib/shoulda/context/context.rb', line 405

def run_all_setup_blocks(binding)
  run_parent_setup_blocks(binding)
  run_current_setup_blocks(binding)
end

- (Object) run_all_teardown_blocks(binding)



420
421
422
423
424
425
# File 'lib/shoulda/context/context.rb', line 420

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

- (Object) run_current_setup_blocks(binding)



414
415
416
417
418
# File 'lib/shoulda/context/context.rb', line 414

def run_current_setup_blocks(binding)
  setup_blocks.each do |setup_block|
    setup_block.bind(binding).call
  end
end

- (Object) run_parent_setup_blocks(binding)



410
411
412
# File 'lib/shoulda/context/context.rb', line 410

def run_parent_setup_blocks(binding)
  self.parent.run_all_setup_blocks(binding) if am_subcontext?
end

- (Object) setup(&blk)



321
322
323
# File 'lib/shoulda/context/context.rb', line 321

def setup(&blk)
  self.setup_blocks << blk
end

- (Object) should(name_or_matcher, options = {}, &blk)



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/shoulda/context/context.rb', line 329

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

- (Object) should_eventually(name, &blk)



350
351
352
# File 'lib/shoulda/context/context.rb', line 350

def should_eventually(name, &blk)
  self.should_eventuallys << { :name => name, :block => blk }
end

- (Object) should_not(matcher)



344
345
346
347
348
# File 'lib/shoulda/context/context.rb', line 344

def should_not(matcher)
  name = matcher.description
  blk = lambda { assert_rejects matcher, subject }
  self.shoulds << { :name => "not #{name}", :block => blk }
end

- (Object) subject(&block)



354
355
356
# File 'lib/shoulda/context/context.rb', line 354

def subject(&block)
  self.subject_block = block
end

- (Object) teardown(&blk)



325
326
327
# File 'lib/shoulda/context/context.rb', line 325

def teardown(&blk)
  self.teardown_blocks << blk
end

- (Object) test_methods



376
377
378
379
380
# File 'lib/shoulda/context/context.rb', line 376

def test_methods
  @test_methods ||= Hash.new { |h,k|
    h[k] = Hash[k.instance_methods.map { |n| [n, true] }]
  }
end

- (Object) test_unit_class



372
373
374
# File 'lib/shoulda/context/context.rb', line 372

def test_unit_class
  am_subcontext? ? parent.test_unit_class : parent
end