class RSpec::Core::Formatters::BaseTextFormatter

Public Instance Methods

close() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 102
def close
  output.close if IO === output && output != $stdout
end
colorise_summary(summary) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 24
def colorise_summary(summary)
  if failure_count > 0
    red(summary)
  elsif pending_count > 0
    yellow(summary)
  else
    green(summary)
  end
end
dump_commands_to_rerun_failed_examples() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 43
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(red("rspec #{RSpec::Core::Metadata::relative_path(example.location)}") + " " + cyan("# #{example.full_description}"))
  end
end
dump_failures() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 13
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 79
def dump_pending
  unless pending_examples.empty?
    output.puts
    output.puts "Pending:"
    pending_examples.each do |pending_example|
      output.puts yellow("  #{pending_example.full_description}")
      output.puts cyan("    # #{pending_example.execution_result[:pending_message]}")
      output.puts cyan("    # #{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
# File lib/rspec/core/formatters/base_text_formatter.rb, line 54
def dump_profile
  sorted_examples = examples.sort_by {|example|
    example.execution_result[:run_time] }.reverse.first(10)

  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 cyan("    #{red(format_seconds(example.execution_result[:run_time]))} #{red("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 34
def dump_summary(duration, example_count, failure_count, pending_count)
  super(duration, example_count, failure_count, pending_count)
  # Don't print out profiled info if there are failures, it just clutters the output
  dump_profile if profile_examples? && failure_count == 0
  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 9
def message(message)
  output.puts message
end
seed(number) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 96
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
# File lib/rspec/core/formatters/base_text_formatter.rb, line 72
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 128
def blue(text)
  color(text, "\e[34m")
end
bold(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 112
def bold(text)
  color(text, "\e[1m")
end
color(text, color_code) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 108
def color(text, color_code)
  color_enabled? ? "#{color_code}#{text}\e[0m" : text
end
cyan(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 136
def cyan(text)
  color(text, "\e[36m")
end
green(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 120
def green(text)
  color(text, "\e[32m")
end
long_padding() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 148
def long_padding
  '     '
end
magenta(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 132
def magenta(text)
  color(text, "\e[35m")
end
red(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 116
def red(text)
  color(text, "\e[31m")
end
short_padding() click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 144
def short_padding
  '  '
end
white(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 140
def white(text)
  color(text, "\e[37m")
end
yellow(text) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 124
def yellow(text)
  color(text, "\e[33m")
end

Private Instance Methods

dump_backtrace(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 158
def dump_backtrace(example)
  format_backtrace(example.execution_result[:exception].backtrace, example).each do |backtrace_info|
    output.puts cyan("#{long_padding}# #{backtrace_info}")
  end
end
dump_failure(example, index) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 173
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 178
def dump_failure_info(example)
  exception = example.execution_result[:exception]
  output.puts "#{long_padding}#{red("Failure/Error:")} #{red(read_failed_line(exception, example).strip)}"
  output.puts "#{long_padding}#{red(exception.class.name << ":")}" unless exception.class.name =~ /RSpec/
  exception.message.to_s.split("\n").each { |line| output.puts "#{long_padding}  #{red(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 164
def dump_pending_fixed(example, index)
  output.puts "#{short_padding}#{index.next}) #{example.full_description} FIXED"
  output.puts blue("#{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 188
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
find_shared_group(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 193
def find_shared_group(example)
  group_and_ancestors(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 154
def format_caller(caller_info)
  backtrace_line(caller_info.to_s.split(':in `block').first)
end
group_and_ancestors(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 197
def group_and_ancestors(example)
  example.example_group.ancestors + [example.example_group]
end
pending_fixed?(example) click to toggle source
# File lib/rspec/core/formatters/base_text_formatter.rb, line 169
def pending_fixed?(example)
  example.execution_result[:exception].pending_fixed?
end