Class: Spinach::Reporter::Console

Inherits:
Spinach::Reporter show all
Includes:
ErrorReporting
Defined in:
lib/spinach-console-reporter/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorReporting

#error_summary, #full_error, #report_error, #report_error_steps, #report_errors, #report_exception, #report_failed_steps, #report_pending_steps, #report_undefined_features, #report_undefined_steps, #summarized_error

Constructor Details

#initialize(*args) ⇒ Console

Initialitzes the runner

Parameters:

  • options (Hash)

    Sets a custom output buffer by setting options Sets a custom error buffer by setting options



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

def initialize(*args)
  super(*args)
  @out = options[:output] || $stdout
  @error = options[:error] || $stdout
  @max_step_name_length = 0
end

Instance Attribute Details

#errorObject (readonly)

The output buffers to store the reports.



8
9
10
# File 'lib/spinach-console-reporter/console.rb', line 8

def error
  @error
end

#outObject (readonly)

The output buffers to store the reports.



8
9
10
# File 'lib/spinach-console-reporter/console.rb', line 8

def out
  @out
end

#scenarioObject

The last scenario



14
15
16
# File 'lib/spinach-console-reporter/console.rb', line 14

def scenario
  @scenario
end

#scenario_errorObject

The last scenario error



11
12
13
# File 'lib/spinach-console-reporter/console.rb', line 11

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



196
197
198
199
200
# File 'lib/spinach-console-reporter/console.rb', line 196

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

#after_scenario_run(scenario, step_definitions = nil) ⇒ Object

Adds an error report and re

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



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

def after_scenario_run(scenario, step_definitions = nil)
  if scenario_error
    report_error(scenario_error, :full)
    self.scenario_error = nil
  end
end

#before_feature_run(feature) ⇒ Object

Prints the feature name to the standard output

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



34
35
36
37
# File 'lib/spinach-console-reporter/console.rb', line 34

def before_feature_run(feature)
  name = feature.name
  out.puts %Q|\n#{magenta("Feature:")} #{magenta(name)}|
end

#before_scenario_run(scenario, step_definitions = nil) ⇒ Object

Prints the scenario name to the standard ouput

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



44
45
46
47
48
# File 'lib/spinach-console-reporter/console.rb', line 44

def before_scenario_run(scenario, step_definitions = nil)
  @max_step_name_length = scenario.steps.map(&:name).map(&:length).max if scenario.steps.any?
  name = scenario.name
  out.puts "\n  #{green('Scenario:')} #{green(name)}"
end

#full_step(step) ⇒ Object

Constructs the full step definition

Parameters:

  • step (Hash)

    The step.



219
220
221
# File 'lib/spinach-console-reporter/console.rb', line 219

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

#on_error_step(step, failure, step_location, step_definitions = nil) ⇒ Object

Adds a step that has raised an error to the output buffer.

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format

  • failure (Exception)

    The exception that caused the failure



98
99
100
101
102
# File 'lib/spinach-console-reporter/console.rb', line 98

def on_error_step(step, failure, step_location, step_definitions = nil)
  output_step('!', step, :red, step_location)
  self.scenario_error = [current_feature, current_scenario, step, failure]
  error_steps << scenario_error
end

#on_failed_step(step, failure, step_location, step_definitions = nil) ⇒ Object

Adds a failing step to the output buffer.

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format

  • failure (Exception)

    The exception that caused the failure



84
85
86
87
88
# File 'lib/spinach-console-reporter/console.rb', line 84

def on_failed_step(step, failure, step_location, step_definitions = nil)
  output_step('', step, :red, step_location)
  self.scenario_error = [current_feature, current_scenario, step, failure]
  failed_steps << scenario_error
end

#on_feature_not_found(feature) ⇒ Object

Adds a feature not found message to the output buffer.

Parameters:

  • feature (Hash)

    the feature in a json gherkin format

  • exception (Spinach::FeatureNotFoundException)

    the related exception



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/spinach-console-reporter/console.rb', line 134

def on_feature_not_found(feature)
  generator = Generators::FeatureGenerator.new(feature)
  lines = "Could not find steps for `#{feature.name}` feature\n\n"
  lines << "\nPlease create the file #{generator.filename} at #{generator.path}, with:\n\n"

  lines << generator.generate

  lines.split("\n").each do |line|
    out.puts "    #{line}".yellow
  end
  out.puts "\n\n"

  undefined_features << feature
end

#on_pending_step(step, failure) ⇒ Object

Adds an undefined step to the output buffer.

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format



120
121
122
123
124
# File 'lib/spinach-console-reporter/console.rb', line 120

def on_pending_step(step, failure)
  output_step('P', step, :yellow)
  self.scenario_error = [current_feature, current_scenario, step, failure]
  pending_steps << scenario_error
end

#on_skipped_step(step, step_definitions = nil) ⇒ Object

Adds a step that has been skipped to the output buffer.

Parameters:

  • step (Hash)

    The step that Gherkin extracts



154
155
156
# File 'lib/spinach-console-reporter/console.rb', line 154

def on_skipped_step(step, step_definitions = nil)
  output_step('~', step, :cyan)
end

#on_successful_step(step, step_location, step_definitions = nil) ⇒ Object

Adds a passed step to the output buffer.

Parameters:

  • step (Step)

    The step.

  • step_location (Array)

    The step source location



70
71
72
73
74
# File 'lib/spinach-console-reporter/console.rb', line 70

def on_successful_step(step, step_location, step_definitions = nil)
  output_step('', step, :green, step_location)
  self.scenario = [current_feature, current_scenario, step]
  successful_steps << scenario
end

#on_undefined_step(step, failure, step_definitions = nil) ⇒ Object

Adds an undefined step to the output buffer.

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format



109
110
111
112
113
# File 'lib/spinach-console-reporter/console.rb', line 109

def on_undefined_step(step, failure, step_definitions = nil)
  output_step('?', step, :yellow)
  self.scenario_error = [current_feature, current_scenario, step, failure]
  undefined_steps << scenario_error
end

#output_step(symbol, step, color, step_location = nil) ⇒ Object

Adds to the output buffer a step result

Parameters:

  • symbol (String)

    A symbol to prepend before the step keyword (might be useful to indicate if everything went ok or not).

  • step (Hash)

    The step in a JSON Gherkin format

  • color (Symbol)

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

  • step_location (Array) (defaults to: nil)

    step source location and file line



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/spinach-console-reporter/console.rb', line 173

def output_step(symbol, step, color, step_location = nil)
  step_location = step_location.first.gsub("#{File.expand_path('.')}/", '# ')+":#{step_location.last.to_s}" if step_location
  max_length = @max_step_name_length + 30 # Colorize and output format correction

  # REMEMBER TO CORRECT PREVIOUS MAX LENGTH IF OUTPUT FORMAT IS MODIFIED
  buffer = []
  buffer << indent(4)
  # buffer << symbol.foreground(color).bright
  buffer << indent(2)
  buffer << send(color, step.keyword)
  buffer << indent(1)
  buffer << send(color, step.name)
  joined = buffer.join.ljust(max_length)

  out.puts(joined + white(step_location.to_s))
end

#run_summaryObject

Prints the feature success summary for this run.



204
205
206
207
208
209
210
211
212
# File 'lib/spinach-console-reporter/console.rb', line 204

def run_summary
  successful_summary = format_summary(:green,  successful_steps, 'Successful')
  undefined_summary  = format_summary(:yellow, 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}, #{undefined_summary}, #{pending_summary}, #{failed_summary}, #{error_summary}\n\n"
end