json_spec 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +13 -13
- data/json_spec.gemspec +1 -0
- data/lib/json_spec/cucumber.rb +30 -10
- data/lib/json_spec/helpers.rb +1 -2
- data/lib/json_spec/matchers.rb +24 -2
- data/lib/json_spec/version.rb +1 -1
- metadata +19 -7
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
json_spec
|
2
2
|
========
|
3
3
|
Easily handle JSON in RSpec and Cucumber
|
4
4
|
|
@@ -14,15 +14,15 @@ Documentation
|
|
14
14
|
-------------
|
15
15
|
Please help write documentation!
|
16
16
|
|
17
|
-
[http://rdoc.info/
|
17
|
+
[http://rdoc.info/gems/json_spec](http://rdoc.info/gems/json_spec)
|
18
18
|
|
19
19
|
Continuous Integration
|
20
20
|
----------------------
|
21
|
-
|
21
|
+
[![Build Status](https://travis-ci.org/collectiveidea/json_spec.png)](http://travis-ci.org/collectiveidea/json_spec)
|
22
22
|
|
23
23
|
RSpec
|
24
24
|
--------------
|
25
|
-
|
25
|
+
json_spec defines four new RSpec matchers:
|
26
26
|
|
27
27
|
* `be_json_eql`
|
28
28
|
* `have_json_path`
|
@@ -56,7 +56,7 @@ The new matchers could be used in RSpec as follows:
|
|
56
56
|
|
57
57
|
### Exclusions
|
58
58
|
|
59
|
-
|
59
|
+
json_spec ignores certain hash keys by default when comparing JSON:
|
60
60
|
|
61
61
|
* `id`
|
62
62
|
* `created_at`
|
@@ -70,13 +70,13 @@ customizeable:
|
|
70
70
|
exclude_keys "created_at", "updated_at"
|
71
71
|
end
|
72
72
|
|
73
|
-
Now, the `id` key will be included in
|
73
|
+
Now, the `id` key will be included in json_spec's comparisons. Keys can also be excluded/included
|
74
74
|
per matcher by chaining the `excluding` or `including` methods (as shown above) which will add or subtract from
|
75
75
|
the globally excluded keys, respectively.
|
76
76
|
|
77
77
|
### Paths
|
78
78
|
|
79
|
-
Each of
|
79
|
+
Each of json_spec's matchers deal with JSON "paths." These are simple strings of "/" separated
|
80
80
|
hash keys and array indexes. For instance, with the following JSON:
|
81
81
|
|
82
82
|
{
|
@@ -94,7 +94,7 @@ We could access the first friend's first name with the path `"friends/0/first_na
|
|
94
94
|
|
95
95
|
Cucumber
|
96
96
|
--------
|
97
|
-
|
97
|
+
json_spec provides Cucumber steps that utilize its RSpec matchers and that's where json_spec really
|
98
98
|
shines. This is perfect for testing your app's JSON API.
|
99
99
|
|
100
100
|
In order to use the Cucumber steps, in your `env.rb` you must:
|
@@ -107,7 +107,7 @@ You also need to define a `last_json` method. If you're using Capybara, it could
|
|
107
107
|
page.source
|
108
108
|
end
|
109
109
|
|
110
|
-
Now, you can use the
|
110
|
+
Now, you can use the json_spec steps in your features:
|
111
111
|
|
112
112
|
Feature: User API
|
113
113
|
Background:
|
@@ -140,8 +140,8 @@ Now, you can use the JsonSpec steps in your features:
|
|
140
140
|
]
|
141
141
|
"""
|
142
142
|
|
143
|
-
The background steps above aren't provided by
|
144
|
-
Capybara. The remaining steps all stem from the five steps that
|
143
|
+
The background steps above aren't provided by json_spec and the "visit" steps are provided by
|
144
|
+
Capybara. The remaining steps all stem from the five steps that json_spec provides. They're
|
145
145
|
versatile and can be used in plenty of different formats:
|
146
146
|
|
147
147
|
Then the JSON should be:
|
@@ -181,7 +181,7 @@ versatile and can be used in plenty of different formats:
|
|
181
181
|
Then the JSON should have 3 keys
|
182
182
|
Then the JSON should have 4 whatevers
|
183
183
|
|
184
|
-
_All instances of "
|
184
|
+
_All instances of "should" above could be followed by "not" and all instances of "JSON" could be downcased and/or followed by "response."_
|
185
185
|
|
186
186
|
Contributing
|
187
187
|
------------
|
@@ -214,7 +214,7 @@ Submitting a Pull Request
|
|
214
214
|
2. Create a topic branch.
|
215
215
|
3. Implement your feature or bug fix.
|
216
216
|
4. Add specs for your feature or bug fix.
|
217
|
-
5. Run
|
217
|
+
5. Run `bundle exec rake`. If your changes are not 100% covered and passing, go back to step 4.
|
218
218
|
6. Commit and push your changes.
|
219
219
|
7. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create your own version for some reason, please do so in a separate commit.)
|
220
220
|
|
data/json_spec.gemspec
CHANGED
data/lib/json_spec/cucumber.rb
CHANGED
@@ -2,23 +2,43 @@ require File.expand_path("../../json_spec", __FILE__)
|
|
2
2
|
|
3
3
|
World(JsonSpec::Helpers)
|
4
4
|
|
5
|
-
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should be:$/ do |path, json|
|
6
|
-
|
5
|
+
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be:$/ do |path, negative, json|
|
6
|
+
if negative
|
7
|
+
last_json.should_not be_json_eql(json).at_path(path)
|
8
|
+
else
|
9
|
+
last_json.should be_json_eql(json).at_path(path)
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
|
-
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|\{.*\}|true|false|null)$/ do |path, value|
|
10
|
-
|
13
|
+
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|\{.*\}|true|false|null)$/ do |path, negative, value|
|
14
|
+
if negative
|
15
|
+
last_json.should_not be_json_eql(value).at_path(path)
|
16
|
+
else
|
17
|
+
last_json.should be_json_eql(value).at_path(path)
|
18
|
+
end
|
11
19
|
end
|
12
20
|
|
13
|
-
Then /^the (?:JSON|json)(?: response)? should have "(.*)"$/ do |path|
|
14
|
-
|
21
|
+
Then /^the (?:JSON|json)(?: response)? should( not)? have "(.*)"$/ do |negative, path|
|
22
|
+
if negative
|
23
|
+
last_json.should_not have_json_path(path)
|
24
|
+
else
|
25
|
+
last_json.should have_json_path(path)
|
26
|
+
end
|
15
27
|
end
|
16
28
|
|
17
|
-
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should be an? (.*)$/ do |path, type|
|
29
|
+
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be an? (.*)$/ do |path, negative, type|
|
18
30
|
klass = Module.const_get(type.gsub(/^./){|x| x.upcase })
|
19
|
-
|
31
|
+
if negative
|
32
|
+
last_json.should_not have_json_type(klass).at_path(path)
|
33
|
+
else
|
34
|
+
last_json.should have_json_type(klass).at_path(path)
|
35
|
+
end
|
20
36
|
end
|
21
37
|
|
22
|
-
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should have (\d+)/ do |path, size|
|
23
|
-
|
38
|
+
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? have (\d+)/ do |path, negative, size|
|
39
|
+
if negative
|
40
|
+
last_json.should_not have_json_size(size.to_i).at_path(path)
|
41
|
+
else
|
42
|
+
last_json.should have_json_size(size.to_i).at_path(path)
|
43
|
+
end
|
24
44
|
end
|
data/lib/json_spec/helpers.rb
CHANGED
data/lib/json_spec/matchers.rb
CHANGED
@@ -29,6 +29,12 @@ RSpec::Matchers.define :be_json_eql do |expected_json|
|
|
29
29
|
message
|
30
30
|
end
|
31
31
|
|
32
|
+
failure_message_for_should_not do
|
33
|
+
message = "Expected inequivalent JSON"
|
34
|
+
message << %( at path "#{@path}") if @path
|
35
|
+
message
|
36
|
+
end
|
37
|
+
|
32
38
|
def scrub(json, path = nil)
|
33
39
|
ruby = path ? ruby_at_json_path(json, path) : parse_json_value(json)
|
34
40
|
pretty_json_value(exclude_keys(ruby)).chomp + "\n"
|
@@ -71,6 +77,10 @@ RSpec::Matchers.define :have_json_path do |path|
|
|
71
77
|
failure_message_for_should do
|
72
78
|
%(Expected JSON path "#{path}")
|
73
79
|
end
|
80
|
+
|
81
|
+
failure_message_for_should_not do
|
82
|
+
%(Expected no JSON path "#{path}")
|
83
|
+
end
|
74
84
|
end
|
75
85
|
|
76
86
|
RSpec::Matchers.define :have_json_type do |klass|
|
@@ -86,7 +96,13 @@ RSpec::Matchers.define :have_json_type do |klass|
|
|
86
96
|
end
|
87
97
|
|
88
98
|
failure_message_for_should do
|
89
|
-
message = "Expected JSON value type
|
99
|
+
message = "Expected JSON value type to be #{klass}"
|
100
|
+
message << %( at path "#{@path}") if @path
|
101
|
+
message
|
102
|
+
end
|
103
|
+
|
104
|
+
failure_message_for_should_not do
|
105
|
+
message = "Expected JSON value type to not be #{klass}"
|
90
106
|
message << %( at path "#{@path}") if @path
|
91
107
|
message
|
92
108
|
end
|
@@ -106,7 +122,13 @@ RSpec::Matchers.define :have_json_size do |expected_size|
|
|
106
122
|
end
|
107
123
|
|
108
124
|
failure_message_for_should do
|
109
|
-
message = "Expected JSON value size
|
125
|
+
message = "Expected JSON value size to be #{expected_size}"
|
126
|
+
message << %( at path "#{@path}") if @path
|
127
|
+
message
|
128
|
+
end
|
129
|
+
|
130
|
+
failure_message_for_should_not do
|
131
|
+
message = "Expected JSON value size to not be #{expected_size}"
|
110
132
|
message << %( at path "#{@path}") if @path
|
111
133
|
message
|
112
134
|
end
|
data/lib/json_spec/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ default_executable:
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: json
|
17
|
-
requirement: &
|
17
|
+
requirement: &2155991800 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '1.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2155991800
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: rspec
|
28
|
-
requirement: &
|
28
|
+
requirement: &2155991300 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,21 @@ dependencies:
|
|
33
33
|
version: '2.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2155991300
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rake
|
39
|
+
requirement: &2155990840 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0.9'
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2155990840
|
37
48
|
- !ruby/object:Gem::Dependency
|
38
49
|
name: cucumber
|
39
|
-
requirement: &
|
50
|
+
requirement: &2155990380 !ruby/object:Gem::Requirement
|
40
51
|
none: false
|
41
52
|
requirements:
|
42
53
|
- - ~>
|
@@ -44,7 +55,7 @@ dependencies:
|
|
44
55
|
version: '1.0'
|
45
56
|
type: :development
|
46
57
|
prerelease: false
|
47
|
-
version_requirements: *
|
58
|
+
version_requirements: *2155990380
|
48
59
|
description: Easily handle JSON in RSpec and Cucumber
|
49
60
|
email:
|
50
61
|
- steve.richert@gmail.com
|
@@ -53,6 +64,7 @@ extensions: []
|
|
53
64
|
extra_rdoc_files: []
|
54
65
|
files:
|
55
66
|
- .gitignore
|
67
|
+
- .travis.yml
|
56
68
|
- Gemfile
|
57
69
|
- LICENSE.md
|
58
70
|
- README.md
|