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, #indent, #linebreaks, #print_element_messages, #print_elements, #print_exception, #print_passing_wip, #print_profile_information, #print_snippets, #print_statistics

Methods included from ANSIColor

apply_custom_colors, #cukes, #green_cukes, #red_cukes, #yellow_cukes

Methods included from Term::ANSIColor

#attributes, included, #uncolored

Methods included from Duration

#format_duration

Constructor Details

#initialize(config) ⇒ Pretty

Returns a new instance of Pretty.



32
33
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
# File 'lib/cucumber/formatter/pretty.rb', line 32

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

Instance Method Details

#attach(src, media_type, filename) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/cucumber/formatter/pretty.rb', line 143

def attach(src, media_type, filename)
  return unless media_type == 'text/x.cucumber.log+plain'

  if filename
    @test_step_output.push("#{filename}: #{src}")
  else
    @test_step_output.push(src)
  end
end

#bind_events(config) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/cucumber/formatter/pretty.rb', line 60

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



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

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

#on_step_activated(event) ⇒ Object



75
76
77
78
# File 'lib/cucumber/formatter/pretty.rb', line 75

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



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cucumber/formatter/pretty.rb', line 123

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cucumber/formatter/pretty.rb', line 80

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



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

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cucumber/formatter/pretty.rb', line 110

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



104
105
106
107
108
# File 'lib/cucumber/formatter/pretty.rb', line 104

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