json_spec 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -183,6 +183,42 @@ versatile and can be used in plenty of different formats:
183
183
 
184
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
+ ### JSON Memory
187
+
188
+ There's one more Cucumber step that json_spec provides which hasn't been used above. It's used to
189
+ memorize JSON for reuse in later steps. You can "keep" all or a portion of the JSON by giving a
190
+ name by which to remember it.
191
+
192
+ Feature: User API
193
+ Scenario: Index action includes full user JSON
194
+ Given the following user exists:
195
+ | id | first_name | last_name |
196
+ | 1 | Steve | Richert |
197
+ And I visit "/users/1.json"
198
+ And I keep the JSON response as "USER_1"
199
+ When I visit "/users.json"
200
+ Then the JSON response should be:
201
+ """
202
+ [
203
+ %{USER_1}
204
+ ]
205
+ """
206
+
207
+ You can memorize JSON at a path:
208
+
209
+ Given I keep the JSON response at "first_name" as "FIRST_NAME"
210
+
211
+ You can remember JSON at a path:
212
+
213
+ Then the JSON response at "0/first_name" should be:
214
+ """
215
+ %{FIRST_NAME}
216
+ """
217
+
218
+ You can also remember JSON inline:
219
+
220
+ Then the JSON response at "0/first_name" should be %{FIRST_NAME}
221
+
186
222
  Contributing
187
223
  ------------
188
224
  In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project.
@@ -0,0 +1,184 @@
1
+ Feature: Memory
2
+ Background:
3
+ Given the JSON is:
4
+ """
5
+ {
6
+ "array": [
7
+
8
+ ],
9
+ "false": false,
10
+ "float": 10.0,
11
+ "hash": {
12
+ },
13
+ "integer": 10,
14
+ "null": null,
15
+ "string": "json_spec",
16
+ "true": true
17
+ }
18
+ """
19
+ And I get the JSON
20
+
21
+ Scenario: Entire JSON
22
+ When I keep the JSON as "JSON"
23
+ Then the JSON should be %{JSON}
24
+ And the JSON should be:
25
+ """
26
+ %{JSON}
27
+ """
28
+
29
+ Scenario: String
30
+ When I keep the JSON at "string" as "STRING"
31
+ Then the JSON at "string" should be %{STRING}
32
+ And the JSON should be:
33
+ """
34
+ {
35
+ "array": [
36
+
37
+ ],
38
+ "false": false,
39
+ "float": 10.0,
40
+ "hash": {
41
+ },
42
+ "integer": 10,
43
+ "null": null,
44
+ "string": %{STRING},
45
+ "true": true
46
+ }
47
+ """
48
+
49
+ Scenario: Integer
50
+ When I keep the JSON at "integer" as "INTEGER"
51
+ Then the JSON at "integer" should be %{INTEGER}
52
+ And the JSON should be:
53
+ """
54
+ {
55
+ "array": [
56
+
57
+ ],
58
+ "false": false,
59
+ "float": 10.0,
60
+ "hash": {
61
+ },
62
+ "integer": %{INTEGER},
63
+ "null": null,
64
+ "string": "json_spec",
65
+ "true": true
66
+ }
67
+ """
68
+
69
+ Scenario: Float
70
+ When I keep the JSON at "float" as "FLOAT"
71
+ Then the JSON at "float" should be %{FLOAT}
72
+ And the JSON should be:
73
+ """
74
+ {
75
+ "array": [
76
+
77
+ ],
78
+ "false": false,
79
+ "float": %{FLOAT},
80
+ "hash": {
81
+ },
82
+ "integer": 10,
83
+ "null": null,
84
+ "string": "json_spec",
85
+ "true": true
86
+ }
87
+ """
88
+
89
+ Scenario: Array
90
+ When I keep the JSON at "array" as "ARRAY"
91
+ Then the JSON at "array" should be %{ARRAY}
92
+ And the JSON should be:
93
+ """
94
+ {
95
+ "array": %{ARRAY},
96
+ "false": false,
97
+ "float": 10.0,
98
+ "hash": {
99
+ },
100
+ "integer": 10,
101
+ "null": null,
102
+ "string": "json_spec",
103
+ "true": true
104
+ }
105
+ """
106
+
107
+ Scenario: Hash
108
+ When I keep the JSON at "hash" as "HASH"
109
+ Then the JSON at "hash" should be %{HASH}
110
+ And the JSON should be:
111
+ """
112
+ {
113
+ "array": [
114
+
115
+ ],
116
+ "false": false,
117
+ "float": 10.0,
118
+ "hash": %{HASH},
119
+ "integer": 10,
120
+ "null": null,
121
+ "string": "json_spec",
122
+ "true": true
123
+ }
124
+ """
125
+
126
+ Scenario: True
127
+ When I keep the JSON at "true" as "TRUE"
128
+ Then the JSON at "true" should be %{TRUE}
129
+ And the JSON should be:
130
+ """
131
+ {
132
+ "array": [
133
+
134
+ ],
135
+ "false": false,
136
+ "float": 10.0,
137
+ "hash": {
138
+ },
139
+ "integer": 10,
140
+ "null": null,
141
+ "string": "json_spec",
142
+ "true": %{TRUE}
143
+ }
144
+ """
145
+
146
+ Scenario: False
147
+ When I keep the JSON at "false" as "FALSE"
148
+ Then the JSON at "false" should be %{FALSE}
149
+ And the JSON should be:
150
+ """
151
+ {
152
+ "array": [
153
+
154
+ ],
155
+ "false": %{FALSE},
156
+ "float": 10.0,
157
+ "hash": {
158
+ },
159
+ "integer": 10,
160
+ "null": null,
161
+ "string": "json_spec",
162
+ "true": true
163
+ }
164
+ """
165
+
166
+ Scenario: Null
167
+ When I keep the JSON at "null" as "NULL"
168
+ Then the JSON at "null" should be %{NULL}
169
+ And the JSON should be:
170
+ """
171
+ {
172
+ "array": [
173
+
174
+ ],
175
+ "false": false,
176
+ "float": 10.0,
177
+ "hash": {
178
+ },
179
+ "integer": 10,
180
+ "null": %{NULL},
181
+ "string": "json_spec",
182
+ "true": true
183
+ }
184
+ """
@@ -3,7 +3,9 @@ require "json_spec/errors"
3
3
  require "json_spec/configuration"
