class Cucumber::Formatter::Pretty

The formatter used for --format pretty (the default formatter).

This formatter prints the result of the feature executions to plain text - exactly how they were parsed.

If the output is STDOUT (and not a file), there are bright colours to watch too.

Attributes

config[R]
current_examples[R]
current_feature_uri[R]
current_scenario_outline[R]
current_test_case[R]
in_scenario_outline[R]
options[R]
print_background_steps[R]

Public Class Methods

new(config) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 34
def initialize(config)
  @io = ensure_io(config.out_stream, config.error_stream)
  @config = config
  @options = config.to_hash
  @snippets_input = []
  @undefined_parameter_types = []
  @total_duration = 0
  @exceptions = []
  @gherkin_sources = {}
  @step_matches = {}
  @ast_lookup = AstLookup.new(config)
  @counts = ConsoleCounts.new(config)
  @issues = ConsoleIssues.new(config, @ast_lookup)
  @first_feature = true
  @current_feature_uri = ''
  @current_scenario_outline = nil
  @current_examples = nil
  @current_test_case = nil
  @in_scenario_outline = false
  @print_background_steps = false
  @test_step_output = []
  @passed_test_cases = []
  @source_indent = 0
  @next_comment_to_be_printed = 0

  bind_events(config)
end

Public Instance Methods

attach(src, media_type) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 141
def attach(src, media_type)
  return unless media_type == 'text/x.cucumber.log+plain'
  @test_step_output.push src
end
bind_events(config) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 62
def bind_events(config)
  config.on_event :gherkin_source_read, &method(:on_gherkin_source_read)
  config.on_event :step_activated, &method(:on_step_activated)
  config.on_event :test_case_started, &method(:on_test_case_started)
  config.on_event :test_step_started, &method(:on_test_step_started)
  config.on_event :test_step_finished, &method(:on_test_step_finished)
  config.on_event :test_case_finished, &method(:on_test_case_finished)
  config.on_event :test_run_finished, &method(:on_test_run_finished)
  config.on_event :undefined_parameter_type, &method(:collect_undefined_parameter_type_names)
