class RSpec::Core::Formatters::BaseTextFormatter

Base for all of RSpec's built-in formatters. See RSpec::Core::Formatters::BaseFormatter to learn more about all of the methods called by the reporter.

@see RSpec::Core::Formatters::BaseFormatter @see RSpec::Core::Reporter

Constants

VT100_COLORS
VT100_COLOR_CODES

Public Instance Methods

close() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 162
def close
  output.close if IO === output && output != $stdout
end
color_code_for(code_or_symbol) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 179
def color_code_for(code_or_symbol)
  if VT100_COLOR_CODES.include?(code_or_symbol)
    code_or_symbol
  else
    VT100_COLORS.fetch(code_or_symbol) do
      color_code_for(:white)
    end
  end
end
colorise_summary(summary) click to toggle source

@api public

Colorizes the output red for failure, yellow for pending, and green otherwise.

@param [String] string

# File lib/rspec/core/formatters/base_text_formatter.rb, line 35
def colorise_summary(summary)
  if failure_count > 0
    color(summary, RSpec.configuration.failure_color)
  elsif pending_count > 0
    color(summary, RSpec.configuration.pending_color)
  else
    color(summary, RSpec.configuration.success_color)
  end
end
colorize(text, code_or_symbol) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 189
def colorize(text, code_or_symbol)
  "\e[#{color_code_for(code_or_symbol)}m#{text}\e[0m"
end
dump_commands_to_rerun_failed_examples() click to toggle source

@api public

Outputs commands which can be used to re-run failed examples.

# File lib/rspec/core/formatters/base_text_formatter.rb, line 57
def dump_commands_to_rerun_failed_examples
  return if failed_examples.empty?
  output.puts
  output.puts("Failed examples:")
  output.puts

  failed_examples.each do |example|
    output.puts(failure_color("rspec #{RSpec::Core::Metadata::relative_path(example.location)}") + " " + detail_color("# #{example.full_description}"))
  end
end
dump_failures() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 18
def dump_failures
  return if failed_examples.empty?
  output.puts
  output.puts "Failures:"
  failed_examples.each_with_index do |example, index|
    output.puts
    pending_fixed?(example) ? dump_pending_fixed(example, index) : dump_failure(example, index)
    dump_backtrace(example)
  end
end
dump_pending() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 139
def dump_pending
  unless pending_examples.empty?
    output.puts
    output.puts "Pending:"
    pending_examples.each do |pending_example|
      output.puts pending_color("  #{pending_example.full_description}")
      output.puts detail_color("    # #{pending_example.execution_result[:pending_message]}")
      output.puts detail_color("    # #{format_caller(pending_example.location)}")
      if pending_example.execution_result[:exception] \
        && RSpec.configuration.show_failures_in_pending_blocks?
        dump_failure_info(pending_example)
        dump_backtrace(pending_example)
      end
    end
  end
end
dump_profile() click to toggle source

@api public

Outputs the slowest examples and example groups in a report when using `–profile COUNT` (default 10).

# File lib/rspec/core/formatters/base_text_formatter.rb, line 72
def dump_profile
  dump_profile_slowest_examples
  dump_profile_slowest_example_groups
end
dump_profile_slowest_example_groups() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 96
def dump_profile_slowest_example_groups
  number_of_examples = RSpec.configuration.profile_examples
  example_groups = {} 

  examples.each do |example|
    location = example.example_group.parent_groups.last.metadata[:example_group][:location]

    example_groups[location] ||= Hash.new(0)
    example_groups[location][:total_time]  += example.execution_result[:run_time]
    example_groups[location][:count]       += 1
    example_groups[location][:description] = example.example_group.top_level_description unless example_groups[location].has_key?(:description)
  end

  # stop if we've only one example group
  return if example_groups.keys.length <= 1
  
  example_groups.each do |loc, hash|
    hash[:average] = hash[:total_time].to_f / hash[:count]
  end
  
  sorted_groups = example_groups.sort_by {|_, hash| -hash[:average]}.first(number_of_examples)

  output.puts "\nTop #{sorted_groups.size} slowest example groups:"
  sorted_groups.each do |loc, hash| 
    average = "#{failure_color(format_seconds(hash[:average]))} #{failure_color("seconds")} average"
    total   = "#{format_seconds(hash[:total_time])} seconds"
    count   = pluralize(hash[:count], "example")
    output.puts "  #{hash[:description]}"
    output.puts detail_color("    #{average} (#{total} / #{count}) #{loc}")
  end
end
dump_profile_slowest_examples() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 77
def dump_profile_slowest_examples
  number_of_examples = RSpec.configuration.profile_examples
  sorted_examples = examples.sort_by {|example|
    example.execution_result[:run_time] }.reverse.first(number_of_examples)

  total, slows = [examples, sorted_examples].map {|exs|
    exs.inject(0.0) {|i, e| i + e.execution_result[:run_time] }}

  time_taken = slows / total
  percentage = '%.1f' % ((time_taken.nan? ? 0.0 : time_taken) * 100)

  output.puts "\nTop #{sorted_examples.size} slowest examples (#{format_seconds(slows)} seconds, #{percentage}% of total time):\n"

  sorted_examples.each do |example|
    output.puts "  #{example.full_description}"
    output.puts detail_color("    #{failure_color(format_seconds(example.execution_result[:run_time]))} #{failure_color("seconds")} #{format_caller(example.location)}")
  end
