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

Commit

Permalink
if AnyArgMatcher is defined, diff_hashes_as_object folds anything
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlHeitmann committed Jun 9, 2024
1 parent c3e9ea9 commit 416cd49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/rspec/support/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,25 @@ def diff_as_string(actual, expected)
# rubocop:enable Metrics/MethodLength

def diff_hashes_as_object(actual, expected)
expected.select { |_, v| RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === v }.each_key do |k|
expected[k] = actual[k]
if defined?(RSpec::Mocks::ArgumentMatchers::AnyArgMatcher)
anything_hash = expected.select { |_, v| RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === v }

anything_hash.each_key do |k|
expected[k] = actual[k]
end

diff_string = diff_as_object(actual, expected)

if defined?(RSpec::Mocks::ArgumentMatchers::AnyArgMatcher)
anything_hash.each do |k, v|
expected[k] = v
end
end

diff_string
else
diff_as_object(actual, expected)
end
diff_as_object(actual, expected)
end

def diff_as_object(actual, expected)
Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ def inspect; "<BrokenObject>"; end
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
Expand Down

0 comments on commit 416cd49

Please sign in to comment.