Class: CommandUnit::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/command-unit/hooks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHooks

Returns a new instance of Hooks.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/command-unit/hooks.rb', line 7

def initialize
  @before_scenario = []
  @scenario_pass = []
  @scenario_fail = []
  @after_scenario = []
  @before_test = []
  @test_pass = []
  @test_fail = []
  @after_test = []
  @before_expectation = []
  @expectation_pass = []
  @expectation_fail = []
  @after_expectation = []
  # Note: I know this is a smell, just trying to get tests passing...
  # Looks ripe for inheriting the hooks, and passing in a composition
  # of all hooks to the test runner...
  @totaliser = Totaliser.new
  add :before_scenario do totaliser.scenario_run end
  add :scenario_pass do totaliser.scenario_pass end
  add :scenario_fail do totaliser.scenario_fail end
  add :before_test do totaliser.test_run end
  add :test_pass do totaliser.test_pass end
  add :test_fail do totaliser.test_fail end
  add :before_expectation do totaliser.expectation_run end
  add :expectation_pass do totaliser.expectation_pass end
  add :expectation_fail do totaliser.expectation_fail end
end

Instance Attribute Details

#after_expectationObject (readonly)

Returns the value of attribute after_expectation.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def after_expectation
  @after_expectation
end

#after_scenarioObject (readonly)

Returns the value of attribute after_scenario.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def after_scenario
  @after_scenario
end

#after_testObject (readonly)

Returns the value of attribute after_test.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def after_test
  @after_test
end

#before_expectationObject (readonly)

Returns the value of attribute before_expectation.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def before_expectation
  @before_expectation
end

#before_scenarioObject (readonly)

Returns the value of attribute before_scenario.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def before_scenario
  @before_scenario
end

#before_testObject (readonly)

Returns the value of attribute before_test.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def before_test
  @before_test
end

#expectation_failObject (readonly)

Returns the value of attribute expectation_fail.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def expectation_fail
  @expectation_fail
end

#expectation_passObject (readonly)

Returns the value of attribute expectation_pass.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def expectation_pass
  @expectation_pass
end

#scenario_failObject (readonly)

Returns the value of attribute scenario_fail.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def scenario_fail
  @scenario_fail
end

#scenario_passObject (readonly)

Returns the value of attribute scenario_pass.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def scenario_pass
  @scenario_pass
end

#test_failObject (readonly)

Returns the value of attribute test_fail.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def test_fail
  @test_fail
end

#test_passObject (readonly)

Returns the value of attribute test_pass.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def test_pass
  @test_pass
end

#totaliserObject (readonly)

Returns the value of attribute totaliser.



35
36
37
# File 'lib/command-unit/hooks.rb', line 35

def totaliser
  @totaliser
end

Instance Method Details

#add(event_name, &handler) ⇒ Object



40
41
42
43
44
# File 'lib/command-unit/hooks.rb', line 40

def add(event_name, &handler)
  raise "CommandUnit::Hooks.add No such event '#{event_name}'" unless self.respond_to? event_name
  handlers = instance_variable_get "@#{event_name}"
  handlers.push handler
end

#fire(event_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/command-unit/hooks.rb', line 46

def fire(event_name)
  raise "CommandUnit::Hooks.fire No such event '#{event_name}'" unless self.respond_to? event_name
  handlers = instance_variable_get "@#{event_name}"
  handlers.each do |handler|
    handler.call
  end
end