-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal_test.go
334 lines (311 loc) · 10.2 KB
/
unmarshal_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
package redmap_test
import (
"errors"
"fmt"
"reflect"
"strconv"
"testing"
"github.com/livingsilver94/redmap"
)
// stubTextUnmarshaler implements the encoding.TextUnmarshaler interface.
type stubTextUnmarshaler struct{ S string }
func (s *stubTextUnmarshaler) UnmarshalText(text []byte) error {
s.S = string(text)
return nil
}
// stubIntTextUnmarshaler is an int that implements encoding.TextUnmarshaler,
// so that we can test if a non-struct type is correctly handled as an interface.
type stubIntTextUnmarshaler int
func (s *stubIntTextUnmarshaler) UnmarshalText(text []byte) error {
v, err := strconv.Atoi(string(text))
if err != nil {
return err
}
*s = stubIntTextUnmarshaler(v)
return nil
}
// stubMapMarshaler implements the redmap.StringMapUnmarshaler interface.
type stubMapUnmarshaler struct {
Field1 string
Field2 string
}
func (s *stubMapUnmarshaler) UnmarshalStringMap(mp map[string]string) error {
s.Field1 = mp["Field1"]
s.Field2 = mp["Field2"]
return nil
}
// StubIntUnmarshaler is an int that implements redmap.StringMapUnmarshaler,
// so that we can test if a non-struct type is correctly handled as an interface.
type stubIntMapUnmarshaler int
func (s *stubIntMapUnmarshaler) UnmarshalStringMap(mp map[string]string) error {
v, err := strconv.Atoi(mp["Field1"])
if err != nil {
return err
}
*s = stubIntMapUnmarshaler(v)
return nil
}
// emptyMap is a non-nil, zero-length map.
var emptyMap = make(map[string]string)
func TestUnmarshalValidType(t *testing.T) {
tests := []interface{}{
stubStringer{},
&stubStringer{},
}
for _, test := range tests {
val := reflect.New(reflect.TypeOf(test))
val.Elem().Set(reflect.ValueOf(test))
if err := redmap.Unmarshal(emptyMap, val.Interface()); err != nil {
t.Fatalf("Unmarshal() of valid type must not return error. Returned %q", err)
}
}
}
func TestUnmarshalNil(t *testing.T) {
err := redmap.Unmarshal(nil, &stubStringer{})
if !errors.Is(err, redmap.ErrNilValue) {
t.Fatal("Unmarshal() with a nil map did not return the specific error")
}
err = redmap.Unmarshal(emptyMap, nil)
if !errors.Is(err, redmap.ErrNilValue) {
t.Fatal("Unmarshal() with invalid target did not return the specific error")
}
var nilPtr *stubStringer
err = redmap.Unmarshal(emptyMap, nilPtr)
if !errors.Is(err, redmap.ErrNilValue) {
t.Fatal("Unmarshal() with nil pointer did not return the specific error")
}
}
func TestUnmarshalInvalidType(t *testing.T) {
tests := []struct {
val func() reflect.Value
expErr error
}{
{
// No pointer passed.
val: func() reflect.Value { return reflect.ValueOf(100) },
expErr: redmap.ErrNotPointer},
{
// Int is not a struct nor StringMapUnmarshaler.
val: func() reflect.Value {
v := 100
return reflect.ValueOf(&v)
},
expErr: redmap.ErrNoCodec},
{
// Interface is not a struct nor StringMapUnmarshaler.
val: func() reflect.Value {
v := fmt.Stringer(stubStringer{})
return reflect.ValueOf(&v)
},
expErr: redmap.ErrNoCodec},
}
for _, test := range tests {
err := redmap.Unmarshal(emptyMap, test.val().Interface())
if !errors.Is(err, test.expErr) {
t.Fatalf("Unmarshal returned %q but should have returned %q", err, test.expErr)
}
}
}
func TestUnmarshalScalars(t *testing.T) {
tests := []struct {
In map[string]string
Out interface{}
}{
{In: map[string]string{"V": "true"}, Out: struct{ V bool }{true}},
{In: map[string]string{"V": "100"}, Out: struct{ V int }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V int8 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V int16 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V int32 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V int64 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V uint }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V uint8 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V uint16 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V uint32 }{100}},
{In: map[string]string{"V": "100"}, Out: struct{ V uint64 }{100}},
{In: map[string]string{"V": "100.1"}, Out: struct{ V float32 }{100.1}},
{In: map[string]string{"V": "100.1"}, Out: struct{ V float64 }{100.1}},
{In: map[string]string{"V": "(100.1+80.1i)"}, Out: struct{ V complex64 }{100.1 + 80.1i}},
{In: map[string]string{"V": "(100.1+80.1i)"}, Out: struct{ V complex128 }{100.1 + 80.1i}},
{In: map[string]string{"V": "str"}, Out: struct{ V string }{"str"}},
{In: map[string]string{"V": "a test"}, Out: struct{ V stubTextUnmarshaler }{stubTextUnmarshaler{S: "a test"}}},
{In: map[string]string{"V": "100"}, Out: struct{ V stubIntTextUnmarshaler }{stubIntTextUnmarshaler(100)}},
}
for _, test := range tests {
zero := reflect.New(reflect.TypeOf(test.Out))
err := redmap.Unmarshal(test.In, zero.Interface())
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(zero.Elem().Interface(), test.Out) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", test.In, test.Out, zero)
}
}
}
func TestUnmarshalInnerStructs(t *testing.T) {
type (
Inner1Level = struct {
String string
}
Inner2Level = struct {
Inner Inner1Level `redmap:",inline"`
}
Root1Level = struct {
Inner Inner1Level `redmap:",inline"`
}
Root2Level = struct {
Inner Inner2Level `redmap:",inline"`
}
RootWithPointer = struct {
Inner *Inner1Level `redmap:",inline"`
}
)
tests := []struct {
In map[string]string
Out interface{}
}{
{In: map[string]string{"Inner.String": "oneLevel"}, Out: Root1Level{Inner: Inner1Level{String: "oneLevel"}}},
{In: map[string]string{"Inner.Inner.String": "twoLevel"}, Out: Root2Level{Inner: Inner2Level{Inner: Inner1Level{String: "twoLevel"}}}},
{In: map[string]string{"Inner.String": "oneLevel"}, Out: RootWithPointer{Inner: &Inner1Level{String: "oneLevel"}}},
}
for _, test := range tests {
zero := reflect.New(reflect.TypeOf(test.Out))
err := redmap.Unmarshal(test.In, zero.Interface())
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(zero.Elem().Interface(), test.Out) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", test.In, test.Out, zero)
}
}
}
func TestUnmarshalUnexported(t *testing.T) {
mp := map[string]string{"Exp": "atest"}
tests := []struct {
Out interface{}
}{
{Out: struct {
Exp string
unexp string
}{Exp: "atest"}},
{Out: struct {
Exp string
unexp *string
}{Exp: "atest"}},
{Out: struct {
Exp string
unexp stubTextUnmarshaler
}{Exp: "atest"}},
}
for _, test := range tests {
zero := reflect.New(reflect.TypeOf(test.Out))
err := redmap.Unmarshal(mp, zero.Interface())
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(zero.Elem().Interface(), test.Out) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", mp, test.Out, zero)
}
}
}
func TestUnmarshalWithTags(t *testing.T) {
expected := struct {
DefaultName string
Renamed string `redmap:"customname"`
Ignored string `redmap:"-"`
OmittedString string `redmap:",omitempty"`
}{
DefaultName: "defaultname",
Renamed: "renamed",
Ignored: "ignored",
OmittedString: "should not change",
}
mp := map[string]string{
"DefaultName": "defaultname",
"customname": "renamed",
"Ignored": "should be ignored",
}
copy := expected
err := redmap.Unmarshal(mp, ©)
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(copy, expected) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", mp, expected, copy)
}
}
func TestUnmarshalMapUnmarshaler(t *testing.T) {
intUn := stubIntMapUnmarshaler(666)
tests := []struct {
In map[string]string
Out redmap.StringMapUnmarshaler
}{
{In: map[string]string{"Field1": "value1", "Field2": "value2"}, Out: &stubMapUnmarshaler{Field1: "value1", Field2: "value2"}},
{In: map[string]string{"Field1": "666"}, Out: &intUn},
}
for _, test := range tests {
actual := reflect.New(reflect.TypeOf(test.Out).Elem())
err := redmap.Unmarshal(test.In, actual.Interface())
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(actual.Interface(), test.Out) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", test.In, test.Out, actual)
}
}
}
func TestUnmarshalInnerMapUnmarshaler(t *testing.T) {
tests := []struct {
In map[string]string
Out interface{}
}{
{In: map[string]string{"RegularField": "regular", "Struct.Field1": "value1", "Struct.Field2": "value2"},
Out: struct {
RegularField string
Struct stubMapUnmarshaler `redmap:",inline"`
}{"regular", stubMapUnmarshaler{Field1: "value1", Field2: "value2"}},
},
{In: map[string]string{"RegularField": "regular", "Inner.Field1": "666"},
Out: struct {
RegularField string
Inner stubIntMapUnmarshaler `redmap:",inline"`
}{"regular", stubIntMapUnmarshaler(666)},
},
}
for _, test := range tests {
actual := reflect.New(reflect.TypeOf(test.Out))
err := redmap.Unmarshal(test.In, actual.Interface())
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(actual.Elem().Interface(), test.Out) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", test.In, test.Out, actual)
}
}
}
func TestUnmarshalNilField(t *testing.T) {
var (
zero = 0
zeroDouble = &zero
)
type stru struct {
Field *int
FieldOmit *int `redmap:",omitempty"`
FieldDouble **int
FieldOmitDouble **int `redmap:",omitempty"`
}
expected := stru{
Field: &zero,
FieldOmit: nil,
FieldDouble: &zeroDouble,
FieldOmitDouble: nil,
}
mp := map[string]string{"Field": "0", "FieldDouble": "0"}
var out stru
err := redmap.Unmarshal(mp, &out)
if err != nil {
t.Fatalf("Unmarshal returned unexpected error %q", err)
}
if !reflect.DeepEqual(out, expected) {
t.Fatalf("Unmarshal's output doesn't match the expected value\n\tIn: %v\n\tExpected: %v\n\tOut: %v", mp, expected, out)
}
}