assert_json 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +860 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/HOWTO.md +3 -0
- data/README.md +8 -0
- data/Rakefile +1 -1
- data/assert_json.gemspec +6 -5
- data/lib/assert_json/assert_json.rb +58 -52
- data/lib/assert_json/version.rb +2 -1
- data/test/assert_json_has_only_test.rb +85 -49
- data/test/assert_json_new_dsl_test.rb +422 -321
- data/test/assert_json_test.rb +200 -177
- data/test/test_helper.rb +3 -2
- metadata +21 -4
data/test/assert_json_test.rb
CHANGED
@@ -1,268 +1,291 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
# nodoc
|
3
4
|
class AssertJsonTest < Minitest::Test
|
4
5
|
include AssertJson
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
json.element 'key'
|
9
|
-
end
|
10
|
-
end
|
11
|
-
def test_string_crosscheck
|
12
|
-
assert_raises(MiniTest::Assertion) do
|
7
|
+
context "strings" do
|
8
|
+
should "test_string" do
|
13
9
|
assert_json '"key"' do |json|
|
14
|
-
json.element '
|
10
|
+
json.element 'key'
|
15
11
|
end
|
16
12
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
assert_json '{"key":"value"}' do |json|
|
25
|
-
json.element 'key', /alu/
|
13
|
+
|
14
|
+
should "test_string_crosscheck" do
|
15
|
+
assert_raises(MiniTest::Assertion) do
|
16
|
+
assert_json '"key"' do |json|
|
17
|
+
json.element 'wrong_key'
|
18
|
+
end
|
19
|
+
end
|
26
20
|
end
|
27
|
-
end
|
28
21
|
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
should "test_regular_expression_for_strings" do
|
23
|
+
assert_json '"string"' do |json|
|
24
|
+
json.element(/tri/)
|
25
|
+
end
|
32
26
|
end
|
33
|
-
|
34
|
-
|
35
|
-
assert_raises(MiniTest::Assertion) do
|
27
|
+
|
28
|
+
should "test_regular_expression_for_hash_values" do
|
36
29
|
assert_json '{"key":"value"}' do |json|
|
37
|
-
json.element '
|
30
|
+
json.element 'key', /alu/
|
38
31
|
end
|
39
32
|
end
|
40
33
|
end
|
41
|
-
|
42
|
-
|
34
|
+
|
35
|
+
context "hashes" do
|
36
|
+
should "test_single_hash" do
|
43
37
|
assert_json '{"key":"value"}' do |json|
|
44
|
-
json.element 'key', '
|
38
|
+
json.element 'key', 'value'
|
45
39
|
end
|
46
40
|
end
|
47
|
-
end
|
48
41
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
55
|
-
def test_not_element_crosscheck
|
56
|
-
assert_raises(MiniTest::Assertion) do
|
57
|
-
assert_json '{"key":"value"}' do |json|
|
58
|
-
json.not_element 'key'
|
42
|
+
should "test_single_hash_crosscheck_for_key" do
|
43
|
+
assert_raises(MiniTest::Assertion) do
|
44
|
+
assert_json '{"key":"value"}' do |json|
|
45
|
+
json.element 'wrong_key', 'value'
|
46
|
+
end
|
59
47
|
end
|
60
48
|
end
|
61
|
-
end
|
62
49
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
50
|
+
should "test_single_hash_crosscheck_for_value" do
|
51
|
+
assert_raises(MiniTest::Assertion) do
|
52
|
+
assert_json '{"key":"value"}' do |json|
|
53
|
+
json.element 'key', 'wrong_value'
|
54
|
+
end
|
55
|
+
end
|
68
56
|
end
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
57
|
+
|
58
|
+
should "test_not_element" do
|
59
|
+
assert_json '{"key":"value"}' do |json|
|
60
|
+
json.element 'key', 'value'
|
61
|
+
json.not_element 'key_not_included'
|
62
|
+
end
|
75
63
|
end
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
64
|
+
|
65
|
+
should "test_not_element_crosscheck" do
|
66
|
+
assert_raises(MiniTest::Assertion) do
|
67
|
+
assert_json '{"key":"value"}' do |json|
|
68
|
+
json.not_element 'key'
|
69
|
+
end
|
81
70
|
end
|
82
71
|
end
|
83
72
|
end
|
84
|
-
|
85
|
-
|
73
|
+
|
74
|
+
context "arrays" do
|
75
|
+
should "test_array" do
|
86
76
|
assert_json '["value1","value2","value3"]' do |json|
|
87
|
-
json.element '
|
77
|
+
json.element 'value1'
|
78
|
+
json.element 'value2'
|
79
|
+
json.element 'value3'
|
88
80
|
end
|
89
81
|
end
|
90
|
-
|
91
|
-
|
92
|
-
assert_raises(MiniTest::Assertion) do
|
93
|
-
assert_json '["value1","value2","value3"]' do |json|
|
82
|
+
should "test_not_element_array" do
|
83
|
+
assert_json '["value1","value2"]' do |json|
|
94
84
|
json.element 'value1'
|
95
|
-
json.element '
|
85
|
+
json.element 'value2'
|
86
|
+
json.not_element 'value3'
|
96
87
|
end
|
97
88
|
end
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
89
|
+
should "test_array_crosscheck_order" do
|
90
|
+
assert_raises(MiniTest::Assertion) do
|
91
|
+
assert_json '["value1","value2","value3"]' do |json|
|
92
|
+
json.element 'value2'
|
93
|
+
end
|
94
|
+
end
|
103
95
|
end
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
96
|
+
should "test_array_crosscheck_for_first_item" do
|
97
|
+
assert_raises(MiniTest::Assertion) do
|
98
|
+
assert_json '["value1","value2","value3"]' do |json|
|
99
|
+
json.element 'wrong_value1'
|
100
|
+
end
|
109
101
|
end
|
110
102
|
end
|
111
|
-
|
112
|
-
|
113
|
-
|
103
|
+
should "test_array_crosscheck_for_second_item" do
|
104
|
+
assert_raises(MiniTest::Assertion) do
|
105
|
+
assert_json '["value1","value2","value3"]' do |json|
|
106
|
+
json.element 'value1'
|
107
|
+
json.element 'wrong_value2'
|
108
|
+
end
|
114
109
|
end
|
115
110
|
end
|
116
|
-
end
|
117
111
|
|
118
|
-
|
119
|
-
|
120
|
-
|
112
|
+
should "test_nested_arrays" do
|
113
|
+
assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
|
114
|
+
json.element [%w(deep another_depp), %w(second_deep)]
|
115
|
+
end
|
121
116
|
end
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
117
|
+
should "test_nested_arrays_crosscheck" do
|
118
|
+
assert_raises(MiniTest::Assertion) do
|
119
|
+
assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
|
120
|
+
json.element [%w(deep wrong_another_depp), %w(second_deep)]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
assert_raises(MiniTest::Assertion) do
|
124
|
+
assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
|
125
|
+
json.element [%w(deep another_depp), %w(wrong_second_deep)]
|
126
|
+
end
|
127
127
|
end
|
128
128
|
end
|
129
|
-
|
130
|
-
|
131
|
-
assert_raises(MiniTest::Assertion) do
|
129
|
+
|
130
|
+
should "test_hash_with_value_array" do
|
132
131
|
assert_json '{"key":["value1","value2"]}' do |json|
|
133
|
-
json.element 'key',
|
132
|
+
json.element 'key', %w(value1 value2)
|
134
133
|
end
|
135
134
|
end
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
135
|
+
should "test_hash_with_value_array_crosscheck_wrong_key" do
|
136
|
+
assert_raises(MiniTest::Assertion) do
|
137
|
+
assert_json '{"key":["value1","value2"]}' do |json|
|
138
|
+
json.element 'wrong_key', %w(value1 value2)
|
139
|
+
end
|
141
140
|
end
|
142
141
|
end
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
json.element 'inner_key1', 'value1'
|
149
|
-
json.element 'inner_key2', 'value2'
|
142
|
+
should "test_hash_with_value_array_crosscheck_wrong_value1" do
|
143
|
+
assert_raises(MiniTest::Assertion) do
|
144
|
+
assert_json '{"key":["value1","value2"]}' do |json|
|
145
|
+
json.element 'key', %w(wrong_value1 value2)
|
146
|
+
end
|
150
147
|
end
|
151
148
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
json.element 'key' do
|
157
|
-
json.element 'wrong_inner_key1', 'value1'
|
149
|
+
should "test_hash_with_value_array_crosscheck_wrong_value2" do
|
150
|
+
assert_raises(MiniTest::Assertion) do
|
151
|
+
assert_json '{"key":["value1","value2"]}' do |json|
|
152
|
+
json.element 'key', %w(value1 wrong_value2)
|
158
153
|
end
|
159
154
|
end
|
160
155
|
end
|
161
|
-
end
|
162
|
-
|
163
|
-
|
156
|
+
end # arrays
|
157
|
+
|
158
|
+
context "array of hashes" do
|
159
|
+
should "test_hash_with_array_of_hashes" do
|
164
160
|
assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
|
165
161
|
json.element 'key' do
|
166
|
-
json.element 'inner_key1', '
|
162
|
+
json.element 'inner_key1', 'value1'
|
163
|
+
json.element 'inner_key2', 'value2'
|
167
164
|
end
|
168
165
|
end
|
169
166
|
end
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
end
|
178
|
-
def test_array_with_nested_hashes
|
179
|
-
assert_json '[{"key1":{"key2":"value2"}}]' do |json|
|
180
|
-
json.element 'key1' do
|
181
|
-
json.element 'key2', 'value2'
|
167
|
+
should "test_hash_with_array_of_hashes_crosscheck_inner_key" do
|
168
|
+
assert_raises(MiniTest::Assertion) do
|
169
|
+
assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
|
170
|
+
json.element 'key' do
|
171
|
+
json.element 'wrong_inner_key1', 'value1'
|
172
|
+
end
|
173
|
+
end
|
182
174
|
end
|
183
175
|
end
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
176
|
+
should "test_hash_with_array_of_hashes_crosscheck_inner_value" do
|
177
|
+
assert_raises(MiniTest::Assertion) do
|
178
|
+
assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
|
179
|
+
json.element 'key' do
|
180
|
+
json.element 'inner_key1', 'wrong_value1'
|
181
|
+
end
|
182
|
+
end
|
190
183
|
end
|
191
184
|
end
|
192
|
-
|
185
|
+
|
186
|
+
should "test_array_with_two_hashes" do
|
193
187
|
assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
|
194
188
|
json.element 'key1', 'value1'
|
195
|
-
json.element 'key2', '
|
189
|
+
json.element 'key2', 'value2'
|
196
190
|
end
|
197
191
|
end
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
json.element 'outer_key' do
|
203
|
-
json.element 'key' do
|
204
|
-
json.element 'inner_key', 'value'
|
192
|
+
should "test_array_with_nested_hashes" do
|
193
|
+
assert_json '[{"key1":{"key2":"value2"}}]' do |json|
|
194
|
+
json.element 'key1' do
|
195
|
+
json.element 'key2', 'value2'
|
205
196
|
end
|
206
197
|
end
|
207
198
|
end
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
199
|
+
should "test_array_with_two_hashes_crosscheck" do
|
200
|
+
assert_raises(MiniTest::Assertion) do
|
201
|
+
assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
|
202
|
+
json.element 'wrong_key1', 'value1'
|
203
|
+
json.element 'key2', 'value2'
|
204
|
+
end
|
205
|
+
end
|
206
|
+
assert_raises(MiniTest::Assertion) do
|
207
|
+
assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
|
208
|
+
json.element 'key1', 'value1'
|
209
|
+
json.element 'key2', 'wrong_value2'
|
210
|
+
end
|
213
211
|
end
|
214
212
|
end
|
215
|
-
|
213
|
+
end # array with hashes
|
214
|
+
|
215
|
+
context "nested hashes" do
|
216
|
+
should "test_nested_hashes" do
|
216
217
|
assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
|
217
218
|
json.element 'outer_key' do
|
218
219
|
json.element 'key' do
|
219
|
-
json.element 'inner_key', '
|
220
|
+
json.element 'inner_key', 'value'
|
220
221
|
end
|
221
222
|
end
|
222
223
|
end
|
223
224
|
end
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
225
|
+
should "test_nested_hashes_crosscheck" do
|
226
|
+
assert_raises(MiniTest::Assertion) do
|
227
|
+
assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
|
228
|
+
json.element 'wrong_outer_key'
|
229
|
+
end
|
230
|
+
end
|
231
|
+
assert_raises(MiniTest::Assertion) do
|
232
|
+
assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
|
233
|
+
json.element 'outer_key' do
|
234
|
+
json.element 'key' do
|
235
|
+
json.element 'inner_key', 'wrong_value'
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
232
239
|
end
|
233
240
|
end
|
241
|
+
end # nested hashes
|
234
242
|
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
243
|
+
context "real life examples" do
|
244
|
+
should "test_real_xws" do
|
245
|
+
assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do |json|
|
246
|
+
json.element 'contact_request' do
|
247
|
+
json.element 'sender_id', '3'
|
248
|
+
json.element 'receiver_id', '2'
|
249
|
+
json.element 'id', 1
|
239
250
|
end
|
240
|
-
json.element 'receiver_id', '2'
|
241
|
-
json.element 'body', 'body'
|
242
251
|
end
|
243
|
-
end
|
244
|
-
end
|
245
252
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
json.element
|
251
|
-
|
253
|
+
test_json = <<JSON
|
254
|
+
[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]
|
255
|
+
JSON
|
256
|
+
assert_json test_json do |json|
|
257
|
+
json.element 'private_message' do
|
258
|
+
json.element 'sender' do
|
259
|
+
json.element 'display_name', 'first last'
|
260
|
+
end
|
261
|
+
json.element 'receiver_id', '2'
|
262
|
+
json.element 'body', 'body'
|
263
|
+
end
|
252
264
|
end
|
253
265
|
end
|
254
|
-
end
|
266
|
+
end # real life examples
|
255
267
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
json.element
|
261
|
-
json.element
|
262
|
-
json.element
|
268
|
+
context "not enough elements" do
|
269
|
+
should "test_not_enough_elements_in_array" do
|
270
|
+
assert_raises(MiniTest::Assertion) do
|
271
|
+
assert_json '["one","two"]' do |json|
|
272
|
+
json.element "one"
|
273
|
+
json.element "two"
|
274
|
+
json.element "three"
|
263
275
|
end
|
264
276
|
end
|
265
277
|
end
|
266
|
-
end
|
267
278
|
|
279
|
+
should "test_not_enough_elements_in_hash_array" do
|
280
|
+
assert_raises(MiniTest::Assertion) do
|
281
|
+
assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do |json|
|
282
|
+
json.element 'key' do
|
283
|
+
json.element 'key1', 'value1'
|
284
|
+
json.element 'key2', 'value2'
|
285
|
+
json.element 'key3'
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end # not enough elements
|
268
291
|
end
|