Class: Gherkin::Formatter::PrettyFormatter

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

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) ⇒ 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)
  @io = io
  @monochrome = monochrome
  @format = MonochromeFormat.new #@monochrome ? MonochromeFormat.new : AnsiColorFormat.new
end

Instance Method Details

#background(statement) ⇒ Object



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

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



78
79
80
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 78

def eof
  # NO-OP
end

#examples(statement, examples_rows) ⇒ Object



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

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

#feature(statement, uri) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 23

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

#scenario(statement) ⇒ Object



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

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



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

def scenario_outline(statement)
  scenario(statement)
end

#step(statement, multiline_arg, result) ⇒ Object



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

def step(statement, multiline_arg, result)
  name = Gherkin::Formatter::Argument.format(statement.name, @format, (result ? result.arguments : []))

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

  print_comments(statement.comments, '    ')
  @io.puts("    #{step}#{indented_step_location!(result ? result.stepdef_location : nil)}")
  case multiline_arg
  when Model::PyString
    py_string(multiline_arg)
  when Array
    table(multiline_arg)
  when NilClass
  else
    raise "Bad multiline_arg: #{multiline_arg.inspect}"
  end
end

#steps(steps) ⇒ Object

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



83
84
85
86
87
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 83

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

#table(rows) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 89

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