Class: Cucumber::Formatter::Pretty

Inherits:
Object
  • Object
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

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

Constructor Details

#initialize(step_mother, io, options) ⇒ Pretty

Returns a new instance of Pretty.



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

def initialize(step_mother, io, options)
  @step_mother, @io, @options = step_mother, io, 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

#step_motherObject (readonly)

Returns the value of attribute step_mother.



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

def step_mother
  @step_mother
end

Instance Method Details

#after_background(background) ⇒ Object



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

def after_background(background)
  @in_background = nil
  @io.puts
  @io.flush
end

#after_feature(*args) ⇒ Object



41
42
43
# File 'lib/cucumber/formatter/pretty.rb', line 41

def after_feature(*args)
  @io.close if @options[:autoformat]
end

#after_feature_element(feature_element) ⇒ Object



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

def after_feature_element(feature_element)
  @io.puts
  @io.flush
end

#after_features(features) ⇒ Object



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

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

#after_multiline_arg(multiline_arg) ⇒ Object



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

def after_multiline_arg(multiline_arg)
  @table = nil
end

#after_outline_table(outline_table) ⇒ Object



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

def after_outline_table(outline_table)
  @table = nil
  @indent = 4
end

#after_table_cell(cell) ⇒ Object



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

def after_table_cell(cell)
  return unless @table
  @col_index += 1
end

#after_table_row(table_row) ⇒ Object



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

def after_table_row(table_row)
  return unless @table
  @io.puts
  if table_row.exception && !@exceptions.include?(table_row.exception)
    print_exception(table_row.exception, :failed, @indent)
  end
end

#after_tags(tags) ⇒ Object



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

def after_tags(tags)
  if @indent == 1
    @io.puts
    @io.flush
  end
end

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



93
94
95
# File 'lib/cucumber/formatter/pretty.rb', line 93

def background_name(keyword, name, file_colon_line, source_indent)        
  print_feature_element_name(keyword, name, file_colon_line, source_indent)
end

#before_background(background) ⇒ Object



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

def before_background(background)
  @indent = 2
  @scenario_indent = 2
  @in_background = true
end

#before_examples_array(examples_array) ⇒ Object



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

def before_examples_array(examples_array)
  @indent = 4
  @io.puts
  @visiting_first_example_name = true
end

#before_feature(feature) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/cucumber/formatter/pretty.rb', line 30

def before_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)
    @io = File.open(file, Cucumber.file_mode('w'))
  end
end

#before_feature_element(feature_element) ⇒ Object



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

def before_feature_element(feature_element)
  record_tag_occurrences(feature_element, @options)
  @indent = 2
  @scenario_indent = 2
end

#before_multiline_arg(multiline_arg) ⇒ Object



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

def before_multiline_arg(multiline_arg)
  return if @options[:no_multiline] || @hide_this_step
  @table = multiline_arg
end

#before_outline_table(outline_table) ⇒ Object



114
115
116
# File 'lib/cucumber/formatter/pretty.rb', line 114

def before_outline_table(outline_table)
  @table = outline_table
end

#before_step(step) ⇒ Object



127
128
129
130
# File 'lib/cucumber/formatter/pretty.rb', line 127

def before_step(step)
  @current_step = step
  @indent = 6
end

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cucumber/formatter/pretty.rb', line 132

def before_step_result(keyword, step_match, multiline_arg, status, exception, source_indent, background)
  @hide_this_step = false
  if exception
    if @exceptions.include?(exception)
      @hide_this_step = true
      return
    end
    @exceptions << exception
  end
  if status != :failed && @in_background ^ background
    @hide_this_step = true
    return
  end
  @status = status
end

#before_table_row(table_row) ⇒ Object



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

def before_table_row(table_row)
  return unless @table
  @col_index = 0
  @io.print '  |'.indent(@indent-2)
end

#comment_line(comment_line) ⇒ Object



45
46
47
48
# File 'lib/cucumber/formatter/pretty.rb', line 45

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

#examples_name(keyword, name) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/cucumber/formatter/pretty.rb', line 103

def examples_name(keyword, name)
  puts unless @visiting_first_example_name
  @visiting_first_example_name = false
  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

#exception(exception, status) ⇒ Object



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

def exception(exception, status)
  return if @hide_this_step
  print_exception(exception, status, @indent)
  @io.flush
end

#feature_name(name) ⇒ Object



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

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

#py_string(string) ⇒ Object



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

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

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



123
124
125
# File 'lib/cucumber/formatter/pretty.rb', line 123

def scenario_name(keyword, name, file_colon_line, source_indent)
  print_feature_element_name keyword, name, file_colon_line, source_indent
end

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



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

def step_name(keyword, step_match, status, source_indent, background)
  return if @hide_this_step
  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

#table_cell_value(value, status) ⇒ Object



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

def table_cell_value(value, status)
  return unless @table
  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

#tag_name(tag_name) ⇒ Object



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

def tag_name(tag_name)
  tag = format_string(tag_name, :tag).indent(@indent)
  @io.print(tag)
  @io.flush
  @indent = 1
end