Skip to content

Commit

Permalink
Fix match_array to truly accept non-array
Browse files Browse the repository at this point in the history
Previously, it was noticed that `match_array` raised an error when given
a single, non-array argument as opposed to an array (which is the
documented usage). An attempt was made to fix this; however, the
conditional added to `match_array` only checked that the single argument
was a string. This commit matches the original implementation of
`match_array` by accepting any type of non-array, not just a string.

Co-authored-by: wata_mac <watassbass@gmail.com>
  • Loading branch information
mcmire and wata727 committed Feb 4, 2024
1 parent 9e46160 commit 9465e9f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
9 changes: 8 additions & 1 deletion lib/super_diff/rspec/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,14 @@ def self.prepended(base)
end

def match_array(items)
BuiltIn::MatchArray.new(items.is_a?(String) ? [items] : items)
# This is a bit strange, but this is fundamentally different from
# splatting `items` in the argument list. It's functionally equivalent
# to, though not quite the same as:
#
# items.is_a?(Array) ? items : [items]
#
items = *items
BuiltIn::MatchArray.new(items)
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions spec/integration/rspec/match_array_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@
end
end

context "when the input value is a string" do
it "produces the correct failure message when used in the positive" do
context "when the input value is not an array, and especially not a value that could be turned into one" do
fit "produces the correct failure message, as though an array had been given" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~TEST.strip
actual = ["Marty", "Jennifer", "Doc"]
expected = "Einie"
actual = [:marty, :jennifer, :doc]
expected = :einie
expect(actual).to match_array(expected)
TEST
program = make_plain_test_program(snippet, color_enabled: color_enabled)
Expand All @@ -397,20 +397,20 @@
proc do
line do
plain "Expected "
actual %|["Marty", "Jennifer", "Doc"]|
actual "[:marty, :jennifer, :doc]"
plain " to match array with "
expected %|"Einie"|
expected ":einie"
plain "."
end
end,
diff:
proc do
plain_line " ["
actual_line %|+ "Marty",|
actual_line %|+ "Jennifer",|
actual_line %|+ "Doc",|
# expected_line %|- "Einie"| # TODO
expected_line %|- "Einie",|
actual_line "+ :marty,"
actual_line "+ :jennifer,"
actual_line "+ :doc,"
# expected_line %|- :einie| # TODO
expected_line "- :einie,"
plain_line " ]"
end
)
Expand Down

0 comments on commit 9465e9f

Please sign in to comment.