Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit d35a3e1

Browse files
committed
Refactor handling of the first line of exception report
It allows detail or failure/error line to be used as the first line by omitting preceding lines, rather than specifying content of detail or failure/error as description.
1 parent 9f101f4 commit d35a3e1

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(exception, example, options={})
1212
@exception = exception
1313
@example = example
1414
@message_color = options.fetch(:message_color) { RSpec.configuration.failure_color }
15-
@description = options.fetch(:description_formatter) { Proc.new { example.full_description } }.call(self)
15+
@description = options.fetch(:description) { example.full_description }
1616
@detail_formatter = options.fetch(:detail_formatter) { Proc.new {} }
1717
@extra_detail_formatter = options.fetch(:extra_detail_formatter) { Proc.new {} }
1818
@backtrace_formatter = options.fetch(:backtrace_formatter) { RSpec.configuration.backtrace_formatter }
@@ -87,10 +87,6 @@ def fully_formatted_lines(failure_number, colorizer)
8787
lines
8888
end
8989

90-
def failure_slash_error_line
91-
@failure_slash_error_line ||= "Failure/Error: #{read_failed_line.strip}"
92-
end
93-
9490
private
9591

9692
def final_exception(exception)
@@ -142,15 +138,20 @@ def exception_class_name(exception=@exception)
142138
end
143139

144140
def failure_lines
145-
@failure_lines ||= begin
146-
lines = []
147-
lines << failure_slash_error_line unless (description == failure_slash_error_line)
148-
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
149-
encoded_string(exception.message.to_s).split("\n").each do |line|
150-
lines << " #{line}"
151-
end
152-
lines
141+
@failure_lines ||= [failure_slash_error_line] + exception_lines
142+
end
143+
144+
def failure_slash_error_line
145+
"Failure/Error: #{read_failed_line.strip}"
146+
end
147+
148+
def exception_lines
149+
lines = []
150+
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
151+
encoded_string(exception.message.to_s).split("\n").each do |line|
152+
lines << " #{line}"
153153
end
154+
lines
154155
end
155156

156157
def add_shared_group_lines(lines, colorizer)
@@ -230,9 +231,9 @@ def options
230231
def pending_options
231232
if @execution_result.pending_fixed?
232233
{
233-
:description_formatter => Proc.new { "#{@example.full_description} FIXED" },
234-
:message_color => RSpec.configuration.fixed_color,
235-
:failure_lines => [
234+
:description => "#{@example.full_description} FIXED",
235+
:message_color => RSpec.configuration.fixed_color,
236+
:failure_lines => [
236237
"Expected pending '#{@execution_result.pending_message}' to fail. No Error was raised."
237238
]
238239
}
@@ -255,8 +256,6 @@ def with_multiple_error_options_as_needed(exception, options)
255256
options[:message_color])
256257
)
257258

258-
options[:description_formatter] &&= Proc.new {}
259-
260259
return options unless exception.aggregation_metadata[:hide_backtrace]
261260
options[:backtrace_formatter] = EmptyBacktraceFormatter
262261
options
@@ -294,7 +293,7 @@ def sub_failure_list_formatter(exception, message_color)
294293
FlatMap.flat_map(exception.all_exceptions.each_with_index) do |failure, index|
295294
options = with_multiple_error_options_as_needed(
296295
failure,
297-
:description_formatter => :failure_slash_error_line.to_proc,
296+
:description => nil,
298297
:indentation => 0,
299298
:message_color => message_color || RSpec.configuration.failure_color,
300299
:skip_shared_group_trace => true

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module RSpec::Core
8181
it 'allows the caller to omit the description' do
8282
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
8383
:detail_formatter => Proc.new { "Detail!" },
84-
:description_formatter => Proc.new { })
84+
:description => nil)
8585

8686
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
8787
|
@@ -94,7 +94,7 @@ module RSpec::Core
9494
end
9595

9696
it 'allows the failure/error line to be used as the description' do
97-
the_presenter = Formatters::ExceptionPresenter.new(exception, example, :description_formatter => lambda { |p| p.failure_slash_error_line })
97+
the_presenter = Formatters::ExceptionPresenter.new(exception, example, :description => nil)
9898

9999
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
100100
|

0 commit comments

Comments
 (0)