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

Commit b98c638

Browse files
committed
Insert blank line between snippet and error except when both are oneline
1 parent 27b187d commit b98c638

File tree

4 files changed

+200
-4
lines changed

4 files changed

+200
-4
lines changed

lib/rspec/core/formatters/exception_presenter.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,17 @@ def exception_class_name(exception=@exception)
140140
end
141141

142142
def failure_lines
143-
@failure_lines ||= failure_slash_error_lines + exception_lines + extra_failure_lines
143+
@failure_lines ||= [].tap do |lines|
144+
lines.concat(failure_slash_error_lines)
145+
146+
sections = [failure_slash_error_lines, exception_lines]
147+
if sections.any? { |section| section.size > 1 } && !exception_lines.first.empty?
148+
lines << ''
149+
end
150+
151+
lines.concat(exception_lines)
152+
lines.concat(extra_failure_lines)
153+
end
144154
end
145155

146156
def failure_slash_error_lines

spec/rspec/core/formatters/exception_presenter_spec.rb

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module RSpec::Core
3232
|
3333
| 1) Example
3434
| Failure/Error: # The failure happened here!#{ encoding_check }
35+
|
3536
| Boom
3637
| Bam
3738
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -43,6 +44,7 @@ module RSpec::Core
4344
|
4445
| 100) Example
4546
| Failure/Error: # The failure happened here!#{ encoding_check }
47+
|
4648
| Boom
4749
| Bam
4850
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -56,6 +58,7 @@ module RSpec::Core
5658
|
5759
| 1) Example
5860
| Failure/Error: # The failure happened here!#{ encoding_check }
61+
|
5962
| Boom
6063
| Bam
6164
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -72,6 +75,7 @@ module RSpec::Core
7275
| 1) Example
7376
| Some Detail
7477
| Failure/Error: # The failure happened here!#{ encoding_check }
78+
|
7579
| Boom
7680
| Bam
7781
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -87,6 +91,7 @@ module RSpec::Core
8791
|
8892
| 1) Detail!
8993
| Failure/Error: # The failure happened here!#{ encoding_check }
94+
|
9095
| Boom
9196
| Bam
9297
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -99,6 +104,7 @@ module RSpec::Core
99104
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
100105
|
101106
| 1) Failure/Error: # The failure happened here!#{ encoding_check }
107+
|
102108
| Boom
103109
| Bam
104110
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -116,6 +122,7 @@ module RSpec::Core
116122
|
117123
| 2) Example
118124
| Failure/Error: # The failure happened here!#{ encoding_check }
125+
|
119126
| Boom
120127
| Bam
121128
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
@@ -140,14 +147,15 @@ module RSpec::Core
140147
|
141148
| 1) Example
142149
| Failure/Error: # The failure happened here!#{ encoding_check }
150+
|
143151
| Boom
144152
| Bam
145153
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
146154
| # ------------------
147155
| # --- Caused by: ---
148156
| # Real
149157
| # culprit
150-
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:133
158+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:140
151159
EOS
152160
end
153161

