-
-
Notifications
You must be signed in to change notification settings - Fork 103
Fix diff output when a fuzzy finder anything is inside an expected hash #599
Changes from 3 commits
b993756
4c22e70
048ac0a
c3e9ea9
416cd49
0dea769
31f086e
dd7a4c7
938096c
7046659
6b7ff69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,12 @@ 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?(actual, expected) && no_numbers?(actual, expected) | ||
diff = diff_as_object(actual, expected) | ||
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 | ||
end | ||
end | ||
|
||
|
@@ -56,6 +60,13 @@ def diff_as_string(actual, expected) | |
end | ||
# rubocop:enable Metrics/MethodLength | ||
|
||
def diff_as_object_with_anything(actual, expected) | ||
expected.select { |_, v| RSpec::Mocks::ArgumentMatchers::AnyArgMatcher === v }.each_key do |k| | ||
expected[k] = actual[k] | ||
end | ||
diff_as_object(actual, expected) | ||
end | ||
|
||
def diff_as_object(actual, expected) | ||
actual_as_string = object_to_string(actual) | ||
expected_as_string = object_to_string(expected) | ||
|
@@ -73,7 +84,15 @@ def initialize(opts={}) | |
|
||
private | ||
|
||
def no_procs?(*args) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m also struggling to understand how this works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed... that's weird... What if I remove the star operator from The purpose of this |
||
no_procs?(args) && no_numbers?(args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. args here will be an array of two elements? Can we check no_procs?(expected) && no_procs?(actual) && no_numbers? …. Each if those methods runs the (potentially expensive?) safe_flatten. I’d prefer this to be done just once (or, better - never!) Can’t we just take top-level keys of each of those arrays without flattening recursively? This would work on 1.8.7 |
||
end | ||
|
||
def no_procs?(args) | ||
safely_flatten(args).none? { |a| Proc === a } | ||
end | ||
|
||
|
@@ -85,7 +104,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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this is due to this. We call safely_flatten with a hash arg, and it calls flatten on it.
This will break 1.8.7. We can’t afford this. Even though I’m a proponent of soft-deprecating older Rubies in our code, this doesn’t include breaking hypothetical suites that might still exist.
Can the same be achieved somehow differently? Like taking
.values
from the hash?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please disregard my note regarding breaking 1.8.7, I’ve just noticed that you have a check for this.
Still, does it make sense to flatten? We could just check the too level of the hash, right?