Class: CommandUnit::Scenario

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, desc, out_stream, &block) ⇒ Scenario

Returns a new instance of Scenario.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/command-unit/scenario.rb', line 6

def initialize(namespace, desc, out_stream, &block)
  @out_stream = out_stream
  @namespace = namespace
  @id = @@scenario_count += 1
  @desc = desc
  @block = block
  @set_up_block = nil
  @tests = []
  @current_test = nil
  @tear_down_block = nil
  @scenario_set_up_block = nil
  @scenario_tear_down_block = nil
  @tests_run = 0
  @expectations_run = 0
  @expectations_met = 0
  @expectations_not_met = 0
  @inconclusive_expectations = 0
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def block
  @block
end

#current_testObject

Returns the value of attribute current_test.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def current_test
  @current_test
end

#descObject

Returns the value of attribute desc.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def desc
  @desc
end

#namespaceObject

Returns the value of attribute namespace.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def namespace
  @namespace
end

#outObject

Returns the value of attribute out.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def out
  @out
end

#scenario_set_up_blockObject

Returns the value of attribute scenario_set_up_block.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def scenario_set_up_block
  @scenario_set_up_block
end

#scenario_tear_down_blockObject

Returns the value of attribute scenario_tear_down_block.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def scenario_tear_down_block
  @scenario_tear_down_block
end

#set_up_blockObject

Returns the value of attribute set_up_block.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def set_up_block
  @set_up_block
end

#tear_down_blockObject

Returns the value of attribute tear_down_block.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def tear_down_block
  @tear_down_block
end

#testsObject

Returns the value of attribute tests.



102
103
104
# File 'lib/command-unit/scenario.rb', line 102

def tests
  @tests
end

Instance Method Details

#add_test(test) ⇒ Object



97
98
99
100
# File 'lib/command-unit/scenario.rb', line 97

def add_test(test)
  @tests.push test
  @current_test = test
end

#run(hooks, out_stream = @out_stream) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/command-unit/scenario.rb', line 25

def run(hooks, out_stream=@out_stream)
  raise "CommandUnit::Scenario.run hooks must be of type Hooks" unless hooks.is_a? Hooks
  hooks.fire(:before_scenario)
  scenario_failed = false
  out_stream.puts "\nRunning scenario #{@id}: #{@desc}"
  @@current_scenario = self
  @block.call
  context = {}
  @scenario_set_up_block.call(context) unless @scenario_set_up_block.nil?
  @tests.each do |test|
    hooks.fire(:before_test)
    test_failed = false
    out_stream.puts "\tWhen I #{test.when_i_text}"
    @tests_run += 1
    @set_up_block.call(context) unless @set_up_block.nil?
    test.when_i_block.call(context) unless test.when_i_block.nil?
    test.expectations.each do |expectation|
      hooks.fire :before_expectation
      out_stream.print "\t\tI expect #{expectation.desc}..."
      result = expectation.block.call(context)
      @expectations_run += 1
      if result.respond_to? :success?
        if result.success?
          hooks.fire :expectation_pass
          @expectations_met +=1
          out_stream.puts "Success! #{result.message}".console_green
        else
          hooks.fire :expectation_fail
          test_failed = true
          scenario_failed = true
          @expectations_not_met +=1
          out_stream.puts "Failure!".console_red
          out_stream.puts result.message
        end
      else
        test_failed = true
        @inconclusive_expectations += 1
        out_stream.puts "Inconclusive! #{result}".console_yellow
      end
      hooks.fire :after_expectation
    end
    if test_failed
      hooks.fire :test_fail
    else
      hooks.fire :test_pass
    end

    hooks.fire :after_test
    @tear_down_block.call(context) unless @tear_down_block.nil?
  end

  if scenario_failed
    hooks.fire :scenario_fail
  else
    hooks.fire :scenario_pass
  end

  hooks.fire :after_scenario
  @scenario_tear_down_block.call(context) unless @scenario_tear_down_block.nil?
  @@current_scenario = nil

  colour = @expectations_not_met == 0 ? String::CONSOLE_GREEN : String::CONSOLE_RED

  out_stream.puts "Scenario #{@id} finished, #{@tests_run} tests, #{@expectations_run} expectations with #{@expectations_met} successful and #{@expectations_not_met} failures.".console_colour(colour)
end

#run_silent(hooks) ⇒ Object



91
92
93
94
95
# File 'lib/command-unit/scenario.rb', line 91

def run_silent(hooks)
  buffer = StringIO.new
  run(hooks, buffer)
  return buffer.string
end