Class: Cucumber::Formatter::Pretty

Inherits:
Ast::Visitor show all
Includes:
Console, FileUtils
Defined in:
lib/cucumber/formatter/pretty.rb

Overview

The formatter used for --format pretty (the default formatter).

This formatter prints features to plain text - exactly how they were parsed, just prettier. That means with proper indentation and alignment of table columns.

If the output is STDOUT (and not a file), there are bright colours to watch too.

Direct Known Subclasses

Cli::LanguageHelpFormatter

Constant Summary

Constants included from Console

Console::FORMATS

Constants included from ANSIColor

ANSIColor::ALIASES

Instance Attribute Summary collapse

Attributes inherited from Ast::Visitor

#options, #step_mother

Instance Method Summary collapse

Methods included from Console

#announce, #format_step, #format_string, #print_counts, #print_elements, #print_exception, #print_passing_wip, #print_snippets, #print_stats, #print_steps, #print_tag_limit_warnings, #record_tag_occurrences

Methods included from ANSIColor

define_grey, define_real_grey, #grey

Methods included from Duration

#format_duration

Methods inherited from Ast::Visitor

#announce, #visit_examples, #visit_steps

Constructor Details

#initialize(step_mother, io, options) ⇒ Pretty

Returns a new instance of Pretty.



18
19
20
21
22
23
24
25
# File 'lib/cucumber/formatter/pretty.rb', line 18

def initialize(step_mother, io, options)
  super(step_mother)
  @io = io
  @options = options
  @exceptions = []
  @indent = 0
  @prefixes = options[:prefixes] || {}
end

Instance Attribute Details

#indent=(value) ⇒ Object (writeonly)

Sets the attribute indent

Parameters:

  • value

    the value to set the attribute indent to.



16
17
18
# File 'lib/cucumber/formatter/pretty.rb', line 16

def indent=(value)
  @indent = value
end

Instance Method Details

#visit_background(background) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/cucumber/formatter/pretty.rb', line 87

def visit_background(background)
  @indent = 2
  @scenario_indent = 2
  @in_background = true
  super
  @in_background = nil
  @io.puts
  @io.flush
end

#visit_background_name(keyword, name, file_colon_line, source_indent) ⇒ Object



97
98
99
# File 'lib/cucumber/formatter/pretty.rb', line 97

def visit_background_name(keyword, name, file_colon_line, source_indent)
  visit_feature_element_name(keyword, name, file_colon_line, source_indent)
end

#visit_comment(comment) ⇒ Object



48
49
50
# File 'lib/cucumber/formatter/pretty.rb', line 48

def visit_comment(comment)
  comment.accept(self)
end

#visit_comment_line(comment_line) ⇒ Object



52
53
54
55
# File 'lib/cucumber/formatter/pretty.rb', line 52

def visit_comment_line(comment_line)
  @io.puts(comment_line.indent(@indent))
  @io.flush
end

#visit_examples_array(examples_array) ⇒ Object



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

def visit_examples_array(examples_array)
  @indent = 4
  @io.puts
  examples_array[0..-2].each { |ea| super(ea) }
  @last_example = true
  super(examples_array.last)
  @last_example = nil
end

#visit_examples_name(keyword, name) ⇒ Object



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

def visit_examples_name(keyword, name)
  names = name.strip.empty? ? [name.strip] : name.split("\n")        
  @io.puts("    #{keyword} #{names[0]}")
  names[1..-1].each {|s| @io.puts "      #{s}" } unless names.empty?
  @io.flush
  @indent = 6
  @scenario_indent = 6
end

#visit_exception(exception, status) ⇒ Object



170
171
172
173
# File 'lib/cucumber/formatter/pretty.rb', line 170

def visit_exception(exception, status)
  print_exception(exception, status, @indent)
  @io.flush
end

#visit_feature(feature) ⇒ Object



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

def visit_feature(feature)
  @exceptions = []
  @indent = 0
  if @options[:autoformat]
    file = File.join(@options[:autoformat], feature.file)
    dir = File.dirname(file)
    mkdir_p(dir) unless File.directory?(dir)
    File.open(file, Cucumber.file_mode('w')) do |io|
      @io = io
      super
    end
  else
    super
  end
end

#visit_feature_element(feature_element) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/cucumber/formatter/pretty.rb', line 78

def visit_feature_element(feature_element)
  record_tag_occurrences(feature_element, @options)
  @indent = 2
  @scenario_indent = 2
  super
  @io.puts
  @io.flush
end

#visit_feature_element_name(keyword, name, file_colon_line, source_indent) ⇒ Object



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

