Module: Cucumber::Formatter::Console

Extended by:
ANSIColor
Includes:
Duration
Included in:
ConsoleCounts, ConsoleIssues, Pretty, Progress, Summary
Defined in:
lib/cucumber/formatter/console.rb

Overview

This module contains helper methods that are used by formatters that print output to the terminal.

FORMAT is a hash of Proc objects, keyed by step-definition types, e.g. “FORMAT”. The Proc is called for each line of the step's output.

format_step calls format_string, format_string calls format_for to obtain the formatting Proc.

Example:

The ANSI color console formatter defines a map of step-type to output color (e.g. “passed” to “green”), then builds methods named for the step-types (e.g. “def passed”), which themselves wrap the corresponding color-named methods provided by Term::ANSIColor (e.g. “def red”).

During output, each line is processed by passing it to the formatter Proc which returns the formatted (e.g. colored) string.

Defined Under Namespace

Classes: SnippetData

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 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

Instance Method Details

#collect_snippet_data(test_step, ast_lookup) ⇒ Object


113
114
115
116
117
# File 'lib/cucumber/formatter/console.rb', line 113

def collect_snippet_data(test_step, ast_lookup)
  # collect snippet data for undefined steps
  keyword = ast_lookup.snippet_step_keyword(test_step)
  @snippets_input << Console::SnippetData.new(keyword, test_step)
end

#do_print_passing_wip(passed_messages) ⇒ Object


152
153
154
155
156
157
158
159
# File 'lib/cucumber/formatter/console.rb', line 152

def do_print_passing_wip(passed_messages)
  if passed_messages.any?
    @io.puts format_string("\nThe --wip switch was used, so I didn't expect anything to pass. These scenarios passed:", :failed)
    print_element_messages(passed_messages, :passed, 'scenarios')
  else
    @io.puts format_string("\nThe --wip switch was used, so the failures were expected. All is good.\n", :passed)
  end
end

#do_print_profile_information(profiles) ⇒ Object


179
180
181
182
183
184
185
186
187
# File 'lib/cucumber/formatter/console.rb', line 179

def do_print_profile_information(profiles)
  profiles_sentence = if profiles.size == 1
                        profiles.first
                      else
                        "#{profiles[0...-1].join(', ')} and #{profiles.last}"
                      end

  @io.puts "Using the #{profiles_sentence} profile#{'s' if profiles.size > 1}..."
end

#do_print_snippets(snippet_text_proc) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cucumber/formatter/console.rb', line 129

def do_print_snippets(snippet_text_proc)
  snippets = @snippets_input.map do |data|
    snippet_text_proc.call(data.actual_keyword, data.step.text, data.step.multiline_arg)
  end.uniq

  text = "\nYou can implement step definitions for undefined steps with these snippets:\n\n"
  text += snippets.join("\n\n")
  @io.puts format_string(text, :undefined)

  @io.puts
  @io.flush
end

#embed(file, mime_type, label) ⇒ Object


161
162
163
# File 'lib/cucumber/formatter/console.rb', line 161

def embed(file, mime_type, label)
  # no-op
end

#exception_message_string(e, indent) ⇒ Object


100
101
102
103
104
105
# File 'lib/cucumber/formatter/console.rb', line 100

def exception_message_string(e, indent)
  message = "#{e.message} (#{e.class})".dup.force_encoding('UTF-8')
  message = linebreaks(message, ENV['CUCUMBER_TRUNCATE_OUTPUT'].to_i)

  "#{message}\n#{e.backtrace.join("\n")}".indent(indent)
end

#format_step(keyword, step_match, status, source_indent) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cucumber/formatter/console.rb', line 33

def format_step(keyword, step_match, status, source_indent)
  comment = if source_indent
              c = ('# ' + step_match.location.to_s).indent(source_indent)
              format_string(c, :comment)
            else
              ''
            end

  format = format_for(status, :param)
  line = keyword + step_match.format_args(format) + comment
  format_string(line, status)
end

#format_string(o, status) ⇒ Object


46
47
48
49
50
51
52
53
54
55
# File 'lib/cucumber/formatter/console.rb', line 46

def format_string(o, status)
  fmt = format_for(status)
  o.to_s.split("\n").map do |line|
    if Proc == fmt.class
      fmt.call(line)
    else
      fmt % line
    end
  end.join("\n")
end

#linebreaks(msg, max) ⇒ Object


108
109
110
111
# File 'lib/cucumber/formatter/console.rb', line 108

def linebreaks(msg, max)
  return msg unless max && max > 0
  msg.gsub(/.{1,#{max}}(?:\s|\Z)/) { ($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n") }.rstrip
end

64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cucumber/formatter/console.rb', line 64

def print_element_messages(element_messages, status, kind)
  if element_messages.any?
    @io.puts(format_string("(::) #{status} #{kind} (::)", status))
    @io.puts
    @io.flush
  end

  element_messages.each do |message|
    @io.puts(format_string(message, status))
    @io.puts
    @io.flush
  end
end

57
58
59
60
61
62
# File 'lib/cucumber/formatter/console.rb', line 57

def print_elements(elements, status, kind)
  return if elements.empty?

  element_messages = element_messages(elements, status)
  print_element_messages(element_messages, status, kind)
end

95
96
97
98
# File 'lib/cucumber/formatter/console.rb', line 95

def print_exception(e, status, indent)
  string = exception_message_string(e, indent)
  @io.puts(format_string(string, status))
end

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

def print_passing_wip(config, passed_test_cases, ast_lookup)
  return unless config.wip?
  messages = passed_test_cases.map do |test_case|
    scenario_source = ast_lookup.scenario_source(test_case)
    keyword = scenario_source.type == :Scenario ? scenario_source.scenario[:keyword] : scenario_source.scenario_outline[:keyword]
    linebreaks("#{test_case.location.on_line(test_case.location.lines.max)}:in `#{keyword}: #{test_case.name}'", ENV['CUCUMBER_TRUNCATE_OUTPUT'].to_i)
  end
  do_print_passing_wip(messages)
end

174
175
176
177
# File 'lib/cucumber/formatter/console.rb', line 174

def print_profile_information
  return if @options[:skip_profile_information] || @options[:profiles].nil? || @options[:profiles].empty?
  do_print_profile_information(@options[:profiles])
end

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

def print_snippets(options)
  return unless options[:snippets]
  return if @snippets_input.empty?

  snippet_text_proc = lambda do |step_keyword, step_name, multiline_arg|
    snippet_text(step_keyword, step_name, multiline_arg)
  end
  do_print_snippets(snippet_text_proc)
end

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cucumber/formatter/console.rb', line 78

def print_statistics(duration, config, counts, issues)
  if issues.any?
    @io.puts issues.to_s
    @io.puts
  end

  @io.puts counts.to_s
  @io.puts(format_duration(duration)) if duration && config.duration?

  if config.randomize?
    @io.puts
    @io.puts "Randomized with seed #{config.seed}"
  end

  @io.flush
end

#puts(*messages) ⇒ Object


165
166
167
168
169
170
171
172
# File 'lib/cucumber/formatter/console.rb', line 165

def puts(*messages)
  return unless @io
  @io.puts
  messages.each do |message|
    @io.puts(format_string(message, :tag))
  end
  @io.flush
end