Class: Gherkin::Formatter::PrettyFormatter

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

Defined Under Namespace

Classes: ColorFormat, MonochromeFormat

Constant Summary

Constants included from Colors

Colors::ALIASES

Instance Method Summary collapse

Methods included from Escaping

#escape_cell

Methods included from Colors

define_grey, define_real_grey, #grey

Constructor Details

#initialize(io, monochrome = false) ⇒ PrettyFormatter

Returns a new instance of PrettyFormatter.



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

def initialize(io, monochrome=false)
  @io = io
  @step_printer = StepPrinter.new
  @monochrome = monochrome
end

Instance Method Details

#arg_format(step) ⇒ Object



113
114
115
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 113

def arg_format(step)
  format(step.status + '_param')
end

#background(statement) ⇒ Object



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

def background(statement)
  @io.puts
  print_comments(statement.comments, '  ')
  @io.puts "  #{statement.keyword}: #{statement.name}#{indented_element_uri!(statement.keyword, statement.name, statement.line)}"
  print_description(statement.description, '    ')
end

#eofObject



130
131
132
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 130

def eof
  # NO-OP
end

#examples(examples) ⇒ Object



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

def examples(examples)
  @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



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

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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 117

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



67
68
69
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 67

def match(match)
  print_step(format('executing'), format('executing_param'), match.arguments, match.location)
end


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 75

def print_step(text_format, arg_format, arguments, location)
  print_comments(@step.comments, '    ')
  @io.write('    ')
  text_format.write_text(@io, @step.keyword)
  @step_printer.write_step(@io, text_format, arg_format, @step.name, arguments)
  print_indented_stepdef_location!(location) if location

  @io.puts
  case @step.multiline_arg
  when Model::PyString
    py_string(@step.multiline_arg)
  when Array
    table(@step.multiline_arg)
  end
end

#result(result) ⇒ Object



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

def result(result)
  # TODO: Print error message
end

#scenario(statement) ⇒ Object



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

def scenario(statement)
  @io.puts
  print_comments(statement.comments, '  ')
  print_tags(statement.tags, '  ')
  @io.puts "  #{statement.keyword}: #{statement.name}#{indented_element_uri!(statement.keyword, statement.name, statement.line)}"
  print_description(statement.description, '    ')
end

#scenario_outline(scenario_outline) ⇒ Object



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

def scenario_outline(scenario_outline)
  scenario(scenario_outline)
end

#step(step) ⇒ Object



62
63
64
65
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 62

def step(step)
  @step = step
  match(Model::Match.new([], nil)) if @monochrome
end

#steps(steps) ⇒ Object

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



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

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

#table(rows) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 141

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

#text_format(step) ⇒ Object



109
110
111
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 109

def text_format(step)
  format(step.status)
end

#uri(uri) ⇒ Object



23
24
25
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 23

def uri(uri)
  @uri = uri
end