Class: Cucumber::Formatters::PrettyFormatter

Inherits:
Object
  • Object
show all
Includes:
ANSIColor
Defined in:
lib/cucumber/formatters/pretty_formatter.rb

Constant Summary collapse

INDENT =
"\n      "
BACKTRACE_FILTER_PATTERNS =
[/vendor\/rails/, /vendor\/plugins\/cucumber/, /spec\/expectations/, /spec\/matchers/]

Constants included from ANSIColor

ANSIColor::ALIASES

Instance Method Summary collapse

Methods included from ANSIColor

#grey

Constructor Details

#initialize(io, step_mother, options = {}) ⇒ PrettyFormatter

Returns a new instance of PrettyFormatter.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 11

def initialize(io, step_mother, options={})
  @io = (io == STDOUT) ? Kernel : io
  @options = options
  @step_mother = step_mother
  @pending_scenarios  = []
  @passed             = []
  @failed             = []
  @pending_steps      = []
  @skipped            = []
  @last_executed_was_row = false
end

Instance Method Details

#dumpObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 150

def dump
  @io.puts

  @io.puts pending("#{@pending_scenarios.length} scenarios pending") if @pending_scenarios.any?

  @io.puts passed("#{@passed.length} steps passed")           if @passed.any?
  @io.puts failed("#{@failed.length} steps failed")           if @failed.any?
  @io.puts skipped("#{@skipped.length} steps skipped")        if @skipped.any?
  @io.puts pending("#{@pending_steps.length} steps pending")  if @pending_steps.any?

  @io.print reset

  print_snippets if @options[:snippets]
end

#feature_executing(feature) ⇒ Object



23
24
25
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 23

def feature_executing(feature)
  @feature = feature
end

#header_executing(header) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 27

def header_executing(header)
  @io.puts if @feature_newline
  @feature_newline = true

  header_lines = header.split("\n")
  header_lines.each_with_index do |line, index|
    @io.print line
    if @options[:source] && index==0
      @io.print padding_spaces(@feature)
      @io.print comment("# #{@feature.file}")
    end
    @io.puts
  end
end

#output_failing_step(step) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 141

def output_failing_step(step)
  backtrace = step.error.backtrace || []
  clean_backtrace = backtrace.map {|b| b.split("\n") }.flatten.reject do |line|
    BACKTRACE_FILTER_PATTERNS.detect{|p| line =~ p}
  end.map { |line| line.strip }
  @io.puts failed("      #{step.error.message.split("\n").join(INDENT)} (#{step.error.class})")
  @io.puts failed("      #{clean_backtrace.join(INDENT)}")
end


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 165

def print_snippets
  snippets = @pending_steps
  snippets.delete_if {|snippet| snippet.row? || @step_mother.has_step_definition?(snippet.name)}

  unless snippets.empty?
    @io.puts "\nYou can use these snippets to implement pending steps:\n\n"

    prev_keyword = nil
    snippets = snippets.map do |step|
      snippet = "#{step.actual_keyword} /^#{escape_regexp_characters(step.name)}$/ do\nend\n\n"
      prev_keyword = step.keyword
      snippet
    end.compact.uniq

    snippets.each do |snippet|
      @io.puts snippet
    end
  end
end

#scenario_executed(scenario) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 65

def scenario_executed(scenario)
  @io.puts
  if !scenario.row? && scenario.table_header
    @table_column_widths = scenario.table_column_widths
    @current_column = -1
    @io.print "    |"
    print_row(scenario.table_header)
    @io.puts
  elsif scenario.row? && @scenario_failed
    @io.puts
    output_failing_step(@failed.last)
  end
end

#scenario_executing(scenario) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 42

def scenario_executing(scenario)
  @scenario_failed = false
  @io.puts if @last_executed_was_row && !scenario.row?
  if scenario.row?
    @last_executed_was_row = true
    @io.print "    |"
  else
    if scenario.pending?
      @pending_scenarios << scenario
      @io.print pending("  #{Cucumber.language['scenario']}: #{scenario.name}")
    else
      @io.print passed("  #{Cucumber.language['scenario']}: #{scenario.name}")
    end
    @last_executed_was_row = false

    if @options[:source]
      @io.print padding_spaces(scenario)
      @io.print comment("# #{scenario.file}:#{scenario.line}")
    end
    @io.puts
  end
end

#step_failed(step, regexp, args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 94

def step_failed(step, regexp, args)
  if step.row?
    @failed << step
    @scenario_failed = true
    print_failed_args(args)
  else
    @failed << step
    @scenario_failed = true
    @io.print failed("    #{step.keyword} #{step.format(regexp){|param| failed_param(param) << failed}}")
    if @options[:source]
      @io.print padding_spaces(step)
      @io.print source_comment(step)
    end
    @io.puts
    output_failing_step(step)
  end
end

#step_passed(step, regexp, args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 79

def step_passed(step, regexp, args)
  if step.row?
    @passed << step
    print_passed_args(args)
  else
    @passed << step
    @io.print passed("    #{step.keyword} #{step.format(regexp){|param| passed_param(param) << passed}}")
    if @options[:source]
      @io.print padding_spaces(step)
      @io.print source_comment(step)
    end
    @io.puts
  end
end

#step_pending(step, regexp, args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 126

def step_pending(step, regexp, args)
  if step.row?
    @pending_steps << step
    print_pending_args(args)
  else
    @pending_steps << step
    @io.print pending("    #{step.keyword} #{step.name}")
    if @options[:source]
      @io.print padding_spaces(step)
      @io.print comment("# #{step.file}:#{step.line}")
    end
    @io.puts
  end
end

#step_skipped(step, regexp, args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cucumber/formatters/pretty_formatter.rb', line 112

def step_skipped(step, regexp, args)
  @skipped << step
  if step.row?
    print_skipped_args(args)
  else
    @io.print skipped("    #{step.keyword} #{step.format(regexp){|param| skipped_param(param) << skipped}}")
    if @options[:source]
      @io.print padding_spaces(step)
      @io.print source_comment(step)
    end
    @io.puts
  end
end