forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textreader_test.go
788 lines (647 loc) · 16 KB
/
textreader_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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package ion
import (
"bytes"
"math"
"math/big"
"testing"
"time"
)
func TestIgnoreValues(t *testing.T) {
r := NewReaderStr("(skip ++ me / please) {skip: me, please: 0}\n[skip, me, please]\nfoo")
_next(t, r, SexpType)
_next(t, r, StructType)
_next(t, r, ListType)
_symbol(t, r, "foo")
_eof(t, r)
}
func TestReadSexps(t *testing.T) {
test := func(str string, f containerhandler) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_sexp(t, r, f)
_eof(t, r)
})
}
test("(\t)", func(t *testing.T, r Reader) {
if r.Next() {
t.Errorf("next returned true")
}
if r.Err() != nil {
t.Fatal(r.Err())
}
})
test("(foo)", func(t *testing.T, r Reader) {
_symbol(t, r, "foo")
})
test("(foo bar baz :: boop)", func(t *testing.T, r Reader) {
_symbol(t, r, "foo")
_symbol(t, r, "bar")
_symbolAF(t, r, "", []string{"baz"}, "boop")
})
}
func TestStructs(t *testing.T) {
test := func(str string, f containerhandler) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_struct(t, r, f)
_eof(t, r)
})
}
test("{\r\n}", func(t *testing.T, r Reader) {
_eof(t, r)
})
test("{foo : bar :: baz}", func(t *testing.T, r Reader) {
_symbolAF(t, r, "foo", []string{"bar"}, "baz")
})
test("{foo: a, bar: b, baz: c}", func(t *testing.T, r Reader) {
_symbolAF(t, r, "foo", nil, "a")
_symbolAF(t, r, "bar", nil, "b")
_symbolAF(t, r, "baz", nil, "c")
})
}
func TestMultipleStructs(t *testing.T) {
r := NewReaderStr("{} {} {}")
for i := 0; i < 3; i++ {
_struct(t, r, func(t *testing.T, r Reader) {
_eof(t, r)
})
}
_eof(t, r)
}
func TestNullStructs(t *testing.T) {
r := NewReaderStr("null.struct 'null'::{foo:bar}")
_null(t, r, StructType)
_nextAF(t, r, StructType, "", []string{"null"})
_eof(t, r)
}
func TestLists(t *testing.T) {
test := func(str string, f containerhandler) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_list(t, r, f)
_eof(t, r)
})
}
test("[ ]", func(t *testing.T, r Reader) {
_eof(t, r)
})
test("[foo]", func(t *testing.T, r Reader) {
_symbol(t, r, "foo")
_eof(t, r)
})
test("[foo, bar, baz::boop]", func(t *testing.T, r Reader) {
_symbol(t, r, "foo")
_symbol(t, r, "bar")
_symbolAF(t, r, "", []string{"baz"}, "boop")
_eof(t, r)
})
}
func TestReadNestedLists(t *testing.T) {
empty := func(t *testing.T, r Reader) {
_eof(t, r)
}
r := NewReaderStr("[[], [[]]]")
_list(t, r, func(t *testing.T, r Reader) {
_list(t, r, empty)
_list(t, r, func(t *testing.T, r Reader) {
_list(t, r, empty)
})
_eof(t, r)
})
_eof(t, r)
}
func TestClobs(t *testing.T) {
test := func(str string, eval []byte) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_next(t, r, ClobType)
val, err := r.ByteValue()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, eval) {
t.Errorf("expected %v, got %v", eval, val)
}
_eof(t, r)
})
}
test("{{\"\"}}", []byte{})
test("{{ \"hello world\" }}", []byte("hello world"))
test("{{'''hello world'''}}", []byte("hello world"))
test("{{'''hello'''\n'''world'''}}", []byte("helloworld"))
}
func TestBlobs(t *testing.T) {
test := func(str string, eval []byte) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_next(t, r, BlobType)
val, err := r.ByteValue()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, eval) {
t.Errorf("expected %v, got %v", eval, val)
}
_eof(t, r)
})
}
test("{{}}", []byte{})
test("{{AA==}}", []byte{0})
test("{{ SGVsbG8g\r\nV29ybGQ= }}", []byte("Hello World"))
}
func TestTimestamps(t *testing.T) {
testA := func(str string, etas []string, eval time.Time) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_nextAF(t, r, TimestampType, "", etas)
val, err := r.TimeValue()
if err != nil {
t.Fatal(err)
}
if !val.Equal(eval) {
t.Errorf("expected %v, got %v", eval, val)
}
_eof(t, r)
})
}
test := func(str string, eval time.Time) {
testA(str, nil, eval)
}
et := time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)
test("2001T", et)
test("2001-01T", et)
test("2001-01-01", et)
test("2001-01-01T", et)
test("2001-01-01T00:00Z", et)
test("2001-01-01T00:00:00Z", et)
test("2001-01-01T00:00:00.000Z", et)
test("2001-01-01T00:00:00.000+00:00", et)
test("2001-01-01T00:00:00.000000Z", et)
test("2001-01-01T00:00:00.000000000Z", et)
test("2001-01-01T00:00:00.000000000999Z", et) // We truncate, at least for now.
testA("foo::'bar'::2001-01-01T00:00:00.000Z", []string{"foo", "bar"}, et)
}
func TestDecimals(t *testing.T) {
testA := func(str string, etas []string, eval string) {
t.Run(str, func(t *testing.T) {
ee := MustParseDecimal(eval)
r := NewReaderStr(str)
_nextAF(t, r, DecimalType, "", etas)
val, err := r.DecimalValue()
if err != nil {
t.Fatal(err)
}
if !ee.Equal(val) {
t.Errorf("expected %v, got %v", ee, val)
}
_eof(t, r)
})
}
test := func(str string, eval string) {
testA(str, nil, eval)
}
test("123.", "123")
test("123.0", "123")
test("123.456", "123.456")
test("123d2", "12300")
test("123d+2", "12300")
test("123d-2", "1.23")
testA(" foo :: 'bar' :: 123. ", []string{"foo", "bar"}, "123")
}
func TestFloats(t *testing.T) {
testA := func(str string, etas []string, eval float64) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_floatAF(t, r, "", etas, eval)
_eof(t, r)
})
}
test := func(str string, eval float64) {
testA(str, nil, eval)
}
test("1e100\n", 1e100)
test("1.2e+0", 1.2)
test("-123.456e-78", -123.456e-78)
test("+inf", math.Inf(1))
test("-inf", math.Inf(-1))
testA("foo::'bar'::1e100", []string{"foo", "bar"}, 1e100)
}
func TestInts(t *testing.T) {
test := func(str string, f func(*testing.T, Reader)) {
t.Run(str, func(t *testing.T) {
r := NewReaderStr(str)
_next(t, r, IntType)
f(t, r)
_eof(t, r)
})
}
test("null.int", func(t *testing.T, r Reader) {
if !r.IsNull() {
t.Fatal("expected isnull=true, got false")
}
})
testInt := func(str string, eval int) {
test(str, func(t *testing.T, r Reader) {
val, err := r.IntValue()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
})
}
testInt("0", 0)
testInt("12_345", 12345)
testInt("-1_2_3_4_5", -12345)
testInt("0b00_0101", 5)
testInt("-0b00_0101", -5)
testInt("0x01_02_0e_0F", 0x01020e0f)
testInt("-0x0102_0e0F", -0x01020e0f)
testInt64 := func(str string, eval int64) {
test(str, func(t *testing.T, r Reader) {
val, err := r.Int64Value()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
})
}
testInt64("0x123_FFFF_FFFF", 0x123FFFFFFFF)
testInt64("-0x123_FFFF_FFFF", -0x123FFFFFFFF)
testBigInt := func(str string, estr string) {
test(str, func(t *testing.T, r Reader) {
val, err := r.BigIntValue()
if err != nil {
t.Fatal(err)
}
eval, _ := (&big.Int{}).SetString(estr, 0)
if eval.Cmp(val) != 0 {
t.Errorf("expected %v, got %v", eval, val)
}
})
}
testBigInt("0xEFFF_FFFF_FFFF_FFFF", "0xEFFFFFFFFFFFFFFF")
testBigInt("0xFFFF_FFFF_FFFF_FFFF", "0xFFFFFFFFFFFFFFFF")
testBigInt("-0x1_FFFF_FFFF_FFFF_FFFF", "-0x1FFFFFFFFFFFFFFFF")
}
func TestStrings(t *testing.T) {
r := NewReaderStr(`foo::"bar" "baz" 'a'::'b'::'''beep''' '''boop''' null.string`)
_stringAF(t, r, "", []string{"foo"}, "bar")
_string(t, r, "baz")
_stringAF(t, r, "", []string{"a", "b"}, "beepboop")
_null(t, r, StringType)
_eof(t, r)
}
func TestSymbols(t *testing.T) {
r := NewReaderStr("'null'::foo bar a::b::'baz' null.symbol")
_symbolAF(t, r, "", []string{"null"}, "foo")
_symbol(t, r, "bar")
_symbolAF(t, r, "", []string{"a", "b"}, "baz")
_null(t, r, SymbolType)
_eof(t, r)
}
func TestSpecialSymbols(t *testing.T) {
r := NewReaderStr("null\nnull.struct\ntrue\nfalse\nnan")
_null(t, r, NullType)
_null(t, r, StructType)
_bool(t, r, true)
_bool(t, r, false)
_float(t, r, math.NaN())
_eof(t, r)
}
func TestOperators(t *testing.T) {
r := NewReaderStr("(a*(b+c))")
_sexp(t, r, func(t *testing.T, r Reader) {
_symbol(t, r, "a")
_symbol(t, r, "*")
_sexp(t, r, func(t *testing.T, r Reader) {
_symbol(t, r, "b")
_symbol(t, r, "+")
_symbol(t, r, "c")
_eof(t, r)
})
_eof(t, r)
})
}
func TestTopLevelOperators(t *testing.T) {
r := NewReaderStr("a + b")
_symbol(t, r, "a")
if r.Next() {
t.Errorf("next returned true")
}
if r.Err() == nil {
t.Error("no error")
}
}
func TestTrsToString(t *testing.T) {
for i := trsDone; i <= trsAfterValue+1; i++ {
str := i.String()
if str == "" {
t.Errorf("expected a non-empty string for trs %v", uint8(i))
}
}
}
type containerhandler func(t *testing.T, r Reader)
func _sexp(t *testing.T, r Reader, f containerhandler) {
_sexpAF(t, r, "", nil, f)
}
func _sexpAF(t *testing.T, r Reader, efn string, etas []string, f containerhandler) {
_containerAF(t, r, SexpType, efn, etas, f)
}
func _struct(t *testing.T, r Reader, f containerhandler) {
_structAF(t, r, "", nil, f)
}
func _structAF(t *testing.T, r Reader, efn string, etas []string, f containerhandler) {
_containerAF(t, r, StructType, efn, etas, f)
}
func _list(t *testing.T, r Reader, f containerhandler) {
_listAF(t, r, "", nil, f)
}
func _listAF(t *testing.T, r Reader, efn string, etas []string, f containerhandler) {
_containerAF(t, r, ListType, efn, etas, f)
}
func _containerAF(t *testing.T, r Reader, et Type, efn string, etas []string, f containerhandler) {
_nextAF(t, r, et, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.%v", et, et)
}
if err := r.StepIn(); err != nil {
t.Fatal(err)
}
f(t, r)
if err := r.StepOut(); err != nil {
t.Fatal(err)
}
}
func _int(t *testing.T, r Reader, eval int) {
_intAF(t, r, "", nil, eval)
}
func _intAF(t *testing.T, r Reader, efn string, etas []string, eval int) {
_nextAF(t, r, IntType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.int", eval)
}
size, err := r.IntSize()
if err != nil {
t.Fatal(err)
}
if size != Int32 {
t.Errorf("expected size=Int32, got %v", size)
}
val, err := r.IntValue()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _int64(t *testing.T, r Reader, eval int64) {
_int64AF(t, r, "", nil, eval)
}
func _int64AF(t *testing.T, r Reader, efn string, etas []string, eval int64) {
_nextAF(t, r, IntType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.int", eval)
}
size, err := r.IntSize()
if err != nil {
t.Fatal(err)
}
if size != Int64 {
t.Errorf("expected size=Int64, got %v", size)
}
val, err := r.Int64Value()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _uint(t *testing.T, r Reader, eval uint64) {
_uintAF(t, r, "", nil, eval)
}
func _uintAF(t *testing.T, r Reader, efn string, etas []string, eval uint64) {
_nextAF(t, r, IntType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.int", eval)
}
size, err := r.IntSize()
if err != nil {
t.Fatal(err)
}
if size != Uint64 {
t.Errorf("expected size=Uint, got %v", size)
}
val, err := r.Uint64Value()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _bigInt(t *testing.T, r Reader, eval *big.Int) {
_bigIntAF(t, r, "", nil, eval)
}
func _bigIntAF(t *testing.T, r Reader, efn string, etas []string, eval *big.Int) {
_nextAF(t, r, IntType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.int", eval)
}
size, err := r.IntSize()
if err != nil {
t.Fatal(err)
}
if size != BigInt {
t.Errorf("expected size=BigInt, got %v", size)
}
val, err := r.BigIntValue()
if err != nil {
t.Fatal(err)
}
if val.Cmp(eval) != 0 {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _float(t *testing.T, r Reader, eval float64) {
_floatAF(t, r, "", nil, eval)
}
func _floatAF(t *testing.T, r Reader, efn string, etas []string, eval float64) {
_nextAF(t, r, FloatType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.float", eval)
}
val, err := r.FloatValue()
if err != nil {
t.Fatal(err)
}
if math.IsNaN(eval) {
if !math.IsNaN(val) {
t.Errorf("expected %v, got %v", eval, val)
}
} else if eval != val {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _decimal(t *testing.T, r Reader, eval *Decimal) {
_decimalAF(t, r, "", nil, eval)
}
func _decimalAF(t *testing.T, r Reader, efn string, etas []string, eval *Decimal) {
_nextAF(t, r, DecimalType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.decimal", eval)
}
val, err := r.DecimalValue()
if err != nil {
t.Fatal(err)
}
if !eval.Equal(val) {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _timestamp(t *testing.T, r Reader, eval time.Time) {
_timestampAF(t, r, "", nil, eval)
}
func _timestampAF(t *testing.T, r Reader, efn string, etas []string, eval time.Time) {
_nextAF(t, r, TimestampType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.timestamp", eval)
}
val, err := r.TimeValue()
if err != nil {
t.Fatal(err)
}
if !val.Equal(eval) {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _string(t *testing.T, r Reader, eval string) {
_stringAF(t, r, "", nil, eval)
}
func _stringAF(t *testing.T, r Reader, efn string, etas []string, eval string) {
_nextAF(t, r, StringType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.string", eval)
}
val, err := r.StringValue()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _symbol(t *testing.T, r Reader, eval string) {
_symbolAF(t, r, "", nil, eval)
}
func _symbolAF(t *testing.T, r Reader, efn string, etas []string, eval string) {
_nextAF(t, r, SymbolType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.symbol", eval)
}
val, err := r.StringValue()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _bool(t *testing.T, r Reader, eval bool) {
_boolAF(t, r, "", nil, eval)
}
func _boolAF(t *testing.T, r Reader, efn string, etas []string, eval bool) {
_nextAF(t, r, BoolType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.bool", eval)
}
val, err := r.BoolValue()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _clob(t *testing.T, r Reader, eval []byte) {
_clobAF(t, r, "", nil, eval)
}
func _clobAF(t *testing.T, r Reader, efn string, etas []string, eval []byte) {
_nextAF(t, r, ClobType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.clob", eval)
}
val, err := r.ByteValue()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, eval) {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _blob(t *testing.T, r Reader, eval []byte) {
_blobAF(t, r, "", nil, eval)
}
func _blobAF(t *testing.T, r Reader, efn string, etas []string, eval []byte) {
_nextAF(t, r, BlobType, efn, etas)
if r.IsNull() {
t.Fatalf("expected %v, got null.blob", eval)
}
val, err := r.ByteValue()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, eval) {
t.Errorf("expected %v, got %v", eval, val)
}
}
func _null(t *testing.T, r Reader, et Type) {
_nullAF(t, r, et, "", nil)
}
func _nullAF(t *testing.T, r Reader, et Type, efn string, etas []string) {
_nextAF(t, r, et, efn, etas)
if !r.IsNull() {
t.Error("isnull returned false")
}
}
func _next(t *testing.T, r Reader, et Type) {
_nextAF(t, r, et, "", nil)
}
func _nextAF(t *testing.T, r Reader, et Type, efn string, etas []string) {
if !r.Next() {
t.Fatal(r.Err())
}
if r.Type() != et {
t.Fatalf("expected %v, got %v", et, r.Type())
}
if efn != r.FieldName() {
t.Errorf("expected fieldname=%v, got %v", efn, r.FieldName())
}
if !_strequals(etas, r.Annotations()) {
t.Errorf("expected type annotations=%v, got %v", etas, r.Annotations())
}
}
func _strequals(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func _eof(t *testing.T, r Reader) {
if r.Next() {
t.Fatal("next returned true")
}
if r.Err() != nil {
t.Fatal(r.Err())
}
}