Class: RunningMan::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/running_man/block.rb

Direct Known Subclasses

ActiveRecordBlock

Defined Under Namespace

Modules: TestClassMethods

Instance Method Summary collapse

Constructor Details

#initialize(block_arg = nil, &block) ⇒ Block

block_arg - Optional Proc of code that runs only once for the test case. &block - The default Proc of code that runs only once. Falls back to

block_arg if provided.

Returns RunningMan::Block instance.



19
20
21
22
23
24
25
26
# File 'lib/running_man/block.rb', line 19

def initialize(block_arg = nil, &block)
  @block = block || block_arg
  @run   = false
  @ivars = {}
  if !@block
    raise ArgumentError, "needs a block."
  end
end

Instance Method Details

#run(binding) ⇒ Object

Public: This is what is run in the test/unit callback. #run_once is called only the first time, and #run_always is always called.

binding - Object that is running the test (usually a Test::Unit::TestCase).

Returns nothing.



46
47
48
49
50
51
52
# File 'lib/running_man/block.rb', line 46

def run(binding)
  if !run_once?
    @run = true
    run_once(binding)
  end
  run_always(binding)
end

#run_always(binding) ⇒ Object

This sets the instance variables set from #run_once on the test case.

binding - The same Object that is given to #run.

Returns nothing.



73
74
75
76
77
# File 'lib/running_man/block.rb', line 73

def run_always(binding)
  @ivars.each do |ivar, value|
    set_ivar(binding, ivar, value)
  end
end

#run_once(binding) ⇒ Object

This runs the block and stores any new instance variables that were set.

binding - The same Object that is given to #run.

Returns nothing.



59
60
61
62
63
64
65
66
# File 'lib/running_man/block.rb', line 59

def run_once(binding)
  @ivars.clear
  before = binding.instance_variables
  binding.instance_eval(&@block)
  (binding.instance_variables - before).each do |ivar|
    @ivars[ivar] = binding.instance_variable_get(ivar)
  end
end

#run_once?Boolean

Determines whether #run_once has already been called.

Returns a Boolean.

Returns:

  • (Boolean)


91
92
93
# File 'lib/running_man/block.rb', line 91

def run_once?
  !!@run
end

#set_ivar(binding, ivar, value) ⇒ Object

Sets the given instance variable to the test case.

binding - The same Object that is given to #run. ivar - String name of the instance variable to set. value - The Object value of the instance variable.



84
85
86
# File 'lib/running_man/block.rb', line 84

def set_ivar(binding, ivar, value)
  binding.instance_variable_set(ivar, value)
end

#setup(test_class) ⇒ Object

Public: Makes sure the test case class runs this block first. By default, This is added as a single #setup callback. Override this method if the underlying TestCase #setup implementation varies.

test_class - A class inheriting from Test::Unit::TestCase

Returns nothing.



35
36
37
38
# File 'lib/running_man/block.rb', line 35

def setup(test_class)
  block = self
  test_class.setup { block.run(self) }
end