-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
288 lines (276 loc) · 6.87 KB
/
parser_test.go
File metadata and controls
288 lines (276 loc) · 6.87 KB
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
package mageconfig
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetTagOrDefault(t *testing.T) {
testCases := []struct {
field reflect.StructField
tag string
want string
}{
{
field: reflect.StructField{Name: "TestField", Tag: `arg:"testArg"`},
tag: "arg",
want: "testArg",
},
{
field: reflect.StructField{Name: "TestField", Tag: `arg:""`},
tag: "arg",
want: "testfield",
},
}
for _, testCase := range testCases {
got := getTagOrDefault(testCase.field, testCase.tag)
assert.Equal(t, testCase.want, got)
}
}
func TestSetFields(t *testing.T) {
type TestConfig struct {
A string `file:"a"`
B int `file:"b"`
}
testCases := []struct {
name string
cfg Config
setValue func(field reflect.StructField, value reflect.Value) error
err string
}{
{
name: "valid configuration",
cfg: &TestConfig{},
setValue: func(field reflect.StructField, value reflect.Value) error {
switch field.Name {
case "A":
value.SetString("Test")
case "B":
value.SetInt(42)
default:
return fmt.Errorf("unexpected field: %s", field.Name)
}
return nil
},
err: "",
},
{
name: "set value returns error",
cfg: &TestConfig{},
setValue: func(field reflect.StructField, value reflect.Value) error {
return fmt.Errorf("forced error")
},
err: "forced error",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := setFields(tc.cfg, tc.setValue)
if tc.err != "" {
assert.Error(t, err)
assert.Equal(t, tc.err, err.Error())
} else {
assert.NoError(t, err)
cfg := tc.cfg.(*TestConfig)
assert.Equal(t, "Test", cfg.A)
assert.Equal(t, 42, cfg.B)
}
})
}
}
func TestSetFieldByKind(t *testing.T) {
type TestConfig struct {
StringSlice []string `default:"one,two,three"`
IntMap map[string]int `default:"key1:1,key2:2,key3:3"`
String string `default:"test"`
}
testCases := []struct {
name string
field reflect.StructField
value reflect.Value
strVal string
err string
}{
{
name: "slice field",
field: reflect.TypeOf(TestConfig{}).Field(0),
value: reflect.New(reflect.TypeOf(TestConfig{}).Field(0).Type).Elem(),
strVal: "one,two,three",
err: "",
},
{
name: "map field",
field: reflect.TypeOf(TestConfig{}).Field(1),
value: reflect.New(reflect.TypeOf(TestConfig{}).Field(1).Type).Elem(),
strVal: "key1:1,key2:2,key3:3",
err: "",
},
{
name: "string field",
field: reflect.TypeOf(TestConfig{}).Field(2),
value: reflect.New(reflect.TypeOf(TestConfig{}).Field(2).Type).Elem(),
strVal: "test",
err: "",
},
{
name: "invalid map field",
field: reflect.TypeOf(TestConfig{}).Field(1),
value: reflect.New(reflect.TypeOf(TestConfig{}).Field(1).Type).Elem(),
strVal: "key1,key2:2,key3:3", // Missing value for key1.
err: "invalid map default value: key1",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := setFieldByKind(tc.field, tc.value, tc.strVal)
if tc.err != "" {
assert.Error(t, err)
assert.Equal(t, tc.err, err.Error())
} else {
assert.NoError(t, err)
// Verify the values are set correctly.
switch v := tc.value.Interface().(type) {
case []string:
assert.Equal(t, strings.Split(tc.strVal, sliceSeparator), v)
case map[string]int:
expected := make(map[string]int)
for _, pair := range strings.Split(tc.strVal, sliceSeparator) {
kv := strings.Split(pair, kvSeparator)
if len(kv) == 2 {
i, _ := strconv.Atoi(kv[1])
expected[kv[0]] = i
}
}
assert.Equal(t, expected, v)
case string:
assert.Equal(t, tc.strVal, v)
}
}
})
}
}
func TestParseStringToType(t *testing.T) {
testCases := []struct {
name string
s string
t reflect.Type
value reflect.Value
err error
}{
{
name: "parse string to time.Duration",
s: "1h30m",
t: reflect.TypeOf(time.Duration(0)),
value: reflect.ValueOf(90 * time.Minute),
err: nil,
},
{
name: "parse invalid string to time.Duration",
s: "invalid",
t: reflect.TypeOf(time.Duration(0)),
value: reflect.Value{},
err: errors.New("time: invalid duration \"invalid\""),
},
{
name: "parse string to time.Time",
s: "2006-01-02T15:04:05Z",
t: reflect.TypeOf(time.Time{}),
value: reflect.ValueOf(time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)),
err: nil,
},
{
name: "parse invalid string to time.Time",
s: "invalid",
t: reflect.TypeOf(time.Time{}),
value: reflect.Value{},
err: errors.New("parsing time \"invalid\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \"invalid\" as \"2006\""),
},
{
name: "parse string to bool",
s: "true",
t: reflect.TypeOf(bool(false)),
value: reflect.ValueOf(true),
err: nil,
},
{
name: "parse invalid string to bool",
s: "invalid",
t: reflect.TypeOf(bool(false)),
value: reflect.Value{},
err: errors.New("strconv.ParseBool: parsing \"invalid\": invalid syntax"),
},
{
name: "parse string to int",
s: "42",
t: reflect.TypeOf(int(0)),
value: reflect.ValueOf(int(42)),
err: nil,
},
{
name: "parse invalid string to int",
s: "invalid",
t: reflect.TypeOf(int(0)),
value: reflect.Value{},
err: errors.New("strconv.ParseInt: parsing \"invalid\": invalid syntax"),
},
{
name: "parse string to uint",
s: "42",
t: reflect.TypeOf(uint(0)),
value: reflect.ValueOf(uint(42)),
err: nil,
},
{
name: "parse invalid string to uint",
s: "invalid",
t: reflect.TypeOf(uint(0)),
value: reflect.Value{},
err: errors.New("strconv.ParseUint: parsing \"invalid\": invalid syntax"),
},
{
name: "parse string to float",
s: "42.42",
t: reflect.TypeOf(float64(0)),
value: reflect.ValueOf(float64(42.42)),
err: nil,
},
{
name: "parse invalid string to float",
s: "invalid",
t: reflect.TypeOf(float64(0)),
value: reflect.Value{},
err: errors.New("strconv.ParseFloat: parsing \"invalid\": invalid syntax"),
},
{
name: "parse string to string",
s: "test",
t: reflect.TypeOf(string("")),
value: reflect.ValueOf("test"),
err: nil,
},
{
name: "parse string to unsupported type",
s: "test",
t: reflect.TypeOf([]byte{}),
value: reflect.Value{},
err: errors.New("unsupported type"),
},
}
assert := assert.New(t)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
value, err := parseStringToType(tc.s, tc.t)
if tc.err != nil {
assert.Error(err)
assert.Equal(tc.err.Error(), err.Error())
} else {
assert.NoError(err)
assert.Equal(tc.value.Interface(), value.Interface())
}
})
}
}