-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
serde_test.go
141 lines (132 loc) · 3.51 KB
/
serde_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
package sr
import (
"bytes"
"encoding/json"
"io"
"reflect"
"testing"
)
func TestSerde(t *testing.T) {
type (
overridden struct {
Ignored string `json:"ignored,omitempty"`
}
overrides struct {
One string `json:"one,omitempty"`
}
idx1 struct {
Two int64 `json:"two,omitempty"`
Three int64 `json:"three,omitempty"`
}
idx2 struct {
Biz string `json:"biz,omitempty"`
Baz string `json:"baz,omitempty"`
}
idx3 struct {
Boz int8 `json:"boz,omitempty"`
}
idx4 struct {
Bingo string `json:"bingo,omitempty"`
}
oneidx struct {
Foo string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
}
)
var serde Serde
serde.SetDefaults(
EncodeFn(json.Marshal),
DecodeFn(json.Unmarshal),
)
serde.Register(127, overridden{}, GenerateFn(func() any { return new(overridden) }))
serde.Register(127, overrides{})
serde.Register(3, idx1{}, Index(0))
serde.Register(3, idx2{}, Index(1), AppendEncodeFn(func(b []byte, v any) ([]byte, error) {
bb, err := json.Marshal(v)
if err != nil {
return b, err
}
return append(b, bb...), nil
}))
serde.Register(3, idx4{}, Index(0, 0, 1))
serde.Register(3, idx3{}, Index(0, 0))
serde.Register(5, oneidx{}, Index(0), GenerateFn(func() any { return &oneidx{Foo: "defoo", Bar: "debar"} }))
for i, test := range []struct {
enc any
expEnc []byte
expDec any
expErr bool
}{
{
enc: overridden{},
expErr: true,
},
{
enc: overrides{"foo"},
expEnc: append([]byte{0, 0, 0, 0, 127}, `{"one":"foo"}`...),
},
{
enc: idx1{Two: 2, Three: 3},
expEnc: append([]byte{0, 0, 0, 0, 3, 0}, `{"two":2,"three":3}`...),
},
{
enc: idx2{Biz: "bizzy", Baz: "bazzy"},
expEnc: append([]byte{0, 0, 0, 0, 3, 2, 2}, `{"biz":"bizzy","baz":"bazzy"}`...),
},
{
enc: idx3{Boz: 8},
expEnc: append([]byte{0, 0, 0, 0, 3, 4, 0, 0}, `{"boz":8}`...),
},
{
enc: idx4{Bingo: "bango"},
expEnc: append([]byte{0, 0, 0, 0, 3, 6, 0, 0, 2}, `{"bingo":"bango"}`...),
},
{
enc: oneidx{Bar: "bar"},
expEnc: append([]byte{0, 0, 0, 0, 5, 0}, `{"bar":"bar"}`...),
expDec: oneidx{Foo: "defoo", Bar: "bar"},
},
} {
b, err := serde.Encode(test.enc)
gotErr := err != nil
if gotErr != test.expErr {
t.Errorf("#%d Encode: got err? %v, exp err? %v", i, gotErr, test.expErr)
continue
}
if test.expErr {
continue
}
if !bytes.Equal(b, test.expEnc) {
t.Errorf("#%d: Encode(%v) != exp(%v)", i, b, test.expEnc)
continue
}
if b2 := serde.MustEncode(test.enc); !bytes.Equal(b2, b) {
t.Errorf("#%d got MustEncode(%v) != Encode(%v)", i, b2, b)
}
if b2 := serde.MustAppendEncode([]byte("foo"), test.enc); !bytes.Equal(b2, append([]byte("foo"), b...)) {
t.Errorf("#%d got MustAppendEncode(%v) != Encode(foo%v)", i, b2, b)
}
v, err := serde.DecodeNew(b)
if err != nil {
t.Errorf("#%d DecodeNew: got unexpected err %v", i, err)
continue
}
v = reflect.Indirect(reflect.ValueOf(v)).Interface() // DecodeNew returns a pointer, we compare values below
exp := test.expDec
if exp == nil {
exp = test.enc
}
if !reflect.DeepEqual(v, exp) {
t.Errorf("#%d round trip: got %v != exp %v", i, v, exp)
}
}
if _, err := serde.DecodeNew([]byte{1, 0, 0, 0, 0, 0}); err != ErrBadHeader {
t.Errorf("got %v != exp ErrBadHeader", err)
}
if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 3}); err != io.EOF {
t.Errorf("got %v != exp io.EOF", err)
}
if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 99}); err != ErrNotRegistered {
t.Errorf("got %v != exp ErrNotRegistered", err)
}
}