Class: Gherkin::Formatter::PrettyFormatter

Inherits:
Object
  • Object
show all
Includes:
AnsiEscapes, Escaping
Defined in:
lib/gherkin/formatter/pretty_formatter.rb

Defined Under Namespace

Classes: ColorFormat, MonochromeFormat

Constant Summary

Constants included from AnsiEscapes

AnsiEscapes::ALIASES, AnsiEscapes::COLORS

Instance Method Summary collapse

Methods included from Escaping

#escape_cell

Methods included from AnsiEscapes

#reset, #up

Constructor Details

#initialize(io, monochrome, executing) ⇒ PrettyFormatter

Returns a new instance of PrettyFormatter.



17
18
19
20
21
22
23
24
25
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 17

def initialize(io, monochrome, executing)
  @io = io
  @step_printer = StepPrinter.new
  @monochrome = monochrome
  @executing = executing
  @background = nil
  @tag_statement = nil
  @steps = []
end

Instance Method Details

#arg_format(key) ⇒ Object



139
140
141
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 139

def arg_format(key)
  format("#{key}_arg")
end

#background(background) ⇒ Object



38
39
40
41
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 38

def background(background)
  replay
  @statement = background
end

#eofObject



156
157
158
159
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 156

def eof
  replay
  # NO-OP
end

#examples(examples) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 77

def examples(examples)
  replay
  @io.puts
  print_comments(examples.comments, '    ')
  print_tags(examples.tags, '    ')
  @io.puts "    #{examples.keyword}: #{examples.name}"
  print_description(examples.description, '      ')
  table(examples.rows)
end

#feature(feature) ⇒ Object



31
32
33
34
35
36
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 31

def feature(feature)
  print_comments(feature.comments, '')
  print_tags(feature.tags, '')
  @io.puts "#{feature.keyword}: #{feature.name}"
  print_description(feature.description, '  ', false)
end

#format(key) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 143

def format(key)
  if @formats.nil?
    if @monochrome
      @formats = Hash.new(MonochromeFormat.new)
    else
      @formats = Hash.new do |formats, status|
        formats[status] = ColorFormat.new(status)
      end
    end
  end
  @formats[key]
end

#match(match) ⇒ Object



91
92
93
94
95
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 91

def match(match)
  @match = match
  print_statement
  print_step('executing', @match.arguments, @match.location, false)
end


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 58

def print_statement
  return if @statement.nil?
  calculate_location_indentations
  @io.puts
  print_comments(@statement.comments, '  ')
  print_tags(@statement.tags, '  ') if @statement.respond_to?(:tags) # Background doesn't
  @io.write "  #{@statement.keyword}: #{@statement.name}"
  location = @executing ? "#{@uri}:#{@statement.line}" : nil
  @io.puts indented_location(location, true)
  print_description(@statement.description, '    ')
  @statement = nil
end


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 102

def print_step(status, arguments, location, proceed)
  step = proceed ? @steps.shift : @steps[0]
  
  text_format = format(status)
  arg_format = arg_format(status)
  
  print_comments(step.comments, '    ')
  @io.write('    ')
  @io.write(text_format.text(step.keyword))
  @step_printer.write_step(@io, text_format, arg_format, step.name, arguments)
  @io.puts(indented_location(location, proceed))
  case step.multiline_arg
  when Model::PyString
    py_string(step.multiline_arg)
  when Array
    table(step.multiline_arg)
  end
end


71
72
73
74
75
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 71

def print_steps
  while(@steps.any?)
    print_step('skipped', [], nil, true)
  end
end

#replayObject



53
54
55
56
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 53

def replay
  print_statement
  print_steps
end

#result(result) ⇒ Object



97
98
99
100
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 97

def result(result)
  @io.write(up(1))
  print_step(result.status, @match.arguments, @match.location, true)
end

#scenario(scenario) ⇒ Object



43
44
45
46
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 43

def scenario(scenario)
  replay
  @statement = scenario
end

#scenario_outline(scenario_outline) ⇒ Object



48
49
50
51
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 48

def scenario_outline(scenario_outline)
  replay
  @statement = scenario_outline
end

#step(step) ⇒ Object



87
88
89
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 87

def step(step)
  @steps << step
end

#table(rows) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 161

def table(rows)
  cell_lengths = rows.map do |row| 
    row.cells.map do |cell| 
      escape_cell(cell).unpack("U*").length
    end
  end
  max_lengths = cell_lengths.transpose.map { |col_lengths| col_lengths.max }.flatten

  rows.each_with_index do |row, i|
    row.comments.each do |comment|
      @io.puts "      #{comment.value}"
    end
    j = -1
    @io.puts '      | ' + row.cells.zip(max_lengths).map { |cell, max_length|
      j += 1
      color(cell, nil, j) + ' ' * (max_length - cell_lengths[i][j])
    }.join(' | ') + ' |'
  end
end

#uri(uri) ⇒ Object



27
28
29
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 27

def uri(uri)
  @uri = uri
end