-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects_test.go
188 lines (151 loc) · 3.72 KB
/
objects_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
package pack
import (
"bytes"
"reflect"
"testing"
)
func TestObjects(t *testing.T) {
t.Parallel()
type objectA struct {
Val string
}
type objectB2 struct {
Name string
}
type objectB struct {
Param int
SubObject *objectB2
}
type objectC struct {
Val string
Param int
}
type recursiveObject struct {
Level int
AnotherOne *recursiveObject
}
var (
options = Options{
WithObjects: NewObjects(objectA{}, objectB{}, objectC{}, recursiveObject{}),
}
buf = bytes.NewBuffer(nil)
packer = NewPacker(buf, options)
unpacker = NewUnpacker(buf, options)
inputs = []any{
objectA{Val: "Hello"},
objectB{Param: 123, SubObject: &objectB2{"sub"}},
objectC{Val: "World", Param: 456},
&objectB{Param: 789, SubObject: &objectB2{"obj"}},
&objectC{Val: "Test", Param: 987},
&objectA{Val: "Another"},
recursiveObject{
Level: 1,
AnotherOne: &recursiveObject{
Level: 2,
AnotherOne: &recursiveObject{
Level: 3,
AnotherOne: nil,
},
},
},
}
)
for _, input := range inputs {
err := packer.Encode(input)
if err != nil {
t.Error(err)
}
}
for _, input := range inputs {
var output any
err := unpacker.Decode(&output)
if err != nil {
t.Error(err)
}
inputType := reflect.TypeOf(input)
expect := input
if inputType.Kind() == reflect.Pointer {
inputType = inputType.Elem()
expect = reflect.ValueOf(input).Elem().Interface()
}
if reflect.TypeOf(output) != reflect.PointerTo(inputType) {
t.Errorf("decoded object type should be a pointer to input object type; expected: %s, got: %s",
reflect.PointerTo(inputType).String(), reflect.TypeOf(output).String())
} else {
output = reflect.ValueOf(output).Elem().Interface()
if !reflect.DeepEqual(expect, output) {
t.Errorf("decoded object should equal input object; expected: %+v, got: %+v", expect, output)
}
}
}
if buf.Len() > 0 {
t.Errorf("expected all bytes to be consumed after decoding all objects, got %d extra bytes: %q", buf.Len(), buf.Bytes())
}
}
func TestSubObjects(t *testing.T) {
t.Parallel()
type objectA struct {
Val string
}
type objectB struct {
Name string
}
type objectTest struct {
RawA any `pack:"objects:a"`
RawB any `pack:"objects:b"`
Slice []any `pack:"objects:b"`
Array [2]any `pack:"objects:b"`
Map map[string]any `pack:"objects:b"`
}
var (
options = Options{
WithObjects: NewObjects(objectTest{}),
WithSubObjects: map[string]Objects{
"a": NewObjects(objectB{}),
"b": NewObjects(objectA{}, objectB{}),
},
}
buf = bytes.NewBuffer(nil)
packer = NewPacker(buf, options)
unpacker = NewUnpacker(buf, options)
input any = objectTest{
RawA: &objectB{"Hello"},
RawB: &objectA{"World"},
Slice: []any{
&objectB{"A"},
&objectA{"B"},
&objectB{"C"},
},
Array: [2]any{
&objectA{"D"},
&objectB{"E"},
},
Map: map[string]any{
"F": &objectB{"G"},
"H": &objectA{"I"},
},
}
output any
)
err := packer.Encode(input)
if err != nil {
t.Fatal(err)
}
err = unpacker.Decode(&output)
if err != nil {
t.Error(err)
}
inputType := reflect.TypeOf(input)
if reflect.TypeOf(output) != reflect.PointerTo(inputType) {
t.Errorf("decoded object type should be a pointer to input object type; expected: %s, got: %s",
reflect.PointerTo(inputType).String(), reflect.TypeOf(output).String())
} else {
output = reflect.ValueOf(output).Elem().Interface()
if !reflect.DeepEqual(input, output) {
t.Errorf("decoded object should equal input object; expected: %+v, got: %+v", input, output)
}
}
if buf.Len() > 0 {
t.Errorf("expected all bytes to be consumed after decoding all objects, got %d extra bytes: %q", buf.Len(), buf.Bytes())
}
}