-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdecoder_test.go
263 lines (243 loc) · 5.07 KB
/
decoder_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
package fwencoder
import (
"fmt"
"math"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type TestStruct struct {
String string
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
Date time.Time `json:"Time"`
Birthday time.Time `column:"CustomDate" format:"02/01/2006"`
PString *string
PBool *bool
PInt8 *int8
PUint8 *uint8
PFloat32 *float32
PBirthday *time.Time `format:"02/01/2006"`
Default int
JSONArr []int
JSONPtr *[]int
}
func TestUnmarshal_Success(t *testing.T) {
b, err := os.ReadFile("./testdata/correct_all_supported.txt")
if assert.NoError(t, err) {
s := "Test Ptr String"
bb := false
i := int8(15)
ui := uint8(16)
f := float32(15.5)
d := time.Date(2017, 12, 28, 0, 0, 0, 0, time.UTC)
expected := []TestStruct{{
String: "Test String",
Bool: true,
Int: -1,
Int8: -2,
Int16: -3,
Int32: -4,
Int64: -5,
Uint: 1,
Uint8: 2,
Uint16: 3,
Uint32: 4,
Uint64: 5,
Float32: 1.5,
Float64: 2.5,
Date: time.Date(2017, 12, 27, 13, 48, 3, 0, time.UTC),
Birthday: time.Date(2017, 12, 27, 0, 0, 0, 0, time.UTC),
PString: &s,
PBool: &bb,
PInt8: &i,
PUint8: &ui,
PFloat32: &f,
PBirthday: &d,
JSONArr: []int{1, 2, 3},
JSONPtr: &[]int{4, 5, 6},
}}
var obtained []TestStruct
if err := Unmarshal(b, &obtained); assert.NoError(t, err) {
assert.Equal(t, expected, obtained)
}
}
}
func TestUnmarshal_Ptr_Success(t *testing.T) {
b, err := os.ReadFile("./testdata/correct_all_supported.txt")
if assert.NoError(t, err) {
s := "Test Ptr String"
bb := false
i := int8(15)
ui := uint8(16)
f := float32(15.5)
d := time.Date(2017, 12, 28, 0, 0, 0, 0, time.UTC)
expected := []*TestStruct{{
String: "Test String",
Bool: true,
Int: -1,
Int8: -2,
Int16: -3,
Int32: -4,
Int64: -5,
Uint: 1,
Uint8: 2,
Uint16: 3,
Uint32: 4,
Uint64: 5,
Float32: 1.5,
Float64: 2.5,
Date: time.Date(2017, 12, 27, 13, 48, 3, 0, time.UTC),
Birthday: time.Date(2017, 12, 27, 0, 0, 0, 0, time.UTC),
PString: &s,
PBool: &bb,
PInt8: &i,
PUint8: &ui,
PFloat32: &f,
PBirthday: &d,
JSONArr: []int{1, 2, 3},
JSONPtr: &[]int{4, 5, 6},
}}
var obtained []*TestStruct
if err := Unmarshal(b, &obtained); assert.NoError(t, err) {
assert.Equal(t, expected, obtained)
}
}
}
type BadData struct {
Data []byte
Error string
}
func TestUnmarshal_Error(t *testing.T) {
badData := []BadData{
{
Data: []byte(`
Int
5.3`),
Error: `filed casting "5.3" to "Int:int"`,
},
{
Data: []byte(`
Bool
5.3 `),
Error: `filed casting "5.3" to "Bool:bool"`,
},
{
Data: []byte(`
Uint
5.3 `),
Error: `filed casting "5.3" to "Uint:uint"`,
},
{
Data: []byte(`
Float32
hello `),
Error: `filed casting "hello" to "Float32:float32"`,
},
{
Data: []byte(`
Time
5.3 `),
Error: `filed casting "5.3" to "Date:time.Time"`,
},
{
Data: []byte(`
Int
5`),
Error: `wrong data length in line 2`,
},
{
Data: []byte(`
Int8
5123`),
Error: `is too big for field Int8:int8`,
},
{
Data: []byte(`
Uint8
5123 `),
Error: `is too big for field Uint8:uint8`,
},
{
Data: []byte(fmt.Sprintf(" %-309s\n%.0f", "Float32", math.MaxFloat64)),
Error: `is too big for field Float32:float32`,
},
}
for _, data := range badData {
var obtained []TestStruct
err := Unmarshal(data.Data[1:], &obtained)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), data.Error)
}
}
}
func TestIncorrectInput(t *testing.T) {
errs := []error{
Unmarshal(nil, 1),
Unmarshal(nil, nil),
Unmarshal(nil, new(string)),
Unmarshal(nil, &([]int{})),
}
for _, err := range errs {
assert.EqualError(t, err, ErrIncorrectInputValue.Error())
}
type B struct {
Int int `column:")Float32"`
}
type A struct {
Float32 B
}
err := Unmarshal([]byte("Float32\nhello "), &([]A{}))
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "error in line 2: can't unmarshal")
}
err = Unmarshal([]byte("Float32\nhello "), &([]B{}))
if assert.Error(t, err) {
assert.Contains(t, err.Error(), "error parsing regexp")
}
}
func TestPtrFieldsOverflow(t *testing.T) {
badData := []BadData{
{
Data: []byte(`
Int8
5123`),
Error: `is too big for field Int8:*int8`,
},
{
Data: []byte(`
Uint8
5123 `),
Error: `is too big for field Uint8:*uint8`,
},
{
Data: []byte(fmt.Sprintf(" %-309s\n%.0f", "Float32", math.MaxFloat64)),
Error: `is too big for field Float32:*float32`,
},
}
// Pointer fields overflow
type A struct {
Int8 *int8
Uint8 *uint8
Float32 *float32
}
for _, data := range badData {
var obtained []A
err := Unmarshal(data.Data[1:], &obtained)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), data.Error)
}
}
}