@@ -71,12 +71,21 @@ def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCo
71
71
end
72
72
73
73
def fully_formatted ( failure_number , colorizer = ::RSpec ::Core ::Formatters ::ConsoleCodes )
74
- alignment_basis = "#{ ' ' * @indentation } #{ failure_number } ) "
75
- indentation = ' ' * alignment_basis . length
74
+ lines = fully_formatted_lines ( failure_number , colorizer )
75
+ lines . join ( "\n " ) << "\n "
76
+ end
76
77
77
- "\n #{ alignment_basis } #{ description_and_detail ( colorizer , indentation ) } " \
78
- "\n #{ formatted_message_and_backtrace ( colorizer , indentation ) } " \
79
- "#{ extra_detail_formatter . call ( failure_number , colorizer , indentation ) } "
78
+ def fully_formatted_lines ( failure_number , colorizer )
79
+ lines = [
80
+ description ,
81
+ detail_formatter . call ( example , colorizer ) ,
82
+ formatted_message_and_backtrace ( colorizer ) ,
83
+ extra_detail_formatter . call ( failure_number , colorizer ) ,
84
+ ] . compact . flatten
85
+
86
+ lines = indent_lines ( lines , failure_number )
87
+ lines . unshift ( "" )
88
+ lines
80
89
end
81
90
82
91
def failure_slash_error_line
@@ -93,12 +102,6 @@ def final_exception(exception)
93
102
end
94
103
end
95
104
96
- def description_and_detail ( colorizer , indentation )
97
- detail = detail_formatter . call ( example , colorizer , indentation )
98
- return ( description || detail ) unless description && detail
99
- "#{ description } \n #{ indentation } #{ detail } "
100
- end
101
-
102
105
if String . method_defined? ( :encoding )
103
106
def encoding_of ( string )
104
107
string . encoding
@@ -118,6 +121,21 @@ def encoded_string(string)
118
121
# :nocov:
119
122
end
120
123
124
+ def indent_lines ( lines , failure_number )
125
+ alignment_basis = "#{ ' ' * @indentation } #{ failure_number } ) "
126
+ indentation = ' ' * alignment_basis . length
127
+
128
+ lines . each_with_index . map do |line , index |
129
+ if index == 0
130
+ "#{ alignment_basis } #{ line } "
131
+ elsif line . empty?
132
+ line
133
+ else
134
+ "#{ indentation } #{ line } "
135
+ end
136
+ end
137
+ end
138
+
121
139
def exception_class_name ( exception = @exception )
122
140
name = exception . class . name . to_s
123
141
name = "(anonymous error class)" if name == ''
@@ -130,7 +148,7 @@ def failure_lines
130
148
lines << failure_slash_error_line unless ( description == failure_slash_error_line )
131
149
lines << "#{ exception_class_name } :" unless exception_class_name =~ /RSpec/
132
150
encoded_string ( exception . message . to_s ) . split ( "\n " ) . each do |line |
133
- lines << " #{ line } "
151
+ lines << ( line . empty? ? line : " #{ line } " )
134
152
end
135
153
unless @extra_failure_lines . empty?
136
154
lines << ''
@@ -180,16 +198,12 @@ def find_failed_line
180
198
end || exception_backtrace . first
181
199
end
182
200
183
- def formatted_message_and_backtrace ( colorizer , indentation )
201
+ def formatted_message_and_backtrace ( colorizer )
184
202
lines = colorized_message_lines ( colorizer ) + colorized_formatted_backtrace ( colorizer )
185
-
186
- formatted = ""
187
-
188
- lines . each do |line |
189
- formatted << RSpec ::Support ::EncodedString . new ( "#{ indentation } #{ line } \n " , encoding_of ( formatted ) )
203
+ encoding = encoding_of ( "" )
204
+ lines . map do |line |
205
+ RSpec ::Support ::EncodedString . new ( line , encoding )
190
206
end
191
-
192
- formatted
193
207
end
194
208
195
209
def exception_backtrace
@@ -262,7 +276,7 @@ def multiple_exceptions_error?(exception)
262
276
end
263
277
264
278
def multiple_exception_summarizer ( exception , prior_detail_formatter , color )
265
- lambda do |example , colorizer , indentation |
279
+ lambda do |example , colorizer |
266
280
summary = if exception . aggregation_metadata [ :hide_backtrace ]
267
281
# Since the backtrace is hidden, the subfailures will come
268
282
# immediately after this, and using `:` will read well.
@@ -275,27 +289,30 @@ def multiple_exception_summarizer(exception, prior_detail_formatter, color)
275
289
276
290
summary = colorizer . wrap ( summary , color || RSpec . configuration . failure_color )
277
291
return summary unless prior_detail_formatter
278
- "#{ prior_detail_formatter . call ( example , colorizer , indentation ) } \n #{ indentation } #{ summary } "
292
+ [
293
+ prior_detail_formatter . call ( example , colorizer ) ,
294
+ summary
295
+ ]
279
296
end
280
297
end
281
298
282
299
def sub_failure_list_formatter ( exception , message_color )
283
300
common_backtrace_truncater = CommonBacktraceTruncater . new ( exception )
284
301
285
- lambda do |failure_number , colorizer , indentation |
286
- exception . all_exceptions . each_with_index . map do |failure , index |
302
+ lambda do |failure_number , colorizer |
303
+ FlatMap . flat_map ( exception . all_exceptions . each_with_index ) do |failure , index |
287
304
options = with_multiple_error_options_as_needed (
288
305
failure ,
289
306
:description_formatter => :failure_slash_error_line . to_proc ,
290
- :indentation => indentation . length ,
307
+ :indentation => 0 ,
291
308
:message_color => message_color || RSpec . configuration . failure_color ,
292
309
:skip_shared_group_trace => true
293
310
)
294
311
295
312
failure = common_backtrace_truncater . with_truncated_backtrace ( failure )
296
313
presenter = ExceptionPresenter . new ( failure , @example , options )
297
- presenter . fully_formatted ( "#{ failure_number } .#{ index + 1 } " , colorizer )
298
- end . join
314
+ presenter . fully_formatted_lines ( "#{ failure_number } .#{ index + 1 } " , colorizer )
315
+ end
299
316
end
300
317
end
301
318
0 commit comments