-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils_test.go
274 lines (245 loc) · 7.3 KB
/
utils_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
package ucon
import (
"reflect"
"testing"
"time"
)
type valueStringTime time.Time
func (t valueStringTime) ParseString(value string) (interface{}, error) {
v, err := time.Parse("2006-01-02", value)
if err != nil {
return valueStringTime(time.Time{}), err
}
return valueStringTime(v), nil
}
type ValueStringMapperSample struct {
AString string
BString string `json:"bStr"`
DInt8 int8
EInt64 int64
FUint8 uint8
GUint64 uint64
HFloat32 float32
IFloat64 float64
JBool bool
KTime valueStringTime
LPtrBool *bool
MPtrPtrBool **bool
ValueStringMapperSampleInner
}
type ValueStringMapperSampleInner struct {
CString string
}
type ValueStringSliceMapperSample struct {
AStrings []string
BStrings []string `json:"bStrs"`
DInt8s []int8
EInt64s []int64
FUint8s []uint8
GUint64s []uint64
HFloat32s []float32
IFloat64s []float64
JBools []bool
KTimes []valueStringTime
// unsupported
// LPtrBools []*bool
// MPtrPtrBools []**bool
ValueStringSliceMapperSampleInner
YString string
ZString string
}
type ValueStringSliceMapperSampleInner struct {
CStrings []string
}
func TestValueStringMapper(t *testing.T) {
obj := &ValueStringMapperSample{}
target := reflect.ValueOf(obj)
valueStringMapper(target, "AString", "This is A")
valueStringMapper(target, "bStr", "This is B")
valueStringMapper(target, "CString", "This is C")
valueStringMapper(target, "DInt8", "1")
valueStringMapper(target, "EInt64", "2")
valueStringMapper(target, "FUint8", "3")
valueStringMapper(target, "GUint64", "4")
valueStringMapper(target, "HFloat32", "1.25")
valueStringMapper(target, "IFloat64", "2.75")
valueStringMapper(target, "JBool", "true")
valueStringMapper(target, "KTime", "2016-01-07")
valueStringMapper(target, "LPtrBool", "true")
valueStringMapper(target, "MPtrPtrBool", "true")
if obj.AString != "This is A" {
t.Errorf("unexpected A: %v", obj.AString)
}
if obj.BString != "This is B" {
t.Errorf("unexpected B: %v", obj.BString)
}
if obj.CString != "This is C" {
t.Errorf("unexpected C: %v", obj.CString)
}
if obj.DInt8 != 1 {
t.Errorf("unexpected D: %v", obj.DInt8)
}
if obj.EInt64 != 2 {
t.Errorf("unexpected E: %v", obj.EInt64)
}
if obj.FUint8 != 3 {
t.Errorf("unexpected F: %v", obj.FUint8)
}
if obj.GUint64 != 4 {
t.Errorf("unexpected G: %v", obj.GUint64)
}
if obj.HFloat32 != 1.25 {
t.Errorf("unexpected H: %v", obj.HFloat32)
}
if obj.IFloat64 != 2.75 {
t.Errorf("unexpected I: %v", obj.IFloat64)
}
if obj.JBool != true {
t.Errorf("unexpected J: %v", obj.JBool)
}
if y, m, d := time.Time(obj.KTime).Date(); y != 2016 {
t.Errorf("unexpected KTime.y: %v", y)
} else if m != 1 {
t.Errorf("unexpected KTime.m: %v", m)
} else if d != 7 {
t.Errorf("unexpected KTime.d: %v", d)
}
if obj.LPtrBool == nil || *obj.LPtrBool != true {
t.Errorf("unexpected L: %v", obj.LPtrBool)
}
if obj.MPtrPtrBool == nil || *obj.MPtrPtrBool == nil || **obj.MPtrPtrBool != true {
t.Errorf("unexpected M: %v", obj.LPtrBool)
}
}
func TestValueStringSliceMapper(t *testing.T) {
obj := &ValueStringSliceMapperSample{}
target := reflect.ValueOf(obj)
valueStringSliceMapper(target, "AStrings", []string{"This is A1", "This is A2"})
valueStringSliceMapper(target, "bStrs", []string{"This is B1", "This is B2"})
valueStringSliceMapper(target, "CStrings", []string{"This is C1", "This is C2"})
valueStringSliceMapper(target, "DInt8s", []string{"1", "11"})
valueStringSliceMapper(target, "EInt64s", []string{"2", "22"})
valueStringSliceMapper(target, "FUint8s", []string{"3", "33"})
valueStringSliceMapper(target, "GUint64s", []string{"4", "44"})
valueStringSliceMapper(target, "HFloat32s", []string{"1.25", "11.25"})
valueStringSliceMapper(target, "IFloat64s", []string{"2.75", "22.75"})
valueStringSliceMapper(target, "JBools", []string{"true", "false"})
valueStringSliceMapper(target, "KTimes", []string{"2016-01-07", "2016-04-05"})
// valueStringSliceMapper(target, "LPtrBools", []string{"true", "false"})
// valueStringSliceMapper(target, "MPtrPtrBools", []string{"true", "false"})
valueStringSliceMapper(target, "YString", []string{"This is Y"})
valueStringSliceMapper(target, "ZString", []string{})
if len(obj.AStrings) != 2 {
t.Fatalf("unexpected A len: %v", len(obj.AStrings))
}
if obj.AStrings[0] != "This is A1" {
t.Errorf("unexpected A[0]: %v", obj.AStrings[0])
}
if obj.AStrings[1] != "This is A2" {
t.Errorf("unexpected A[1]: %v", obj.AStrings[1])
}
if len(obj.BStrings) != 2 {
t.Fatalf("unexpected B len: %v", len(obj.BStrings))
}
if obj.BStrings[0] != "This is B1" {
t.Errorf("unexpected B[0]: %v", obj.BStrings[0])
}
if obj.BStrings[1] != "This is B2" {
t.Errorf("unexpected B[1]: %v", obj.BStrings[1])
}
if len(obj.CStrings) != 2 {
t.Fatalf("unexpected C len: %v", len(obj.CStrings))
}
if obj.CStrings[0] != "This is C1" {
t.Errorf("unexpected C[0]: %v", obj.CStrings[0])
}
if obj.CStrings[1] != "This is C2" {
t.Errorf("unexpected C[1]: %v", obj.CStrings[1])
}
if len(obj.DInt8s) != 2 {
t.Fatalf("unexpected D len: %v", len(obj.DInt8s))
}
if obj.DInt8s[0] != 1 {
t.Errorf("unexpected D[0]: %v", obj.DInt8s[0])
}
if obj.DInt8s[1] != 11 {
t.Errorf("unexpected D[1]: %v", obj.DInt8s[1])
}
if len(obj.EInt64s) != 2 {
t.Fatalf("unexpected E len: %v", len(obj.EInt64s))
}
if obj.EInt64s[0] != 2 {
t.Errorf("unexpected E[0]: %v", obj.EInt64s[0])
}
if obj.EInt64s[1] != 22 {
t.Errorf("unexpected E[1]: %v", obj.EInt64s[1])
}
if len(obj.FUint8s) != 2 {
t.Fatalf("unexpected F len: %v", len(obj.FUint8s))
}
if obj.FUint8s[0] != 3 {
t.Errorf("unexpected F[0]: %v", obj.FUint8s[0])
}
if obj.FUint8s[1] != 33 {
t.Errorf("unexpected F[1]: %v", obj.FUint8s[1])
}
if len(obj.GUint64s) != 2 {
t.Fatalf("unexpected G len: %v", len(obj.GUint64s))
}
if obj.GUint64s[0] != 4 {
t.Errorf("unexpected G[0]: %v", obj.GUint64s[0])
}
if obj.GUint64s[1] != 44 {
t.Errorf("unexpected G[1]: %v", obj.GUint64s[1])
}
if len(obj.HFloat32s) != 2 {
t.Fatalf("unexpected H len: %v", len(obj.HFloat32s))
}
if obj.HFloat32s[0] != 1.25 {
t.Errorf("unexpected H[0]: %v", obj.HFloat32s[0])
}
if obj.HFloat32s[1] != 11.25 {
t.Errorf("unexpected H[1]: %v", obj.HFloat32s[1])
}
if len(obj.IFloat64s) != 2 {
t.Fatalf("unexpected I len: %v", len(obj.IFloat64s))
}
if obj.IFloat64s[0] != 2.75 {
t.Errorf("unexpected I[0]: %v", obj.IFloat64s[0])
}
if obj.IFloat64s[1] != 22.75 {
t.Errorf("unexpected I[1]: %v", obj.IFloat64s[1])
}
if len(obj.JBools) != 2 {
t.Fatalf("unexpected J len: %v", len(obj.JBools))
}
if obj.JBools[0] != true {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
}
if obj.JBools[1] != false {
t.Errorf("unexpected J[1]: %v", obj.JBools[1])
}
if len(obj.KTimes) != 2 {
t.Fatalf("unexpected K len: %v", len(obj.KTimes))
}
if y, m, d := time.Time(obj.KTimes[0]).Date(); y != 2016 {
t.Errorf("unexpected KTime[0].y: %v", y)
} else if m != 1 {
t.Errorf("unexpected KTime[0].m: %v", m)
} else if d != 7 {
t.Errorf("unexpected KTime[0].d: %v", d)
}
if y, m, d := time.Time(obj.KTimes[1]).Date(); y != 2016 {
t.Errorf("unexpected KTime[1].y: %v", y)
} else if m != 4 {
t.Errorf("unexpected KTime[1].m: %v", m)
} else if d != 5 {
t.Errorf("unexpected KTime[1].d: %v", d)
}
if obj.YString != "This is Y" {
t.Errorf("unexpected Y: %v", obj.YString)
}
if obj.ZString != "" {
t.Errorf("unexpected Z: %v", obj.ZString)
}
}