Skip to content

Commit df5e718

Browse files
wata727mcmire
authored andcommitted
Add support for array_including argument matcher
1 parent aaa9b20 commit df5e718

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

lib/super_diff/rspec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def self.a_collection_including_something?(value)
3838
!(value.expecteds.one? && value.expecteds.first.is_a?(::Hash))
3939
end
4040

41+
def self.array_including_something?(value)
42+
value.is_a?(::RSpec::Mocks::ArgumentMatchers::ArrayIncludingMatcher)
43+
end
44+
4145
def self.an_object_having_some_attributes?(value)
4246
fuzzy_object?(value) &&
4347
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::HaveAttributes)

lib/super_diff/rspec/differs/collection_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 CollectionIncluding < SuperDiff::Differs::Array
55
def self.applies_to?(expected, actual)
6-
SuperDiff::RSpec.a_collection_including_something?(expected) &&
7-
actual.is_a?(::Array)
6+
(
7+
SuperDiff::RSpec.a_collection_including_something?(expected) ||
8+
SuperDiff::RSpec.array_including_something?(expected)
9+
) && actual.is_a?(::Array)
810
end
911

1012
private

lib/super_diff/rspec/object_inspection/inspectors/collection_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 CollectionIncluding < SuperDiff::ObjectInspection::Inspectors::Base
66
def self.applies_to?(value)
7-
SuperDiff::RSpec.a_collection_including_something?(value)
7+
SuperDiff::RSpec.a_collection_including_something?(value) || SuperDiff::RSpec.array_including_something?(value)
88
end
99

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

16-
nested do |aliased_matcher|
17-
insert_array_inspection_of(aliased_matcher.expecteds)
16+
nested do |value|
17+
array = if SuperDiff::RSpec.a_collection_including_something?(value)
18+
value.expecteds
19+
else
20+
value.instance_variable_get(:@expected)
21+
end
22+
insert_array_inspection_of(array)
1823
end
1924

2025
add_break

lib/super_diff/rspec/operation_tree_builders/collection_including.rb

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

1012
def initialize(expected:, actual:, **rest)
@@ -16,7 +18,12 @@ def initialize(expected:, actual:, **rest)
1618
private
1719

1820
def actual_with_extra_items_in_expected_at_end
19-
actual + (expected.expecteds - actual)
21+
value = if SuperDiff::RSpec.a_collection_including_something?(expected)
22+
expected.expecteds
23+
else
24+
expected.instance_variable_get(:@expected)
25+
end
26+
actual + (value - actual)
2027
end
2128
end
2229
end

spec/integration/rspec/match_matcher_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,47 @@
689689
end
690690
end
691691

692+
context "when the expected value is an array-including-<something>" do
693+
it "produces the correct failure message" do
694+
as_both_colored_and_uncolored do |color_enabled|
695+
snippet = <<~TEST.strip
696+
expected = array_including("a")
697+
actual = ["b"]
698+
expect(actual).to match(expected)
699+
TEST
700+
program = make_plain_test_program(
701+
snippet,
702+
color_enabled: color_enabled,
703+
)
704+
705+
expected_output = build_expected_output(
706+
color_enabled: color_enabled,
707+
snippet: %|expect(actual).to match(expected)|,
708+
expectation: proc {
709+
line do
710+
plain %|Expected |
711+
actual %|["b"]|
712+
plain %| to match |
713+
expected %|#<a collection including ("a")>|
714+
plain %|.|
715+
end
716+
},
717+
diff: proc {
718+
plain_line %| [|
719+
plain_line %| "b"|
720+
# expected_line %|- "a",| # FIXME
721+
expected_line %|- "a"|
722+
plain_line %| ]|
723+
},
724+
)
725+
726+
expect(program).
727+
to produce_output_when_run(expected_output).
728+
in_color(color_enabled)
729+
end
730+
end
731+
end
732+
692733
context "when the expected value is an object-having-attributes" do
693734
context "that is small" do
694735
it "produces the correct failure message when used in the positive" do

0 commit comments

Comments
 (0)