Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make include_json a subset relation #62

Closed
wants to merge 1 commit into from

2 participants

@myitcv

Rewrite of #61 which included a wrong branch name and commit message

@charlierudolph

Since you are looking for specifically a subset test, how about a new matcher instead of adding this into the include_json matcher. Maybe have_json_subset which returns false if actual is not a hash, otherwise returns whether or not expected is a subset of actual. The matcher should also allow at_path like a few of the other matchers. What do you think?

@myitcv

Sounds good. I suspect that's probably the best way to go if you want to retain the include semantics.

Want me to go ahead and add the implementation to my pull request?

@myitcv

Actually, probably makes more sense to close this PR and create another one with correct naming (given that we are no longer going to modify include_json)

@charlierudolph

Yeah, probably cleaner as a fresh PR. Thank you.

@myitcv myitcv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 2, 2013
  1. @myitcv
This page is out of date. Refresh to see the latest.
View
4 lib/json_spec/matchers/include_json.rb
@@ -15,7 +15,9 @@ 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
+ expected.is_a?(Hash) && actual.select { |k,v| expected.include? k } == expected ||
+ 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
View
10 spec/json_spec/matchers/include_json_spec.rb
@@ -45,6 +45,16 @@
json.should include_json(%([5,6]))
end
+ it "is a subset relation" do
+ json = %({"one":1})
+ json.should include_json(%({"one":1}))
+ end
+
+ it "matches where expected is a proper subset of actual" do
+ json = %({"one":1, "two": 2})
+ json.should include_json(%({"one":1}))
+ end
+
it "matches a substring" do
json = %("json")
json.should include_json(%("js"))
Something went wrong with that request. Please try again.