4
4
  require "json_spec/matchers"
5
5
  require "json_spec/helpers"
6
+ require "json_spec/memory"
6
7
 
7
8
  module JsonSpec
8
9
  extend Configuration
10
+ extend Memory
9
11
  end
@@ -2,19 +2,27 @@ require File.expand_path("../../json_spec", __FILE__)
2
2
 
3
3
  World(JsonSpec::Helpers)
4
4
 
5
+ After do
6
+ JsonSpec.forget
7
+ end
8
+
9
+ When /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ do |path, key|
10
+ JsonSpec.memorize(key, path ? json_at_path(last_json, path) : last_json)
11
+ end
12
+
5
13
  Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be:$/ do |path, negative, json|
6
14
  if negative
7
- last_json.should_not be_json_eql(json).at_path(path)
15
+ last_json.should_not be_json_eql(JsonSpec.remember(json)).at_path(path)
8
16
  else
9
- last_json.should be_json_eql(json).at_path(path)
17
+ last_json.should be_json_eql(JsonSpec.remember(json)).at_path(path)
10
18
  end
11
19
  end
12
20
 
13
- Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|\{.*\}|true|false|null)$/ do |path, negative, value|
21
+ Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ do |path, negative, value|
14
22
  if negative
15
- last_json.should_not be_json_eql(value).at_path(path)
23
+ last_json.should_not be_json_eql(JsonSpec.remember(value)).at_path(path)
16
24
  else
17
- last_json.should be_json_eql(value).at_path(path)
25
+ last_json.should be_json_eql(JsonSpec.remember(value)).at_path(path)
18
26
  end
19
27
  end
20
28
 
