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

Commit c3e9ea9

Browse files
committed
introduces Differ#all_hashes to check actual and expected are Hash
1 parent 048ac0a commit c3e9ea9

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

lib/rspec/support/differ.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ def diff(actual, expected)
1818
if any_multiline_strings?(actual, expected)
1919
diff = diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
2020
end
21-
elsif no_procs_and_no_numbers?(actual, expected)
22-
if (RUBY_VERSION.to_f > 1.8) && hash_with_anything?(expected)
23-
diff = diff_as_object_with_anything(actual, expected)
24-
else
25-
diff = diff_as_object(actual, expected)
26-
end
21+
elsif all_hashes?(actual, expected)
22+
diff = diff_hashes_as_object(actual, expected)
23+
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
24+
diff = diff_as_object(actual, expected)
2725
end
2826
end
2927

@@ -60,7 +58,7 @@ def diff_as_string(actual, expected)
6058
end
6159
# rubocop:enable Metrics/MethodLength
6260

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

8583
private
8684

87-
def hash_with_anything?(arg)
88-
Hash === arg && safely_flatten(arg).any? { |a| RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === a }
89-
end
90-
91-
def no_procs_and_no_numbers?(*args)
92-
no_procs?(args) && no_numbers?(args)
85+
def no_procs?(*args)
86+
safely_flatten(args).none? { |a| Proc === a }
9387
end
9488

95-
def no_procs?(args)
96-
safely_flatten(args).none? { |a| Proc === a }
89+
def all_hashes?(actual, expected)
90+
defined?(RSpec::Mocks::ArgumentMatchers::AnyArgMatcher) && (Hash === actual) && (Hash === expected)
9791
end
9892

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

107-
def no_numbers?(args)
101+
def no_numbers?(*args)
108102
safely_flatten(args).none? { |a| Numeric === a }
109103
end
110104

spec/rspec/support/differ_spec.rb

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -556,24 +556,21 @@ def inspect; "<BrokenObject>"; end
556556
end
557557
end
558558

559-
unless RUBY_VERSION == '1.8.7'
560-
describe "fuzzy matcher anything" do
561-
it "outputs only key value pair that triggered diff, anything_key should absorb actual value" do
562-
actual = { :fixed => "fixed", :trigger => "trigger", :anything_key => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8" }
563-
expected = { :fixed => "fixed", :trigger => "wrong", :anything_key => anything }
564-
diff = differ.diff(actual, expected)
565-
expected_diff = dedent(<<-'EOD')
566-
|
567-
|@@ -1,4 +1,4 @@
568-
| :anything_key => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8",
569-
| :fixed => "fixed",
570-
|-:trigger => "wrong",
571-
|+:trigger => "trigger",
572-
|
573-
EOD
574-
# puts diff
575-
expect(diff).to be_diffed_as(expected_diff)
576-
end
559+
describe "fuzzy matcher anything" do
560+
it "outputs only key value pair that triggered diff, anything_key should absorb actual value" do
561+
actual = { :fixed => "fixed", :trigger => "trigger", :anything_key => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8" }
562+
expected = { :fixed => "fixed", :trigger => "wrong", :anything_key => anything }
563+
diff = differ.diff(actual, expected)
564+
expected_diff = dedent(<<-'EOD')
565+
|
566+
|@@ -1,4 +1,4 @@
567+
| :anything_key => "bcdd0399-1cfe-4de1-a481-ca6b17d41ed8",
568+
| :fixed => "fixed",
569+
|-:trigger => "wrong",
570+
|+:trigger => "trigger",
571+
|
572+
EOD
573+
expect(diff).to be_diffed_as(expected_diff)
577574
end
578575
end
579576
end

0 commit comments

Comments
 (0)