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.

Constant Summary

Constants included from Console

Console::FORMATS

Constants included from ANSIColor

ANSIColor::ALIASES

Instance Attribute Summary collapse

Attributes inherited from Ast::Visitor

#options

Instance Method Summary collapse

Methods included from Console

#format_step, #format_string, #print_counts, #print_elements, #print_exception, #print_snippets, #print_steps, #print_undefined_scenarios

Methods included from ANSIColor

#grey

Methods inherited from Ast::Visitor

#current_feature_lines, #current_feature_lines=, #step_definition, #world

Constructor Details

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

Returns a new instance of Pretty.



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

def initialize(step_mother, io, options, delim='|')
  super(step_mother)
  @io = io
  @options = options
  @delim = delim
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_comment(comment) ⇒ Object



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

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

#visit_comment_line(comment_line) ⇒ Object



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

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

#visit_examples(examples) ⇒ Object



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

def visit_examples(examples)
  examples.accept(self)
end

#visit_examples_name(keyword, name) ⇒ Object



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

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

#visit_feature(feature) ⇒ Object



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

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
      with_color do
        feature.accept(self)
      end
    end
  else
    with_color do
      feature.accept(self)
    end
  end
end

#visit_feature_element(feature_element) ⇒ Object



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

def visit_feature_element(feature_element)
  @indent = 2
  @last_undefined = feature_element.undefined?
  feature_element.accept(self)
  @io.puts
  @io.flush
end

#visit_feature_name(name) ⇒ Object



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

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

#visit_features(features) ⇒ Object



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

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

#visit_multiline_arg(multiline_arg, status) ⇒ Object



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

def visit_multiline_arg(multiline_arg, status)
  multiline_arg.accept(self, status)
end

#visit_py_string(string, status) ⇒ Object



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

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_line, source_indent) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cucumber/formatter/pretty.rb', line 97

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

#visit_step(step) ⇒ Object



109
110
111
112
113
# File 'lib/cucumber/formatter/pretty.rb', line 109

def visit_step(step)
  @indent = 6
  exception = step.accept(self)
  print_exception(exception, @indent) if exception
end

#visit_step_name(keyword, step_name, status, step_definition, source_indent) ⇒ Object



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

def visit_step_name(keyword, step_name, status, step_definition, source_indent)
  source_indent = nil unless @options[:source]
  formatted_step_name = format_step(keyword, step_name, status, step_definition, source_indent)
  @io.puts("    " + formatted_step_name)
  @io.flush
end

#visit_table_cell(table_cell, status) ⇒ Object



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

def visit_table_cell(table_cell, status)
  table_cell.accept(self, status)
end

#visit_table_cell_value(value, width, status) ⇒ Object



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

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

#visit_table_row(table_row, status) ⇒ Object



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

def visit_table_row(table_row, status)
  @io.print @delim.indent(@indent)
  exception = table_row.accept(self, status)
  @io.puts
  print_exception(exception, 6) if exception
end

#visit_tag_name(tag_name) ⇒ Object



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

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



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

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