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

Added have_json_type_boolean matcher and relative cucumber step. #10

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion features/types.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Feature: Types
"float": 10.0,
"hash": {},
"integer": 10,
"string": "json_spec"
"string": "json_spec",
"boolean" : true
}
"""
When I get the JSON
Expand All @@ -16,3 +17,4 @@ Feature: Types
And the JSON at "float" should be a float
And the JSON at "hash" should be a hash
And the JSON at "integer" should be an integer
And the JSON at "boolean" should be a boolean
16 changes: 12 additions & 4 deletions lib/json_spec/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be an? (.*)$/ do |path, negative, type|
klass = Module.const_get(type.gsub(/^./){|x| x.upcase })
if negative
last_json.should_not have_json_type(klass).at_path(path)
if type == "boolean"
if negative
last_json.should_not have_json_type_boolean.at_path(path)
else
last_json.should have_json_type_boolean.at_path(path)
end
else
last_json.should have_json_type(klass).at_path(path)
klass = Module.const_get(type.gsub(/^./){|x| x.upcase })
if negative
last_json.should_not have_json_type(klass).at_path(path)
else
last_json.should have_json_type(klass).at_path(path)
end
end
end

Expand Down
29 changes: 29 additions & 0 deletions lib/json_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ def actual
end
end

RSpec::Matchers.define :have_json_type_boolean do
include JsonSpec::Helpers

match do |json|
@json = json
actual.kind_of? TrueClass or actual.kind_of? FalseClass
end

chain :at_path do |path|
@path = path
end

failure_message_for_should do
message = "Expected JSON value type to be a boolean, got #{actual.class}"
message << %( at path "#{@path}") if @path
message
end

failure_message_for_should_not do
message = "Expected JSON value type to not be a boolean, got #{actual.class}"
message << %( at path "#{@path}") if @path
message
end

def actual
parse_json(@json, @path)
end
end

RSpec::Matchers.define :have_json_size do |expected_size|
include JsonSpec::Helpers

Expand Down
10 changes: 10 additions & 0 deletions spec/json_spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@
end
end

context "have_json_type_boolean" do
it "matches true" do
%(true).should have_json_type_boolean
end

it "matches false" do
%(false).should have_json_type_boolean
end
end

context "have_json_type" do
it "matches hashes" do
%({}).should have_json_type(Hash)
Expand Down