Class: Spinach::Reporter::Stdout

Inherits:
Spinach::Reporter show all
Includes:
Reporting
Defined in:
lib/spinach/reporter/stdout.rb

Overview

The Stdout reporter outputs the runner results to the standard output

Instance Attribute Summary

Attributes included from Reporting

#error, #out, #scenario, #scenario_error

Attributes inherited from Spinach::Reporter

#current_feature, #current_scenario, #error_steps, #failed_steps, #options, #pending_steps, #successful_steps, #undefined_features, #undefined_steps

Instance Method Summary collapse

Methods included from Reporting

#after_run, #before_run, #error_summary, #format_summary, #full_error, #full_step, #report_error, #report_error_steps, #report_errors, #report_exception, #report_failed_steps, #report_pending_steps, #report_undefined_features, #report_undefined_steps, #run_summary, #summarized_error

Methods inherited from Spinach::Reporter

#after_feature_run, #after_run, #around_scenario_run, #before_run, #bind, #clear_current_feature, #clear_current_scenario, #set_current_feature, #set_current_scenario

Constructor Details

#initialize(*args) ⇒ Stdout

Initializes the reporter

Parameters:

  • options (Hash)

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



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

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

Instance Method Details

#after_scenario_run(scenario, step_definitions = nil) ⇒ Object

Adds an error report and re

Parameters:

  • data (Hash)

    The feature in a JSON GherkinRubyRuby format



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

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 GherkinRubyRuby format



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

def before_feature_run(feature)
  name = feature.name
  out.puts "\n#{'Feature:'.magenta} #{name.light_magenta}"
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 GherkinRubyRuby format



39
40
41
42
43
# File 'lib/spinach/reporter/stdout.rb', line 39

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  #{'Scenario:'.green} #{name.light_green}"
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 GherkinRubyRuby format

  • failure (Exception)

    The exception that caused the failure



93
94
95
96
97
# File 'lib/spinach/reporter/stdout.rb', line 93

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 GherkinRubyRuby format

  • failure (Exception)

    The exception that caused the failure



79
80
81
82
83
# File 'lib/spinach/reporter/stdout.rb', line 79

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/spinach/reporter/stdout.rb', line 129

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}".red
  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 GherkinRubyRuby format



115
116
117
118
119
# File 'lib/spinach/reporter/stdout.rb', line 115

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 GherkinRubyRuby extracts



149
150
151
# File 'lib/spinach/reporter/stdout.rb', line 149

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



65
66
67
68
69
# File 'lib/spinach/reporter/stdout.rb', line 65

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 GherkinRubyRuby format



104
105
106
107
108
# File 'lib/spinach/reporter/stdout.rb', line 104

def on_undefined_step(step, failure, step_definitions = nil)
  output_step('?', step, :red)
  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 GherkinRubyRuby 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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/spinach/reporter/stdout.rb', line 168

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 + 60 # Colorize and output format correction

  # REMEMBER TO CORRECT PREVIOUS MAX LENGTH IF OUTPUT FORMAT IS MODIFIED
  buffer = []
  buffer << indent(4)
  buffer << symbol.colorize(:"light_#{color}")
  buffer << indent(2)
  buffer << step.keyword.colorize(:"light_#{color}")
  buffer << indent(1)
  buffer << step.name.colorize(color)
  joined = buffer.join.ljust(max_length)

  out.puts(joined + step_location.to_s.colorize(:grey))
end