end
dump_summary(duration, example_count, failure_count, pending_count) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 45
def dump_summary(duration, example_count, failure_count, pending_count)
  super(duration, example_count, failure_count, pending_count)
  dump_profile unless mute_profile_output?(failure_count)
  output.puts "\nFinished in #{format_duration(duration)}\n"
  output.puts colorise_summary(summary_line(example_count, failure_count, pending_count))
  dump_commands_to_rerun_failed_examples
end
message(message) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 14
def message(message)
  output.puts message
end
seed(number) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 156
def seed(number)
  output.puts
  output.puts "Randomized with seed #{number}"
  output.puts
end
summary_line(example_count, failure_count, pending_count) click to toggle source

@api public

Outputs summary with number of examples, failures and pending.

# File lib/rspec/core/formatters/base_text_formatter.rb, line 132
def summary_line(example_count, failure_count, pending_count)
  summary = pluralize(example_count, "example")
  summary << ", " << pluralize(failure_count, "failure")
  summary << ", #{pending_count} pending" if pending_count > 0
  summary
end

Protected Instance Methods

blue(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 242
def blue(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#blue", :replacement => "#fixed_color")
  color(text, :blue)
end
bold(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 195
def bold(text)
  color_enabled? ? "\e[1m#{text}\e[0m" : text
end
color(text, color_code) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 199
def color(text, color_code)
  color_enabled? ? colorize(text, color_code) : text
end
cyan(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 252
def cyan(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#cyan", :replacement => "#detail_color")
  color(text, :cyan)
end
default_color(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 223
def default_color(text)
  color(text, RSpec.configuration.default_color)
end
detail_color(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 219
def detail_color(text)
  color(text, RSpec.configuration.detail_color)
end
failure_color(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 203
def failure_color(text)
  color(text, RSpec.configuration.failure_color)
end
fixed_color(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 215
def fixed_color(text)
  color(text, RSpec.configuration.fixed_color)
end
green(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 232
def green(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#green", :replacement => "#success_color")
  color(text, :green)
end
long_padding() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 266
def long_padding
  '     '
end
magenta(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 247
def magenta(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#magenta")
  color(text, :magenta)
end
pending_color(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 211
def pending_color(text)
  color(text, RSpec.configuration.pending_color)
end
red(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 227
def red(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#red", :replacement => "#failure_color")
  color(text, :red)
end
short_padding() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 262
def short_padding
  '  '
end
success_color(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 207
def success_color(text)
  color(text, RSpec.configuration.success_color)
end
white(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 257
def white(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#white", :replacement => "#default_color")
  color(text, :white)
end
yellow(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 237
def yellow(text)
  RSpec.deprecate("RSpec::Core::Formatters::BaseTextFormatter#yellow", :replacement => "#pending_color")
  color(text, :yellow)
end

Private Instance Methods

dump_backtrace(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 276
def dump_backtrace(example)
  format_backtrace(example.execution_result[:exception].backtrace, example).each do |backtrace_info|
    output.puts detail_color("#{long_padding}# #{backtrace_info}")
  end
end
dump_failure(example, index) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 291
def dump_failure(example, index)
  output.puts "#{short_padding}#{index.next}) #{example.full_description}"
  dump_failure_info(example)
end
dump_failure_info(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 296
def dump_failure_info(example)
  exception = example.execution_result[:exception]
  exception_class_name = exception_class_name_for(exception)
  output.puts "#{long_padding}#{failure_color("Failure/Error:")} #{failure_color(read_failed_line(exception, example).strip)}"
  output.puts "#{long_padding}#{failure_color(exception_class_name)}:" unless exception_class_name =~ /RSpec/
  exception.message.to_s.split("\n").each { |line| output.puts "#{long_padding}  #{failure_color(line)}" } if exception.message

  if shared_group = find_shared_group(example)
    dump_shared_failure_info(shared_group)
  end
end
dump_pending_fixed(example, index) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 282
def dump_pending_fixed(example, index)
  output.puts "#{short_padding}#{index.next}) #{example.full_description} FIXED"
  output.puts fixed_color("#{long_padding}Expected pending '#{example.metadata[:execution_result][:pending_message]}' to fail. No Error was raised.")
end
dump_shared_failure_info(group) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 314
def dump_shared_failure_info(group)
  output.puts "#{long_padding}Shared Example Group: \"#{group.metadata[:shared_group_name]}\" called from " +
    "#{backtrace_line(group.metadata[:example_group][:location])}"
end
exception_class_name_for(exception) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 308
def exception_class_name_for(exception)
  name = exception.class.name.to_s
  name ="(anonymous error class)" if name == ''
  name
end
find_shared_group(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 319
def find_shared_group(example)
  group_and_parent_groups(example).find {|group| group.metadata[:shared_group_name]}
end
format_caller(caller_info) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 272
def format_caller(caller_info)
  backtrace_line(caller_info.to_s.split(':in `block').first)
end
group_and_parent_groups(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 323
def group_and_parent_groups(example)
  example.example_group.parent_groups + [example.example_group]
end
pending_fixed?(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 287
def pending_fixed?(example)
  example.execution_result[:pending_fixed]
end