Skip to content

Commit d144751

Browse files
mcmirewata727
andauthored
Fix match_array to truly accept non-array (#213)
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 <[email protected]>
1 parent 9e46160 commit d144751

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,14 @@ def self.prepended(base)
803803
end
804804

805805
def match_array(items)
806-
BuiltIn::MatchArray.new(items.is_a?(String) ? [items] : items)
806+
# This is a bit strange, but this is fundamentally different from
807+
# splatting `items` in the argument list. It's functionally equivalent
808+
# to, though not quite the same as:
809+
#
810+
# items.is_a?(Array) ? items : [items]
811+
#
812+
items = *items
813+
BuiltIn::MatchArray.new(items)
807814
end
808815
end
809816
end

spec/integration/rspec/match_array_matcher_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@
379379
end
380380
end
381381

382-
context "when the input value is a string" do
383-
it "produces the correct failure message when used in the positive" do
382+
context "when the input value is not an array, and especially not a value that could be turned into one" do
383+
fit "produces the correct failure message, as though an array had been given" do
384384
as_both_colored_and_uncolored do |color_enabled|
385385
snippet = <<~TEST.strip
386-
actual = ["Marty", "Jennifer", "Doc"]
387-
expected = "Einie"
386+
actual = [:marty, :jennifer, :doc]
387+
expected = :einie
388388
expect(actual).to match_array(expected)
389389
TEST
390390
program = make_plain_test_program(snippet, color_enabled: color_enabled)
@@ -397,20 +397,20 @@
397397
proc do
398398
line do
399399
plain "Expected "
400-
actual %|["Marty", "Jennifer", "Doc"]|
400+
actual "[:marty, :jennifer, :doc]"
401401
plain " to match array with "
402-
expected %|"Einie"|
402+
expected ":einie"
403403
plain "."
404404
end
405405
end,
406406
diff:
407407
proc do
408408
plain_line " ["
409-
actual_line %|+ "Marty",|
410-
actual_line %|+ "Jennifer",|
411-
actual_line %|+ "Doc",|
412-
# expected_line %|- "Einie"| # TODO
413-
expected_line %|- "Einie",|
409+
actual_line "+ :marty,"
410+
actual_line "+ :jennifer,"
411+
actual_line "+ :doc,"
412+
# expected_line %|- :einie| # TODO
413+
expected_line "- :einie,"
414414
plain_line " ]"
415415
end
416416
)

0 commit comments

Comments
 (0)