module Cucumber::Formatter::Console

This module contains helper methods that are used by formatters that print output to the terminal.

FORMAT is a hash of Proc objects, keyed by step-definition types, e.g. “FORMAT”. The Proc is called for each line of the step's output.

format_step calls format_string, format_string calls format_for to obtain the formatting Proc.

Example:

The ANSI color console formatter defines a map of step-type to output color (e.g. “passed” to “green”), then builds methods named for the step-types (e.g. “def passed”), which themselves wrap the corresponding color-named methods provided by Term::ANSIColor (e.g. “def red”).

During output, each line is processed by passing it to the formatter Proc which returns the formatted (e.g. colored) string.

Constants

FORMATS

Public Instance Methods

collect_failing_scenarios(runtime) click to toggle source
# File lib/cucumber/formatter/console.rb, line 103
def collect_failing_scenarios(runtime)
  # TODO: brittle - stop coupling to types
  scenario_class = LegacyApi::Ast::Scenario
  example_table_class = Core::Ast::ExamplesTable

  runtime.scenarios(:failed).select do |s|
    [scenario_class, example_table_class].include?(s.class)
  end.map do |s|
    s.is_a?(example_table_class)? s.scenario_outline : s
  end
end
collect_snippet_data(test_step, result) click to toggle source
# File lib/cucumber/formatter/console.rb, line 139
def collect_snippet_data(test_step, result)
  # collect snippet data for undefined steps
  unless hook?(test_step)
    keyword = test_step.source.last.actual_keyword(@previous_step_keyword)
    @previous_step_keyword = keyword
    if result.undefined?
      @snippets_input << Console::SnippetData.new(keyword, test_step.source.last)
    end
  end
end
embed(file, mime_type, label) click to toggle source
# File lib/cucumber/formatter/console.rb, line 177
def embed(file, mime_type, label)
  # no-op
end
empty_messages() click to toggle source
# File lib/cucumber/formatter/console.rb, line 214
def empty_messages
  @delayed_messages = []
end
format_step(keyword, step_match, status, source_indent) click to toggle source
# File lib/cucumber/formatter/console.rb, line 33
def format_step(keyword, step_match, status, source_indent)
  comment = if source_indent
    c = ('# ' + step_match.location.to_s).indent(source_indent)
    format_string(c, :comment)
  else
    ''
  end

  format = format_for(status, :param)
  line = keyword + step_match.format_args(format) + comment
  format_string(line, status)
end
format_string(o, status) click to toggle source
# File lib/cucumber/formatter/console.rb, line 46
def format_string(o, status)
  fmt = format_for(status)
  o.to_s.split("\n").map do |line|
    if Proc === fmt
      fmt.call(line)
    else
      fmt % line
    end
  end.join("\n")
end
linebreaks(s, max) click to toggle source

blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/10655

# File lib/cucumber/formatter/console.rb, line 134
def linebreaks(s, max)
  return s unless max && max > 0
  s.gsub(/.{1,#{max}}(?:\s|\Z)/){($& + 5.chr).gsub(/\n\005/,"\n").gsub(/\005/,"\n")}.rstrip
end
print_elements(elements, status, kind) click to toggle source
print_exception(e, status, indent) click to toggle source
print_failing_scenarios(failures, custom_profiles, given_source) click to toggle source
print_message(message) click to toggle source
print_messages() click to toggle source
print_passing_wip(options) click to toggle source
print_profile_information() click to toggle source
print_snippets(options) click to toggle source
print_statistics(duration, options) click to toggle source
print_stats(features, options) click to toggle source
print_steps(status) click to toggle source
print_table_row_messages() click to toggle source
puts(*messages) click to toggle source

define @delayed_messages = [] in your Formatter if you want to activate this feature

# File lib/cucumber/formatter/console.rb, line 183
def puts(*messages)
  if @delayed_messages
    @delayed_messages += messages
  else
    if @io
      @io.puts
      messages.each do |message|
        @io.puts(format_string(message, :tag))
      end
      @io.flush
    end
  end
end

Private Instance Methods

format_for(*keys) click to toggle source
# File lib/cucumber/formatter/console.rb, line 232
def format_for(*keys)
  key = keys.join('_').to_sym
  fmt = FORMATS[key]
  raise "No format for #{key.inspect}: #{FORMATS.inspect}" if fmt.nil?
  fmt
end
hook?(test_step) click to toggle source
# File lib/cucumber/formatter/console.rb, line 239
def hook?(test_step)
  not test_step.source.last.respond_to?(:actual_keyword)
end