Class: Cucumber::Formatter::Pretty

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

Overview

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_snippets, #print_steps

Methods included from ANSIColor

#grey

Methods inherited from Ast::Visitor

#announce, #current_feature_lines=, #excluded_by_tags?, #included_by_tags?, #matches_lines?, #matches_scenario_names?, #visit_examples, #visit_outline_table, #visit_steps, #visit_table_cell

Constructor Details

#initialize(step_mother, io, options, delim = '|') ⇒ Pretty

Returns a new instance of Pretty.



16
17
18
19
20
21
22
# File 'lib/cucumber/formatter/pretty.rb', line 16

def initialize(step_mother, io, options, delim='|')
  super(step_mother)
  @io = io
  @options = options
  @delim = delim
  @indent = 0
end

Instance Attribute Details

#indent=(value) ⇒ Object (writeonly)

Sets the attribute indent

Parameters:

  • value

    the value to set the attribute indent to.



14
15
16
# File 'lib/cucumber/formatter/pretty.rb', line 14

def indent=(value)
  @indent = value
end

Instance Method Details

#visit_background(background) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/cucumber/formatter/pretty.rb', line 83

def visit_background(background)
  @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



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

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



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

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

#visit_comment_line(comment_line) ⇒ Object



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

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

#visit_examples_name(keyword, name) ⇒ Object



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

def visit_examples_name(keyword, name)
  @io.puts("\n  #{keyword} #{name}")
  @io.flush
  @indent = 4
end

#visit_exception(exception, status) ⇒ Object



140
141
142
143
144
# File 'lib/cucumber/formatter/pretty.rb', line 140

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

#visit_feature(feature) ⇒ Object



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

def visit_feature(feature)
  @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



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

def visit_feature_element(feature_element)
  @indent = 2
  super
  @io.puts
  @io.flush
end

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



106
107
108
109
110
111
112
113
114
115
# File 'lib/cucumber/formatter/pretty.rb', line 106

def visit_feature_element_name(keyword, name, file_colon_line, source_indent)
  line = "  #{keyword} #{name}"
  @io.print(line)
  if @options[:source]
    line_comment = " # #{file_colon_line}".indent(source_indent)
    @io.print(format_string(line_comment, :comment))
  end
  @io.puts
  @io.flush
end

#visit_feature_name(name) ⇒ Object



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

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

#visit_features(features) ⇒ Object



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

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

#visit_multiline_arg(multiline_arg) ⇒ Object



135
136
137
138
# File 'lib/cucumber/formatter/pretty.rb', line 135

def visit_multiline_arg(multiline_arg)
  return if @options[:no_multiline] || @skip_step
  super
end

#visit_py_string(string, status) ⇒ Object



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

def visit_py_string(string, status)
  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



102
103
104
# File 'lib/cucumber/formatter/pretty.rb', line 102

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



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

def visit_step(step)
  @indent = 6
  super
end

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



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

def visit_step_name(keyword, step_match, status, source_indent, background)
  @step_matches ||= []
  non_failed_background_step_outside_background = !@in_background && background && (status != :failed)
  @skip_step = @step_matches.index(step_match) || non_failed_background_step_outside_background
  @step_matches << step_match
  
  unless(@skip_step)
    source_indent = nil unless @options[:source]
    formatted_step_name = format_step(keyword, step_match, status, source_indent)
    @io.puts("    " + formatted_step_name)
  end
end

#visit_table_cell_value(value, width, status) ⇒ Object



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

def visit_table_cell_value(value, width, status)
  @io.print(' ' + format_string((value.to_s || '').ljust(width), status) + " #{@delim}")
  @io.flush
end

#visit_table_row(table_row) ⇒ Object



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

def visit_table_row(table_row)
  @io.print @delim.indent(@indent)
  super
  @io.puts
  print_exception(table_row.exception, :failed, @indent) if table_row.exception
end

#visit_tag_name(tag_name) ⇒ Object



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

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



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

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