-
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
New matcher that asserts presence of the given fields #35
Open
AlexanderPavlenko
wants to merge
7
commits into
collectiveidea:master
Choose a base branch
from
AlexanderPavlenko:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1bac970
HaveJsonFields matcher
5c667c7
accept symbols as field names
f0a1d90
rename variable
f5de25d
support cucumber have_json_keys matcher; improve tests; fix bug with …
bec8724
fix check logic
03866ad
support arbitrary json keys
6db3b90
keys feature
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Feature: Paths | ||
Background: | ||
Given the JSON is: | ||
""" | ||
{ | ||
"a": 1, | ||
"b": 2, | ||
"c": 3, | ||
"z" : { | ||
"d": 4, | ||
"e": 5 | ||
}, | ||
"strange keys: '\_(\")_/', FTW!": true | ||
} | ||
""" | ||
|
||
Scenario: Base paths | ||
When I get the JSON | ||
Then the JSON should have keys "a", "b", "c" | ||
And the JSON should not have keys "d", "e" | ||
And the JSON at "z" should have keys "d", "e" | ||
And the JSON at "z" should not have keys "m", "z" | ||
And the JSON should have keys "strange keys: '\_(\")_/', FTW!", "a", "b" | ||
|
||
Scenario: Table format | ||
When I get the JSON | ||
Then the JSON should have the following keys: | ||
| a | | ||
| b | | ||
| c | | ||
And the JSON should not have the following keys: | ||
| d | | ||
| e | | ||
And the JSON at "z" should have the following keys: | ||
| d | | ||
| e | | ||
And the JSON at "z" should not have the following keys: | ||
| m | | ||
| z | |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,16 @@ | |
end | ||
end | ||
|
||
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? have the following keys:$/ do |base, negative, table| | ||
match = have_json_keys(table.raw) | ||
match = match.at_path(base) if base | ||
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 rename
Nice drying up. Will make use of this pattern to dry up the rest of cucumber later. |
||
if negative | ||
last_json.should_not match | ||
else | ||
last_json.should match | ||
end | ||
end | ||
|
||
Then /^the (?:JSON|json)(?: response)? should( not)? have "(.*)"$/ do |negative, path| | ||
if negative | ||
last_json.should_not have_json_path(path) | ||
|
@@ -78,6 +88,17 @@ | |
end | ||
end | ||
|
||
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? have keys (.*)$/ do |base, negative, keys| | ||
keys = eval("[#{keys}]") | ||
match = have_json_keys(keys) | ||
match = match.at_path(base) if base | ||
if negative | ||
last_json.should_not match | ||
else | ||
last_json.should match | ||
end | ||
end | ||
|
||
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be an? (.*)$/ do |path, negative, type| | ||
if negative | ||
last_json.should_not have_json_type(type).at_path(path) | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module JsonSpec | ||
module Matchers | ||
class HaveJsonKeys | ||
include JsonSpec::Helpers | ||
include JsonSpec::Messages | ||
|
||
def initialize(keys) | ||
@keys = keys | ||
end | ||
|
||
def matches?(json) | ||
@data = parse_json(json, @path) | ||
return false unless @data.is_a?(Hash) | ||
@keys.all?{|key| @data.has_key?(key.to_s)} | ||
end | ||
|
||
def at_path(path) | ||
@path = path | ||
self | ||
end | ||
|
||
def failure_message_for_should | ||
message_with_path("Expected JSON to contain all of the following keys #{@keys.join(", ")}") | ||
end | ||
|
||
def failure_message_for_should_not | ||
message_with_path("Expected JSON to not contain any of the following keys #{@keys.join(", ")}") | ||
end | ||
|
||
def description | ||
message_with_path(%(have JSON keys "#{@keys.join(", ")}")) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "spec_helper" | ||
|
||
describe JsonSpec::Matchers::HaveJsonKeys do | ||
it "fails for non-hashes" do | ||
%([1,2,3]).should_not have_json_keys | ||
%(1).should_not have_json_keys | ||
%("test").should_not have_json_keys | ||
end | ||
|
||
it "fails for bigger set of keys" do | ||
%({"a": "1", "b": "2"}).should_not have_json_keys(:a, :b, :c) | ||
end | ||
|
||
it "matches for equal set of keys" do | ||
[:a, :b, :c].permutation.each do |keys| | ||
%({"a": "1", "b": "2", "c": "3"}).should have_json_keys(*keys) | ||
end | ||
end | ||
|
||
it "matches for smaller set of keys" do | ||
%({"a": "1", "b": "2", "c": "3"}).should have_json_keys(:a, :b) | ||
end | ||
|
||
it "matches for empty set of keys" do | ||
%({"a": "1", "b": "2", "c": "3"}).should have_json_keys | ||
end | ||
|
||
it "accepts array of symbol keys" do | ||
%({"a": "1", "b": "2"}).should have_json_keys([:a, :b]) | ||
end | ||
|
||
it "accepts array of string keys" do | ||
%({"a": "1", "b": "2"}).should have_json_keys(['a', 'b']) | ||
end | ||
|
||
it "accepts symbol keys" do | ||
%({"a": "1", "b": "2"}).should have_json_keys(:a, :b) | ||
end | ||
|
||
it "accepts string keys" do | ||
%({"a": "1", "b": "2"}).should have_json_keys('a', 'b') | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Also need to update the count of methods this gem adds a few lines above.