-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow matching out-of-order arrays with .in_any_order #83
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module JsonSpec | ||
module Indifference | ||
extend self | ||
|
||
def toggle_indifference( toggle ) | ||
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. Please omit spaces around arguments in the method signature. |
||
@indifferent = !!toggle | ||
end | ||
|
||
def indifferize(ruby) | ||
return ruby unless @indifferent | ||
|
||
case ruby | ||
when Hash | ||
ruby.sort.inject({}) do |hash, (key, value)| | ||
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. Were hashes ever failing due to order dependence issues? If not, we should not bother sorting the incoming hash. 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. No; good catch. I'll remove that. I'll write a test spec for that case just to be sure this module can't break hash order (in)dependence in the future. |
||
hash[key] = indifferize(value) | ||
hash | ||
end | ||
when Array | ||
ruby.map{|v| indifferize(v) }.sort_by{|v| generate_normalized_json(v) } | ||
else ruby | ||
end | ||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ module Matchers | |
class BeJsonEql | ||
include JsonSpec::Helpers | ||
include JsonSpec::Exclusion | ||
include JsonSpec::Indifference | ||
include JsonSpec::Messages | ||
|
||
attr_reader :expected, :actual | ||
|
@@ -42,6 +43,11 @@ def including(*keys) | |
self | ||
end | ||
|
||
def order_indifferent(state = true) | ||
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. Thoughts about a more human-friendly name for this like expect(foo).to be_json_eql(bar).in_any_order Just spitballing! 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 like that a lot better, actually. I'll include that along with the other changes. |
||
toggle_indifference(state) | ||
self | ||
end | ||
|
||
def failure_message | ||
message_with_path("Expected equivalent JSON") | ||
end | ||
|
@@ -58,7 +64,7 @@ def description | |
|
||
private | ||
def scrub(json, path = nil) | ||
generate_normalized_json(exclude_keys(parse_json(json, path))).chomp + "\n" | ||
generate_normalized_json(indifferize(exclude_keys(parse_json(json, path)))).chomp + "\n" | ||
end | ||
end | ||
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.
I'd like to see this called
OrderIndifference
to be clearer about its function.