From 416cd49be56c5394ab49ff712cf8bea78b4e7abd Mon Sep 17 00:00:00 2001 From: Karl Heitmann Date: Sat, 8 Jun 2024 21:59:44 -0400 Subject: [PATCH] if AnyArgMatcher is defined, diff_hashes_as_object folds anything --- lib/rspec/support/differ.rb | 21 ++++++++++++++++++--- spec/rspec/support/differ_spec.rb | 6 ++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/rspec/support/differ.rb b/lib/rspec/support/differ.rb index 2ef5d437..fba79929 100644 --- a/lib/rspec/support/differ.rb +++ b/lib/rspec/support/differ.rb @@ -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) diff --git a/spec/rspec/support/differ_spec.rb b/spec/rspec/support/differ_spec.rb index 04f1422e..38e0a61a 100644 --- a/spec/rspec/support/differ_spec.rb +++ b/spec/rspec/support/differ_spec.rb @@ -572,6 +572,12 @@ def inspect; ""; 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