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
26
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 17

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

  # Prevent warnings, initialize fields
  @background = @tag_statement = @formats = @statement = nil
end

Instance Method Details

#arg_format(key) ⇒ Object



137
138
139
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 137

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

#background(background) ⇒ Object



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

def background(background)
  replay
  @statement = background
end

#doneObject



159
160
161
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 159

def done
  # NO-OP
end

#eofObject



154
155
156
157
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 154

def eof
  replay
  # NO-OP
end

#examples(examples) ⇒ Object



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

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



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

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



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

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



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

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


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

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


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

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))
  
  doc_string(step.doc_string) if step.doc_string
  table(step.rows) if step.rows
end


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

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

#replayObject



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

def replay
  print_statement
  print_steps
end

#result(result) ⇒ Object



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

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

#scenario(scenario) ⇒ Object



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

def scenario(scenario)
  replay
  @statement = scenario
end

#scenario_outline(scenario_outline) ⇒ Object



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

def scenario_outline(scenario_outline)
  replay
  @statement = scenario_outline
end

#step(step) ⇒ Object



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

def step(step)
  @steps << step
end

#table(rows) ⇒ Object



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

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



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

def uri(uri)
  @uri = uri
end