Module: Lucid::Formatter::Console

Extended by:
ANSIColor
Includes:
Duration, Summary
Included in:
Progress, Standard, Usage
Defined in:
lib/lucid/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.

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

define_grey, define_real_grey, green_lucid, grey, lucid, red_lucid, yellow_lucid

Methods included from Term::ANSIColor

attributes, coloring=, coloring?, included, #uncolored

Methods included from Summary

#scenario_summary, #step_summary

Methods included from Duration

#format_duration

Instance Method Details

#embed(file, mime_type, label) ⇒ Object



165
166
167
# File 'lib/lucid/formatter/console.rb', line 165

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

#empty_messagesObject



202
203
204
# File 'lib/lucid/formatter/console.rb', line 202

def empty_messages
  @delayed_messages = []
end

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



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

def format_step(keyword, step_match, status, source_indent)
  comment = if source_indent
    c = (' # ' + step_match.file_colon_line).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/lucid/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
      fmt.call(line)
    else
      fmt % line
    end
  end.join("\n")
end

#linebreaks(s, max) ⇒ Object



112
113
114
# File 'lib/lucid/formatter/console.rb', line 112

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


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

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

  elements.each_with_index do |element, i|
    if status == :failed
      print_exception(element.exception, status, 0)
    else
      @io.puts(format_string(element.backtrace_line, status))
    end
    @io.puts
    @io.flush
  end
end


101
102
103
104
105
106
107
108
109
# File 'lib/lucid/formatter/console.rb', line 101

def print_exception(e, status, indent)
  message = "#{e.message} (#{e.class})"
  if ENV['LUCID_TRUNCATE_OUTPUT']
    message = linebreaks(message, ENV['LUCID_TRUNCATE_OUTPUT'].to_i)
  end

  string = "#{message}\n#{e.backtrace.join("\n")}".indent(indent)
  @io.puts(format_string(string, status))
end


154
155
156
157
158
159
160
161
162
163
# File 'lib/lucid/formatter/console.rb', line 154

def print_failing_strict(options)
  return unless options[:strict]
  undefined = runtime.steps(:undefined)
  pending = runtime.steps(:pending)
  if undefined.any? || pending.any?
    @io.puts format_string("\nThe --strict switch was used, so nothing was expected to be undefined or pending. These scenarios violate that:", :failed)
    print_elements(undefined, :undefined, "steps")
    print_elements(pending, :pending, "steps")
  end
end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lucid/formatter/console.rb', line 116

def print_matchers(options)
  return unless options[:matchers]
  undefined = runtime.steps(:undefined)
  return if undefined.empty?

  unknown_programming_language = runtime.unknown_programming_language?
  matchers = undefined.map do |step|
    step_name = Undefined === step.exception ? step.exception.step_name : step.name
    step_multiline_class = step.multiline_arg ? step.multiline_arg.class : nil
    matcher = @runtime.matcher_text(step.actual_keyword, step_name, step_multiline_class)
    matcher
  end.compact.uniq

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

  if unknown_programming_language
    @io.puts format_string("\nNote: no test definitions were found in this repository or any" +
                           "\nother associated locations. These test definition files should" +
                           "\nbe where you put any of the suggested matchers.", :failed)
  end

  @io.puts
  @io.flush
end


197
198
199
200
# File 'lib/lucid/formatter/console.rb', line 197

def print_message(message)
  @io.puts(format_string(message, :tag).indent(@indent))
  @io.flush
end


185
186
187
188
# File 'lib/lucid/formatter/console.rb', line 185

def print_messages
  @delayed_messages.each {|message| print_message(message)}
  empty_messages
end


143
144
145
146
147
148
149
150
151
152
# File 'lib/lucid/formatter/console.rb', line 143

def print_passing_wip(options)
  return unless options[:wip]
  passed = runtime.scenarios(:passed)
  if passed.any?
    @io.puts format_string("\nThe --wip switch was used, so nothing was expected to pass. These scenarios passed:", :failed)
    print_elements(passed, :passed, "scenarios")
  else
    @io.puts format_string("\nThe --wip switch was used, so any non-passing scenarios were expected.\n", :passed)
  end
end


206
207
208
209
210
211
212
213
214
# File 'lib/lucid/formatter/console.rb', line 206

def print_profile_information
  return if @options[:skip_profile_information] || @options[:profiles].nil? || @options[:profiles].empty?
  profiles = @options[:profiles]
  profiles_sentence = ''
  profiles_sentence = profiles.size == 1 ? profiles.first :
    "#{profiles[0...-1].join(', ')} and #{profiles.last}"

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


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lucid/formatter/console.rb', line 79

def print_stats(features, options)
  @failures = runtime.scenarios(:failed).select { |s| s.is_a?(Lucid::AST::Scenario) || s.is_a?(Lucid::AST::OutlineTable::ExampleRow) }
  @failures.collect! { |s| (s.is_a?(Lucid::AST::OutlineTable::ExampleRow)) ? s.scenario_outline : s }

  if !@failures.empty?
    @io.puts format_string("Failing Scenarios:", :failed)
    @failures.each do |failure|
      profiles_string = options.custom_profiles.empty? ? '' : (options.custom_profiles.map{|profile| "-p #{profile}" }).join(' ') + ' '
      source = options[:source] ? format_string(" # Scenario: " + failure.name, :comment) : ''
      @io.puts format_string("Lucid #{profiles_string}" + failure.file_colon_line, :failed) + source
    end
    @io.puts
  end

  @io.puts scenario_summary(runtime) {|status_count, status| format_string(status_count, status)}
  @io.puts step_summary(runtime) {|status_count, status| format_string(status_count, status)}

  @io.puts(format_duration(features.duration)) if features && features.duration

  @io.flush
end


57
58
59
# File 'lib/lucid/formatter/console.rb', line 57

def print_steps(status)
  print_elements(runtime.steps(status), status, 'steps')
end


190
191
192
193
194
195
# File 'lib/lucid/formatter/console.rb', line 190

def print_table_row_messages
  return if @delayed_messages.empty?
  @io.print(format_string(@delayed_messages.join(', '), :tag).indent(2))
  @io.flush
  empty_messages
end

#puts(*messages) ⇒ Object

define @delayed_messages = [] in your Formatter if you want to activate this feature



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/lucid/formatter/console.rb', line 171

def puts(*messages)
  if @delayed_messages
    @delayed_messages += messages
  else
    if @io
      @io.puts
      messages.each do |message|
        @io.puts(format_string(message, :tag))
      end
      @io.flush
    end
  end
end