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

Commit 9f101f4

Browse files
committed
Simplify handling of linefeed and indentation in ExceptionPresenter
... by using Array for lines until final rendering.
1 parent 8c9341c commit 9f101f4

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,21 @@ def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCo
7070
end
7171

7272
def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::ConsoleCodes)
73-
alignment_basis = "#{' ' * @indentation}#{failure_number}) "
74-
indentation = ' ' * alignment_basis.length
73+
lines = fully_formatted_lines(failure_number, colorizer)
74+
lines.join("\n") << "\n"
75+
end
7576

76-
"\n#{alignment_basis}#{description_and_detail(colorizer, indentation)}" \
77-
"\n#{formatted_message_and_backtrace(colorizer, indentation)}" \
78-
"#{extra_detail_formatter.call(failure_number, colorizer, indentation)}"
77+
def fully_formatted_lines(failure_number, colorizer)
78+
lines = [
79+
description,
80+
detail_formatter.call(example, colorizer),
81+
formatted_message_and_backtrace(colorizer),
82+
extra_detail_formatter.call(failure_number, colorizer),
83+
].compact.flatten
84+
85+
lines = indent_lines(lines, failure_number)
86+
lines.unshift("")
87+
lines
7988
end
8089

8190
def failure_slash_error_line
@@ -92,12 +101,6 @@ def final_exception(exception)
92101
end
93102
end
94103

95-
def description_and_detail(colorizer, indentation)
96-
detail = detail_formatter.call(example, colorizer, indentation)
97-
return (description || detail) unless description && detail
98-
"#{description}\n#{indentation}#{detail}"
99-
end
100-
101104
if String.method_defined?(:encoding)
102105
def encoding_of(string)
103106
string.encoding
@@ -117,6 +120,21 @@ def encoded_string(string)
117120
# :nocov:
118121
end
119122

123+
def indent_lines(lines, failure_number)
124+
alignment_basis = "#{' ' * @indentation}#{failure_number}) "
125+
indentation = ' ' * alignment_basis.length
126+
127+
lines.each_with_index.map do |line, index|
128+
if index == 0
129+
"#{alignment_basis}#{line}"
130+
elsif line.empty?
131+
line
132+
else
133+
"#{indentation}#{line}"
134+
end
135+
end
136+
end
137+
120138
def exception_class_name(exception=@exception)
121139
name = exception.class.name.to_s
122140
name = "(anonymous error class)" if name == ''
@@ -171,16 +189,12 @@ def find_failed_line
171189
end
172190
end
173191

174-
def formatted_message_and_backtrace(colorizer, indentation)
192+
def formatted_message_and_backtrace(colorizer)
175193
lines = colorized_message_lines(colorizer) + colorized_formatted_backtrace(colorizer)
176-
177-
formatted = ""
178-
179-
lines.each do |line|
180-
formatted << RSpec::Support::EncodedString.new("#{indentation}#{line}\n", encoding_of(formatted))
194+
encoding = encoding_of("")
195+
lines.map do |line|
196+
RSpec::Support::EncodedString.new(line, encoding)
181197
end
182-
183-
formatted
184198
end
185199

186200
def exception_backtrace
@@ -253,7 +267,7 @@ def multiple_exceptions_error?(exception)
253267
end
254268

255269
def multiple_exception_summarizer(exception, prior_detail_formatter, color)
256-
lambda do |example, colorizer, indentation|
270+
lambda do |example, colorizer|
257271
summary = if exception.aggregation_metadata[:hide_backtrace]
258272
# Since the backtrace is hidden, the subfailures will come
259273
# immediately after this, and using `:` will read well.
@@ -266,27 +280,30 @@ def multiple_exception_summarizer(exception, prior_detail_formatter, color)
266280

267281
summary = colorizer.wrap(summary, color || RSpec.configuration.failure_color)
268282
return summary unless prior_detail_formatter
269-
"#{prior_detail_formatter.call(example, colorizer, indentation)}\n#{indentation}#{summary}"
283+
[
284+
prior_detail_formatter.call(example, colorizer),
285+
summary
286+
]
270287
end
271288
end
272289

273290
def sub_failure_list_formatter(exception, message_color)
274291
common_backtrace_truncater = CommonBacktraceTruncater.new(exception)
275292

276-
lambda do |failure_number, colorizer, indentation|
277-
exception.all_exceptions.each_with_index.map do |failure, index|
293+
lambda do |failure_number, colorizer|
294+
FlatMap.flat_map(exception.all_exceptions.each_with_index) do |failure, index|
278295
options = with_multiple_error_options_as_needed(
279296
failure,
280297
:description_formatter => :failure_slash_error_line.to_proc,
281-
:indentation => indentation.length,
298+
:indentation => 0,
282299
:message_color => message_color || RSpec.configuration.failure_color,
283300
:skip_shared_group_trace => true
284301
)
285302

286303
failure = common_backtrace_truncater.with_truncated_backtrace(failure)
287304
presenter = ExceptionPresenter.new(failure, @example, options)
288-
presenter.fully_formatted("#{failure_number}.#{index + 1}", colorizer)
289-
end.join
305+
presenter.fully_formatted_lines("#{failure_number}.#{index + 1}", colorizer)
306+
end
290307
end
291308
end
292309

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ module RSpec::Core
107107

108108
it 'allows a caller to specify extra details that are added to the bottom' do
109109
the_presenter = Formatters::ExceptionPresenter.new(
110-
exception, example, :extra_detail_formatter => lambda do |failure_number, colorizer, indentation|
111-
"#{indentation}extra detail for failure: #{failure_number}\n"
110+
exception, example, :extra_detail_formatter => lambda do |failure_number, colorizer|
111+
"extra detail for failure: #{failure_number}"
112112
end
113113
)
114114

0 commit comments

Comments
 (0)