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, included, io?, url?

Methods included from Console

#collect_snippet_data, #collect_undefined_parameter_type_names, #do_print_passing_wip, #do_print_profile_information, #do_print_snippets, #do_print_undefined_parameter_type_snippet, #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?, included, #uncolored

Methods included from Duration

#format_duration

Constructor Details

#initialize(config) ⇒ Pretty

Returns a new instance of 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
# 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 = []
  @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

Instance Method Details

#attach(src, media_type) ⇒ Object


141
142
143
144
# 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) ⇒ Object


62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object


73
74
75
# 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) ⇒ Object


77
78
79
80
# 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) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
# 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) ⇒ Object


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object


135
136
137
138
139
# 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) ⇒ Object


111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object


106
107
108
109
# 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