-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_test.go
302 lines (245 loc) · 6.64 KB
/
bind_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
package grocery
import (
"strconv"
"testing"
"time"
)
type CustomString string
type CustomInt int
type CustomFloat float64
const (
customStringVal CustomString = "asdf"
customIntVal CustomInt = 5
customFloatVal CustomFloat = 3.2
)
type BindTestEmbeddedStruct struct {
ES string `grocery:"es"`
}
type bindTestStruct struct {
// Embedded struct
BindTestEmbeddedStruct
// Primitives
S string `grocery:"s"`
P *string `grocery:"p"`
I int `grocery:"i"`
I8 int8 `grocery:"i8"`
I16 int16 `grocery:"i16"`
I32 int32 `grocery:"i32"`
I64 int64 `grocery:"i64"`
U uint `grocery:"u"`
U8 uint8 `grocery:"u8"`
U16 uint16 `grocery:"u16"`
U32 uint32 `grocery:"u32"`
U64 uint64 `grocery:"u64"`
F32 float32 `grocery:"f32"`
F64 float64 `grocery:"f64"`
B bool `grocery:"b"`
// Custom types
CS CustomString `grocery:"cs"`
CI CustomInt `grocery:"ci"`
CF CustomFloat `grocery:"cf"`
// Timestamp
T time.Time `grocery:"t"`
// Don't store
NoStore string
NoStoreDash string `grocery:"-"`
// Can't set unexported values
unexported string `grocery:"us"`
}
func TestEmbedding(t *testing.T) {
data := map[string]string{
"es": "asdf",
}
ptr := new(bindTestStruct)
err := bind("", "", data, ptr)
if err != nil {
t.Errorf("embedding FAILED, got error %v", err)
}
if data["es"] == ptr.ES {
t.Log("embedded string PASSED")
} else {
t.Errorf("embedded string FAILED, expected %v but got %v", data["es"], ptr.ES)
}
}
func TestPrimitives(t *testing.T) {
data := map[string]string{
"s": "asdf",
"p": "asdfg",
"i": "-4",
"i8": "-34",
"i16": "-5432",
"i32": "-235873",
"i64": "-4300000000",
"u": "4",
"u8": "34",
"u16": "65432",
"u32": "235873",
"u64": "4300000000",
"f32": "329.14",
"f64": "-2190.3895",
"b": "true",
}
ptr := new(bindTestStruct)
ptr.P = new(string)
err := bind("", "", data, ptr)
if err != nil {
t.Errorf("primitives FAILED, got error %v", err)
}
if data["s"] != ptr.S {
t.Errorf("string FAILED, expected %v but got %v", data["s"], ptr.S)
}
if data["p"] != *ptr.P {
t.Errorf("string pointer FAILED, expected %v but got %v", data["p"], *ptr.P)
}
iVal, _ := strconv.ParseInt(data["i"], 10, 64)
if int(iVal) != ptr.I {
t.Errorf("int FAILED, expected %v but got %v", data["i"], ptr.I)
}
i8Val, _ := strconv.ParseInt(data["i8"], 10, 8)
if int8(i8Val) != ptr.I8 {
t.Errorf("int8 FAILED, expected %v but got %v", data["i8"], ptr.I8)
}
i16Val, _ := strconv.ParseInt(data["i16"], 10, 16)
if int16(i16Val) != ptr.I16 {
t.Errorf("int16 FAILED, expected %v but got %v", data["i16"], ptr.I16)
}
i32Val, _ := strconv.ParseInt(data["i32"], 10, 32)
if int32(i32Val) != ptr.I32 {
t.Errorf("int32 FAILED, expected %v but got %v", data["i32"], ptr.I32)
}
i64Val, _ := strconv.ParseInt(data["i64"], 10, 64)
if int64(i64Val) != ptr.I64 {
t.Errorf("int64 FAILED, expected %v but got %v", data["i64"], ptr.I64)
}
uVal, _ := strconv.ParseUint(data["u"], 10, 64)
if uint(uVal) != ptr.U {
t.Errorf("uint FAILED, expected %v but got %v", data["u"], ptr.U)
}
u8Val, _ := strconv.ParseUint(data["u8"], 10, 8)
if uint8(u8Val) != ptr.U8 {
t.Errorf("uint8 FAILED, expected %v but got %v", data["u8"], ptr.U8)
}
u16Val, _ := strconv.ParseUint(data["u16"], 10, 16)
if uint16(u16Val) != ptr.U16 {
t.Errorf("uint16 FAILED, expected %v but got %v", data["u16"], ptr.U16)
}
u32Val, _ := strconv.ParseUint(data["u32"], 10, 32)
if uint32(u32Val) != ptr.U32 {
t.Errorf("uint32 FAILED, expected %v but got %v", data["u32"], ptr.U32)
}
u64Val, _ := strconv.ParseUint(data["u64"], 10, 64)
if uint64(u64Val) != ptr.U64 {
t.Errorf("uint64 FAILED, expected %v but got %v", data["u64"], ptr.U64)
}
f32Val, _ := strconv.ParseFloat(data["f32"], 32)
if float32(f32Val) != ptr.F32 {
t.Errorf("float32 FAILED, expected %v but got %v", data["f32"], ptr.F32)
}
f64Val, _ := strconv.ParseFloat(data["f64"], 64)
if float64(f64Val) != ptr.F64 {
t.Errorf("float64 FAILED, expected %v but got %v", data["f64"], ptr.F64)
}
bVal, _ := strconv.ParseBool(data["b"])
if bVal != ptr.B {
t.Errorf("bool FAILED, expected %v but got %v", data["b"], ptr.B)
}
}
func TestCustomMapType(t *testing.T) {
data := map[string]string{
"cs": string(customStringVal),
"ci": strconv.Itoa(int(customIntVal)),
"cf": strconv.FormatFloat(float64(customFloatVal), 'f', 10, 64),
}
ptr := new(bindTestStruct)
err := bind("", "", data, ptr)
if err != nil {
t.Errorf("custom map type FAILED, got error %v", err)
}
if data["cs"] == string(ptr.CS) {
t.Log("custom string PASSED")
} else {
t.Errorf("custom string FAILED, expected %v but got %v", data["cs"], ptr.CS)
}
ciVal, _ := strconv.ParseInt(data["ci"], 10, 64)
if int(ciVal) == int(ptr.CI) {
t.Log("custom int PASSED")
} else {
t.Errorf("custom int FAILED, expected %v but got %v", data["ci"], ptr.CI)
}
cfVal, _ := strconv.ParseFloat(data["cf"], 64)
if cfVal == float64(ptr.CF) {
t.Log("custom float PASSED")
} else {
t.Errorf("custom float FAILED, expected %v but got %v", data["cf"], ptr.CF)
}
}
func TestUnexported(t *testing.T) {
data := map[string]string{
"us": "unexported",
}
ptr := new(bindTestStruct)
err := bind("", "", data, ptr)
if err != nil {
t.Errorf("unexported FAILED, got error %v", err)
}
if ptr.unexported == "" {
t.Log("unexported PASSED")
} else {
t.Errorf("unexported FAILED, expected %v but got %v", "", ptr.unexported)
}
}
func TestTimestamp(t *testing.T) {
data := map[string]string{
"t": "963210120",
}
ptr := new(bindTestStruct)
err := bind("", "", data, ptr)
if err != nil {
t.Errorf("timestamp FAILED, got error %v", err)
}
tVal, _ := strconv.ParseInt(data["t"], 10, 64)
if tVal == ptr.T.Unix() {
t.Log("timestamp PASSED")
} else {
t.Errorf("timestamp FAILED, expected %v but got %v", data["t"], ptr.T)
}
}
func TestInvalidValue(t *testing.T) {
data := map[string]string{
"i": "asdf",
}
ptr := new(bindTestStruct)
err := bind("", "", data, ptr)
if err != nil {
t.Log("invalid value PASSED")
} else {
t.Error("invalid value FAILED, expecting error")
}
}
func TestInvalidPointer(t *testing.T) {
data := map[string]string{
"s": "asdf",
}
err := bind("", "", data, nil)
if err != nil {
t.Log("nil pointer PASSED")
} else {
t.Error("nil pointer FAILED, expecting error")
}
err = bind("", "", data, 4)
if err != nil {
t.Log("nil pointer PASSED")
} else {
t.Error("nil pointer FAILED, expecting error")
}
}
func TestEmptyData(t *testing.T) {
emptyData := map[string]string{}
ptr := new(bindTestStruct)
err := bind("", "", emptyData, ptr)
if err != nil {
t.Log("empty data PASSED")
} else {
t.Error("empty data FAILED, expecting error")
}
}