Module: Spinach::Reporter::Reporting

Included in:
Progress, Stdout
Defined in:
lib/spinach/reporter/reporting.rb

Overview

This module handles Stdoutā€™s reporter error reporting capabilities.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorObject (readonly)

The output buffers to store the reports.



7
8
9
# File 'lib/spinach/reporter/reporting.rb', line 7

def error
  @error
end

#outObject (readonly)

The output buffers to store the reports.



7
8
9
# File 'lib/spinach/reporter/reporting.rb', line 7

def out
  @out
end

#scenarioObject

The last scenario



13
14
15
# File 'lib/spinach/reporter/reporting.rb', line 13

def scenario
  @scenario
end

#scenario_errorObject

The last scenario error



10
11
12
# File 'lib/spinach/reporter/reporting.rb', line 10

def scenario_error
  @scenario_error
end

Instance Method Details

#after_run(success) ⇒ Object

It prints the error summary if the run has failed It always print feature success summary

Parameters:

  • success (True, False)

    whether the run has succeed or not



208
209
210
211
212
# File 'lib/spinach/reporter/reporting.rb', line 208

def after_run(success)
  error_summary unless success
  out.puts ""
  run_summary
end

#before_runObject



198
199
200
# File 'lib/spinach/reporter/reporting.rb', line 198

def before_run(*)
  @start_time = Time.now
end

#error_summaryObject

Prints the errors for this run.



17
18
19
20
21
22
23
24
# File 'lib/spinach/reporter/reporting.rb', line 17

def error_summary
  error.puts "\nError summary:\n"
  report_error_steps
  report_failed_steps
  report_undefined_features
  report_undefined_steps
  report_pending_steps
end

#format_summary(color, steps, message) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/spinach/reporter/reporting.rb', line 189

def format_summary(color, steps, message)
  buffer = []
  buffer << "(".colorize(color)
  buffer << steps.length.to_s.colorize(:"light_#{color}")
  buffer << ") ".colorize(color)
  buffer << message.colorize(color)
  buffer.join
end

#full_error(error) ⇒ String

Returns a complete error report

Parameters:

  • error (Array)

    An array containing the feature, scenario, step and exception

Returns:

  • (String)

    The coplete error report



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/spinach/reporter/reporting.rb', line 128

def full_error(error)
  feature, scenario, step, exception = error
  output = "\n"
  output += report_exception(exception)
  output +="\n"

  if exception.kind_of?(Spinach::StepNotDefinedException)
    output << "\n"
    output << "        You can define it with: \n\n".red
    suggestion = Generators::StepGenerator.new(step).generate
    suggestion.split("\n").each do |line|
      output << "          #{line}\n".red
    end
    output << "\n"
  elsif exception.kind_of?(Spinach::StepPendingException)
    output << "        Reason: '#{exception.reason}'".yellow
    output << "\n"
  else
    if options[:backtrace]
      output += "\n"
      exception.backtrace.map do |line|
        output << "        #{line}\n"
      end
    else
      output << "        #{exception.backtrace[0]}"
    end
  end
  output
end

#full_step(step) ⇒ Object

Constructs the full step definition

Parameters:

  • step (Hash)

    The step.



185
186
187
# File 'lib/spinach/reporter/reporting.rb', line 185

def full_step(step)
  "#{step.keyword} #{step.name}"
end

#report_error(error, format = :summarized) ⇒ String

Prints an error in a nice format

Parameters:

  • error (Array)

    An array containing the feature, scenario, step and exception

  • format (Symbol) (defaults to: :summarized)

    The format to output the error. Currently supproted formats are :summarized (default) and :full

Returns:

  • (String)

    The error report



88
89
90
91
92
93
94
95
96
97
# File 'lib/spinach/reporter/reporting.rb', line 88

def report_error(error, format=:summarized)
  case format
  when :summarized
    self.error.puts summarized_error(error)
  when :full
    self.error.puts full_error(error)
  else
    raise "Format not defined"
  end
