-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder_test.go
628 lines (567 loc) · 10.9 KB
/
decoder_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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package json6
import (
"fmt"
"testing"
)
func TestDecodeObject(t *testing.T) {
input :=
`
ident: 'single quote string',
'sigle quote ident': "double quote string",
"double quote ident":` + "`back tick string`," +
"`back tick ident`: " + `{
// sigle line comment
innerObject: "inner val",
identwith\u1234unicode: "string with\u1234unicode",
identWith\x12hexaEsc: "string with\x12 hexa escape",
moreString: "Lorem ipsum dolor\
sit amet",
nullVal: null,
undefinedVal: undefined //comment
,
boolFalseVal: false//comment
,
boolTrueVal: true//comment
,
minusInt1: -123//comment
,
minusInt2: ---123//comment
,
plusInt1: 123//comment
,
plusInt2: +123//comment
,
plusInt3: --123,
hexaDecimal1: 0x123,
hexaDecimal2: 0X123//comment
,
binary1: 0b1010,
binary2: 0B1010//comment
,
octalDecimal1: 0o123,
octalDecimal2: 0O123//comment
,
double1: 0.123//comment
,
double2: .123//comment
,
double3: 123.,
exponent1: 1e123//comment
,
exponent2: 1e-123,
exponent3: 1e+123,
exponent4: 1E123,
exponent5: 1E-123,
exponent6: 1E+123,
exponent7: 1.e123,
exponent8: 1.e-123,
exponent9: 1.e+123,
exponent10: 1.E123,
exponent11: 1.E-123,
exponent12: 1.E+123,
exponent13: .1e123,
exponent14: .1e-123,
exponent15: .1e+123,
exponent16: .1E123,
exponent17: .1E-123,
exponent18: .1E+123,
NaNNum1: NaN,
NaNNum2: -NaN//comment
,
NaNNum3: +NaN,
InfinityNum1: Infinity//comment
,
InfinityNum2: +Infinity,
InfinityNum3: -Infinity//comment
},
/*
multiline comment
*/
}
`
var val struct{}
dec, err := newDecoderFromBytes([]byte(input), &val)
if err != nil {
t.Error(err.Error())
return
}
_, err = decodeObject(dec.lx.tokenReader)
if err != nil {
t.Error(err.Error())
}
// fmt.Printf("%#v\n", val.objVal)
}
func TestDecodeArray(t *testing.T) {
input :=
`
{
"amount": 20000,
"timestamp": "2021-10-12",
"description": "MO1922 - Teknisi EDC PT. Mahapay di Cibubur",
"status": "Approved",
"category": "incentive"
},
{
"amount": 5000,
"timestamp": "2021-10-12",
"description": "MO1922 - Teknisi EDC PT. Mahapay di Jatinegara",
"status": "Completed",
"category": "fee"
},
{
"amount": -30000,
"timestamp": "2021-10-12",
"description": "Bank Mandiri - Andini Septia",
"status": "Transfer Out",
"category": "price"
},
{
"amount": 0,
"timestamp": "2021-10-12",
"description": "MO1920 - Nobar EPL Week 6",
"status": "Canceled",
"category": "others"
},,,,
{
"amount": 0,
"timestamp": "2021-10-11",
"description": "MO1919 - Komando IDM Asem Baris",
"status": "Revoked",
"category": "incentive"
},
[],
]
`
var val struct{}
dec, err := newDecoderFromBytes([]byte(input), &val)
if err != nil {
t.Error(err.Error())
return
}
_, err = decodeArray(dec.lx.tokenReader)
if err != nil {
t.Error(err.Error())
}
// fmt.Printf("%#v\n", val.arrVal)
}
func TestDecodeValue(t *testing.T) {
input :=
`
{
ident// comment
: 'single quote string',
'sigle quote ident': "double quote string",
"double quote ident":` + "`back tick string`," +
"`back tick ident`: " + `{
// sigle line comment
innerObject: "inner val",
identwith\u1234unicode: "string with\u1234unicode",
identWith\x12hexaEsc: "string with\x12 hexa escape",
moreString: "Lorem ipsum dolor\
sit amet",
nullVal: null,
undefinedVal: undefined //comment
,
boolFalseVal: false//comment
,
boolTrueVal: true//comment
,
minusInt1: -123//comment
,
minusInt2: ---123//comment
,
plusInt1: 123//comment
,
plusInt2: +123//comment
,
plusInt3: --123,
hexaDecimal1: 0x123,
hexaDecimal2: 0X123//comment
,
binary1: 0b1010,
binary2: 0B1010//comment
,
octalDecimal1: 0o123,
octalDecimal2: 0O123//comment
,
double1: 0.123//comment
,
double2: .123//comment
,
double3: 123.,
exponent1: 1e123//comment
,
exponent2: 1e-123,
exponent3: 1e+123,
exponent4: 1E123,
exponent5: 1E-123,
exponent6: 1E+123,
exponent7: 1.e123,
exponent8: 1.e-123,
exponent9: 1.e+123,
exponent10: 1.E123,
exponent11: 1.E-123,
exponent12: 1.E+123,
exponent13: .1e123,
exponent14: .1e-123,
exponent15: .1e+123,
exponent16: .1E123,
exponent17: .1E-123,
exponent18: .1E+123,
NaNNum1: NaN,
NaNNum2: -NaN//comment
,
NaNNum3: +NaN,
InfinityNum1: Infinity//comment
,
InfinityNum2: +Infinity,
InfinityNum3: -Infinity//comment
},
/*
multiline comment
*/
}
`
var val struct{}
dec, err := newDecoderFromBytes([]byte(input), &val)
if err != nil {
t.Error(err.Error())
return
}
if err := dec.decodeValue(); err != nil {
t.Error(err.Error())
}
}
func TestUnmarshal(t *testing.T) {
src := []byte("-3000")
expected := -3000
var val int
if err := Unmarshal(src, &val); err != nil {
t.Error(err.Error())
return
}
if val != expected {
t.Errorf("unexpected value '%d', expecting '%d'", val, expected)
}
}
type CurrentJob struct {
Title string `json6:"title"`
Company string `json:"company"`
Year int `json:"year"`
}
type Profile struct {
Name string `json6:"name"`
Age int `json5:"age"`
Sex string
Address string `json:"address"`
CurrentJob CurrentJob `json:"currentJob"`
}
func TestUnmarshalObject(t *testing.T) {
src :=
`
{
name: "Franklin Collin Tamboto",
age: 21,
Sex: 'L',
address: 'Bandung, Jawa Barat',
currentJob: {
title: 'Golang Developer',
company: 'PT. Dwidasa Samsara Indonesia',
year: 1
}
}
`
var val Profile
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalObjectMismatchType(t *testing.T) {
src :=
`
{
name: "Franklin Collin Tamboto",
age: 21,
Sex: 'L',
address: 'Bandung, Jawa Barat',
currentJob: {
title: 'Golang Developer',
company: 'PT. Dwidasa Samsara Indonesia',
year: '1'
}
}
`
var val Profile
if err := Unmarshal([]byte(src), &val); err == nil {
t.Error("expecting type mismatch error")
return
} else {
fmt.Println(err.Error())
}
}
func TestUnmarshalObjectToMap(t *testing.T) {
src :=
`
{
name: "Franklin Collin Tamboto",
age: 21,
Sex: 'L',
address: 'Bandung, Jawa Barat',
currentJob: {
title: 'Golang Developer',
company: 'PT. Dwidasa Samsara Indonesia',
year: 1
}
}
`
val := make(map[string]interface{})
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalObjectToInvalidMap(t *testing.T) {
src :=
`
{
name: "Franklin Collin Tamboto",
age: 21,
Sex: 'L',
address: 'Bandung, Jawa Barat',
currentJob: {
title: 'Golang Developer',
company: 'PT. Dwidasa Samsara Indonesia',
year: 1
}
}
`
val := make(map[int]interface{})
if err := Unmarshal([]byte(src), &val); err == nil {
t.Error("expecting error invalid map")
return
} else {
fmt.Println(err.Error())
}
}
func TestUnmarshalObjectToInvalidType(t *testing.T) {
src :=
`
{
name: "Franklin Collin Tamboto",
age: 21,
Sex: 'L',
address: 'Bandung, Jawa Barat',
currentJob: {
title: 'Golang Developer',
company: 'PT. Dwidasa Samsara Indonesia',
year: 1
}
}
`
val := 0
if err := Unmarshal([]byte(src), &val); err == nil {
t.Error("expecting error invalid type")
return
} else {
fmt.Println(err.Error())
}
}
func TestUnmarshalObjectToInterface(t *testing.T) {
src :=
`
{
name: "Franklin Collin Tamboto",
age: 21,
Sex: 'L',
address: 'Bandung, Jawa Barat',
currentJob: {
title: 'Golang Developer',
company: 'PT. Dwidasa Samsara Indonesia',
year: 1
}
}
`
var val interface{}
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalArrayToSlice(t *testing.T) {
src :=
`
[
1,
-2,
3,
0x4,
0e5,
]
`
var val []int
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalArrayWithNullElemToSlice(t *testing.T) {
src :=
`
[
1,
null,
-2,
3,
0x4,
0e5,
]
`
var val []int
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalArrayWithUndefinedElemToSlice(t *testing.T) {
src :=
`
[
1,
undefined,
-2,
3,
0x4,
0e5,
]
`
var val []int
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalArrayToArray(t *testing.T) {
src :=
`
[
1,
-2,
null,
0x4,
0e5,
]
`
var val [3]int
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalArrayToInterface(t *testing.T) {
src :=
`
[
1,
-2,
null,
0x4,
0e5,
]
`
var val interface{}
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalStrFromd3x0r_JSON6ToMap1(t *testing.T) {
src :=
`
{
foo: 'bar',
while: true,
nothing : undefined, // why not?
this: 'is a \
multi-line string',
thisAlso: 'is a
multi-line string; but keeps newline',
// this is an inline comment
here: 'is another', // inline comment
/* this is a block comment
that continues on another line */
hex: 0xDEAD_beef,
binary: 0b0110_1001,
decimal: 123_456_789,
octal: 0o123,
half: .5,
delta: +10,
negative : ---123,
to: Infinity, // and beyond!
finally: 'a trailing comma',
oh: [
"we shouldn't forget",
'arrays can have',
'trailing commas too',
],
}
`
val := make(map[string]interface{})
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}
func TestUnmarshalStrFromd3x0r_JSON6ToMap2(t *testing.T) {
src :=
`
{
name: 'JSON6',
version: '0.1.105',
description: 'JSON for the ES6 era.',
keywords: ['json', 'es6'],
author: 'd3x0r <d3x0r@github.com>',
contributors: [
// TODO: Should we remove this section in favor of GitHub's list?
// https://github.com/d3x0r/JSON6/contributors
],
main: 'lib/JSON6.js',
bin: 'lib/cli.js',
files: ["lib/"],
dependencies: {},
devDependencies: {
gulp: "^3.9.1",
'gulp-jshint': "^2.0.0",
jshint: "^2.9.1",
'jshint-stylish': "^2.1.0",
mocha: "^2.4.5"
},
scripts: {
build: 'node ./lib/cli.js -c package.JSON6',
test: 'mocha --ui exports --reporter spec',
// TODO: Would it be better to define these in a mocha.opts file?
},
homepage: 'http://github.com/d3x0r/JSON6/',
license: 'MIT',
repository: {
type: 'git',
url: 'https://github.com/d3x0r/JSON6',
},
}
`
val := make(map[string]interface{})
if err := Unmarshal([]byte(src), &val); err != nil {
t.Error(err)
return
}
fmt.Printf("%#v\n", val)
}