Skip to content
Browse files

Add JSON type matcher and steps

  • Loading branch information...
1 parent 8706422 commit 3cdf4f7c4acaf8820d940f88447e455659f34faa @laserlemon laserlemon committed
Showing with 58 additions and 16 deletions.
  1. +18 −0 features/types.feature
  2. +9 −4 lib/json_spec/cucumber.rb
  3. +31 −12 lib/json_spec/matchers.rb
View
18 features/types.feature
@@ -0,0 +1,18 @@
+Feature: Types
+ Scenario: All types
+ Given the JSON is:
+ """
+ {
+ "array": [],
+ "float": 10.0,
+ "hash": {},
+ "integer": 10,
+ "string": "json_spec"
+ }
+ """
+ When I get the JSON
+ Then the JSON should be a hash
+ And the JSON at "array" should be an array
+ 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
View
13 lib/json_spec/cucumber.rb
@@ -2,10 +2,6 @@
World(JsonSpec::Helpers)
-Then /^the (?:JSON|json)(?: response)? should have "(.*)"$/ do |path|
- last_json.should have_json_path(path)
-end
-
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should be:$/ do |path, json|
last_json.should be_json_eql(json).at_path(path)
end
@@ -14,6 +10,15 @@
last_json.should be_json_eql(value).at_path(path)
end
+Then /^the (?:JSON|json)(?: response)? should have "(.*)"$/ do |path|
+ last_json.should have_json_path(path)
+end
+
+Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should be an? (.*)$/ do |path, type|
+ klass = Module.const_get(type.gsub(/^./){|x| x.upcase })
+ last_json.should have_json_type(klass).at_path(path)
+end
+
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should have (\d+)/ do |path, size|
last_json.should have_json_size(size.to_i).at_path(path)
end
View
43 lib/json_spec/matchers.rb
@@ -56,13 +56,29 @@ def excluded_keys
end
end
-RSpec::Matchers.define :have_json_size do |expected_size|
+RSpec::Matchers.define :have_json_path do |path|
+ include JsonSpec::Helpers
+
+ match do |json|
+ begin
+ ruby_at_json_path(json, path)
+ true
+ rescue JsonSpec::MissingPathError
+ false
+ end
+ end
+
+ failure_message_for_should do
+ %(Expected JSON path "#{path}")
+ end
+end
+
+RSpec::Matchers.define :have_json_type do |klass|
include JsonSpec::Helpers
match do |json|
ruby = @path ? ruby_at_json_path(json, @path) : parse_json_value(json)
- actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
- actual_size == expected_size
+ ruby.is_a?(klass)
end
chain :at_path do |path|
@@ -70,25 +86,28 @@ def excluded_keys
end
failure_message_for_should do
- message = "Expected JSON value size of #{expected_size}"
+ message = "Expected JSON value type of #{klass}"
message << %( at path "#{@path}") if @path
message
end
end
-RSpec::Matchers.define :have_json_path do |path|
+RSpec::Matchers.define :have_json_size do |expected_size|
include JsonSpec::Helpers
match do |json|
- begin
- ruby_at_json_path(json, path)
- true
- rescue JsonSpec::MissingPathError
- false
- end
+ ruby = @path ? ruby_at_json_path(json, @path) : parse_json_value(json)
+ actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
+ actual_size == expected_size
+ end
+
+ chain :at_path do |path|
+ @path = path
end
failure_message_for_should do
- %(Expected JSON path "#{path}")
+ message = "Expected JSON value size of #{expected_size}"
+ message << %( at path "#{@path}") if @path
+ message
end
end

0 comments on commit 3cdf4f7

Please sign in to comment.
Something went wrong with that request. Please try again.