Class: GreenPepper::ThenResults

Inherits:
ExampleResult show all
Includes:
KeywordFormat
Defined in:
lib/greenpepper/writer/freetextresult.rb

Instance Attribute Summary collapse

Attributes inherited from ExampleResult

#silent

Instance Method Summary collapse

Methods included from KeywordFormat

#error, #green_highlight, #grey_highlight, #highlight, #indent, #indent2, #newline, #red_highlight, #yellow_highlight

Methods inherited from ExampleResult

#error?, #failure?, #ignored?, #success?

Constructor Details

#initialize(regex, expectations) ⇒ ThenResults

Returns a new instance of ThenResults.



82
83
84
# File 'lib/greenpepper/writer/freetextresult.rb', line 82

def initialize(regex, expectations)
  @regex, @expectations = regex, expectations
end

Instance Attribute Details

#expectationsObject

Returns the value of attribute expectations.



80
81
82
# File 'lib/greenpepper/writer/freetextresult.rb', line 80

def expectations
  @expectations
end

Instance Method Details

#then_highlight(string, result) ⇒ Object



123
124
125
# File 'lib/greenpepper/writer/freetextresult.rb', line 123

def then_highlight(string, result)
  result ? green_highlight(string) : red_highlight(string)
end

#update_stats(results) ⇒ Object



127
128
129
130
131
# File 'lib/greenpepper/writer/freetextresult.rb', line 127

def update_stats(results)
  @expectations[0..-1].each do |exp|
    exp.check_result ? results.add_success : results.add_failure
  end
end

#write(action) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/greenpepper/writer/freetextresult.rb', line 86

def write(action)
  expected_lines = []
  match_data = @regex.match action
  i = match_data.size - 1
  while i > 0
    start_index = match_data.offset(i)[0]
    end_index = match_data.offset(i)[1]
    
    # Type conversion of expected and received value
    converter = TypeConverter.instance
    expected = converter.format_object(match_data[i],
      @expectations[i-1].expected)
    received = converter.format_object(match_data[i],
      @expectations[i-1].received)

    # Highlight match
    action = action[0...start_index] + 
      then_highlight(expected, 
      @expectations[i-1].check_result) + 
      action[end_index..-1]

    # Write expected lines for failures
    unless @expectations[i-1].check_result
      expected_lines << indent( 
        red_highlight("Expected value was #{expected}, " +
        "but #{received} was received"))
    end
    i -= 1
  end

  expected_lines.reverse.each do |line|
    action += line
  end

  action
end