assert_json 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
- def test_string
7
- assert_json '"key"' do |json|
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 'wrong_key'
10
+ json.element 'key'
15
11
  end
16
12
  end
17
- end
18
- def test_regular_expression_for_strings
19
- assert_json '"string"' do |json|
20
- json.element /tri/
21
- end
22
- end
23
- def test_regular_expression_for_hash_values
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
- def test_single_hash
30
- assert_json '{"key":"value"}' do |json|
31
- json.element 'key', 'value'
22
+ should "test_regular_expression_for_strings" do
23
+ assert_json '"string"' do |json|
24
+ json.element(/tri/)
25
+ end
32
26
  end
33
- end
34
- def test_single_hash_crosscheck_for_key
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 'wrong_key', 'value'
30
+ json.element 'key', /alu/
38
31
  end
39
32
  end
40
33
  end
41
- def test_single_hash_crosscheck_for_value
42
- assert_raises(MiniTest::Assertion) do
34
+
35
+ context "hashes" do
36
+ should "test_single_hash" do
43
37
  assert_json '{"key":"value"}' do |json|
44
- json.element 'key', 'wrong_value'
38
+ json.element 'key', 'value'
45
39
  end
46
40
  end
47
- end
48
41
 
49
- def test_not_element
50
- assert_json '{"key":"value"}' do |json|
51
- json.element 'key', 'value'
52
- json.not_element 'key_not_included'
53
- end
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
- def test_array
64
- assert_json '["value1","value2","value3"]' do |json|
65
- json.element 'value1'
66
- json.element 'value2'
67
- json.element 'value3'
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
- end
70
- def test_not_element_array
71
- assert_json '["value1","value2"]' do |json|
72
- json.element 'value1'
73
- json.element 'value2'
74
- json.not_element 'value3'
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
- end
77
- def test_array_crosscheck_order
78
- assert_raises(MiniTest::Assertion) do
79
- assert_json '["value1","value2","value3"]' do |json|
80
- json.element 'value2'
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
- def test_array_crosscheck_for_first_item
85
- assert_raises(MiniTest::Assertion) do
73
+
74
+ context "arrays" do
75
+ should "test_array" do
86
76
  assert_json '["value1","value2","value3"]' do |json|
87
- json.element 'wrong_value1'
77
+ json.element 'value1'
78
+ json.element 'value2'
79
+ json.element 'value3'
88
80
  end
89
81
  end
90
- end
91
- def test_array_crosscheck_for_second_item
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 'wrong_value2'
85
+ json.element 'value2'
86
+ json.not_element 'value3'
96
87
  end
97
88
  end
98
- end
99
-
100
- def test_nested_arrays
101
- assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
102
- json.element [["deep","another_depp"],["second_deep"]]
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
- end
105
- def test_nested_arrays_crosscheck
106
- assert_raises(MiniTest::Assertion) do
107
- assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
108
- json.element [["deep","wrong_another_depp"],["second_deep"]]
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
- assert_raises(MiniTest::Assertion) do
112
- assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
113
- json.element [["deep","another_depp"],["wrong_second_deep"]]
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
- def test_hash_with_value_array
119
- assert_json '{"key":["value1","value2"]}' do |json|
120
- json.element 'key', ['value1', 'value2']
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
- end
123
- def test_hash_with_value_array_crosscheck_wrong_key
124
- assert_raises(MiniTest::Assertion) do
125
- assert_json '{"key":["value1","value2"]}' do |json|
126
- json.element 'wrong_key', ['value1', 'value2']
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
- end
130
- def test_hash_with_value_array_crosscheck_wrong_value1
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', ['wrong_value1', 'value2']
132
+ json.element 'key', %w(value1 value2)
134
133
  end
135
134
  end
136
- end
137
- def test_hash_with_value_array_crosscheck_wrong_value2
138
- assert_raises(MiniTest::Assertion) do
139
- assert_json '{"key":["value1","value2"]}' do |json|
140
- json.element 'key', ['value1', 'wrong_value2']
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
- end
144
-
145
- def test_hash_with_array_of_hashes
146
- assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
147
- json.element 'key' do
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
- end
153
- def test_hash_with_array_of_hashes_crosscheck_inner_key
154
- assert_raises(MiniTest::Assertion) do
155
- assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
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
- def test_hash_with_array_of_hashes_crosscheck_inner_value
163
- assert_raises(MiniTest::Assertion) do
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', 'wrong_value1'
162
+ json.element 'inner_key1', 'value1'
163
+ json.element 'inner_key2', 'value2'
167
164
  end
168
165
  end
169
166
  end
170
- end
171
-
172
- def test_array_with_two_hashes
173
- assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
174
- json.element 'key1', 'value1'
175
- json.element 'key2', 'value2'
176
- end
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
- end
185
- def test_array_with_two_hashes_crosscheck
186
- assert_raises(MiniTest::Assertion) do
187
- assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
188
- json.element 'wrong_key1', 'value1'
189
- json.element 'key2', 'value2'
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
- assert_raises(MiniTest::Assertion) do
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', 'wrong_value2'
189
+ json.element 'key2', 'value2'
196
190
  end
197
191
  end
198
- end
199
-
200
- def test_nested_hashes
201
- assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
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
- end
209
- def test_nested_hashes_crosscheck
210
- assert_raises(MiniTest::Assertion) do
211
- assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
212
- json.element 'wrong_outer_key'
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
- assert_raises(MiniTest::Assertion) do
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', 'wrong_value'
220
+ json.element 'inner_key', 'value'
220
221
  end
221
222
  end
222
223
  end
223
224
  end
224
- end
225
-
226
- def test_real_xws
227
- assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do |json|
228
- json.element 'contact_request' do
229
- json.element 'sender_id', '3'
230
- json.element 'receiver_id', '2'
231
- json.element 'id', 1
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
- assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do |json|
236
- json.element 'private_message' do
237
- json.element 'sender' do
238
- json.element 'display_name', 'first last'
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
- def test_not_enough_elements_in_array
247
- assert_raises(MiniTest::Assertion) do
248
- assert_json '["one","two"]' do |json|
249
- json.element "one"
250
- json.element "two"
251
- json.element "three"
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
- def test_not_enough_elements_in_hash_array
257
- assert_raises(MiniTest::Assertion) do
258
- assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do |json|
259
- json.element 'key' do
260
- json.element 'key1', 'value1'
261
- json.element 'key2', 'value2'
262
- json.element 'key3'
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