Skip to content
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 include_json to match against subset hash at current position #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/json_spec/matchers/include_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ def matches?(actual_json)
actual = parse_json(actual_json, @path)
expected = exclude_keys(parse_json(@expected_json))
case actual
when Hash then actual.values.map { |v| exclude_keys(v) }.include?(expected)
when Hash then
if expected.instance_of?(Hash)
return true if expected.keys.all?{|key| actual[key] == expected[key]}
end
actual.values.map { |v| exclude_keys(v) }.include?(expected)
when Array then actual.map { |e| exclude_keys(e) }.include?(expected)
when String then actual.include?(expected)
else false
Expand Down
6 changes: 6 additions & 0 deletions spec/json_spec/matchers/include_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@
JsonSpec.directory = files_path
%({"one":{"value":"from_file"},"four":{"five":6}}).should include_json.from_file("one.json")
end

it "matches subset hash included in current position" do
json = %({"one": 1, "two": 2, "three": {"foo": 1, "bar": 2, "baz": 3}})
json.should include_json(%({"one": 1, "two": 2}))
json.should include_json(%({"foo": 1, "baz": 3})).at_path("three")
end
end