Skip to content

Commit

Permalink
introduces Differ#all_hashes to check actual and expected are Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlHeitmann committed Jun 9, 2024
1 parent 048ac0a commit c3e9ea9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
26 changes: 10 additions & 16 deletions lib/rspec/support/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ def diff(actual, expected)
if any_multiline_strings?(actual, expected)
diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
end
elsif no_procs_and_no_numbers?(actual, expected)
if (RUBY_VERSION.to_f > 1.8) && hash_with_anything?(expected)
diff = diff_as_object_with_anything(actual, expected)
else
diff = diff_as_object(actual, 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
end

Expand Down Expand Up @@ -60,7 +58,7 @@ def diff_as_string(actual, expected)
end
# rubocop:enable Metrics/MethodLength

def diff_as_object_with_anything(actual, expected)
def diff_hashes_as_object(actual, expected)
expected.select { |_, v| RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === v }.each_key do |k|
expected[k] = actual[k]
end
Expand All @@ -84,16 +82,12 @@ def initialize(opts={})

private

def hash_with_anything?(arg)
Hash === arg && safely_flatten(arg).any? { |a| RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === a }
end

def no_procs_and_no_numbers?(*args)
no_procs?(args) && no_numbers?(args)
def no_procs?(*args)
safely_flatten(args).none? { |a| Proc === a }
end

def no_procs?(args)
safely_flatten(args).none? { |a| Proc === a }
def all_hashes?(actual, expected)
defined?(RSpec::Mocks::ArgumentMatchers::AnyArgMatcher) && (Hash === actual) && (Hash === expected)
end

def all_strings?(*args)
Expand All @@ -104,7 +98,7 @@ def any_multiline_strings?(*args)
all_strings?(*args) && safely_flatten(args).any? { |a| multiline?(a) }
end

def no_numbers?(args)
def no_numbers?(*args)
safely_flatten(args).none? { |a| Numeric === a }
end

Expand Down
33 changes: 15 additions & 18 deletions spec/rspec/support/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,24 +556,21 @@ def inspect; "<BrokenObject>"; end
end
end

unless RUBY_VERSION == '1.8.7'
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 => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8",
| :fixed => "fixed",
|-:trigger => "wrong",
|+:trigger => "trigger",
|
EOD
# puts diff
expect(diff).to be_diffed_as(expected_diff)
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 => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8",
| :fixed => "fixed",
|-:trigger => "wrong",
|+:trigger => "trigger",
|
EOD
expect(diff).to be_diffed_as(expected_diff)
end
end
end
Expand Down

0 comments on commit c3e9ea9

Please sign in to comment.