-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
443 lines (385 loc) · 14.7 KB
/
json_test.go
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package nosj_test
import (
"reflect"
"github.com/totherme/nosj"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("JSON", func() {
var rawjson string
BeforeEach(func() {
rawjson = `{"name": "fred",
"othernames": [
"alice",
"bob",
"ezekiel"
],
"life": 42,
"things": {
"more": "things"
},
"beauty": true,
"not": null
}`
})
It("looks the same, whether it's JSON or YAML", func() {
rawyaml := `
name: "fred"
othernames:
- "alice"
- "bob"
- "ezekiel"
life: 42
things:
more: "things"
beauty: true
not: null
`
json, err := nosj.ParseJSON(rawjson)
Expect(err).NotTo(HaveOccurred())
yaml, err := nosj.ParseYAML(rawyaml)
Expect(err).NotTo(HaveOccurred())
Expect(json).To(BeEquivalentTo(yaml))
})
Context("when my JSON represents an object", func() {
var err error
var json nosj.JSON
BeforeEach(func() {
json, err = nosj.ParseJSON(rawjson)
})
It("parses the json successfully", func() {
Expect(err).NotTo(HaveOccurred())
})
It("tells me that my json represents an object", func() {
Expect(json.IsOb()).To(BeTrue(), "this json represents an object")
simpleObJson, err := nosj.ParseJSON(`{"string":1 , "otherstring":2}`)
Expect(err).NotTo(HaveOccurred())
Expect(simpleObJson.IsOb()).To(BeTrue(), "this json represents an object")
})
It("can get that object", func() {
obVal := json.ObValue()
Expect(obVal).To(HaveLen(6))
Expect(obVal).To(HaveKey("name"))
Expect(obVal).To(HaveKey("othernames"))
Expect(obVal).To(HaveKey("life"))
Expect(obVal).To(HaveKey("things"))
Expect(obVal).To(HaveKey("beauty"))
Expect(obVal).To(HaveKey("not"))
})
It("can update fields on that object", func() {
json.SetField("name", "david")
Expect(json.F("name").StringValue()).To(Equal("david"))
})
It("can add fields to that object", func() {
json.SetField("newfield", "new value")
Expect(json.F("newfield").StringValue()).To(Equal("new value"))
})
It("tells me it doesn't represent anything else", func() {
Expect(json.IsString()).To(BeFalse(), "not a string")
Expect(json.IsNum()).To(BeFalse(), "not a number")
Expect(json.IsBool()).To(BeFalse(), "not a bool")
Expect(json.IsList()).To(BeFalse(), "not a list")
Expect(json.IsNull()).To(BeFalse(), "not null")
})
It("tells me that extant keys exist, and others do not", func() {
Expect(json.HasKey("name")).To(BeTrue(), "the name key should exist")
Expect(json.HasKey("othernames")).To(BeTrue(), "the othernames key should exist")
Expect(json.HasKey("life")).To(BeTrue(), "the life key should exist")
Expect(json.HasKey("wat?")).To(BeFalse(), "the wat key should not exist")
})
It("tells me that extant pointers exist, and others do not", func() {
Expect(json.HasPointer("/name")).To(BeTrue(), "the pointer should exist")
Expect(json.HasPointer("/life")).To(BeTrue(), "the pointer should exist")
Expect(json.HasPointer("/wat?")).To(BeFalse(), "the pointer should not exist")
Expect(json.HasPointer("/things/more")).To(BeTrue(), "the pointer should exist")
Expect(json.HasPointer("/not/there")).To(BeFalse(), "the pointer should not exist")
})
Context("when we pass an invalid pointer", func() {
It("returns a helpful error message", func() {
_, err = json.HasPointer("invalid/pointer")
Expect(err).To(MatchError(ContainSubstring("JSON pointer must be empty or start with a \"/\"")))
})
})
It("can get an extant key", func() {
newJson := json.GetField("things")
Expect(newJson.IsOb()).To(BeTrue(), "the inner object is also an object")
Expect(newJson.HasKey("more")).To(BeTrue(), "the inner object has the 'more' field")
})
It("can chain extant keys", func() {
Expect(json.GetField("things").GetField("more").StringValue()).To(Equal("things"))
})
It("can get by pointer", func() {
got, err := json.GetByPointer("/things/more")
Expect(err).NotTo(HaveOccurred())
Expect(got.StringValue()).To(Equal("things"))
})
DescribeTable("F and GetByPointer both mirror GetField for single-level paths", func(key string) {
Expect(json.F(key)).To(Equal(json.GetField(key)))
Expect(json.GetByPointer("/" + key)).To(Equal(json.GetField(key)))
},
Entry("existing object key", "things"),
Entry("existing string key", "name"),
Entry("existing list key", "othernames"),
Entry("existing number key", "life"),
Entry("existing boolean key", "beauty"),
Entry("existing null key", "not"),
)
Context("when I try to get a key that doesn't exist", func() {
It("panics", func() {
Expect(func() { json.GetField("oh noe!") }).To(Panic())
Expect(func() { json.F("oh noe!") }).To(Panic())
})
})
Context("when I try to do non-objectey things with it", func() {
It("panics", func() {
Expect(func() { json.StringValue() }).To(Panic())
Expect(func() { json.NumValue() }).To(Panic())
Expect(func() { json.BoolValue() }).To(Panic())
Expect(func() { json.ListValue() }).To(Panic())
Expect(json.SetElem(0, "some-value")).To(MatchError(ContainSubstring("not a list")))
})
})
Describe("error handling of GetByPointer", func() {
Context("when we pass a pointer that is invalid", func() {
It("returns a helpful error message", func() {
_, err = json.GetByPointer("not/starting/with/slash")
Expect(err).To(MatchError(ContainSubstring("JSON pointer must be empty or start with a \"/\"")))
})
})
Context("when we pass a pointer to a non-existing key", func() {
It("returns a helpful error message", func() {
_, err = json.GetByPointer("/not/there")
Expect(err).To(MatchError(ContainSubstring("Invalid token reference")))
})
})
})
It("has a raw value equal to the parsed JSON", func() {
Expect(json.RawValue()).To(HaveLen(6))
Expect(json.RawValue()).To(HaveKey("name"))
Expect(json.RawValue().(map[string]interface{})["name"]).To(Equal("fred"))
Expect(json.RawValue()).To(HaveKey("othernames"))
Expect(json.RawValue().(map[string]interface{})["othernames"]).To(HaveLen(3))
Expect(json.RawValue().(map[string]interface{})["othernames"]).To(ContainElement("alice"))
Expect(json.RawValue().(map[string]interface{})["othernames"]).To(ContainElement("bob"))
Expect(json.RawValue().(map[string]interface{})["othernames"]).To(ContainElement("ezekiel"))
Expect(json.RawValue()).To(HaveKey("life"))
Expect(json.RawValue().(map[string]interface{})["life"]).To(Equal(42.0))
Expect(json.RawValue()).To(HaveKey("things"))
Expect(json.RawValue().(map[string]interface{})["things"]).To(Equal(map[string]interface{}{
"more": "things",
}))
Expect(json.RawValue()).To(HaveKey("beauty"))
Expect(json.RawValue().(map[string]interface{})["beauty"]).To(Equal(true))
Expect(json.RawValue()).To(HaveKey("not"))
Expect(json.RawValue().(map[string]interface{})["not"]).To(BeNil())
})
})
Context("when my json represents a string", func() {
var json nosj.JSON
var err error
BeforeEach(func() {
json, err = nosj.ParseJSON(`"this is a string"`)
Expect(err).NotTo(HaveOccurred())
})
It("tells me my json represents a string", func() {
Expect(json.IsString()).To(BeTrue(), "this json represents a string")
})
It("tells me it doesn't represent anything else", func() {
Expect(json.IsOb()).To(BeFalse(), "not an object")
Expect(json.IsNum()).To(BeFalse(), "not a number")
Expect(json.IsBool()).To(BeFalse(), "not a bool")
Expect(json.IsList()).To(BeFalse(), "not a list")
Expect(json.IsNull()).To(BeFalse(), "not null")
})
It("can get that string", func() {
Expect(json.StringValue()).To(Equal("this is a string"))
Expect(json.RawValue()).To(Equal("this is a string"))
})
Context("when I try to do non-string things", func() {
It("panics", func() {
Expect(func() { json.HasKey("wat?") }).To(Panic())
Expect(func() { json.GetField("oh noe!") }).To(Panic())
Expect(func() { json.NumValue() }).To(Panic())
Expect(func() { json.BoolValue() }).To(Panic())
Expect(func() { json.ListValue() }).To(Panic())
})
})
})
Context("when my json represents a number", func() {
var json nosj.JSON
var err error
BeforeEach(func() {
json, err = nosj.ParseJSON(`3.141`)
Expect(err).NotTo(HaveOccurred())
})
It("tells me my json represents a number", func() {
Expect(err).NotTo(HaveOccurred())
Expect(json.IsNum()).To(BeTrue(), "this is a number")
})
It("tells me it doesn't represent anything else", func() {
Expect(json.IsOb()).To(BeFalse(), "not an object")
Expect(json.IsString()).To(BeFalse(), "not a string")
Expect(json.IsBool()).To(BeFalse(), "not a bool")
Expect(json.IsList()).To(BeFalse(), "not a list")
Expect(json.IsNull()).To(BeFalse(), "not null")
})
It("can get that number", func() {
Expect(json.NumValue()).To(BeNumerically("==", 3.141))
Expect(json.RawValue()).To(BeNumerically("==", 3.141))
})
Context("when I try to do non-number things", func() {
It("panics", func() {
Expect(func() { json.HasKey("wat?") }).To(Panic())
Expect(func() { json.GetField("oh noe!") }).To(Panic())
Expect(func() { json.StringValue() }).To(Panic())
Expect(func() { json.BoolValue() }).To(Panic())
Expect(func() { json.ListValue() }).To(Panic())
})
})
})
Context("when my json represents a bool", func() {
var json nosj.JSON
var err error
BeforeEach(func() {
json, err = nosj.ParseJSON(`true`)
Expect(err).NotTo(HaveOccurred())
})
It("tells me my json represents a bool", func() {
Expect(err).NotTo(HaveOccurred())
Expect(json.IsBool()).To(BeTrue(), "this is a bool")
})
It("tells me it doesn't represent anything else", func() {
Expect(json.IsOb()).To(BeFalse(), "not an object")
Expect(json.IsString()).To(BeFalse(), "not a string")
Expect(json.IsNum()).To(BeFalse(), "not a number")
Expect(json.IsList()).To(BeFalse(), "not a list")
Expect(json.IsNull()).To(BeFalse(), "not null")
})
It("can get that bool", func() {
Expect(json.BoolValue()).To(BeTrue(), "actually should be the value 'true'")
Expect(json.RawValue()).To(BeTrue(), "actually should be the value 'true'")
})
Context("when I try to do non-bool things", func() {
It("panics", func() {
Expect(func() { json.HasKey("wat?") }).To(Panic())
Expect(func() { json.GetField("oh noe!") }).To(Panic())
Expect(func() { json.StringValue() }).To(Panic())
Expect(func() { json.NumValue() }).To(Panic())
Expect(func() { json.ListValue() }).To(Panic())
})
})
})
Context("when my json represents a list", func() {
var json nosj.JSON
var err error
BeforeEach(func() {
json, err = nosj.ParseJSON(`[true, 32, {"this":"that"}]`)
Expect(err).NotTo(HaveOccurred())
})
It("tells me my json represents a list", func() {
Expect(err).NotTo(HaveOccurred())
Expect(json.IsList()).To(BeTrue(), "this is a list")
})
It("tells me it doesn't represent anything else", func() {
Expect(json.IsOb()).To(BeFalse(), "not an object")
Expect(json.IsString()).To(BeFalse(), "not a string")
Expect(json.IsNum()).To(BeFalse(), "not a number")
Expect(json.IsBool()).To(BeFalse(), "not a bool")
Expect(json.IsNull()).To(BeFalse(), "not null")
})
It("can get that list", func() {
Expect(json.ListValue()).To(HaveLen(3))
Expect(reflect.TypeOf(json.ListValue()[0])).To(Equal(reflect.TypeOf(json)))
Expect(json.ListValue()[0].IsBool()).To(BeTrue())
Expect(json.ListValue()[1].IsNum()).To(BeTrue())
})
It("can get that list in raw form", func() {
Expect(json.RawValue()).To(HaveLen(3))
Expect(reflect.TypeOf(json.RawValue())).To(Equal(reflect.TypeOf([]interface{}{})))
Expect(reflect.TypeOf(json.RawValue().([]interface{})[0])).To(Equal(reflect.TypeOf(true)))
Expect(json.RawValue().([]interface{})[0]).To(BeTrue())
Expect(json.RawValue().([]interface{})[1]).To(Equal(32.0))
})
It("can set items in that list", func() {
err := json.SetElem(1, "badgers")
Expect(err).NotTo(HaveOccurred())
Expect(json.ListValue()[1].StringValue()).To(Equal("badgers"))
})
Context("when I try to do non-list things", func() {
It("panics", func() {
Expect(func() { json.HasKey("wat?") }).To(Panic())
Expect(func() { json.GetField("oh noe!") }).To(Panic())
Expect(func() { json.StringValue() }).To(Panic())
Expect(func() { json.NumValue() }).To(Panic())
Expect(func() { json.BoolValue() }).To(Panic())
})
})
})
Context("when my json represents null", func() {
var json nosj.JSON
var err error
BeforeEach(func() {
json, err = nosj.ParseJSON(`null`)
Expect(err).NotTo(HaveOccurred())
})
It("tells me my json represents null", func() {
Expect(json.IsNull()).To(BeTrue(), "this is null")
})
It("has raw value nil", func() {
Expect(json.RawValue()).To(BeNil())
})
It("tells me it doesn't represent anything else", func() {
Expect(json.IsOb()).To(BeFalse(), "not an object")
Expect(json.IsString()).To(BeFalse(), "not a string")
Expect(json.IsNum()).To(BeFalse(), "not a number")
Expect(json.IsBool()).To(BeFalse(), "not a bool")
Expect(json.IsList()).To(BeFalse(), "not a list")
})
Context("when I try to do ...well... things", func() {
It("panics", func() {
Expect(func() { json.HasKey("wat?") }).To(Panic())
Expect(func() { json.GetField("oh noe!") }).To(Panic())
Expect(func() { json.StringValue() }).To(Panic())
Expect(func() { json.NumValue() }).To(Panic())
Expect(func() { json.BoolValue() }).To(Panic())
Expect(func() { json.ListValue() }).To(Panic())
})
})
})
Context("when I look at some invalid JSON", func() {
It("returns a helpful error", func() {
_, err := nosj.ParseJSON("this isn't even slightly json")
Expect(err).To(MatchError(ContainSubstring("parse error")))
})
})
Describe("the IsOfType convenience method", func() {
var json nosj.JSON
BeforeEach(func() {
var err error
json, err = nosj.ParseJSON(rawjson)
Expect(err).NotTo(HaveOccurred())
})
DescribeTable("IsOfType does the same as the individual type methods", func(key string) {
field := json.F(key)
Expect(field.IsOfType(nosj.JSONOb)).To(Equal(field.IsOb()))
Expect(field.IsOfType(nosj.JSONString)).To(Equal(field.IsString()))
Expect(field.IsOfType(nosj.JSONList)).To(Equal(field.IsList()))
Expect(field.IsOfType(nosj.JSONNum)).To(Equal(field.IsNum()))
Expect(field.IsOfType(nosj.JSONBool)).To(Equal(field.IsBool()))
Expect(field.IsOfType(nosj.JSONNull)).To(Equal(field.IsNull()))
},
Entry("an object key", "things"),
Entry("an string key", "name"),
Entry("an list key", "othernames"),
Entry("an number key", "life"),
Entry("an boolean key", "beauty"),
Entry("a null key", "not"),
)
Context("when we give a string that isn't a JSON type", func() {
It("panics", func() {
Expect(func() { json.IsOfType("badgers") }).To(Panic())
})
})
})
})