def visit_feature_element_name(keyword, name, file_colon_line, source_indent)
  @io.puts if @scenario_indent == 6
  names = name.empty? ? [name] : name.split("\n")
  line = "#{keyword} #{names[0]}".indent(@scenario_indent)
  @io.print(line)
  if @options[:source]
    line_comment = " # #{file_colon_line}".indent(source_indent)
    @io.print(format_string(line_comment, :comment))
  end
  @io.puts
  names[1..-1].each {|s| @io.puts "    #{s}"}
  @io.flush
end

#visit_feature_name(name) ⇒ Object



72
73
74
75
76
# File 'lib/cucumber/formatter/pretty.rb', line 72

def visit_feature_name(name)
  @io.puts(name)
  @io.puts
  @io.flush
end

#visit_features(features) ⇒ Object



27
28
29
30
# File 'lib/cucumber/formatter/pretty.rb', line 27

def visit_features(features)
  super
  print_summary(features) unless @options[:autoformat]
end

#visit_multiline_arg(multiline_arg) ⇒ Object



164
165
166
167
168
# File 'lib/cucumber/formatter/pretty.rb', line 164

def visit_multiline_arg(multiline_arg)
  return if @options[:no_multiline]
  @table = multiline_arg
  super
end

#visit_outline_table(outline_table) ⇒ Object



119
120
121
122
123
# File 'lib/cucumber/formatter/pretty.rb', line 119

def visit_outline_table(outline_table)
  super
  @indent = 4
  @io.puts unless @last_example
end

#visit_py_string(string) ⇒ Object



185
186
187
188
189
190
# File 'lib/cucumber/formatter/pretty.rb', line 185

def visit_py_string(string)
  s = %{"""\n#{string}\n"""}.indent(@indent)
  s = s.split("\n").map{|l| l =~ /^\s+$/ ? '' : l}.join("\n")
  @io.puts(format_string(s, @status))
  @io.flush
end

#visit_scenario_name(keyword, name, file_colon_line, source_indent) ⇒ Object



125
126
127
# File 'lib/cucumber/formatter/pretty.rb', line 125

def visit_scenario_name(keyword, name, file_colon_line, source_indent)
  visit_feature_element_name(keyword, name, file_colon_line, source_indent)
end

#visit_step(step) ⇒ Object



143
144
145
146
# File 'lib/cucumber/formatter/pretty.rb', line 143

def visit_step(step)
  @indent = 6
  super
end

#visit_step_name(keyword, step_match, status, source_indent, background) ⇒ Object



158
159
160
161
162
# File 'lib/cucumber/formatter/pretty.rb', line 158

def visit_step_name(keyword, step_match, status, source_indent, background)
  source_indent = nil unless @options[:source]
  formatted_step_name = format_step(keyword, step_match, status, source_indent)
  @io.puts(formatted_step_name.indent(@scenario_indent + 2))
end

#visit_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/cucumber/formatter/pretty.rb', line 148

def visit_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  if exception
    return if @exceptions.index(exception)
    @exceptions << exception
  end
  return if status != :failed && @in_background ^ background
  @status = status
  super
end

#visit_table_cell(cell) ⇒ Object



192
193
194
195
# File 'lib/cucumber/formatter/pretty.rb', line 192

def visit_table_cell(cell)
  super
  @col_index += 1
end

#visit_table_cell_value(value, status) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/cucumber/formatter/pretty.rb', line 197

def visit_table_cell_value(value, status)
  status ||= @status || :passed
  width = @table.col_width(@col_index)
  cell_text = value.to_s || ''
  padded = cell_text + (' ' * (width - cell_text.jlength))
  prefix = cell_prefix(status)
  @io.print(' ' + format_string("#{prefix}#{padded}", status) + ::Term::ANSIColor.reset(" |"))
  @io.flush
end

#visit_table_row(table_row) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/cucumber/formatter/pretty.rb', line 175

def visit_table_row(table_row)
  @col_index = 0
  @io.print '  |'.indent(@indent-2)
  super
  @io.puts
  if table_row.exception && !@exceptions.index(table_row.exception)
    print_exception(table_row.exception, :failed, @indent)
  end
end

#visit_tag_name(tag_name) ⇒ Object



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

def visit_tag_name(tag_name)
  tag = format_string("@#{tag_name}", :tag).indent(@indent)
  @io.print(tag)
  @io.flush
  @indent = 1
end

#visit_tags(tags) ⇒ Object



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

def visit_tags(tags)
  tags.accept(self)
  if @indent == 1
    @io.puts
    @io.flush
  end
end