forked from buger/jsonparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
515 lines (485 loc) · 11.9 KB
/
parser_test.go
File metadata and controls
515 lines (485 loc) · 11.9 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package jsonparser
import (
"bytes"
_ "fmt"
"reflect"
"testing"
)
// Set it to non-empty value if want to run only specific test
var activeTest = ""
func toArray(data []byte) (result [][]byte) {
ArrayEach(data, func(value []byte, dataType int, offset int, err error) {
result = append(result, value)
})
return
}
func toStringArray(data []byte) (result []string) {
ArrayEach(data, func(value []byte, dataType int, offset int, err error) {
result = append(result, string(value))
})
return
}
type Test struct {
desc string
json string
path []string
isErr bool
isFound bool
data interface{}
}
var getTests = []Test{
// Found key tests
Test{
desc: "handling multiple nested keys with same name",
json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`,
path: []string{"c", "c"},
isFound: true,
data: `[1,2]`,
},
Test{
desc: "read basic key",
json: `{"a":"b"}`,
path: []string{"a"},
isFound: true,
data: `b`,
},
Test{
desc: "read basic key with space",
json: `{"a": "b"}`,
path: []string{"a"},
isFound: true,
data: `b`,
},
Test{
desc: "read composite key",
json: `{"a": { "b":{"c":"d" }}}`,
path: []string{"a", "b", "c"},
isFound: true,
data: `d`,
},
Test{
desc: `read numberic value as string`,
json: `{"a": "b", "c": 1}`,
path: []string{"c"},
isFound: true,
data: `1`,
},
Test{
desc: `handle multiple nested keys with same name`,
json: `{"a":[{"b":1},{"b":2},3],"c":{"c":[1,2]}} }`,
path: []string{"c", "c"},
isFound: true,
data: `[1,2]`,
},
Test{
desc: `read string values with quotes`,
json: `{"a": "string\"with\"quotes"}`,
path: []string{"a"},
isFound: true,
data: `string\"with\"quotes`,
},
Test{
desc: `read object`,
json: `{"a": { "b":{"c":"d" }}}`,
path: []string{"a", "b"},
isFound: true,
data: `{"c":"d" }`,
},
Test{
desc: `empty path`,
json: `{"c":"d" }`,
path: []string{},
isFound: true,
data: `{"c":"d" }`,
},
Test{
desc: `formatted JSON value`,
json: "{\n \"a\": \"b\"\n}",
path: []string{"a"},
isFound: true,
data: `b`,
},
Test{
desc: `formatted JSON value 2`,
json: "{\n \"a\":\n {\n\"b\":\n {\"c\":\"d\",\n\"e\": \"f\"}\n}\n}",
path: []string{"a", "b"},
isFound: true,
data: "{\"c\":\"d\",\n\"e\": \"f\"}",
},
Test{
desc: `whitespace`,
json: " \n\r\t{ \n\r\t\"whitespace\" \n\r\t: \n\r\t333 \n\r\t} \n\r\t",
path: []string{"whitespace"},
isFound: true,
data: "333",
},
Test{
desc: `escaped backslash quote`,
json: `{"a": "\\\""}`,
path: []string{"a"},
isFound: true,
data: `\\\"`,
},
Test{
desc: `unescaped backslash quote`,
json: `{"a": "\\"}`,
path: []string{"a"},
isFound: true,
data: `\\`,
},
Test{
desc: `unicode in JSON`,
json: `{"a": "15°C"}`,
path: []string{"a"},
isFound: true,
data: `15°C`,
},
// Not found key tests
Test{
desc: "non-existent key 1",
json: `{"a":"b"}`,
path: []string{"c"},
isFound: false,
isErr: true,
},
Test{
desc: "non-existent key 2",
json: `{"a":"b"}`,
path: []string{"b"},
isFound: false,
isErr: true,
},
Test{
desc: "non-existent key 3",
json: `{"aa":"b"}`,
path: []string{"a"},
isFound: false,
isErr: true,
},
Test{
desc: "apply scope of parent when search for nested key",
json: `{"a": { "b": 1}, "c": 2 }`,
path: []string{"a", "b", "c"},
isFound: false,
isErr: true,
},
Test{
desc: `apply scope to key level`,
json: `{"a": { "b": 1}, "c": 2 }`,
path: []string{"b"},
isFound: false,
isErr: true,
},
Test{
desc: `handle escaped quote in key name in JSON`,
json: `{"key\"key": 1}`,
path: []string{"key"},
isFound: false,
isErr: true,
},
// Error/invalid tests
Test{
desc: `handle escaped quote in key name in JSON`,
json: `{"key\"key": 1}`,
path: []string{"key"},
isFound: false,
isErr: true,
},
Test{
desc: `missing closing brace, but can still find key`,
json: `{"a":"b"`,
path: []string{"a"},
isFound: true,
data: `b`,
},
Test{
desc: `missing value closing quote`,
json: `{"a":"b`,
path: []string{"a"},
isErr: true,
},
Test{
desc: `missing value closing curly brace`,
json: `{"a": { "b": "c"`,
path: []string{"a"},
isErr: true,
},
Test{
desc: `missing value closing square bracket`,
json: `{"a": [1, 2, 3 }`,
path: []string{"a"},
isErr: true,
},
Test{
desc: `missing value 1`,
json: `{"a":`,
path: []string{"a"},
isErr: true,
},
Test{
desc: `missing value 2`,
json: `{"a": `,
path: []string{"a"},
isErr: true,
},
Test{
desc: `missing value 3`,
json: `{"a":}`,
path: []string{"a"},
isErr: true,
},
Test{
desc: "malformed key (followed by comma followed by colon)",
json: `{"a",:1}`,
path: []string{"a"},
isErr: true,
},
}
var getIntTests = []Test{
Test{
desc: `read numeric value as number`,
json: `{"a": "b", "c": 1}`,
path: []string{"c"},
isFound: true,
data: int64(1),
},
Test{
desc: `read numeric value as number in formatted JSON`,
json: "{\"a\": \"b\", \"c\": 1 \n}",
path: []string{"c"},
isFound: true,
data: int64(1),
},
}
var getFloatTests = []Test{
Test{
desc: `read numeric value as number`,
json: `{"a": "b", "c": 1.123}`,
path: []string{"c"},
isFound: true,
data: float64(1.123),
},
Test{
desc: `read numeric value as number in formatted JSON`,
json: "{\"a\": \"b\", \"c\": 23.41323 \n}",
path: []string{"c"},
isFound: true,
data: float64(23.41323),
},
}
var getStringTests = []Test{
Test{
desc: `Translate unicode symbols`,
json: `{"c": "test"}`,
path: []string{"c"},
isFound: true,
data: `test`,
},
Test{
desc: `Translate unicode symbols`,
json: `{"c": "15\u00b0C"}`,
path: []string{"c"},
isFound: true,
data: `15°C`,
},
Test{
desc: `Translate escape symbols`,
json: `{"c": "\\\""}`,
path: []string{"c"},
isFound: true,
data: `\"`,
},
}
var getBoolTests = []Test{
Test{
desc: `read boolean true as boolean`,
json: `{"a": "b", "c": true}`,
path: []string{"c"},
isFound: true,
data: true,
},
Test{
desc: `boolean true in formatted JSON`,
json: "{\"a\": \"b\", \"c\": true \n}",
path: []string{"c"},
isFound: true,
data: true,
},
Test{
desc: `read boolean false as boolean`,
json: `{"a": "b", "c": false}`,
path: []string{"c"},
isFound: true,
data: false,
},
Test{
desc: `boolean true in formatted JSON`,
json: "{\"a\": \"b\", \"c\": false \n}",
path: []string{"c"},
isFound: true,
data: false,
},
Test{
desc: `read fake boolean true`,
json: `{"a": txyz}`,
path: []string{"a"},
isErr: true,
},
Test{
desc: `read fake boolean false`,
json: `{"a": fwxyz}`,
path: []string{"a"},
isErr: true,
},
Test{
desc: `read boolean true with whitespace and another key`,
json: "{\r\t\n \"a\"\r\t\n :\r\t\n true\r\t\n ,\r\t\n \"b\": 1}",
path: []string{"a"},
isFound: true,
data: true,
},
}
var getArrayTests = []Test{
Test{
desc: `read array of simple values`,
json: `{"a": { "b":[1,2,3,4]}}`,
path: []string{"a", "b"},
isFound: true,
data: []string{`1`, `2`, `3`, `4`},
},
Test{
desc: `read array via empty path`,
json: `[1,2,3,4]`,
path: []string{},
isFound: true,
data: []string{`1`, `2`, `3`, `4`},
},
Test{
desc: `read array of objects`,
json: `{"a": { "b":[{"x":1},{"x":2},{"x":3},{"x":4}]}}`,
path: []string{"a", "b"},
isFound: true,
data: []string{`{"x":1}`, `{"x":2}`, `{"x":3}`, `{"x":4}`},
},
Test{
desc: `read nested array`,
json: `{"a": [[[1]],[[2]]]}`,
path: []string{"a"},
isFound: true,
data: []string{`[[1]]`, `[[2]]`},
},
}
// checkFoundAndNoError checks the dataType and error return from Get*() against the test case expectations.
// Returns true the test should proceed to checking the actual data returned from Get*(), or false if the test is finished.
func checkFoundAndNoError(t *testing.T, testKind string, test Test, jtype int, value interface{}, err error) bool {
isFound := (jtype != NotExist)
isErr := (err != nil)
if test.isErr != isErr {
// If the call didn't match the error expectation, fail
t.Errorf("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Value: %v", testKind, test.desc, test.isErr, isErr, err, value)
return false
} else if isErr {
// Else, if there was an error, don't fail and don't check isFound or the value
return false
} else if test.isFound != isFound {
// Else, if the call didn't match the is-found expectation, fail
t.Errorf("%s test '%s' isFound mismatch: expected %t, obtained %t", testKind, test.desc, test.isFound, isFound)
return false
} else if !isFound {
// Else, if no value was found, don't fail and don't check the value
return false
} else {
// Else, there was no error and a value was found, so check the value
return true
}
}
func runTests(t *testing.T, tests []Test, runner func(Test) (interface{}, int, error), typeChecker func(Test, interface{}) (bool, interface{})) {
for _, test := range tests {
if activeTest != "" && test.desc != activeTest {
continue
}
// fmt.Println("Running:", test.desc)
value, dataType, err := runner(test)
if checkFoundAndNoError(t, "Get()", test, dataType, value, err) {
if test.data == nil {
t.Errorf("MALFORMED TEST: %v", test)
continue
}
if ok, expected := typeChecker(test, value); !ok {
t.Errorf("Test '%s' expected to return value %v, but did returned %v instead", test.desc, expected, value)
}
}
}
}
func TestGet(t *testing.T) {
runTests(t, getTests,
func(test Test) (value interface{}, dataType int, err error) {
value, dataType, _, err = Get([]byte(test.json), test.path...)
return
},
func(test Test, value interface{}) (bool, interface{}) {
expected := []byte(test.data.(string))
return bytes.Equal(expected, value.([]byte)), expected
},
)
}
func TestGetString(t *testing.T) {
runTests(t, getStringTests,
func(test Test) (value interface{}, dataType int, err error) {
value, err = GetString([]byte(test.json), test.path...)
return value, String, err
},
func(test Test, value interface{}) (bool, interface{}) {
expected := test.data.(string)
return expected == value.(string), expected
},
)
}
func TestGetInt(t *testing.T) {
runTests(t, getIntTests,
func(test Test) (value interface{}, dataType int, err error) {
value, err = GetInt([]byte(test.json), test.path...)
return value, Number, err
},
func(test Test, value interface{}) (bool, interface{}) {
expected := test.data.(int64)
return expected == value.(int64), expected
},
)
}
func TestGetFloat(t *testing.T) {
runTests(t, getFloatTests,
func(test Test) (value interface{}, dataType int, err error) {
value, err = GetFloat([]byte(test.json), test.path...)
return value, Number, err
},
func(test Test, value interface{}) (bool, interface{}) {
expected := test.data.(float64)
return expected == value.(float64), expected
},
)
}
func TestGetBoolean(t *testing.T) {
runTests(t, getBoolTests,
func(test Test) (value interface{}, dataType int, err error) {
value, err = GetBoolean([]byte(test.json), test.path...)
return value, Boolean, err
},
func(test Test, value interface{}) (bool, interface{}) {
expected := test.data.(bool)
return expected == value.(bool), expected
},
)
}
func TestGetSlice(t *testing.T) {
runTests(t, getArrayTests,
func(test Test) (value interface{}, dataType int, err error) {
value, dataType, _, err = Get([]byte(test.json), test.path...)
return
},
func(test Test, value interface{}) (bool, interface{}) {
expected := test.data.([]string)
return reflect.DeepEqual(expected, toStringArray(value.([]byte))), expected
},
)
}