Class: RSpec::Core::Formatters::BaseTextFormatter

Inherits:
BaseFormatter
  • Object
show all
Defined in:
lib/rspec/core/formatters/base_text_formatter.rb

Overview

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.

Constant Summary

Constants included from Helpers

Helpers::DEFAULT_PRECISION, Helpers::SUB_SECOND_PRECISION

Instance Attribute Summary

Attributes inherited from BaseFormatter

#duration, #example_count, #example_group, #examples, #failed_examples, #failure_count, #output, #pending_count, #pending_examples

Instance Method Summary collapse

Methods inherited from BaseFormatter

#example_failed, #example_group_finished, #example_group_started, #example_passed, #example_pending, #example_started, #format_backtrace, #initialize, #start, #start_dump, #stop

Methods included from Helpers

#format_duration, #format_seconds, #pluralize, #strip_trailing_zeroes

Methods included from BacktraceFormatter

#format_backtrace

Constructor Details

This class inherits a constructor from RSpec::Core::Formatters::BaseFormatter

Instance Method Details

#closeObject



124
125
126
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 124

def close
  output.close if IO === output && output != $stdout
end

#colorise_summary(summary) ⇒ Object

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

Parameters:

  • string (String)


34
35
36
37
38
39
40
41
42
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 34

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_examplesObject

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



57
58
59
60
61
62
63
64
65
66
# 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(red("rspec #{RSpec::Core::Metadata::relative_path(example.location)}") + " " + cyan("# #{example.full_description}"))
  end
end

#dump_failuresObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 17

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_pendingObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 101

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_profileObject

Outputs the 10 slowest examples in a report when using --profile.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 72

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) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 44

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) ⇒ Object



13
14
15
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 13

def message(message)
  output.puts message
end

#seed(number) ⇒ Object



118
119
120
121
122
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 118

def seed(number)
  output.puts
  output.puts "Randomized with seed #{number}"
  output.puts
end

#summary_line(example_count, failure_count, pending_count) ⇒ Object

Outputs summary with number of examples, failures and pending.



94
95
96
97
98
99
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 94

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