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



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



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

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

#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
67
68
69
70
71
72
73
74
75
76
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 62

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

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

  print_comments(step.comments, '    ')
  @io.puts("    #{step_text}#{indented_step_location!(step.result ? step.result.stepdef_location : nil)}")
  case step.multiline_arg
  when Model::PyString
    py_string(step.multiline_arg)
  when Array
    table(step.multiline_arg)
  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

#uri(uri) ⇒ Object



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

def uri(uri)
  @uri = uri
end