Class: Gherkin::Tools::PrettyListener

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/gherkin/tools/pretty_listener.rb

Overview

TODO: Rename to Gherkin::Pretty::PrettyReporter - that’s what this class does (The fact that it conforms to the Gherkin Listener interface is secondary)

Constant Summary

Constants included from Colors

Colors::ALIASES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

define_grey, define_real_grey, #grey

Constructor Details

#initialize(io, monochrome = false) ⇒ PrettyListener

Returns a new instance of PrettyListener.



24
25
26
27
28
29
30
# File 'lib/gherkin/tools/pretty_listener.rb', line 24

def initialize(io, monochrome=false)
  @io = io
  @monochrome = monochrome
  @format = @monochrome ? Format::MonochromeFormat.new : Format::AnsiColorFormat.new
  @tags = nil
  @comments = nil
end

Class Method Details

.new(io, monochrome = false) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/gherkin/tools/pretty_listener.rb', line 14

def new(io, monochrome=false)
  if defined?(JRUBY_VERSION)
    require 'gherkin.jar'
    Java::GherkinFormatter::PrettyFormatter.new(io, monochrome)
  else
    super
  end
end

Instance Method Details

#background(keyword, name, line) ⇒ Object



46
47
48
# File 'lib/gherkin/tools/pretty_listener.rb', line 46

def background(keyword, name, line)
  @io.puts "\n#{grab_comments!('  ')}  #{keyword}: #{indent(name, '    ')}"
end

#comment(content, line) ⇒ Object



37
38
39
40
# File 'lib/gherkin/tools/pretty_listener.rb', line 37

def comment(content, line)
  @comments ||= []
  @comments << content
end

#eofObject



90
91
92
# File 'lib/gherkin/tools/pretty_listener.rb', line 90

def eof
  flush_table
end

#examples(keyword, name, line) ⇒ Object



60
61
62
63
# File 'lib/gherkin/tools/pretty_listener.rb', line 60

def examples(keyword, name, line)
  flush_table
  @io.puts "\n#{grab_comments!('    ')}#{grab_tags!('    ')}    #{keyword}: #{indent(name, '    ')}"
end

#exception(exception) ⇒ Object



101
102
103
104
# File 'lib/gherkin/tools/pretty_listener.rb', line 101

def exception(exception)
  exception_text = "#{exception.message} (#{exception.class})\n#{(exception.backtrace || []).join("\n")}".gsub(/^/, '      ')
  @io.puts(failed(exception_text, @monochrome))
end

#feature(keyword, name, line) ⇒ Object



42
43
44
# File 'lib/gherkin/tools/pretty_listener.rb', line 42

def feature(keyword, name, line)
  @io.puts "#{grab_comments!('')}#{grab_tags!('')}#{keyword}: #{indent(name, '  ')}"
end

#flush_table(exception = nil, statuses = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gherkin/tools/pretty_listener.rb', line 106

def flush_table(exception=nil, statuses=nil)
  return if @rows.nil?
  cell_lengths = @rows.map { |col| col.map { |cell| cell.unpack("U*").length }}
  max_lengths = cell_lengths.transpose.map { |col_lengths| col_lengths.max }.flatten

  @rows.each_with_index do |row, i|
    j = -1
    @io.puts '      | ' + row.zip(max_lengths).map { |cell, max_length|
      j += 1
      color(cell, statuses, j) + ' ' * (max_length - cell_lengths[i][j])
    }.join(' | ') + ' |'
    exception(exception) if exception
  end
  @rows = nil
end

#py_string(string, line) ⇒ Object



82
83
84
# File 'lib/gherkin/tools/pretty_listener.rb', line 82

def py_string(string, line)
  @io.puts "      \"\"\"\n" + string.gsub(START, '      ') + "\n      \"\"\""
end

#row(row, line) ⇒ Object



77
78
79
80
# File 'lib/gherkin/tools/pretty_listener.rb', line 77

def row(row, line)
  @rows ||= []
  @rows << row
end

#scenario(keyword, name, line, location = nil) ⇒ Object



50
51
52
53
# File 'lib/gherkin/tools/pretty_listener.rb', line 50

def scenario(keyword, name, line, location=nil)
  flush_table
  @io.puts "\n#{grab_comments!('  ')}#{grab_tags!('  ')}  #{keyword}: #{indent(name, '    ')}#{indented_scenario_location!(keyword, name, location)}"
end

#scenario_outline(keyword, name, line) ⇒ Object



55
56
57
58
# File 'lib/gherkin/tools/pretty_listener.rb', line 55

def scenario_outline(keyword, name, line)
  flush_table
  @io.puts "\n#{grab_comments!('  ')}#{grab_tags!('  ')}  #{keyword}: #{indent(name, '    ')}"
end

#step(keyword, name, line, status = nil, arguments = nil, location = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gherkin/tools/pretty_listener.rb', line 65

def step(keyword, name, line, status=nil, arguments=nil, location=nil)
  flush_table
  status_param = "#{status}_param" if status
  name = Gherkin::Format::Argument.format(name, @format, (arguments || [])) 
  #{|arg| status_param ? self.__send__(status_param, arg, @monochrome) : arg} if arguments

  step = "#{keyword}#{indent(name, '    ')}"
  step = self.__send__(status, step, @monochrome) if status

  @io.puts("#{grab_comments!('    ')}    #{step}#{indented_step_location!(location)}")
end

#steps(steps) ⇒ Object

This method can be invoked before a #scenario, to ensure location arguments are aligned



95
96
97
98
99
# File 'lib/gherkin/tools/pretty_listener.rb', line 95

def steps(steps)
  @step_lengths = steps.map {|keyword, name| (keyword+name).unpack("U*").length}
  @max_step_length = @step_lengths.max
  @step_index = -1
end

#syntax_error(state, event, legal_events, line) ⇒ Object



86
87
88
# File 'lib/gherkin/tools/pretty_listener.rb', line 86

def syntax_error(state, event, legal_events, line)
  raise "SYNTAX ERROR"
end

#tag(name, line) ⇒ Object



32
33
34
35
# File 'lib/gherkin/tools/pretty_listener.rb', line 32

def tag(name, line)
  @tags ||= []
  @tags << "@#{name}"
end