@@ -160,6 +168,7 @@ module RSpec::Core
160168
|
161169
| 1) Example
162170
| Failure/Error: # The failure happened here!#{ encoding_check }
171+
|
163172
| Boom
164173
| Bam
165174
|
@@ -168,7 +177,175 @@ module RSpec::Core
168177
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
169178
EOS
170179
end
180+
181+
describe 'line format' do
182+
let(:exception) do
183+
begin
184+
expression
185+
rescue RSpec::Support::AllExceptionsExceptOnesWeMustNotRescue => exception
186+
exception
187+
end
188+
end
189+
190+
context 'with single line expression and single line RSpec exception message' do
191+
let(:expression) do
192+
expect('RSpec').to be_a(Integer)
193+
end
194+
195+
it 'crams them without blank line' do
196+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
197+
|
198+
| 1) Example
199+
| Failure/Error: expect('RSpec').to be_a(Integer)
200+
| expected "RSpec" to be a kind of Integer
201+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
202+
EOS
203+
end
204+
end
205+
206+
context 'with multiline expression and single line RSpec exception message', :if => RSpec::Support::RubyFeatures.ripper_supported? do
207+
let(:expression) do
208+
expect('RSpec').
209+
to be_a(Integer)
210+
end
211+
212+
it 'inserts a blank line between the expression and the message' do
213+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
214+
|
215+
| 1) Example
216+
| Failure/Error:
217+
| expect('RSpec').
218+
| to be_a(Integer)
219+
|
220+
| expected "RSpec" to be a kind of Integer
221+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
222+
EOS
223+
end
224+
end
225+
226+
context 'with single line expression and multiline RSpec exception message' do
227+
let(:expression) do
228+
expect('RSpec').to be_falsey
229+
end
230+
231+
it 'inserts a blank line between the expression and the message' do
232+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
233+
|
234+
| 1) Example
235+
| Failure/Error: expect('RSpec').to be_falsey
236+
|
237+
| expected: falsey value
238+
| got: "RSpec"
239+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
240+
EOS
241+
end
242+
end
243+
244+
context 'with multiline expression and multiline RSpec exception message', :if => RSpec::Support::RubyFeatures.ripper_supported? do
245+
let(:expression) do
246+
expect('RSpec').
247+
to be_falsey
248+
end
249+
250+
it 'inserts a blank line between the expression and the message' do
251+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
252+
|
253+
| 1) Example
254+
| Failure/Error:
255+
| expect('RSpec').
256+
| to be_falsey
257+
|
258+
| expected: falsey value
259+
| got: "RSpec"
260+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
261+
EOS
262+
end
263+
end
264+
265+
context 'with single line expression and RSpec exception message starting with linefeed (like `eq` matcher)' do
266+
let(:expression) do
267+
expect('Rspec').to eq('RSpec')
268+
end
269+
270+
it 'does not insert a superfluous blank line' do
271+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
272+
|
273+
| 1) Example
274+
| Failure/Error: expect('Rspec').to eq('RSpec')
275+
|
276+
| expected: "RSpec"
277+
| got: "Rspec"
278+
|
279+
| (compared using ==)
280+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
281+
EOS
282+
end
283+
end
284+
285+
context 'with multiline expression and RSpec exception message starting with linefeed (like `eq` matcher)', :if => RSpec::Support::RubyFeatures.ripper_supported? do
286+
let(:expression) do
287+
expect('Rspec').
288+
to eq('RSpec')
289+
end
290+
291+
it 'does not insert a superfluous blank line' do
292+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
293+
|
294+
| 1) Example
295+
| Failure/Error:
296+
| expect('Rspec').
297+
| to eq('RSpec')
298+
|
299+
| expected: "RSpec"
300+
| got: "Rspec"
301+
|
302+
| (compared using ==)
303+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
304+
EOS
305+
end
306+
end
307+
308+
context 'with single line expression and single line non-RSpec exception message' do
309+
let(:expression) do
310+
expect { fail 'Something is wrong!' }.to change { RSpec }
311+
end
312+
313+
it 'inserts a blank line between the expression and the message' do
314+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
315+
|
316+
| 1) Example
317+
| Failure/Error: expect { fail 'Something is wrong!' }.to change { RSpec }
318+
|
319+
| RuntimeError:
320+
| Something is wrong!
321+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
322+
EOS
323+
end
324+
end
325+
326+
context 'with multiline expression and single line non-RSpec exception message', :if => RSpec::Support::RubyFeatures.ripper_supported? do
327+
let(:expression) do
328+
expect { fail 'Something is wrong!' }.
329+
to change { RSpec }
330+
end
331+
332+
it 'inserts a blank line between the expression and the message' do
333+
expect(presenter.fully_formatted(1)).to start_with(<<-EOS.gsub(/^ +\|/, '').chomp)
334+
|
335+
| 1) Example
336+
| Failure/Error:
337+
| expect { fail 'Something is wrong!' }.
338+
| to change { RSpec }
339+
|
340+
| RuntimeError:
341+
| Something is wrong!
342+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:
343+
EOS
344+
end
345+
end
346+
end
171347
end
348+
172349
describe "#read_failed_lines" do
173350
def read_failed_lines
174351
presenter.send(:read_failed_lines)

