Skip to content

Commit a85ecac

Browse files
authored
1 parent 7321553 commit a85ecac

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- Fix ActiveRecord's `attributes_for_super_diff` and tree builders related to Active Records to handle models that do not have a primary key.
1818
[#282](https://github.com/splitwise/super_diff/pull/282) by [@atranson-electra](https://github.com/atranson-electra)
19+
- Fix failure case for chained matchers. [#288](https://github.com/splitwise/super_diff/pull/288)
1920

2021
### Other changes
2122

docs/contributors/architecture/how-super-diff-works.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ or when a matcher whose `#does_not_match?` method returns `true`
3939
is passed to `expect(...).not_to`
4040
RSpec will call the `RSpec::Expectations::ExpectationHelper#handle_failure` method,
4141
which will call `RSpec::Expectations.fail_with`.
42-
This method will use `RSpec::Matchers::ExpectedsForMultipleDiffs`
42+
This method will use `RSpec::Matchers::MultiMatcherDiff`
43+
(or in RSpec < 3.13.0, `RSpec::Matchers::ExpectedsForMultipleDiffs`)
4344
and the differ object that `RSpec::Expectations.differ` returns
4445
to generate a diff,
4546
combining it with the failure message from the matcher,
@@ -89,7 +90,7 @@ Here are all of the places that SuperDiff patches RSpec:
8990
as it interferes with the previous patches)
9091
- `RSpec::Support::ObjectFormatter`
9192
(to use SuperDiff's object inspection logic)
92-
- `RSpec::Matchers::ExpectedsForMultipleDiffs`
93+
- `RSpec::Matchers::ExpectedsForMultipleDiffs` and `RSpec::Matchers::MultiMatcherDiff`
9394
(to add a key above the diff,
9495
add spacing around the diff,
9596
and colorize the word "Diff:")

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,26 @@ def from(expected)
352352
new([[expected, text]])
353353
end
354354

355+
def for_many_matchers(matchers)
356+
# Look for expected_for_diff and actual_for_diff if possible
357+
diff_tuples = matchers.map do |m|
358+
expected = if m.respond_to?(:expected_for_diff)
359+
m.expected_for_diff
360+
else
361+
m.expected
362+
end
363+
364+
actual = if m.respond_to?(:actual_for_diff)
365+
m.actual_for_diff
366+
else
367+
m.actual
368+
end
369+
[expected, diff_label_for(m), actual]
370+
end
371+
372+
new(diff_tuples)
373+
end
374+
355375
def colorizer
356376
RSpec::Core::Formatters::ConsoleCodes
357377
end
@@ -423,6 +443,26 @@ def from(expected, actual)
423443
new([[expected, text, actual]])
424444
end
425445

446+
def for_many_matchers(matchers)
447+
# Look for expected_for_diff and actual_for_diff if possible
448+
diff_tuples = matchers.map do |m|
449+
expected = if m.respond_to?(:expected_for_diff)
450+
m.expected_for_diff
451+
else
452+
m.expected
453+
end
454+
455+
actual = if m.respond_to?(:actual_for_diff)
456+
m.actual_for_diff
457+
else
458+
m.actual
459+
end
460+
[expected, diff_label_for(m), actual]
461+
end
462+
463+
new(diff_tuples)
464+
end
465+
426466
def colorizer
427467
RSpec::Core::Formatters::ConsoleCodes
428468
end

spec/integration/rspec/raise_error_matcher_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,4 +1487,39 @@
14871487
end
14881488
end
14891489
end
1490+
1491+
context 'when part of an expectation chain' do
1492+
context 'when the expected error and/or actual message is short' do
1493+
it 'produces the correct failure message' do
1494+
as_both_colored_and_uncolored do |color_enabled|
1495+
snippet = <<~TEST.strip
1496+
expect { raise StandardError.new('boo') }.to raise_error(RuntimeError, 'bar').and(change(Random, :rand))
1497+
TEST
1498+
program =
1499+
make_plain_test_program(snippet, color_enabled: color_enabled)
1500+
1501+
expected_output =
1502+
build_expected_output(
1503+
color_enabled: color_enabled,
1504+
snippet:
1505+
"expect { raise StandardError.new('boo') }.to raise_error(RuntimeError, 'bar').and(change(Random, :rand))",
1506+
expectation:
1507+
proc do
1508+
line do
1509+
plain 'Expected raised exception '
1510+
actual %(#<StandardError "boo">)
1511+
plain ' to match '
1512+
expected 'a kind of RuntimeError with message "bar"'
1513+
plain '.'
1514+
end
1515+
end
1516+
)
1517+
1518+
expect(program).to produce_output_when_run(
1519+
expected_output
1520+
).in_color(color_enabled)
1521+
end
1522+
end
1523+
end
1524+
end
14901525
end

0 commit comments

Comments
 (0)