/
matchers_spec.rb
321 lines (258 loc) · 10 KB
/
matchers_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
require "spec_helper"
describe "Matchers:" do
context "be_json_eql" do
it "matches identical JSON" do
%({"json":"spec"}).should be_json_eql(%({"json":"spec"}))
end
it "matches differently-formatted JSON" do
%({"json": "spec"}).should be_json_eql(%({"json":"spec"}))
end
it "matches out-of-order hashes" do
%({"laser":"lemon","json":"spec"}).should be_json_eql(%({"json":"spec","laser":"lemon"}))
end
it "doesn't match out-of-order arrays" do
%(["json","spec"]).should_not be_json_eql(%(["spec","json"]))
end
it "matches valid JSON values, yet invalid JSON documents" do
%("json_spec").should be_json_eql(%("json_spec"))
end
it "matches at a path" do
%({"json":["spec"]}).should be_json_eql(%("spec")).at_path("json/0")
end
it "ignores excluded-by-default hash keys" do
JsonSpec.excluded_keys.should_not be_empty
actual = expected = {"json" => "spec"}
JsonSpec.excluded_keys.each{|k| actual[k] = k }
actual.to_json.should be_json_eql(expected.to_json)
end
it "ignores custom excluded hash keys" do
JsonSpec.exclude_keys("ignore")
%({"json":"spec","ignore":"please"}).should be_json_eql(%({"json":"spec"}))
end
it "ignores nested, excluded hash keys" do
JsonSpec.exclude_keys("ignore")
%({"json":"spec","please":{"ignore":"this"}}).should be_json_eql(%({"json":"spec","please":{}}))
end
it "ignores hash keys when included in the expected value" do
JsonSpec.exclude_keys("ignore")
%({"json":"spec","ignore":"please"}).should be_json_eql(%({"json":"spec","ignore":"this"}))
end
it "doesn't match Ruby-equivalent, JSON-inequivalent values" do
%({"one":1}).should_not be_json_eql(%({"one":1.0}))
end
it "matches different looking, JSON-equivalent values" do
%({"ten":10.0}).should be_json_eql(%({"ten":1e+1}))
end
it "excludes extra hash keys per matcher" do
JsonSpec.excluded_keys = %w(ignore)
%({"id":1,"json":"spec","ignore":"please"}).should be_json_eql(%({"id":2,"json":"spec","ignore":"this"})).excluding("id")
end
it "excludes extra hash keys given as symbols" do
JsonSpec.excluded_keys = []
%({"id":1,"json":"spec"}).should be_json_eql(%({"id":2,"json":"spec"})).excluding(:id)
end
it "excludes multiple keys" do
JsonSpec.excluded_keys = []
%({"id":1,"json":"spec"}).should be_json_eql(%({"id":2,"json":"different"})).excluding(:id, :json)
end
it "includes globally-excluded hash keys per matcher" do
JsonSpec.excluded_keys = %w(id ignore)
%({"id":1,"json":"spec","ignore":"please"}).should_not be_json_eql(%({"id":2,"json":"spec","ignore":"this"})).including("id")
end
it "includes globally-included hash keys given as symbols" do
JsonSpec.excluded_keys = %w(id)
%({"id":1,"json":"spec"}).should_not be_json_eql(%({"id":2,"json":"spec"})).including(:id)
end
it "includes multiple keys" do
JsonSpec.excluded_keys = %w(id json)
%({"id":1,"json":"spec"}).should_not be_json_eql(%({"id":2,"json":"different"})).including(:id, :json)
end
it "provide a description message" do
matcher = be_json_eql(%({"id":2,"json":"spec"}))
matcher.matches?(%({"id":1,"json":"spec"}))
matcher.description.should == "equal JSON"
end
it "provide a description message with path" do
matcher = be_json_eql(%({"id":1,"json":["spec"]})).at_path("json/0")
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(equal JSON at path "json/0")
end
end
context "include_json" do
it "matches included array elements" do
json = %(["one",1,1.0,true,false,null])
json.should include_json(%("one"))
json.should include_json(%(1))
json.should include_json(%(1.0))
json.should include_json(%(true))
json.should include_json(%(false))
json.should include_json(%(null))
end
it "matches an array included in an array" do
json = %([[1,2,3],[4,5,6]])
json.should include_json(%([1,2,3]))
json.should include_json(%([4,5,6]))
end
it "matches a hash included in an array" do
json = %([{"one":1},{"two":2}])
json.should include_json(%({"one":1}))
json.should include_json(%({"two":2}))
end
it "matches include hash values" do
json = %({"string":"one","integer":1,"float":1.0,"true":true,"false":false,"null":null})
json.should include_json(%("one"))
json.should include_json(%(1))
json.should include_json(%(1.0))
json.should include_json(%(true))
json.should include_json(%(false))
json.should include_json(%(null))
end
it "matches a hash included in a hash" do
json = %({"one":{"two":3},"four":{"five":6}})
json.should include_json(%({"two":3}))
json.should include_json(%({"five":6}))
end
it "matches an array included in a hash" do
json = %({"one":[2,3],"four":[5,6]})
json.should include_json(%([2,3]))
json.should include_json(%([5,6]))
end
it "matches at a path" do
%({"one":{"two":[3,4]}}).should include_json(%([3,4])).at_path("one")
end
it "ignores excluded keys" do
%([{"id":1,"two":3}]).should include_json(%({"two":3}))
end
it "provide a description message" do
matcher = include_json(%({"json":"spec"}))
matcher.matches?(%({"id":1,"json":"spec"}))
matcher.description.should == "include JSON"
end
it "provide a description message with path" do
matcher = include_json(%("spec")).at_path("json/0")
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(include JSON at path "json/0")
end
end
context "have_json_size" do
it "counts array entries" do
%([1,2,3]).should have_json_size(3)
end
it "counts null array entries" do
%([1,null,3]).should have_json_size(3)
end
it "counts hash key/value pairs" do
%({"one":1,"two":2,"three":3}).should have_json_size(3)
end
it "counts null hash values" do
%({"one":1,"two":null,"three":3}).should have_json_size(3)
end
it "matches at a path" do
%({"one":[1,2,3]}).should have_json_size(3).at_path("one")
end
it "provides a failure message for should" do
matcher = have_json_size(3)
matcher.matches?(%([1,2]))
matcher.failure_message_for_should.should == "Expected JSON value size to be 3, got 2"
end
it "provides a failure message for should not" do
matcher = have_json_size(3)
matcher.matches?(%([1,2,3]))
matcher.failure_message_for_should_not.should == "Expected JSON value size to not be 3, got 3"
end
it "provide a description message" do
matcher = have_json_size(1)
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(have JSON size "1")
end
it "provide a description message with path" do
matcher = have_json_size(1).at_path("json")
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(have JSON size "1" at path "json")
end
end
context "have_json_path" do
it "matches hash keys" do
%({"one":{"two":{"three":4}}}).should have_json_path("one/two/three")
end
it "doesn't match values" do
%({"one":{"two":{"three":4}}}).should_not have_json_path("one/two/three/4")
end
it "matches array indexes" do
%([1,[1,2,[1,2,3,4]]]).should have_json_path("1/2/3")
end
it "respects null array values" do
%([null,[null,null,[null,null,null,null]]]).should have_json_path("1/2/3")
end
it "matches hash keys and array indexes" do
%({"one":[1,2,{"three":4}]}).should have_json_path("one/2/three")
end
it "provide a description message" do
matcher = have_json_path("json")
matcher.matches?(%({"id":1,"json":"spec"}))
matcher.description.should == %(have JSON path "json")
end
end
context "have_json_type" do
it "matches hashes" do
%({}).should have_json_type(Hash)
end
it "matches arrays" do
%([]).should have_json_type(Array)
end
it "matches at a path" do
%({"root":[]}).should have_json_type(Array).at_path("root")
end
it "matches strings" do
%(["json_spec"]).should have_json_type(String).at_path("0")
end
it "matches a valid JSON value, yet invalid JSON document" do
%("json_spec").should have_json_type(String)
end
it "matches empty strings" do
%("").should have_json_type(String)
end
it "matches integers" do
%(10).should have_json_type(Integer)
end
it "matches floats" do
%(10.0).should have_json_type(Float)
%(1e+1).should have_json_type(Float)
end
it "matches ancestor classes" do
%(10).should have_json_type(Numeric)
%(10.0).should have_json_type(Numeric)
end
it "provides a failure message for should" do
matcher = have_json_type(Numeric)
matcher.matches?(%("foo"))
matcher.failure_message_for_should.should == "Expected JSON value type to be Numeric, got String"
end
it "provides a failure message for should not" do
matcher = have_json_type(Numeric)
matcher.matches?(%(10))
matcher.failure_message_for_should_not.should == "Expected JSON value type to not be Numeric, got Fixnum"
end
it "provide a description message" do
matcher = have_json_type(String)
matcher.matches?(%({"id":1,"json":"spec"}))
matcher.description.should == %(have JSON type "String")
end
it "provide a description message with path" do
matcher = have_json_type(String).at_path("json")
matcher.matches?(%({"id":1,"json":"spec"}))
matcher.description.should == %(have JSON type "String" at path "json")
end
context "somewhat uselessly" do
it "matches true" do
%(true).should have_json_type(TrueClass)
end
it "matches false" do
%(false).should have_json_type(FalseClass)
end
it "matches null" do
%(null).should have_json_type(NilClass)
end
end
end
end