end

#report_error_stepsObject



26
27
28
# File 'lib/spinach/reporter/reporting.rb', line 26

def report_error_steps
  report_errors('Errors', error_steps, :light_red) if error_steps.any?
end

#report_errors(banner, steps, color) ⇒ Object

Prints the error for a given set of steps

Parameters:

  • banner (String)

    the text to prepend as the title

  • steps (Array)

    the steps to output

  • color (Symbol)

    The color code to use with Colorize to colorize the output.



68
69
70
71
72
73
74
# File 'lib/spinach/reporter/reporting.rb', line 68

def report_errors(banner, steps, color)
  error.puts "  #{banner} (#{steps.length})".colorize(color)
  steps.each do |error|
    report_error error
  end
  error.puts ""
end

#report_exception(exception) ⇒ String

Prints a information when an exception is raised.

Parameters:

  • exception (Exception)

    The exception to report

Returns:

  • (String)

    The exception report



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/spinach/reporter/reporting.rb', line 166

def report_exception(exception)
  output = exception.message.split("\n").map{ |line|
    "        #{line}"
  }.join("\n")

  if exception.kind_of?(Spinach::StepNotDefinedException)
    output.red
  elsif exception.kind_of?(Spinach::StepPendingException)
    output.yellow
  else
    output.red
  end
end

#report_failed_stepsObject



30
31
32
# File 'lib/spinach/reporter/reporting.rb', line 30

def report_failed_steps
  report_errors('Failures', failed_steps, :light_red) if failed_steps.any?
end

#report_pending_stepsObject



41
42
43
44
45
46
# File 'lib/spinach/reporter/reporting.rb', line 41

def report_pending_steps
  if pending_steps.any?
    error.puts "\nPending steps summary:\n"
    report_errors('Pending steps', pending_steps, :yellow)
  end
end

#report_undefined_featuresObject



48
49
50
51
52
53
54
55
# File 'lib/spinach/reporter/reporting.rb', line 48

def report_undefined_features
  if undefined_features.any?
    error.puts "  Undefined features (#{undefined_features.length})".red
    undefined_features.each do |feature|
      error.puts "    #{feature.name}".red
    end
  end
end

#report_undefined_stepsObject



34
35
36
37
38
39
# File 'lib/spinach/reporter/reporting.rb', line 34

def report_undefined_steps
  if undefined_steps.any?
    error.puts "\nUndefined steps summary:\n"
    report_errors('Undefined steps', undefined_steps, :red)
  end
end

#run_summaryObject

Prints the feature success summary for this run.



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/spinach/reporter/reporting.rb', line 216

def run_summary
  successful_summary = format_summary(:green,  successful_steps, 'Successful')
  undefined_summary  = format_summary(:red,    undefined_steps,  'Undefined')
  pending_summary    = format_summary(:yellow, pending_steps,    'Pending')
  failed_summary     = format_summary(:red,    failed_steps,     'Failed')
  error_summary      = format_summary(:red,    error_steps,      'Error')

  out.puts "Steps Summary: #{successful_summary}, #{pending_summary}, #{undefined_summary}, #{failed_summary}, #{error_summary}\n\n"
  out.puts "Finished in #{Time.now - @start_time} seconds\n\n" if @start_time

  @orderer.attach_summary(out) if @orderer
end

#summarized_error(error) ⇒ String

Returns summarized error report

Parameters:

  • error (Array)

    An array containing the feature, scenario, step and exception

Returns:

  • (String)

    The summarized error report



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spinach/reporter/reporting.rb', line 107

def summarized_error(error)
  feature, scenario, step, exception = error
  summary = "    #{feature.name} :: #{scenario.name} :: #{full_step step}"
  if exception.kind_of?(Spinach::StepNotDefinedException)
    summary.red
  elsif exception.kind_of?(Spinach::StepPendingException)
    summary += "\n      Reason: '#{exception.reason}'\n"
    summary.yellow
  else
    summary.red
  end
end