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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
package envelope
import (
"net"
"net/url"
"reflect"
"testing"
"time"
"git.eugenetriguba.com/checkmate/assert"
)
// GIVEN a string parser
// WHEN we parse a string value
// THEN it should return the string value as-is
func TestStringParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf("")]
result, err := parser.Parse("test string")
assert.Nil(t, err)
assert.Equal(t, "test string", result)
}
// GIVEN various integer parsers
// WHEN we parse string representations of integers
// THEN it should return the correct integer type
func TestIntParsers(t *testing.T) {
tests := []struct {
name string
typ reflect.Type
input string
expected any
}{
{
name: "int",
typ: reflect.TypeOf(int(0)),
input: "42",
expected: int(42),
},
{
name: "int8",
typ: reflect.TypeOf(int8(0)),
input: "42",
expected: int8(42),
},
{
name: "int16",
typ: reflect.TypeOf(int16(0)),
input: "42",
expected: int16(42),
},
{
name: "int32",
typ: reflect.TypeOf(int32(0)),
input: "42",
expected: int32(42),
},
{
name: "int64",
typ: reflect.TypeOf(int64(0)),
input: "42",
expected: int64(42),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := typeParserRegistry[tt.typ]
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// GIVEN various unsigned integer parsers
// WHEN we parse string representations of integers
// THEN it should return the correct unsigned integer type
func TestUintParsers(t *testing.T) {
tests := []struct {
name string
typ reflect.Type
input string
expected any
}{
{
name: "uint",
typ: reflect.TypeOf(uint(0)),
input: "42",
expected: uint(42),
},
{
name: "uint8",
typ: reflect.TypeOf(uint8(0)),
input: "42",
expected: uint8(42),
},
{
name: "uint16",
typ: reflect.TypeOf(uint16(0)),
input: "42",
expected: uint16(42),
},
{
name: "uint32",
typ: reflect.TypeOf(uint32(0)),
input: "42",
expected: uint32(42),
},
{
name: "uint64",
typ: reflect.TypeOf(uint64(0)),
input: "42",
expected: uint64(42),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := typeParserRegistry[tt.typ]
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// GIVEN a boolean parser
// WHEN we parse string representations of boolean values
// THEN it should:
// - Return true for various true-like values (true, TRUE, 1, on, yes, etc.)
// - Return false for various false-like values (false, FALSE, 0, off, no, etc.)
// - Return an error for invalid boolean values
func TestBoolParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf(bool(false))]
for _, val := range []string{"true", "TRUE", "True", "1", "t", "T", "on", "ON", "yes", "YES", "y", "Y"} {
result, err := parser.Parse(val)
assert.Nil(t, err)
assert.Equal(t, true, result)
}
for _, val := range []string{"false", "FALSE", "False", "0", "f", "F", "off", "OFF", "no", "NO", "n", "N"} {
result, err := parser.Parse(val)
assert.Nil(t, err)
assert.Equal(t, false, result)
}
_, err := parser.Parse("not-a-bool")
assert.NotNil(t, err)
}
// GIVEN float parsers (float32, float64)
// WHEN we parse string representations of floating point numbers
// THEN it should return the correct float type with proper precision
func TestFloatParsers(t *testing.T) {
tests := []struct {
name string
typ reflect.Type
input string
expected any
}{
{
name: "float32",
typ: reflect.TypeOf(float32(0)),
input: "3.14159",
expected: float32(3.14159),
},
{
name: "float64",
typ: reflect.TypeOf(float64(0)),
input: "3.14159265359",
expected: float64(3.14159265359),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := typeParserRegistry[tt.typ]
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// GIVEN a time.Time parser
// WHEN we parse:
// - A valid RFC3339 formatted time string
// - An invalid time string
//
// THEN it should return the correct time.Time value or an error respectively
func TestTimeParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf(time.Time{})]
result, err := parser.Parse("2023-08-31T12:34:56Z")
assert.Nil(t, err)
expected, _ := time.Parse(time.RFC3339, "2023-08-31T12:34:56Z")
assert.Equal(t, expected, result)
_, err = parser.Parse("not-a-time")
assert.NotNil(t, err)
}
// GIVEN a time.Duration parser
// WHEN we parse various duration strings (5s, 1m30s, 1h, 24h)
// THEN it should:
// - Convert each string to the correct time.Duration value
// - Return an error for invalid duration strings
func TestDurationParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf(time.Duration(0))]
tests := []struct {
input string
expected time.Duration
}{
{"5s", 5 * time.Second},
{"1m30s", 90 * time.Second},
{"1h", time.Hour},
{"24h", 24 * time.Hour},
}
for _, tt := range tests {
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
assert.Equal(t, tt.expected, result)
}
_, err := parser.Parse("not-a-duration")
assert.NotNil(t, err)
}
// GIVEN a net.IP parser
// WHEN we parse various IP address formats (IPv4 and IPv6)
// THEN it should:
// - Correctly convert each string to a net.IP value
// - Return an error for invalid IP addresses
func TestIPParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf(net.IP{})]
tests := []string{
"192.168.1.1",
"127.0.0.1",
"::1",
"2001:db8::1",
}
for _, testip := range tests {
result, err := parser.Parse(testip)
assert.Nil(t, err)
ip, ok := result.(net.IP)
assert.True(t, ok)
assert.Equal(t, testip, ip.String())
}
_, err := parser.Parse("not-an-ip")
assert.NotNil(t, err)
}
// GIVEN a net.IPNet parser
// WHEN we parse CIDR notation strings
// THEN it should:
// - Correctly convert each string to a net.IPNet value
// - Return an error for invalid CIDR notations
func TestIPNetParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf(net.IPNet{})]
tests := []string{
"192.168.1.0/24",
"10.0.0.0/8",
"2001:db8::/32",
}
for _, ipnet := range tests {
result, err := parser.Parse(ipnet)
assert.Nil(t, err)
ipNet, ok := result.(net.IPNet)
assert.True(t, ok)
assert.Equal(t, ipnet, ipNet.String())
}
_, err := parser.Parse("not-a-cidr")
assert.NotNil(t, err)
}
// GIVEN a url.URL parser
// WHEN we parse URL strings
// THEN it should:
// - Correctly convert each string to a url.URL value
// - Return an error for invalid URL strings
func TestURLParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf(url.URL{})]
tests := []struct {
input string
expected string
}{
{"https://example.com", "https://example.com"},
{"http://localhost:8080/path?query=value", "http://localhost:8080/path?query=value"},
{"postgres://user:pass@host:5432/dbname", "postgres://user:pass@host:5432/dbname"},
}
for _, tt := range tests {
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
url, ok := result.(url.URL)
assert.True(t, ok)
assert.Equal(t, tt.expected, url.String())
}
_, err := parser.Parse(":")
assert.NotNil(t, err)
}
// GIVEN a string slice parser
// WHEN we parse comma-separated string values
// THEN it should correctly split the input into a string slice
func TestStringSliceParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf([]string{})]
tests := []struct {
input string
expected []string
}{
{"one,two,three", []string{"one", "two", "three"}},
{"", []string{}},
{"single", []string{"single"}},
{"with space, and another", []string{"with space", " and another"}},
}
for _, tt := range tests {
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
slice, ok := result.([]string)
assert.True(t, ok)
assert.Equal(t, len(tt.expected), len(slice))
for i, v := range tt.expected {
assert.Equal(t, v, slice[i])
}
}
}
// GIVEN an int slice parser
// WHEN we parse comma-separated integer values
// THEN it should:
// - Correctly convert each element to an integer
// - Return an error if any element cannot be parsed as an integer
func TestIntSliceParser(t *testing.T) {
parser := typeParserRegistry[reflect.TypeOf([]int{})]
tests := []struct {
input string
expected []int
}{
{"1,2,3", []int{1, 2, 3}},
{"", []int{}},
{"42", []int{42}},
{"1, 2, 3", []int{1, 2, 3}},
}
for _, tt := range tests {
result, err := parser.Parse(tt.input)
assert.Nil(t, err)
slice, ok := result.([]int)
assert.True(t, ok)
assert.Equal(t, len(tt.expected), len(slice))
for i, v := range tt.expected {
assert.Equal(t, v, slice[i])
}
}
_, err := parser.Parse("1,not-an-int,3")
assert.NotNil(t, err)
}
// GIVEN the RegisterTypeParser function
// WHEN we register a custom type parser
// THEN it should:
// - Successfully add the parser to the registry
// - Correctly use the parser when parsing values
func TestRegisterTypeParserSupportsRegisteringCustomTypes(t *testing.T) {
type CustomType struct {
Value string
}
defer delete(typeParserRegistry, reflect.TypeOf(CustomType{}))
RegisterTypeParser(reflect.TypeOf(CustomType{}), TypeParserFunc(func(value string) (any, error) {
return CustomType{Value: "prefix-" + value}, nil
}))
parser, exists := typeParserRegistry[reflect.TypeOf(CustomType{})]
assert.True(t, exists)
result, err := parser.Parse("test")
assert.Nil(t, err)
customValue, ok := result.(CustomType)
assert.True(t, ok)
assert.Equal(t, "prefix-test", customValue.Value)
}
// GIVEN various type parsers
// WHEN we provide invalid string values
// THEN each parser should return an appropriate error
func TestParseInvalidValues(t *testing.T) {
tests := []struct {
name string
typ reflect.Type
input string
}{
{"Invalid int", reflect.TypeOf(int(0)), "not-an-int"},
{"Invalid int8", reflect.TypeOf(int8(0)), "not-an-int"},
{"Invalid int16", reflect.TypeOf(int16(0)), "not-an-int"},
{"Invalid int32", reflect.TypeOf(int32(0)), "not-an-int"},
{"Invalid int64", reflect.TypeOf(int64(0)), "not-an-int"},
{"Invalid uint", reflect.TypeOf(uint(0)), "not-a-uint"},
{"Invalid uint8", reflect.TypeOf(uint8(0)), "not-an-int"},
{"Invalid uint16", reflect.TypeOf(uint16(0)), "not-an-int"},
{"Invalid uint32", reflect.TypeOf(uint32(0)), "not-an-int"},
{"Invalid uint64", reflect.TypeOf(uint64(0)), "not-an-int"},
{"Invalid uint negative", reflect.TypeOf(uint(0)), "-5"},
{"Invalid float32", reflect.TypeOf(float32(0)), "not-a-float"},
{"Invalid float64", reflect.TypeOf(float64(0)), "not-a-float"},
{"Invalid bool", reflect.TypeOf(false), "not-a-bool"},
{"Invalid time", reflect.TypeOf(time.Time{}), "not-a-time"},
{"Invalid duration", reflect.TypeOf(time.Duration(0)), "not-a-duration"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := typeParserRegistry[tt.typ]
_, err := parser.Parse(tt.input)
assert.NotNil(t, err)
})
}
}
|