spec/rspec/core/notifications_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,14 @@ def normalize_one_backtrace(exception)
229229
end
230230

231231
it 'lists both types in the exception listing' do
232-
expect(fully_formatted.lines.to_a.last(9)).to eq(dedent(<<-EOS).lines.to_a)
232+
expect(fully_formatted.lines.to_a.last(10)).to eq(dedent(<<-EOS).lines.to_a)
233233
|
234234
| 1.1) Failure/Error: expect(1).to fail_with_description("foo")
235235
| expected pass, but foo
236236
| # #{RSpec::Core::Metadata.relative_path(__FILE__)}:#{aggregate_line + 1}
237237
|
238238
| 1.2) Failure/Error: raise "boom"
239+
|
239240
| RuntimeError:
240241
| boom
241242
| # #{RSpec::Core::Metadata.relative_path(__FILE__)}:#{aggregate_line + 2}
@@ -283,13 +284,15 @@ def normalize_one_backtrace(exception)
283284
let(:exception) { RSpec::Core::MultipleExceptionError.new(sub_failure_1, sub_failure_2) }
284285

285286
it "lists each sub-failure, just like with MultipleExpectationsNotMetError" do
286-
expect(fully_formatted.lines.to_a.last(8)).to eq(dedent(<<-EOS).lines.to_a)
287+
expect(fully_formatted.lines.to_a.last(10)).to eq(dedent(<<-EOS).lines.to_a)
287288
|
288289
| 1.1) Failure/Error: Unable to find matching line from backtrace
290+
|
289291
| StandardError:
290292
| foo
291293
|
292294
| 1.2) Failure/Error: Unable to find matching line from backtrace
295+
|
293296
| StandardError:
294297
| bar
295298
EOS

spec/support/formatter_support.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,21 @@ def expected_summary_output_for_example_specs
8080
|
8181
| 3) a failing spec with odd backtraces fails with a backtrace that has no file
8282
| Failure/Error: Unable to find (erb) to read failed line
83+
|
8384
| RuntimeError:
8485
| foo
8586
| # (erb):1
8687
|
8788
| 4) a failing spec with odd backtraces fails with a backtrace containing an erb file
8889
| Failure/Error: Unable to find /foo.html.erb to read failed line
90+
|
8991
| Exception:
9092
| Exception
9193
| # /foo.html.erb:1:in `<main>': foo (RuntimeError)
9294
|
9395
| 5) a failing spec with odd backtraces with a `nil` backtrace raises
9496
| Failure/Error: Unable to find matching line from backtrace
97+
|
9598
| RuntimeError:
9699
| boom
97100
|
@@ -150,6 +153,7 @@ def expected_summary_output_for_example_specs
150153
|
151154
| 3) a failing spec with odd backtraces fails with a backtrace that has no file
152155
| Failure/Error: ERB.new("<%= raise 'foo' %>").result
156+
|
153157
| RuntimeError:
154158
| foo
155159
| # (erb):1:in `<main>'
@@ -160,12 +164,14 @@ def expected_summary_output_for_example_specs
160164
|
161165
| 4) a failing spec with odd backtraces fails with a backtrace containing an erb file
162166
| Failure/Error: Unable to find /foo.html.erb to read failed line
167+
|
163168
| Exception:
164169
| Exception
165170
| # /foo.html.erb:1:in `<main>': foo (RuntimeError)
166171
|
167172
| 5) a failing spec with odd backtraces with a `nil` backtrace raises
168173
| Failure/Error: Unable to find matching line from backtrace
174+
|
169175
| RuntimeError:
170176
| boom
171177
|

0 commit comments

Comments
 (0)