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.

See Also:

Direct Known Subclasses

DocumentationFormatter, HtmlFormatter, ProgressFormatter

Constant Summary

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

Class Method Summary (collapse)

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

Class Method Details

+ (Object) const_missing(name)

171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 171
def self.const_missing(name)
  base = "RSpec::Core::Formatters::"
  case name
    when :VT100_COLORS then
      RSpec.deprecate("#{base}#{name}", :replacement => "#{base}ConsoleCodes::VT100_CODES")
      RSpec::Core::Formatters::ConsoleCodes::VT100_CODES
    when :VT100_COLOR_CODES then
      RSpec.deprecate("#{base}#{name}", :replacement => "#{base}ConsoleCodes::VT100_CODE_VALUES")
      require 'set'
      RSpec::Core::Formatters::ConsoleCodes::VT100_CODES.to_set
  else
    super
  end
end

Instance Method Details

- (Object) close

164
165
166
167
168
169
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 164
def close
  return unless IO === output
  return if output.closed? || output == $stdout
  output.close
end

- (Object) color_code_for(code_or_symbol)

186
187
188
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 186
def color_code_for(code_or_symbol)
  ConsoleCodes.console_code_for(code_or_symbol)
end

- (Object) colorise_summary(summary)

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

Parameters:

  • string (String)
35
36
37
38
39
40
41
42
43
# 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

- (Object) colorize(text, code_or_symbol)

190
191
192
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 190
def colorize(text, code_or_symbol)
  ConsoleCodes.wrap(text, code_or_symbol)
end

- (Object) dump_commands_to_rerun_failed_examples

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(failure_color("rspec #{RSpec::Core::Metadata::relative_path(example.location)}") + " " + detail_color("# #{example.full_description}"))
  end
end

- (Object) dump_failures

18
19
20
21
22
23
24
25
26
27
# 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

- (Object) dump_pending

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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)}")
      # instance_variable_get is a hack to avoid a deprecation warning,
      # it's only for 2.99.
      if pending_example.execution_result[:exception] \
        && RSpec.configuration.instance_variable_get(:@show_failures_in_pending_blocks)
        dump_failure_info(pending_example)
        dump_backtrace(pending_example)
      end
    end
  end
end

- (Object) dump_profile

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

72
73
74
75
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 72
def dump_profile
  dump_profile_slowest_examples
  dump_profile_slowest_example_groups
end

- (Object) dump_profile_slowest_example_groups

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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.[: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

- (Object) dump_profile_slowest_examples

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

- (Object) dump_summary(duration, example_count, failure_count, pending_count)

45
46
47
48
49
50
51
# 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

- (Object) message(message)

14
15
16
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 14
def message(message)
  output.puts message
end

- (Object) seed(number)

158
159
160
161
162
# File 'lib/rspec/core/formatters/base_text_formatter.rb', line 158
def seed(number)
  output.puts
  output.puts "Randomized with seed #{number}"
  output.puts
end

- (Object) summary_line(example_count, failure_count, pending_count)

Outputs summary with number of examples, failures and pending.

132
133
134
135
136
137
# 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