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(key) ⇒ Object



115
116
117
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 115

def arg_format(key)
  format("#{key}_arg")
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



132
133
134
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 132

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



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

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



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

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


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 78

def print_step(status, arguments, location)
  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)
  print_indented_step_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



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

def result(result)
  @io.write("\033[1A")
  print_step(result.status, @match.arguments, @match.location)
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
66
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 62

def step(step)
  @step = step
  @step_index += 1 if @step_index
  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



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

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



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

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



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

def uri(uri)
  @uri = uri
end