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.



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

def initialize(io, monochrome)
  @io = io
  @monochrome = monochrome
  @format = MonochromeFormat.new #@monochrome ? MonochromeFormat.new : AnsiColorFormat.new
end

Instance Method Details

#background(comments, keyword, name, description, line) ⇒ Object



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

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

#eofObject



82
83
# File 'lib/gherkin/formatter/pretty_formatter.rb', line 82

def eof
end

#examples(comments, tags, keyword, name, description, line, examples_table) ⇒ Object



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

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

#feature(comments, tags, keyword, name, description, uri) ⇒ Object



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

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

#scenario(comments, tags, keyword, name, description, line) ⇒ Object



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

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

#scenario_outline(comments, tags, keyword, name, description, line) ⇒ Object



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

def scenario_outline(comments, tags, keyword, name, description, line)
  scenario(comments, tags, keyword, name, description, line)
end

#step(comments, keyword, name, line, multiline_arg, status, exception, arguments, stepdef_location) ⇒ Object



58
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 58

def step(comments, keyword, name, line, multiline_arg, status, exception, arguments, stepdef_location)
  status_param = "#{status}_param" if status
  name = Gherkin::Formatter::Argument.format(name, @format, (arguments || [])) 

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

  print_comments(comments, '    ')
  @io.puts("    #{step}#{indented_step_location!(stepdef_location)}")
  case multiline_arg
  when String
    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



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

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

#syntax_error(state, event, legal_events, line) ⇒ Object



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

def syntax_error(state, event, legal_events, line)
  raise "SYNTAX ERROR"
end

#table(rows) ⇒ Object



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

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}"
    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