-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflected_value_test.go
373 lines (344 loc) · 8.32 KB
/
reflected_value_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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package structflag_test
import (
"flag"
"reflect"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/surajbarkale/structflag"
)
func reflectValue(x interface{}) flag.Value {
return structflag.NewReflectedValue(reflect.ValueOf(x).Elem(), "")
}
func TestGetValueAsString(t *testing.T) {
type ts struct {
A string
N int
K struct {
X float32
Y float32
}
}
var np *int
var tr = true
var f = false
var i int = 124
var ip = &i
var i32 int32 = -8413
var str string = "hjb ubv73 ,svu83 "
var list = []float32{0.1, .33, .24, 14215125, 235.58e3}
var s = &ts{
A: "shvb",
N: 325,
K: struct {
X float32
Y float32
}{
X: 45.5,
Y: 3.157,
},
}
tests := []struct {
name string
value interface{}
expected string
}{
{"nil", &np, ""},
{"true", &tr, "true"},
{"false", &f, "false"},
{"int", &i, "124"},
{"intptr", &ip, "124"},
{"int32", &i32, "-8413"},
{"string", &str, "hjb ubv73 ,svu83 "},
{"list", &list, "[0.1,0.33,0.24,14215125,235580]"},
{"struct", &s, `{"A":"shvb","N":325,"K":{"X":45.5,"Y":3.157}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := reflectValue(tt.value).String()
assert.Equal(t, tt.expected, res)
})
}
}
func TestSetStringValue(t *testing.T) {
src := "test-string abc "
var val string
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, src, val)
var ptr *string
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, src, *ptr)
}
func TestSetJSONStringValue(t *testing.T) {
exp := "mnbc3yu uqhiq q 33u "
src := `"` + exp + `"`
var val string
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, exp, val)
}
func TestSetShouldNotReplacePointer(t *testing.T) {
src := "test-string abc"
var val string
ptr := &val
require.NoError(t, reflectValue(&ptr).Set(src))
assert.Equal(t, ptr, &val)
assert.Equal(t, src, *ptr)
}
func TestErrorOnIntOverflow(t *testing.T) {
var i int8
assert.Error(t, reflectValue(&i).Set("555"))
var j uint8
assert.Error(t, reflectValue(&j).Set("555"))
}
func TestSetMapFromJSON(t *testing.T) {
exp := map[string]string{
"a": "x",
"b": "y",
}
src := `{"a": "x", "b": "y"}`
var val map[string]string
var ptr *map[string]string
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, val, exp)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, exp, *ptr)
}
func TestSetStructFromJSON(t *testing.T) {
type ts struct {
X int
Y int
Str string
}
exp := ts{6, 2, "data"}
src := `{"x": 6, "y": 2, "str": "data"}`
var val ts
var ptr *ts
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, val, exp)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, exp, *ptr)
}
func TestSetIntSliceFromJSON(t *testing.T) {
exp := []int{1, 2, 4, 4}
src := "[1, 2, 4, 4]"
var val []int
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, val, exp)
}
func TestSetStringSliceFromJSON(t *testing.T) {
exp := []string{"a", "p", "cd", "b"}
src := `["a", "p", "cd", "b"]`
var val []string
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, val, exp)
}
func TestSetFloat32SliceFromJSON(t *testing.T) {
exp := []float32{0.5, 0.9, 123, 1e-7}
src := "[0.5, 0.9, 123, 1e-7]"
var val []float32
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, val, exp)
}
func TestSetSliceOfSliceFromJSON(t *testing.T) {
exp := [][]string{{"a", "b"}, {"x"}, {"p", "q", "r"}}
src := `[["a", "b"], ["x"], ["p", "q", "r"]]`
var val [][]string
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, val, exp)
}
func TestSetReturnsErrorOnInvalidJSON(t *testing.T) {
src := "[0.5, 0.9, 123, 1e-7"
var val []float32
assert.Error(t, reflectValue(&val).Set(src))
}
func TestSetBoolValueWithStrconv(t *testing.T) {
tests := []string{
"true",
"TRUE",
"t",
"T",
"1",
"false",
"FALSE",
"f",
"F",
"0",
"yes",
"no",
}
for _, src := range tests {
t.Run("bool "+src, func(t *testing.T) {
exp, expErr := strconv.ParseBool(src)
var val bool
var ptr *bool
if expErr != nil {
require.Error(t, reflectValue(&val).Set(src))
require.Error(t, reflectValue(&ptr).Set(src))
} else {
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, exp, val)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, exp, *ptr)
}
})
}
}
func TestSetIntValueWithStrconv(t *testing.T) {
tests := []string{
"invalid",
"0",
"+0",
"-0",
"1343",
"+961343",
"-542253",
"7553432235352",
"invalid",
}
for _, src := range tests {
t.Run("int "+src, func(t *testing.T) {
exp, expErr := strconv.ParseInt(src, 10, 64)
var val int
var ptr *int
if expErr != nil {
require.Error(t, reflectValue(&val).Set(src))
require.Error(t, reflectValue(&ptr).Set(src))
} else {
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, int(exp), val)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, int(exp), *ptr)
}
})
}
for _, src := range tests {
t.Run("int32 "+src, func(t *testing.T) {
exp, expErr := strconv.ParseInt(src, 10, 32)
var val int32
var ptr *int32
if expErr != nil {
require.Error(t, reflectValue(&val).Set(src))
require.Error(t, reflectValue(&ptr).Set(src))
} else {
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, int32(exp), val)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, int32(exp), *ptr)
}
})
}
}
func TestSetUintValueWithStrconv(t *testing.T) {
tests := []string{
"0",
"1343",
"7553432235352",
"+1",
"-1",
"invalid",
}
for _, src := range tests {
t.Run("uint "+src, func(t *testing.T) {
exp, expErr := strconv.ParseUint(src, 10, 64)
var val uint
var ptr *uint
if expErr != nil {
require.Error(t, reflectValue(&val).Set(src))
require.Error(t, reflectValue(&ptr).Set(src))
} else {
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, uint(exp), val)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, uint(exp), *ptr)
}
})
}
for _, src := range tests {
t.Run("uint32 "+src, func(t *testing.T) {
exp, expErr := strconv.ParseUint(src, 10, 32)
var val uint32
var ptr *uint32
if expErr != nil {
require.Error(t, reflectValue(&val).Set(src))
require.Error(t, reflectValue(&ptr).Set(src))
} else {
require.NoError(t, reflectValue(&val).Set(src))
assert.Equal(t, uint32(exp), val)
require.NoError(t, reflectValue(&ptr).Set(src))
require.NotNil(t, ptr)
assert.Equal(t, uint32(exp), *ptr)
}
})
}
}
func TestSetFloat32ValueWithStrconv(t *testing.T) {
type entry struct {
name string
src string
}
tests := []entry{
{"zero", "0"},
{"inf", "inf"},
{"positive", "1343"},
{"exponent", "325.687e32"},
{"large", "7553432235352e-156"},
{"invalid", "invalid"},
{"overflow", "1e50"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exp, expErr := strconv.ParseFloat(tt.src, 32)
var val float32
var ptr *float32
if expErr != nil {
require.Error(t, reflectValue(&val).Set(tt.src))
require.Error(t, reflectValue(&ptr).Set(tt.src))
} else {
require.NoError(t, reflectValue(&val).Set(tt.src))
assert.Equal(t, float32(exp), val)
require.NoError(t, reflectValue(&ptr).Set(tt.src))
require.NotNil(t, ptr)
assert.Equal(t, float32(exp), *ptr)
}
})
}
}
func TestSetFloat64ValueWithStrconv(t *testing.T) {
type entry struct {
name string
src string
}
tests := []entry{
{"zero", "0"},
{"inf", "inf"},
{"positive", "1343"},
{"exponent", "325.687e32"},
{"large", "7553432235352e-156"},
{"invalid", "invalid"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exp, expErr := strconv.ParseFloat(tt.src, 64)
var val float64
var ptr *float64
if expErr != nil {
require.Error(t, reflectValue(&val).Set(tt.src))
require.Error(t, reflectValue(&ptr).Set(tt.src))
} else {
require.NoError(t, reflectValue(&val).Set(tt.src))
assert.Equal(t, exp, val)
require.NoError(t, reflectValue(&ptr).Set(tt.src))
require.NotNil(t, ptr)
assert.Equal(t, exp, *ptr)
}
})
}
}