-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyrup_test.go
501 lines (485 loc) · 11.5 KB
/
syrup_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
package syrup
import (
"bytes"
"fmt"
"math/big"
"reflect"
"testing"
)
// TODO: Unterminated list error test
type Struct1 struct {
I int
f float32
Do float64
unum uint64
Str string
}
type Struct2 struct {
S1 *Struct1
sUnexp *Struct1
}
type Struct3 struct {
Struct1
}
type Struct4 struct {
*Struct1
}
type test struct {
name string
// If goValueExpect is set, decode will test against it (good for
// unexported fields).
goValue interface{}
goValueExpect interface{}
decode interface{}
// Either set encoding or encodings. Not both. Encodings is there due to
// golang's map non-determinism.
encoding []byte
encodings [][]byte
}
// Addressable vars for tests
var astring string
var abyte []byte
var aint8 int8
var aint16 int16
var aint32 int32
var aint64 int64
var abigint *big.Int
var aint int
var auint8 uint8
var auint16 uint16
var auint32 uint32
var auint64 uint64
var auint uint
var abool bool
var afloat32 float32
var afloat64 float64
var asymbol Symbol
var arecord Record
var astringarr []string
var aset Set
var aMapStringInt map[string]int
var astruct1 Struct1
var astruct2 Struct2
var astruct3 Struct3
var astruct4 Struct4
var ainterface interface{}
func resetAddressables() {
abyte = nil
astringarr = nil
aMapStringInt = nil
ainterface = nil
}
var tests = []test{
{
name: "String",
goValue: "Hello, World!",
encoding: []byte("13\"Hello, World!"),
decode: &astring,
},
{
name: "Int8",
goValue: int8(5),
encoding: []byte("i5e"),
decode: &aint8,
},
{
name: "Int16",
goValue: int16(128),
encoding: []byte("i128e"),
decode: &aint16,
},
{
name: "Int32",
goValue: int32(32768),
encoding: []byte("i32768e"),
decode: &aint32,
},
{
name: "Int64",
goValue: int64(2147483648),
encoding: []byte("i2147483648e"),
decode: &aint64,
},
{
name: "BigInt",
goValue: big.NewInt(0).Mul(big.NewInt(9223372036854775807), big.NewInt(10)),
encoding: []byte("i92233720368547758070e"),
decode: &abigint,
},
{
name: "Negative Int",
goValue: int(-919),
encoding: []byte("i-919e"),
decode: &aint,
},
{
name: "Uint8",
goValue: uint8(5),
encoding: []byte("i5e"),
decode: &auint8,
},
{
name: "Uint16",
goValue: uint16(128),
encoding: []byte("i128e"),
decode: &auint16,
},
{
name: "Unt32",
goValue: uint32(32768),
encoding: []byte("i32768e"),
decode: &auint32,
},
{
name: "Uint64",
goValue: uint64(2147483648),
encoding: []byte("i2147483648e"),
decode: &auint64,
},
{
name: "Uint",
goValue: uint(919),
encoding: []byte("i919e"),
decode: &auint,
},
{
name: "True",
goValue: true,
encoding: []byte("t"),
decode: &abool,
},
{
name: "False",
goValue: false,
encoding: []byte("f"),
decode: &abool,
},
{
name: "Float32",
goValue: float32(3.14159),
encoding: []byte{'F', 64, 73, 15, 208},
decode: &afloat32,
},
{
name: "Float64",
goValue: float64(3.14159),
encoding: []byte{'D', 64, 9, 33, 249, 240, 27, 134, 110},
decode: &afloat64,
},
{
name: "Symbol",
goValue: Symbol("PtrToIt"),
encoding: []byte("7'PtrToIt"),
decode: &asymbol,
},
{
name: "Record",
goValue: Record{
Label: "Napalm",
Values: []interface{}{
int64(-5),
Symbol("Yep"),
"Hello",
},
},
encoding: []byte("<6\"Napalmi-5e3'Yep5\"Hello>"),
decode: &arecord,
},
{
name: "[]byte",
goValue: []byte{1, 1, 2, 3, 5, 8, 13, 8, 5, 3, 2, 1, 1},
encoding: []byte{'1', '3', ':', 1, 1, 2, 3, 5, 8, 13, 8, 5, 3, 2, 1, 1},
decode: &abyte,
},
{
name: "[]byte in list into interface",
goValue: []interface{}{
"str",
5,
[]byte{49, 50, 51},
[]byte{54, 55, 56, 57},
},
goValueExpect: []interface{}{
"str",
int64(5),
[]byte{49, 50, 51},
[]byte{54, 55, 56, 57},
},
encoding: []byte("[3\"stri5e3:1234:6789]"),
decode: &ainterface,
},
{
name: "[]string",
goValue: []string{"Hello", "World!"},
encoding: []byte("[5\"Hello6\"World!]"),
decode: &astringarr,
},
{
name: "Set",
goValue: Set([]interface{}{"Hello", int64(42)}),
encoding: []byte("#5\"Helloi42e$"),
decode: &aset,
},
{
name: "map[string]int",
goValue: map[string]int{"in": 2, "out of here": -99},
encodings: [][]byte{
[]byte("{2\"ini2e11\"out of herei-99e}"),
[]byte("{11\"out of herei-99e2\"ini2e}"),
},
decode: &aMapStringInt,
},
{
name: "Struct1",
goValue: Struct1{
I: -5,
f: 4.321,
Do: 3.14159,
unum: 256,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
goValueExpect: Struct1{
I: -5,
Do: 3.14159,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
encoding: append(
append([]byte("{1\"Ii-5e2\"Do"),
[]byte{'D', 64, 9, 33, 249, 240, 27, 134, 110}...),
[]byte("3\"Str61\"Life's a bitch, then you rejuvenate and do it all over again.}")...),
decode: &astruct1,
},
{
name: "Struct2",
goValue: Struct2{
S1: &Struct1{
I: -5,
f: 4.321,
Do: 3.14159,
unum: 256,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
sUnexp: &Struct1{
I: 1,
f: 1.28,
Do: 1.2825,
unum: 128,
Str: "How you humans survive so much experience is something I shall never understand.",
},
},
goValueExpect: Struct2{
S1: &Struct1{
I: -5,
Do: 3.14159,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
},
encoding: append(
append([]byte("{2\"S1{1\"Ii-5e2\"Do"),
[]byte{'D', 64, 9, 33, 249, 240, 27, 134, 110}...),
[]byte("3\"Str61\"Life's a bitch, then you rejuvenate and do it all over again.}}")...),
decode: &astruct2,
},
{
name: "Struct3",
goValue: Struct3{
Struct1{
I: -5,
f: 4.321,
Do: 3.14159,
unum: 256,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
},
goValueExpect: Struct3{
Struct1{
I: -5,
Do: 3.14159,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
},
encoding: append(
append([]byte("{7\"Struct1{1\"Ii-5e2\"Do"),
[]byte{'D', 64, 9, 33, 249, 240, 27, 134, 110}...),
[]byte("3\"Str61\"Life's a bitch, then you rejuvenate and do it all over again.}}")...),
decode: &astruct3,
},
{
name: "Struct4",
goValue: Struct4{
&Struct1{
I: -5,
f: 4.321,
Do: 3.14159,
unum: 256,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
},
goValueExpect: Struct4{
&Struct1{
I: -5,
Do: 3.14159,
Str: "Life's a bitch, then you rejuvenate and do it all over again.",
},
},
encoding: append(
append([]byte("{7\"Struct1{1\"Ii-5e2\"Do"),
[]byte{'D', 64, 9, 33, 249, 240, 27, 134, 110}...),
[]byte("3\"Str61\"Life's a bitch, then you rejuvenate and do it all over again.}}")...),
decode: &astruct4,
},
}
func TestEncode(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(NewPrototypeEncoding(), &buf)
err := enc.Encode(test.goValue)
if err != nil {
t.Errorf("got error %v", err)
} else if test.encodings != nil {
ok := false
for _, encoding := range test.encodings {
ok = ok || bytes.Equal(buf.Bytes(), encoding)
}
if !ok {
t.Errorf("got %v, which matched no encodings", buf.Bytes())
t.Errorf("len got %d", len(buf.Bytes()))
}
} else if !bytes.Equal(buf.Bytes(), test.encoding) {
t.Errorf("got %v, encoding %v", buf.Bytes(), test.encoding)
t.Errorf("len got %d, len encoding %d", len(buf.Bytes()), len(test.encoding))
}
})
ptrName := "Pointer To " + test.name
t.Run(ptrName, func(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(NewPrototypeEncoding(), &buf)
err := enc.Encode(&test.goValue)
if err != nil {
t.Errorf("got error %v", err)
} else if test.encodings != nil {
ok := false
for _, encoding := range test.encodings {
ok = ok || bytes.Equal(buf.Bytes(), encoding)
}
if !ok {
t.Errorf("got %v, which matched no encodings", buf.Bytes())
t.Errorf("len got %d", len(buf.Bytes()))
}
} else if !bytes.Equal(buf.Bytes(), test.encoding) {
t.Errorf("got %v, encoding %v", buf.Bytes(), test.encoding)
t.Errorf("len got %d, len encoding %d", len(buf.Bytes()), len(test.encoding))
}
})
}
}
func TestDecode(t *testing.T) {
for _, test := range tests {
if len(test.encoding) > 0 {
t.Run(test.name, func(t *testing.T) {
resetAddressables()
buf := bytes.NewBuffer([]byte(test.encoding))
dec := NewDecoder(NewPrototypeEncoding(), buf)
err := dec.Decode(test.decode)
if err != nil {
t.Errorf("got error %v", err)
return
}
pme := reflect.ValueOf(test.decode)
expect := test.goValue
if test.goValueExpect != nil {
expect = test.goValueExpect
}
if !reflect.DeepEqual(pme.Elem().Interface(), expect) {
t.Errorf("got %v, want %v", pme.Elem().Interface(), test.goValue)
}
})
} else {
for i, encoding := range test.encodings {
t.Run(fmt.Sprintf("%s_%d", test.name, i), func(t *testing.T) {
resetAddressables()
buf := bytes.NewBuffer([]byte(encoding))
dec := NewDecoder(NewPrototypeEncoding(), buf)
err := dec.Decode(test.decode)
if err != nil {
t.Errorf("got error %v", err)
return
}
pme := reflect.ValueOf(test.decode)
expect := test.goValue
if test.goValueExpect != nil {
expect = test.goValueExpect
}
if !reflect.DeepEqual(pme.Elem().Interface(), expect) {
t.Errorf("got %v, want %v", pme.Elem().Interface(), test.goValue)
}
})
}
}
}
}
func TestDecodeInterface(t *testing.T) {
buf := bytes.NewBuffer([]byte("[5\"Helloi42e]"))
dec := NewDecoder(NewPrototypeEncoding(), buf)
var v interface{}
err := dec.Decode(&v)
if err != nil {
t.Errorf("got error %v", err)
return
}
expected := []interface{}{"Hello", int64(42)}
if varr, ok := v.([]interface{}); !ok {
t.Errorf("v is not a slice")
} else if len(varr) != len(expected) {
t.Errorf("len: got %#v, want %#v", v, expected)
} else if varr[0] != expected[0] {
t.Errorf("0: got %#v, want %#v", v, expected)
t.Errorf("0: got %T, want %T", varr[0], expected[0])
} else if varr[1] != expected[1] {
t.Errorf("1: got %#v, want %#v", v, expected)
t.Errorf("1: got %T, want %T", varr[1], expected[1])
}
}
func TestDecodeArraySilentDrop(t *testing.T) {
buf := bytes.NewBuffer([]byte("[5\"Hello7\"PtrToIt6\"World!]"))
dec := NewDecoder(NewPrototypeEncoding(), buf)
var s [2]string
err := dec.Decode(&s)
if err != nil {
t.Errorf("got error %v", err)
return
}
expected := [2]string{"Hello", "PtrToIt"}
if !reflect.DeepEqual(s, expected) {
t.Errorf("got %v, want %v", s, expected)
}
}
func TestDecodeArrayExtrasZero(t *testing.T) {
buf := bytes.NewBuffer([]byte("[5\"Hello7\"PtrToIt6\"World!]"))
dec := NewDecoder(NewPrototypeEncoding(), buf)
var s [5]string
err := dec.Decode(&s)
if err != nil {
t.Errorf("got error %v", err)
return
}
expected := [5]string{"Hello", "PtrToIt", "World!", "", ""}
if !reflect.DeepEqual(s, expected) {
t.Errorf("got %v, want %v", s, expected)
}
}
func TestDecodeArrayNilInterface(t *testing.T) {
buf := bytes.NewBuffer([]byte("[5\"Hello7\"PtrToIt6\"World!]"))
dec := NewDecoder(NewPrototypeEncoding(), buf)
var v interface{}
err := dec.Decode(&v)
if err != nil {
t.Errorf("got error %v", err)
return
}
expected := []interface{}{"Hello", "PtrToIt", "World!"}
if !reflect.DeepEqual(v, expected) {
t.Errorf("got %v, want %v", v, expected)
}
}