end
on_gherkin_source_read(event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 73
def on_gherkin_source_read(event)
  @gherkin_sources[event.path] = event.body
end
on_step_activated(event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 77
def on_step_activated(event)
  test_step, step_match = *event.attributes
  @step_matches[test_step.to_s] = step_match
end
on_test_case_finished(event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 122
def on_test_case_finished(event)
  @total_duration += DurationExtractor.new(event.result).result_duration
  @passed_test_cases << event.test_case if config.wip? && event.result.passed?
  if in_scenario_outline && !options[:expand]
    print_row_data(event.test_case, event.result)
  else
    exception_to_be_printed = find_exception_to_be_printed(event.result)
    return unless exception_to_be_printed
    print_exception(exception_to_be_printed, event.result.to_sym, 6)
    @exceptions << exception_to_be_printed
  end
end
on_test_case_started(event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 82
def on_test_case_started(event)
  if !same_feature_as_previous_test_case?(event.test_case.location)
    if first_feature?
      @first_feature = false
      print_profile_information
    else
      print_comments(gherkin_source.split("\n").length, 0)
      @io.puts
    end
    @current_feature_uri = event.test_case.location.file
    @exceptions = []
    print_feature_data
    if feature_has_background?
      print_background_data
      @print_background_steps = true
      @in_scenario_outline = false
    end
  else
    @print_background_steps = false
  end
  @current_test_case = event.test_case
  print_step_header(current_test_case) unless print_background_steps
end
on_test_run_finished(_event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 135
def on_test_run_finished(_event)
  print_comments(gherkin_source.split("\n").length, 0) unless current_feature_uri.empty?
  @io.puts
  print_summary
end
on_test_step_finished(event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 111
def on_test_step_finished(event)
  collect_snippet_data(event.test_step, @ast_lookup) if event.result.undefined?
  return if in_scenario_outline && !options[:expand]
  exception_to_be_printed = find_exception_to_be_printed(event.result)
  print_step_data(event.test_step, event.result) if print_step_data?(event, exception_to_be_printed)
  print_step_output
  return unless exception_to_be_printed
  print_exception(exception_to_be_printed, event.result.to_sym, 6)
  @exceptions << exception_to_be_printed
end
on_test_step_started(event) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 106
def on_test_step_started(event)
  return if event.test_step.hook?
  print_step_header(current_test_case) if first_step_after_printing_background_steps?(event.test_step)
end

Private Instance Methods

calculate_source_indent(test_case) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 156
def calculate_source_indent(test_case)
  scenario = scenario_source(test_case).scenario
  @source_indent = calculate_source_indent_for_ast_node(scenario)
end
calculate_source_indent_for_ast_node(ast_node) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 161
def calculate_source_indent_for_ast_node(ast_node)
  indent = 4 + ast_node.keyword.length
  indent += 1 + ast_node.name.length
  ast_node.steps.each do |step|
    step_indent = 5 + step.keyword.length + step.text.length
    indent = step_indent if step_indent > indent
  end
  indent
end
calculate_source_indent_for_expanded_test_case(test_case, scenario_keyword, expanded_name) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 171
def calculate_source_indent_for_expanded_test_case(test_case, scenario_keyword, expanded_name)
  indent = 7 + scenario_keyword.length
  indent += 2 + expanded_name.length
  test_case.test_steps.each do |step|
    if !step.hook? && step.location.lines.max >= test_case.location.lines.max
      step_indent = 9 + test_step_keyword(step).length + step.text.length
      indent = step_indent if step_indent > indent
    end
  end
  indent
end
feature_has_background?() click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 196
def feature_has_background?
  feature_children = gherkin_document.feature.children
  return false if feature_children.empty?
  !feature_children.first.background.nil?
end
find_exception_to_be_printed(result) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 148
def find_exception_to_be_printed(result)
  return nil if result.ok?(options[:strict])
  result = result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
  exception = result.failed? ? result.exception : result
  return nil if @exceptions.include?(exception)
  exception
end
first_feature?() click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 188
def first_feature?
  @first_feature
end
first_step_after_printing_background_steps?(test_step) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 239
def first_step_after_printing_background_steps?(test_step)
  return false unless print_background_steps
  return false unless test_step.location.lines.max >= current_test_case.location.lines.max
  @print_background_steps = false
  true
end
from_scenario_outline?(test_case) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 234
def from_scenario_outline?(test_case)
  scenario = scenario_source(test_case)
  scenario.type != :Scenario
end
gherkin_document() click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 365
def gherkin_document
  @ast_lookup.gherkin_document(current_feature_uri)
end
gherkin_source() click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 361
def gherkin_source
  @gherkin_sources[current_feature_uri]
end
print_background_data() click to toggle source
print_background_line(background) click to toggle source
print_comments(up_to_line, indent_amount) click to toggle source
print_data_table(data_table, status, indent_amount) click to toggle source
print_description(description) click to toggle source
print_doc_string(content, status, indent_amount) click to toggle source
print_examples_data(examples) click to toggle source
print_expanded_row_data(test_case) click to toggle source
print_feature_data() click to toggle source
print_feature_line(feature) click to toggle source
print_keyword_name(keyword, name, indent_amount, location = nil) click to toggle source
print_language_comment(feature_line) click to toggle source
print_multiline_argument(test_step, result, indent) click to toggle source
print_outline_data(scenario_outline) click to toggle source
print_row_data(test_case, result) click to toggle source
print_scenario_data(test_case) click to toggle source
print_scenario_line(scenario, location = nil) click to toggle source
print_step_data(test_step, result) click to toggle source
print_step_data?(event, exception_to_be_printed) click to toggle source
print_step_header(test_case) click to toggle source
print_step_output() click to toggle source
print_summary() click to toggle source
print_tags(tags, indent_amount) click to toggle source
same_examples_as_previous_test_case?(test_case) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 230
def same_examples_as_previous_test_case?(test_case)
  scenario_source(test_case).examples == current_examples
end
same_feature_as_previous_test_case?(location) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 192
def same_feature_as_previous_test_case?(location)
  location.file == current_feature_uri
end
same_outline_as_previous_test_case?(test_case) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 226
def same_outline_as_previous_test_case?(test_case)
  scenario_source(test_case).scenario_outline == current_scenario_outline
end
scenario_source(test_case) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 357
def scenario_source(test_case)
  @ast_lookup.scenario_source(test_case)
end
step_source(test_step) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 353
def step_source(test_step)
  @ast_lookup.step_source(test_step)
end
test_step_keyword(test_step) click to toggle source
# File lib/cucumber/formatter/pretty.rb, line 348
def test_step_keyword(test_step)
  step = step_source(test_step).step
  step.keyword
end