Class: Spinach::Reporter::Stdout

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

Overview

The Stdout reporter outputs the runner results to the standard output

Instance Attribute Summary collapse

Attributes inherited from Spinach::Reporter

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

Instance Method Summary collapse

Methods inherited from Spinach::Reporter

#after_feature_run, #bind, #clear_current_feature, #clear_current_scenario, #feature_runner, #runner, #scenario_runner

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



21
22
23
24
25
26
# File 'lib/spinach/reporter/stdout.rb', line 21

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.



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

def error
  @error
end

#outObject (readonly)

The output buffers to store the reports.



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

def out
  @out
end

#scenario_errorObject

The last scenario error



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

def scenario_error
  @scenario_error
end

Instance Method Details

#after_run(success) ⇒ Object

It prints the error summary if the run has failed

Parameters:

  • success (True, False)

    whether the run has succeed or not



174
175
176
# File 'lib/spinach/reporter/stdout.rb', line 174

def after_run(success)
  error_summary unless success
end

#after_scenario_run(data) ⇒ Object

Adds an error report and re

Parameters:

  • data (Hash)

    The feature in a JSON Gherkin format



54
55
56
57
58
59
# File 'lib/spinach/reporter/stdout.rb', line 54

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



33
34
35
36
# File 'lib/spinach/reporter/stdout.rb', line 33

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



43
44
45
46
47
# File 'lib/spinach/reporter/stdout.rb', line 43

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

#error_summaryObject

Prints the errors for ths run.



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

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

#full_error(error) ⇒ Object

Returns a complete error report

Parameters:

  • error (Array)

    An array containing the feature, scenario, step and exception



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/spinach/reporter/stdout.rb', line 283

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

  if options[:backtrace]
    output += "\n"
    exception.backtrace.map do |line|
      output << "        #{line}\n"
    end
  else
    output << "        #{exception.backtrace[0]}"
  end
  output
end

#full_step(step) ⇒ Object

Constructs the full step definition

Parameters:

  • step (Hash)

    The step in a JSON Gherkin format



305
306
307
# File 'lib/spinach/reporter/stdout.rb', line 305

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



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

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



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

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, exception) ⇒ 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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/spinach/reporter/stdout.rb', line 117

def on_feature_not_found(feature, exception)
  lines = "#{exception.message}\n"

  lines << "\nPlease create the file #{Spinach::Support.underscore(exception.missing_class)}.rb at #{Spinach.config[:step_definitions_path]}, with:\n\n"

  lines << "Feature '#{feature['name']}' do\n"

  # TODO: Write the actual steps. We can do this since we have the entire
  # feature just here. We should iterate over all the scenarios and return
  # the different steps
  #
  lines << "  # Write your steps here"
  lines << "end\n\n"

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

  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



143
144
145
# File 'lib/spinach/reporter/stdout.rb', line 143

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



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

def on_successful_step(step, step_location)
  output_step('', step, :green, step_location)
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



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

def on_undefined_step(step, failure)
  output_step('?', step, :yellow)
  self.scenario_error = [current_feature, current_scenario, step]
  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



162
163
164
165
166
167
# File 'lib/spinach/reporter/stdout.rb', line 162

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

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

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



246
247
248
249
250
251
252
253
254
255
# File 'lib/spinach/reporter/stdout.rb', line 246

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

Prints the steps that raised an error.



190
191
192
# File 'lib/spinach/reporter/stdout.rb', line 190

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.



226
227
228
229
230
231
232
# File 'lib/spinach/reporter/stdout.rb', line 226

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) ⇒ Object

Prints a information when an exception is raised.

Parameters:

  • exception (Exception)

    The exception to report



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/spinach/reporter/stdout.rb', line 317

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

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

#report_failed_stepsObject

Prints failing steps.



196
197
198
# File 'lib/spinach/reporter/stdout.rb', line 196

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

#report_undefined_featuresObject



206
207
208
209
210
211
212
213
# File 'lib/spinach/reporter/stdout.rb', line 206

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

Prints undefined steps.



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

def report_undefined_steps
  report_errors('Undefined steps', undefined_steps, :yellow) if undefined_steps.any?
end

#summarized_error(error) ⇒ Object

Returns summarized error report

Parameters:

  • error (Array)

    An array containing the feature, scenario, step and exception



265
266
267
268
269
270
271
272
273
# File 'lib/spinach/reporter/stdout.rb', line 265

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
  else
    summary.red
  end
end