class Gherkin::Formatter::PrettyFormatter

Constants

START
TRIPLE_QUOTES

Public Class Methods

new(io, monochrome, executing) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 16
def initialize(io, monochrome, executing)
  @io = io
  @step_printer = StepPrinter.new
  @monochrome = monochrome
  @executing = executing
  @steps = []

  # Prevent warnings, initialize fields
  @background = @tag_statement = @formats = @statement = nil
end

Public Instance Methods

arg_format(key) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 136
def arg_format(key)
  format("#{key}_arg")
end
background(background) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 38
def background(background)
  replay
  @statement = background
end
done() click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 158
def done
  # NO-OP
end
eof() click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 153
def eof
  replay
  # NO-OP
end
examples(examples) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 77
def examples(examples)
  replay
  @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) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 31
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) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 140
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) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 91
def match(match)
  @match = match
  print_statement
  print_step('executing', @match.arguments, @match.location, false)
end
print_statement() click to toggle source
print_step(status, arguments, location, proceed) click to toggle source
print_steps() click to toggle source
replay() click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 53
def replay
  print_statement
  print_steps
end
result(result) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 97
def result(result)
  @io.write(up(1))
  print_step(result.status, @match.arguments, @match.location, true)
end
scenario(scenario) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 43
def scenario(scenario)
  replay
  @statement = scenario
end
scenario_outline(scenario_outline) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 48
def scenario_outline(scenario_outline)
  replay
  @statement = scenario_outline
end
step(step) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 87
def step(step)
  @steps << step
end
table(rows) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 162
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) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 27
def uri(uri)
  @uri = uri
end

Private Instance Methods

calculate_location_indentations() click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 237
def calculate_location_indentations
  line_widths = ([@statement] + @steps).map {|step| (step.keyword+step.name).unpack("U*").length}
  max_line_width = line_widths.max
  @indentations = line_widths.map{|w| max_line_width - w}
end
color(cell, statuses, col) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 193
def color(cell, statuses, col)
  if statuses
    self.__send__(statuses[col], escape_cell(cell)) + reset
  else
    escape_cell(cell)
  end
end
doc_string(doc_string) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 184
def doc_string(doc_string)
  @io.puts "      \"\"\"" + doc_string.content_type + "\n" + escape_triple_quotes(indent(doc_string.value, '      ')) + "\n      \"\"\""
end
escape_triple_quotes(s) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 213
def escape_triple_quotes(s)
  s.gsub(TRIPLE_QUOTES, '\"\"\"')
end
exception(exception) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 188
def exception(exception)
  exception_text = "#{exception.message} (#{exception.class})\n#{(exception.backtrace || []).join("\n")}".gsub(/^/, '      ')
  @io.puts(failed(exception_text))
end
indent(string, indentation) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 209
def indent(string, indentation)
  string.gsub(START, indentation)
end
indented_location(location, proceed) click to toggle source
# File lib/gherkin/formatter/pretty_formatter.rb, line 232
def indented_location(location, proceed)
  indentation = proceed ? @indentations.shift : @indentations[0]
  location ? (' ' * indentation + ' ' + comments + "# #{location}" + reset) : ''
end
print_comments(comments, indent) click to toggle source
print_description(description, indent, newline=true) click to toggle source
print_tags(tags, indent) click to toggle source