-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode_test.go
330 lines (315 loc) · 6.25 KB
/
decode_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
package fixedwidth
import (
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"log"
"reflect"
"testing"
)
func TestUnmarshal(t *testing.T) {
tests := []struct {
name string
data string
want interface{}
}{
{
name: "single line ",
data: "Huy Đặng 25 Engineer",
want: []person{
{
FirstName: "Huy",
LastName: "Đặng",
Age: 25,
Job: "Engineer",
},
},
},
{
name: "multiple lines",
data: "Huy Dang 25 Engineer\nDidier Drogba 41 Retired \nLâm Đặng 26 Thủ môn ",
want: []person{
{
FirstName: "Huy",
LastName: "Dang",
Age: 25,
Job: "Engineer",
},
{
FirstName: "Didier",
LastName: "Drogba",
Age: 41,
Job: "Retired",
},
{
FirstName: "Lâm",
LastName: "Đặng",
Age: 26,
Job: "Thủ môn",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var p []person
err := Unmarshal([]byte(tt.data), &p)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(p, tt.want) {
t.Error(errors.New("incorrect result"))
}
})
}
t.Run("nested struct with tag", func(t *testing.T) {
want := nestedStructWithTag{Cat: cat{Name: "June", Gender: "mal"}}
var s nestedStructWithTag
err := Unmarshal([]byte("June mal"), &s)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(s, want) {
t.Error(errors.New("incorrect result"))
}
})
t.Run("nested struct without tag", func(t *testing.T) {
want := nestedStructWithoutTag{Cat: cat{Name: "June", Gender: "male"}}
var s nestedStructWithoutTag
err := Unmarshal([]byte("June male "), &s)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(s, want) {
t.Error(errors.New("incorrect result"))
}
})
t.Run("embedded struct with tag", func(t *testing.T) {
want := embeddedStructWithTag{
Number: 15,
person: person{
FirstName: "Drogba",
LastName: "Didie",
Age: 0,
Job: "",
},
}
var s embeddedStructWithTag
err := Unmarshal([]byte("15 Drogba Didie"), &s)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(s, want) {
t.Error(errors.New("incorrect result"))
}
})
t.Run("embedded struct without tag", func(t *testing.T) {
want := embeddedStruct{
Number: 15,
person: person{
FirstName: "Drogba",
LastName: "Didier",
Age: 41,
Job: "Retired",
},
}
var s embeddedStruct
err := Unmarshal([]byte("15 Drogba Didier 41 Retired "), &s)
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(s, want) {
t.Error(errors.New("incorrect result"))
}
})
t.Run("mixed type", func(t *testing.T) {
want := mixedStructForUnmarshal{
F1: "the f",
F2: stringp("sec"),
cat: cat{
Name: "P",
Gender: "female",
},
F3: 10.5,
F4: float64p(7.22),
F5: "what i",
F6: "7",
F7: cat{
Name: "Ali",
Gender: "",
},
F8: &cat{
Name: "wow",
Gender: "male",
},
F9: intp(1),
F10: 2,
F11: int8p(3),
F12: 4,
F13: int16p(5),
F14: 6,
F15: int32p(7),
F16: 8,
F17: int64p(9),
F18: 1,
F19: uintp(2),
F20: 3,
F21: uint8p(4),
F22: 5,
F23: uint16p(6),
F24: 7,
F25: uint32p(8),
F26: 9,
F27: uint64p(10),
F28: float32p(1.12),
F29: 2.23,
F30: stringp(""),
}
var s mixedStruct
err := Unmarshal([]byte("the fsecP female10.57.22what i7 Ali wow male 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1.12 2.23 "), &s)
if err != nil {
t.Error(err)
return
}
if !cmp.Equal(s.F1, want.F1) {
t.Error("incorrect F1")
return
}
if !cmp.Equal(s.F2, want.F2) {
t.Error("incorrect F2")
return
}
if !cmp.Equal(s.cat, want.cat) {
t.Error("incorrect cat")
return
}
if !cmp.Equal(s.F3, want.F3) {
t.Error("incorrect F3")
return
}
if !cmp.Equal(s.F4, want.F4) {
t.Error("incorrect F4")
return
}
if !cmp.Equal(s.F5, want.F5) {
t.Error("incorrect F5")
return
}
if !cmp.Equal(s.F6, want.F6) {
t.Error("incorrect F6")
return
}
if !cmp.Equal(s.F7, want.F7) {
t.Error("incorrect F7")
return
}
if !cmp.Equal(s.F8, want.F8) {
t.Error("incorrect F8")
return
}
if !cmp.Equal(s.F9, want.F9) {
t.Error("incorrect F9")
return
}
if !cmp.Equal(s.F10, want.F10) {
t.Error("incorrect F10")
return
}
if !cmp.Equal(s.F11, want.F11) {
t.Error("incorrect F11")
return
}
if !cmp.Equal(s.F12, want.F12) {
t.Error("incorrect F12")
return
}
if !cmp.Equal(s.F13, want.F13) {
t.Error("incorrect F13")
return
}
if !cmp.Equal(s.F14, want.F14) {
t.Error("incorrect F14")
return
}
if !cmp.Equal(s.F15, want.F15) {
t.Error("incorrect F15")
return
}
if !cmp.Equal(s.F16, want.F16) {
t.Error("incorrect F16")
return
}
if !cmp.Equal(s.F17, want.F17) {
t.Error("incorrect F17")
return
}
if !cmp.Equal(s.F18, want.F18) {
t.Error("incorrect F18")
return
}
if !cmp.Equal(s.F19, want.F19) {
t.Error("incorrect F19")
return
}
if !cmp.Equal(s.F20, want.F20) {
t.Error("incorrect F20")
return
}
if !cmp.Equal(s.F21, want.F21) {
t.Error("incorrect F21")
return
}
if !cmp.Equal(s.F22, want.F22) {
t.Error("incorrect F22")
return
}
if !cmp.Equal(s.F23, want.F23) {
t.Error("incorrect F23")
return
}
if !cmp.Equal(s.F24, want.F24) {
t.Error("incorrect F24")
return
}
if !cmp.Equal(s.F25, want.F25) {
t.Error("incorrect F25")
return
}
if !cmp.Equal(s.F26, want.F26) {
t.Error("incorrect F26")
return
}
if !cmp.Equal(s.F27, want.F27) {
t.Error("incorrect F27")
return
}
if !cmp.Equal(s.F28, want.F28) {
t.Error("incorrect F28")
return
}
if !cmp.Equal(s.F29, want.F29) {
t.Error("incorrect F29")
return
}
if !cmp.Equal(s.F30, want.F30) {
t.Error("incorrect F30")
return
}
})
}
func ExampleUnmarshaler_Unmarshal() {
var p person
m := NewUnmarshaler()
err := m.Unmarshal([]byte("Alexander Goodword 40 Software"), &p)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v", p)
// Output:
// {FirstName:Alexander LastName:Goodword Age:40 Job:Software}
}