@@ -0,0 +1,20 @@
1
+ module JsonSpec
2
+ module Memory
3
+ def memory
4
+ @memory ||= {}
5
+ end
6
+
7
+ def memorize(key, value)
8
+ memory[key] = value
9
+ end
10
+
11
+ def remember(json)
12
+ return json if memory.empty?
13
+ json.gsub(/%\{(#{memory.keys.map{|k| Regexp.quote(k) }.join("|")})\}/){ memory[$1] }
14
+ end
15
+
16
+ def forget
17
+ memory.clear
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module JsonSpec
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,68 +1,93 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: json_spec
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Steve Richert
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-07-08 00:00:00.000000000 -04:00
17
+
18
+ date: 2011-07-08 00:00:00 -04:00
13
19
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
16
22
  name: json
17
- requirement: &2155991800 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
18
25
  none: false
19
- requirements:
26
+ requirements:
20
27
  - - ~>
21
- - !ruby/object:Gem::Version
22
- version: '1.0'
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 1
32
+ - 0
33
+ version: "1.0"
23
34
  type: :runtime
24
- prerelease: false
25
- version_requirements: *2155991800
26
- - !ruby/object:Gem::Dependency
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
27
37
  name: rspec
28
- requirement: &2155991300 !ruby/object:Gem::Requirement
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
29
40
  none: false
30
- requirements:
41
+ requirements:
31
42
  - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '2.0'
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 2
47
+ - 0
48
+ version: "2.0"
34
49
  type: :runtime
35
- prerelease: false
36
- version_requirements: *2155991300
37
- - !ruby/object:Gem::Dependency
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
38
52
  name: rake
39
- requirement: &2155990840 !ruby/object:Gem::Requirement
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
40
55
  none: false
41
- requirements:
56
+ requirements:
42
57
  - - ~>
43
- - !ruby/object:Gem::Version
44
- version: '0.9'
58
+ - !ruby/object:Gem::Version
59
+ hash: 25
60
+ segments:
61
+ - 0
62
+ - 9
63
+ version: "0.9"
45
64
  type: :development
46
- prerelease: false
47
- version_requirements: *2155990840
48
- - !ruby/object:Gem::Dependency
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
49
67
  name: cucumber
50
- requirement: &2155990380 !ruby/object:Gem::Requirement
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
51
70
  none: false
52
- requirements:
71
+ requirements:
53
72
  - - ~>
54
- - !ruby/object:Gem::Version
55
- version: '1.0'
73
+ - !ruby/object:Gem::Version
74
+ hash: 15
75
+ segments:
76
+ - 1
77
+ - 0
78
+ version: "1.0"
56
79
  type: :development
57
- prerelease: false
58
- version_requirements: *2155990380
80
+ version_requirements: *id004
59
81
  description: Easily handle JSON in RSpec and Cucumber
60
- email:
82
+ email:
61
83
  - steve.richert@gmail.com
62
84
  executables: []
85
+
63
86
  extensions: []
87
+
64
88
  extra_rdoc_files: []
65
- files:
89
+
90
+ files:
66
91
  - .gitignore
67
92
  - .travis.yml
68
93
  - Gemfile
@@ -70,6 +95,7 @@ files:
70
95
  - README.md
71
96
  - Rakefile
72
97
  - features/equivalence.feature
98
+ - features/memory.feature
73
99
  - features/paths.feature
74
100
  - features/sizes.feature
75
101
  - features/step_definitions/steps.rb
@@ -82,6 +108,7 @@ files:
82
108
  - lib/json_spec/errors.rb
83
109
  - lib/json_spec/helpers.rb
84
110
  - lib/json_spec/matchers.rb
111
+ - lib/json_spec/memory.rb
85
112
  - lib/json_spec/version.rb
86
113
  - spec/json_spec/configuration_spec.rb
87
114
  - spec/json_spec/matchers_spec.rb
@@ -90,30 +117,40 @@ files:
90
117
  has_rdoc: true
91
118
  homepage: https://github.com/collectiveidea/json_spec
92
119
  licenses: []
120
+
93
121
  post_install_message:
94
122
  rdoc_options: []
95
- require_paths:
123
+
124
+ require_paths:
96
125
  - lib
97
- required_ruby_version: !ruby/object:Gem::Requirement
126
+ required_ruby_version: !ruby/object:Gem::Requirement
98
127
  none: false
99
- requirements:
100
- - - ! '>='
101
- - !ruby/object:Gem::Version
102
- version: '0'
103
- required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
136
  none: false
105
- requirements:
106
- - - ! '>='
107
- - !ruby/object:Gem::Version
108
- version: '0'
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
143
+ version: "0"
109
144
  requirements: []
145
+
110
146
  rubyforge_project: json_spec
111
- rubygems_version: 1.6.2
147
+ rubygems_version: 1.3.7
112
148
  signing_key:
113
149
  specification_version: 3
114
150
  summary: Easily handle JSON in RSpec and Cucumber
115
- test_files:
151
+ test_files:
116
152
  - features/equivalence.feature
153
+ - features/memory.feature
117
154
  - features/paths.feature
118
155
  - features/sizes.feature
119
156
  - features/step_definitions/steps.rb