Created
July 4, 2012 11:05
-
-
Save chancancode/3046772 to your computer and use it in GitHub Desktop.
JSON Matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyTestCase < MiniTest::Unit::TestCase | |
# ... | |
WILDCARD_MATCHER = Object.new | |
def WILDCARD_MATCHER.==(other) | |
true | |
end | |
def WILDCARD_MATCHER.is_a?(klass) | |
false | |
end | |
def WILDCARD_MATCHER.inspect | |
'WILDCARD_MATCHER' | |
end | |
def assert_json_matches(matcher, json) | |
assert match_json(matcher, json), "Expected\n#{matcher.pretty_inspect}\nto match\n#{json.pretty_inspect}" | |
end | |
def match_json(matcher, json) | |
if matcher.is_a? Array | |
return false unless json.is_a? Array | |
return false unless match_array matcher, json | |
elsif matcher.is_a? Hash | |
return false unless json.is_a? Hash | |
return false unless match_hash matcher, json | |
elsif matcher.is_a? Regexp | |
return false unless matcher =~ json | |
else | |
return false unless matcher == json | |
end | |
true | |
end | |
def match_array(matcher, array) | |
return false unless matcher.length == array.length | |
matcher = matcher.clone | |
array = array.clone | |
begin | |
# This might fail | |
matcher.sort! | |
array.sort! | |
matched = [] | |
matcher.zip(array).each_with_index do |(v1, v2), i| | |
matched << i if match_json v1, v2 | |
end | |
matcher.delete_if.with_index { |_, i| matched.include? i } | |
array.delete_if.with_index { |_, i| matched.include? i } | |
rescue Exception | |
end | |
# O(N^2) matching for remaining items | |
matcher.each do |v1| | |
matched = false | |
array.each_with_index do |v2, i| | |
if match_json v1, v2 | |
matched = true | |
array.delete_at i | |
break | |
end | |
end | |
return false unless matched | |
end | |
true | |
end | |
def match_hash(matcher, hash) | |
return false unless matcher.length == hash.length | |
return false unless matcher.keys.sort == hash.keys.sort | |
matcher.keys.each do |k| | |
return false unless match_json(matcher[k], hash[k]) | |
end | |
true | |
end | |
end |
Author
chancancode
commented
Jul 4, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment