Module: Spinach::Reporter::ErrorReporting

Included in:
Console
Defined in:
lib/spinach-console-reporter/error_reporting.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#error_summaryObject

Prints the errors for this run.



8
9
10
11
12
13
14
15
# File 'lib/spinach-console-reporter/error_reporting.rb', line 8

def error_summary
  error.puts "\nError summary:\n"
  report_error_steps
  report_failed_steps
  report_undefined_features
  report_undefined_steps
  report_pending_steps
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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/spinach-console-reporter/error_reporting.rb', line 113

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".yellow
    suggestion = Generators::StepGenerator.new(step).generate
    suggestion.split("\n").each do |line|
      output << "          #{line}\n".yellow
    end
    output << "\n"
  elsif exception.kind_of?(Spinach::StepPendingException)
    output << yellow("        Reason: '#{exception.reason}'")
    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

#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



73
74
75
76
77
78
79
80
81
82
# File 'lib/spinach-console-reporter/error_reporting.rb', line 73

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



17
18
19
# File 'lib/spinach-console-reporter/error_reporting.rb', line 17

def report_error_steps
  report_errors('Errors', error_steps, :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.



53
54
55
56
57
58
59
# File 'lib/spinach-console-reporter/error_reporting.rb', line 53

def report_errors(banner, steps, color)
  error.puts send(color, "  #{banner} (#{steps.length})")
  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



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/spinach-console-reporter/error_reporting.rb', line 152

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

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

#report_failed_stepsObject



21
22
23
# File 'lib/spinach-console-reporter/error_reporting.rb', line 21

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

#report_pending_stepsObject



29
30
31
# File 'lib/spinach-console-reporter/error_reporting.rb', line 29

def report_pending_steps
  report_errors('Pending steps', pending_steps, :yellow) if pending_steps.any?
end

#report_undefined_featuresObject



33
34
35
36
37
38
39
40
# File 'lib/spinach-console-reporter/error_reporting.rb', line 33

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

#report_undefined_stepsObject



25
26
27
# File 'lib/spinach-console-reporter/error_reporting.rb', line 25

def report_undefined_steps
  report_errors('Undefined steps', undefined_steps, :yellow) if undefined_steps.any?
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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spinach-console-reporter/error_reporting.rb', line 92

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