Class: Spinach::Reporter::Stdout

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

Overview

The Stdout reporter outputs the runner results to the standard output

Defined Under Namespace

Modules: ErrorReporting

Instance Attribute Summary collapse

Attributes inherited from Spinach::Reporter

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

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_undefined_features, #report_undefined_steps, #summarized_error

Methods inherited from Spinach::Reporter

#after_feature_run, #bind, #clear_current_feature, #clear_current_scenario, #set_current_feature, #set_current_scenario

Constructor Details

#initialize(*args) ⇒ Stdout

Initialitzes the runner

Parameters:

  • options (Hash)

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



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

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

Instance Attribute Details

#errorObject (readonly)

The output buffers to store the reports.



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

def error
  @error
end

#outObject (readonly)

The output buffers to store the reports.



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

def out
  @out
end

#scenarioObject

The last scenario



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

def scenario
  @scenario
end

#scenario_errorObject

The last scenario error



16
17
18
# File 'lib/spinach/reporter/stdout.rb', line 16

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



180
181
182
183
184
# File 'lib/spinach/reporter/stdout.rb', line 180

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

#after_scenario_run(data) ⇒ Object

Adds an error report and re

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



60
61
62
63
64
65
# File 'lib/spinach/reporter/stdout.rb', line 60

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

#before_feature_run(data) ⇒ Object

Prints the feature name to the standard output

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



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

def before_feature_run(data)
  name = data['name']
  out.puts "\n#{'Feature:'.magenta} #{name.light_magenta}"
end

#before_scenario_run(data) ⇒ Object

Prints the scenario name to the standard ouput

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



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

def before_scenario_run(data)
  @max_step_name_length = data['steps'].map{|step| step['name'].length}.max if data['steps']
  name = data['name']
  out.puts "\n  #{'Scenario:'.green} #{name.light_green}"
end

#full_step(step) ⇒ Object

Constructs the full step definition

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format



201
202
203
# File 'lib/spinach/reporter/stdout.rb', line 201

def full_step(step)
  "#{step['keyword'].strip} #{step['name'].strip}"
end

#on_error_step(step, failure, step_location) ⇒ 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



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

def on_error_step(step, failure, step_location)
  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) ⇒ 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



89
90
91
92
93
# File 'lib/spinach/reporter/stdout.rb', line 89

def on_failed_step(step, failure, step_location)
  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



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

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_skipped_step(step) ⇒ Object

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

Parameters:

  • step (Hash)

    The step that Gherkin extracts



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

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

#on_successful_step(step, step_location) ⇒ Object

Adds a passed step to the output buffer.

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format

  • step_location (Array)

    The step source location



75
76
77
78
79
# File 'lib/spinach/reporter/stdout.rb', line 75

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

#on_undefined_step(step, failure) ⇒ Object

Adds an undefined step to the output buffer.

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format



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

def on_undefined_step(step, failure)
  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



167
168
169
170
171
172
# File 'lib/spinach/reporter/stdout.rb', line 167

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
  out.puts "    #{symbol.colorize(:"light_#{color}")}  #{step['keyword'].strip.colorize(:"light_#{color}")} #{step['name'].strip.colorize(color)} ".ljust(max_length) + step_location.to_s.colorize(:grey)
end

#run_summaryObject

Prints the feature success summary for this run.



188
189
190
191
192
193
194
# File 'lib/spinach/reporter/stdout.rb', line 188

def run_summary
  successful_summary = "(".colorize(:green)+successful_steps.length.to_s.colorize(:light_green)+") Successful".colorize(:green)
  undefined_summary = "(".colorize(:yellow)+undefined_steps.length.to_s.colorize(:light_yellow)+") Undefined".colorize(:yellow)
  failed_summary = "(".colorize(:red)+failed_steps.length.to_s.colorize(:light_red)+") Failed".colorize(:red)
  error_summary = "(".colorize(:red)+error_steps.length.to_s.colorize(:light_red)+") Error".colorize(:red)
  out.puts "Steps Summary: #{successful_summary}, #{undefined_summary}, #{failed_summary}, #{error_summary}\n\n"
end