Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #599 from KarlHeitmann/fix_diff_fuzzy_matcher_anyt…
Browse files Browse the repository at this point in the history
…hing_v2

Fix diff output when a fuzzy finder anything is inside an expected hash
  • Loading branch information
JonRowe authored Jun 30, 2024
2 parents 5334c64 + 6b7ff69 commit d6bdd9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/rspec/support/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def diff(actual, expected)
if any_multiline_strings?(actual, expected)
diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
end
elsif all_hashes?(actual, expected)
diff = diff_hashes_as_object(actual, expected)
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
diff = diff_as_object(actual, expected)
end
Expand Down Expand Up @@ -56,6 +58,25 @@ def diff_as_string(actual, expected)
end
# rubocop:enable Metrics/MethodLength

if defined?(RSpec::Mocks::ArgumentMatchers::AnyArgMatcher)
def diff_hashes_as_object(actual, expected)
actual_to_diff =
actual.keys.reduce({}) do |hash, key|
if RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === expected[key]
hash[key] = expected[key]
else
hash[key] = actual[key]
end
hash
end
diff_as_object(actual_to_diff, expected)
end
else
def diff_hashes_as_object(actual, expected)
diff_as_object(actual, expected)
end
end

def diff_as_object(actual, expected)
actual_as_string = object_to_string(actual)
expected_as_string = object_to_string(expected)
Expand All @@ -77,6 +98,10 @@ def no_procs?(*args)
safely_flatten(args).none? { |a| Proc === a }
end

def all_hashes?(actual, expected)
(Hash === actual) && (Hash === expected)
end

def all_strings?(*args)
safely_flatten(args).all? { |a| String === a }
end
Expand Down
27 changes: 26 additions & 1 deletion spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module RSpec
module Support
RSpec.describe Differ do
RSpec.describe "Differ" do
include Spec::DiffHelpers

describe '#diff' do
Expand Down Expand Up @@ -555,6 +555,31 @@ def inspect; "<BrokenObject>"; end
expect(differ.diff(false, true)).to_not be_empty
end
end

describe "fuzzy matcher anything" do
it "outputs only key value pair that triggered diff, anything_key should absorb actual value" do
actual = { :fixed => "fixed", :trigger => "trigger", :anything_key => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8" }
expected = { :fixed => "fixed", :trigger => "wrong", :anything_key => anything }
diff = differ.diff(actual, expected)
expected_diff = dedent(<<-'EOD')
|
|@@ -1,4 +1,4 @@
| :anything_key => anything,
| :fixed => "fixed",
|-:trigger => "wrong",
|+:trigger => "trigger",
|
EOD
expect(diff).to be_diffed_as(expected_diff)
end

it "checks the 'expected' var continues having the 'anything' fuzzy matcher, it has not mutated" do
actual = { :fixed => "fixed", :trigger => "trigger", :anything_key => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8" }
expected = { :fixed => "fixed", :trigger => "wrong", :anything_key => anything }
differ.diff(actual, expected)
expect(expected).to eq({ :fixed => "fixed", :trigger => "wrong", :anything_key => anything })
end
end
end
end
end
Expand Down

0 comments on commit d6bdd9f

Please sign in to comment.