Class: Cucumber::Formatter::Pretty

Inherits:
Object
  • Object
show all
Includes:
Console, Io, Gherkin::Formatter::Escaping, FileUtils
Defined in:
lib/cucumber/formatter/pretty.rb

Overview

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.

Constant Summary

Constants included from ANSIColor

ANSIColor::ALIASES

Constants included from Term::ANSIColor

Term::ANSIColor::ATTRIBUTES, Term::ANSIColor::ATTRIBUTE_NAMES, Term::ANSIColor::COLORED_REGEXP

Instance Method Summary collapse

Methods included from Gherkin::Formatter::Escaping

#escape_cell

Methods included from Io

ensure_dir, ensure_file, ensure_io

Methods included from Console

#collect_snippet_data, #do_print_passing_wip, #do_print_profile_information, #do_print_snippets, #embed, #exception_message_string, #format_step, #format_string, #linebreaks, #print_element_messages, #print_elements, #print_exception, #print_passing_wip, #print_profile_information, #print_snippets, #print_statistics

Methods included from ANSIColor

#cukes, define_grey, define_real_grey, #green_cukes, #red_cukes, #yellow_cukes

Methods included from Term::ANSIColor

attributes, coloring=, coloring?, #uncolored

Methods included from Duration

#format_duration

Constructor Details

#initialize(config) ⇒ Pretty


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cucumber/formatter/pretty.rb', line 34

def initialize(config)
  @io = ensure_io(config.out_stream)
  @config = config
  @options = config.to_hash
  @snippets_input = []
  @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
  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)
end

Instance Method Details

#on_gherkin_source_read(event) ⇒ Object


66
67
68
# File 'lib/cucumber/formatter/pretty.rb', line 66

def on_gherkin_source_read(event)
  @gherkin_sources[event.path] = event.body
end

#on_step_activated(event) ⇒ Object


70
71
72
73
# File 'lib/cucumber/formatter/pretty.rb', line 70

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


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cucumber/formatter/pretty.rb', line 117

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cucumber/formatter/pretty.rb', line 75

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


130
131
132
133
134
# File 'lib/cucumber/formatter/pretty.rb', line 130

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

rubocop:disable Metrics/PerceivedComplexity


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cucumber/formatter/pretty.rb', line 104

def on_test_step_finished(event) # rubocop:disable Metrics/PerceivedComplexity
  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)  # rubocop:disable Metrics/LineLength

  print_step_data(event.test_step, event.result) if !event.test_step.hook? && (print_background_steps || event.test_step.location.lines.max >= current_test_case.location.lines.max || exception_to_be_printed)  # rubocop:enable Metrics/LineLength

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


99
100
101
102
# File 'lib/cucumber/formatter/pretty.rb', line 99

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

#puts(*messages) ⇒ Object


136
137
138
139
140
# File 'lib/cucumber/formatter/pretty.rb', line 136

def puts(*messages)
  messages.each do |message|
    @test_step_output.push message
  end
end