Skip to content

Commit aaa9b20

Browse files
wata727mcmire
authored andcommitted
Add support for hash_including argument matcher
1 parent 3aac008 commit aaa9b20

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

lib/super_diff/rspec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ def self.a_hash_including_something?(value)
2626
value.expecteds.first.is_a?(::Hash)
2727
end
2828

29+
# HINT: `a_hash_including` is an alias of `include` in the rspec-expectations gem.
30+
# `hash_including` is an argument matcher in the rspec-mocks gem.
31+
def self.hash_including_something?(value)
32+
value.is_a?(::RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher)
33+
end
34+
2935
def self.a_collection_including_something?(value)
3036
fuzzy_object?(value) &&
3137
value.respond_to?(:expecteds) &&

lib/super_diff/rspec/differs/hash_including.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module RSpec
33
module Differs
44
class HashIncluding < SuperDiff::Differs::Hash
55
def self.applies_to?(expected, actual)
6-
SuperDiff::RSpec.a_hash_including_something?(expected) &&
7-
actual.is_a?(::Hash)
6+
(
7+
SuperDiff::RSpec.a_hash_including_something?(expected) ||
8+
SuperDiff::RSpec.hash_including_something?(expected)
9+
) && actual.is_a?(::Hash)
810
end
911

1012
private

lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ObjectInspection
44
module Inspectors
55
class HashIncluding < SuperDiff::ObjectInspection::Inspectors::Base
66
def self.applies_to?(value)
7-
SuperDiff::RSpec.a_hash_including_something?(value)
7+
SuperDiff::RSpec.a_hash_including_something?(value) || SuperDiff::RSpec.hash_including_something?(value)
88
end
99

1010
protected
@@ -13,9 +13,14 @@ def inspection_tree
1313
SuperDiff::ObjectInspection::InspectionTree.new do
1414
add_text "#<a hash including ("
1515

16-
nested do |aliased_matcher|
16+
nested do |value|
17+
hash = if SuperDiff::RSpec.a_hash_including_something?(value)
18+
value.expecteds.first
19+
else
20+
value.instance_variable_get(:@expected)
21+
end
1722
insert_hash_inspection_of(
18-
aliased_matcher.expecteds.first,
23+
hash,
1924
initial_break: nil,
2025
)
2126
end

lib/super_diff/rspec/operation_tree_builders/hash_including.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ module RSpec
33
module OperationTreeBuilders
44
class HashIncluding < SuperDiff::OperationTreeBuilders::Hash
55
def self.applies_to?(expected, actual)
6-
SuperDiff::RSpec.a_hash_including_something?(expected) &&
7-
actual.is_a?(::Hash)
6+
(
7+
SuperDiff::RSpec.a_hash_including_something?(expected) ||
8+
SuperDiff::RSpec.hash_including_something?(expected)
9+
) && actual.is_a?(::Hash)
810
end
911

1012
def initialize(expected:, **rest)
11-
super(expected: expected.expecteds.first, **rest)
13+
hash = if SuperDiff::RSpec.a_hash_including_something?(expected)
14+
expected.expecteds.first
15+
else
16+
expected.instance_variable_get(:@expected)
17+
end
18+
super(expected: hash, **rest)
1219
end
1320

1421
private

spec/integration/rspec/match_matcher_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,48 @@
341341
end
342342
end
343343

344+
# HINT: `a_hash_including` is an alias of `include` in the rspec-expectations gem.
345+
# `hash_including` is an argument matcher in the rspec-mocks gem.
346+
context "when the expected value is `hash-including-<something>`, not `a-hash-including-<something>`" do
347+
it "produces the correct failure message" do
348+
as_both_colored_and_uncolored do |color_enabled|
349+
snippet = <<~TEST.strip
350+
expected = hash_including(city: "Hill Valley")
351+
actual = { city: "Burbank" }
352+
expect(actual).to match(expected)
353+
TEST
354+
program = make_plain_test_program(
355+
snippet,
356+
color_enabled: color_enabled,
357+
)
358+
359+
expected_output = build_expected_output(
360+
color_enabled: color_enabled,
361+
snippet: %|expect(actual).to match(expected)|,
362+
expectation: proc {
363+
line do
364+
plain %|Expected |
365+
actual %|{ city: "Burbank" }|
366+
plain %| to match |
367+
expected %|#<a hash including (city: "Hill Valley")>|
368+
plain %|.|
369+
end
370+
},
371+
diff: proc {
372+
plain_line %| {|
373+
expected_line %|- city: "Hill Valley"|
374+
actual_line %|+ city: "Burbank"|
375+
plain_line %| }|
376+
},
377+
)
378+
379+
expect(program).
380+
to produce_output_when_run(expected_output).
381+
in_color(color_enabled)
382+
end
383+
end
384+
end
385+
344386
context "when the expected value is a collection-including-<something>" do
345387
context "that is small" do
346388
it "produces the correct failure message when used in the positive" do

0 commit comments

Comments
 (0)