-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstringLike_test.go
More file actions
647 lines (616 loc) · 18.8 KB
/
Copy pathstringLike_test.go
File metadata and controls
647 lines (616 loc) · 18.8 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package zog
import (
"fmt"
"regexp"
"strings"
"testing"
p "github.com/Oudwins/zog/pkgs/internals"
"github.com/Oudwins/zog/pkgs/internals/tutils"
"github.com/Oudwins/zog/zconst"
"github.com/stretchr/testify/assert"
)
// Custom string type for testing
// (stringer is not required, but this is a placeholder for custom string type)
//
//go:generate stringer -type=CustomString
type CustomString string
func TestStringLikeParse(t *testing.T) {
tests := []struct {
name string
data any
expectErr bool
expected CustomString
}{
{"Valid string value", "hello", false, CustomString("hello")},
{"Valid empty string", "", false, CustomString("")},
{"Valid type (int)", 123, false, CustomString("123")},
{"Valid type (bool)", true, false, CustomString("true")},
{"Invalid type nil optional so fine", nil, false, CustomString("")},
}
strProc := StringLike[CustomString]()
for i, test := range tests {
t.Run(test.name, func(t *testing.T) {
var result CustomString
errs := strProc.Parse(test.data, &result)
if len(errs) > 0 && !test.expectErr {
t.Errorf("Unexpected errors i = %d: %v", i, errs)
}
if len(errs) > 0 {
tutils.VerifyDefaultIssueMessages(t, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeSchemaOption(t *testing.T) {
s := StringLike[CustomString](WithCoercer(func(original any) (value any, err error) {
return CustomString("coerced"), nil
}))
var result CustomString
err := s.Parse(123, &result)
assert.Nil(t, err)
assert.Equal(t, CustomString("coerced"), result)
}
func TestStringLikeExecOption(t *testing.T) {
t.Run("Parse context is passed to parsing option", func(t *testing.T) {
strProc := StringLike[CustomString]()
var result CustomString
var contextPassed bool
fakeOption := func(p *p.ExecCtx) {
if p != nil {
contextPassed = true
}
}
errs := strProc.Parse("hello", &result, fakeOption)
if len(errs) > 0 {
t.Errorf("Unexpected errors: %v", errs)
}
if !contextPassed {
t.Error("Parse context was not passed to the parsing option")
}
})
}
func TestStringLikeRequired(t *testing.T) {
tests := []struct {
name string
data interface{}
expectErr bool
expected CustomString
}{
{"Valid string value", "abc", false, CustomString("abc")},
{"Empty string", "", false, CustomString("")},
{"Nil value", nil, true, CustomString("")},
}
strProc := StringLike[CustomString]().Required(Message("test"))
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("On Run %s -> Expected error: %v, got: %v", test.name, test.expectErr, errs)
}
if test.expectErr && errs[0].Message != "test" {
t.Errorf("On Run %s -> Expected error: %v, got: %v", test.name, "test", errs[0].Message)
}
if !test.expectErr && result != test.expected {
t.Errorf("On Run %s -> Expected %v, but got %v", test.name, test.expected, result)
}
})
}
}
func TestStringLikeOptional(t *testing.T) {
tests := []struct {
name string
data interface{}
expectErr bool
expected CustomString
}{
{"Valid string value", "abc", false, CustomString("abc")},
{"Empty string", "", false, CustomString("")},
{"Nil value", nil, false, CustomString("")},
}
strProc := StringLike[CustomString]().Optional()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if len(errs) > 0 {
tutils.VerifyDefaultIssueMessages(t, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeDefault(t *testing.T) {
tests := []struct {
name string
data interface{}
default_ CustomString
expectErr bool
expected CustomString
}{
{"Valid string value", "abc", CustomString("def"), false, CustomString("abc")},
{"Nil value with default", nil, CustomString("def"), false, CustomString("def")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().Default(test.default_)
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("%s -> Expected error: %v, got: %v", test.name, test.expectErr, errs)
}
if len(errs) > 0 {
tutils.VerifyDefaultIssueMessages(t, errs)
}
if result != test.expected {
t.Errorf("%s -> Expected %v, but got %v", test.name, test.expected, result)
}
})
}
}
func TestStringLikeCatch(t *testing.T) {
tests := []struct {
name string
data interface{}
catch CustomString
expectErr bool
expected CustomString
}{
{"Valid string value", "abc", CustomString("catch"), false, CustomString("abc")},
{"Valid type with catch", 123, CustomString("catch"), false, CustomString("123")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().Catch(test.catch)
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("%s -> Expected error: %v, got: %v", test.name, test.expectErr, errs)
}
if len(errs) > 0 {
tutils.VerifyDefaultIssueMessages(t, errs)
}
if result != test.expected {
t.Errorf("%s -> Expected %v, but got %v", test.name, test.expected, result)
}
})
}
}
func TestStringLikeOneOf(t *testing.T) {
enum := []CustomString{"a", "b", "c"}
strProc := StringLike[CustomString]().OneOf(enum)
tests := []struct {
name string
data any
expectErr bool
expected CustomString
}{
{"Valid enum value", "a", false, CustomString("a")},
{"Invalid enum value", "z", true, CustomString("z")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeMinMaxLen(t *testing.T) {
strProc := StringLike[CustomString]().Min(2).Max(4).Len(3)
tests := []struct {
name string
data any
expectErr bool
expected CustomString
}{
{"Valid length", "abc", false, CustomString("abc")},
{"Too short", "a", true, CustomString("a")},
{"Too long", "abcde", true, CustomString("abcde")},
{"Wrong length", "ab", true, CustomString("ab")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeEmailURL(t *testing.T) {
emailProc := StringLike[CustomString]().Email()
urlProc := StringLike[CustomString]().URL()
t.Run("Valid email", func(t *testing.T) {
var result CustomString
errs := emailProc.Parse("test@example.com", &result)
assert.Empty(t, errs)
assert.Equal(t, CustomString("test@example.com"), result)
})
t.Run("Invalid email", func(t *testing.T) {
var result CustomString
errs := emailProc.Parse("not-an-email", &result)
assert.NotEmpty(t, errs)
})
t.Run("Valid URL", func(t *testing.T) {
var result CustomString
errs := urlProc.Parse("https://example.com", &result)
assert.Empty(t, errs)
assert.Equal(t, CustomString("https://example.com"), result)
})
t.Run("Invalid URL", func(t *testing.T) {
var result CustomString
errs := urlProc.Parse("not-a-url", &result)
assert.NotEmpty(t, errs)
})
}
func TestStringLikePrefixSuffixContains(t *testing.T) {
strProc := StringLike[CustomString]().HasPrefix("pre").HasSuffix("suf").Contains("mid")
tests := []struct {
name string
data any
expectErr bool
expected CustomString
}{
{"Valid string", "pre-midsuf", false, CustomString("pre-midsuf")},
{"Missing prefix", "midsuf", true, CustomString("midsuf")},
{"Missing suffix", "pre-mid", true, CustomString("pre-mid")},
{"Missing contains", "presuf", true, CustomString("presuf")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeContainsUpperDigitSpecial(t *testing.T) {
upperProc := StringLike[CustomString]().ContainsUpper()
digitProc := StringLike[CustomString]().ContainsDigit()
specialProc := StringLike[CustomString]().ContainsSpecial()
t.Run("Contains upper", func(t *testing.T) {
var result CustomString
errs := upperProc.Parse("abcD", &result)
assert.Empty(t, errs)
})
t.Run("No upper", func(t *testing.T) {
var result CustomString
errs := upperProc.Parse("abcd", &result)
assert.NotEmpty(t, errs)
})
t.Run("Contains digit", func(t *testing.T) {
var result CustomString
errs := digitProc.Parse("abc1", &result)
assert.Empty(t, errs)
})
t.Run("No digit", func(t *testing.T) {
var result CustomString
errs := digitProc.Parse("abcd", &result)
assert.NotEmpty(t, errs)
})
t.Run("Contains special", func(t *testing.T) {
var result CustomString
errs := specialProc.Parse("abc$", &result)
assert.Empty(t, errs)
})
t.Run("No special", func(t *testing.T) {
var result CustomString
errs := specialProc.Parse("abcd", &result)
assert.NotEmpty(t, errs)
})
}
func TestStringLikeUUIDMatch(t *testing.T) {
uuidProc := StringLike[CustomString]().UUID()
matchProc := StringLike[CustomString]().Match(regexp.MustCompile(`^abc[0-9]+$`))
t.Run("Valid UUID", func(t *testing.T) {
var result CustomString
errs := uuidProc.Parse("123e4567-e89b-12d3-a456-426614174000", &result)
assert.Empty(t, errs)
})
t.Run("Invalid UUID", func(t *testing.T) {
var result CustomString
errs := uuidProc.Parse("not-a-uuid", &result)
assert.NotEmpty(t, errs)
})
t.Run("Valid match", func(t *testing.T) {
var result CustomString
errs := matchProc.Parse("abc123", &result)
assert.Empty(t, errs)
})
t.Run("Invalid match", func(t *testing.T) {
var result CustomString
errs := matchProc.Parse("def123", &result)
assert.NotEmpty(t, errs)
})
}
func TestStringLikeTransform(t *testing.T) {
tests := []struct {
name string
data interface{}
transform p.Transform[*CustomString]
expectErr bool
expected CustomString
}{
{"To upper", "abc", func(val *CustomString, ctx Ctx) error {
*val = CustomString(strings.ToUpper(string(*val)))
return nil
}, false, CustomString("ABC")},
{"No change", "abc", func(val *CustomString, ctx Ctx) error { return nil }, false, CustomString("abc")},
{"Invalid transform", "abc", func(val *CustomString, ctx Ctx) error { return fmt.Errorf("fail") }, true, CustomString("abc")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().Transform(test.transform)
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeTrim(t *testing.T) {
strProc := StringLike[CustomString]().Trim()
var result CustomString
errs := strProc.Parse(" abc ", &result)
assert.Empty(t, errs)
assert.Equal(t, CustomString("abc"), result)
}
func TestStringLikeCustomTest(t *testing.T) {
validator := StringLike[CustomString]().TestFunc(func(val *CustomString, ctx Ctx) bool {
return *val == CustomString("pass")
}, Message("custom"))
tests := []struct {
name string
input string
expectErr bool
expected CustomString
}{
{"valid value", "pass", false, CustomString("pass")},
{"invalid value", "fail", true, CustomString("fail")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var dest CustomString
errs := validator.Parse(tt.input, &dest)
if (len(errs) > 0) != tt.expectErr {
t.Errorf("got errors %v, expectErr %v", errs, tt.expectErr)
}
if !tt.expectErr {
assert.Equal(t, tt.expected, dest)
}
})
}
}
func TestStringLikeEQ(t *testing.T) {
tests := []struct {
name string
data interface{}
eqValue CustomString
expectErr bool
expected CustomString
}{
{"Equal value", "abc", CustomString("abc"), false, CustomString("abc")},
{"Not equal value", "def", CustomString("abc"), true, CustomString("def")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().TestFunc(func(val *CustomString, ctx Ctx) bool {
return *val == test.eqValue
})
var result CustomString
errs := strProc.Parse(test.data, &result)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if result != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
})
}
}
func TestStringLikeGetType(t *testing.T) {
s := StringLike[CustomString]()
assert.Equal(t, zconst.TypeString, s.getType())
}
// Validation tests
func TestStringLikeValidate(t *testing.T) {
tests := []struct {
name string
data CustomString
}{
{"Valid value", CustomString("abc")},
{"Empty value", CustomString("")},
}
strProc := StringLike[CustomString]()
for i, test := range tests {
t.Run(test.name, func(t *testing.T) {
errs := strProc.Validate(&test.data)
if len(errs) > 0 {
t.Errorf("Unexpected errors i = %d: %v", i, errs)
}
})
}
}
func TestStringLikeValidateExecOption(t *testing.T) {
t.Run("Parse context is passed to parsing option", func(t *testing.T) {
strProc := StringLike[CustomString]()
var result CustomString
var contextPassed bool
fakeOption := func(p *p.ExecCtx) {
if p != nil {
contextPassed = true
}
}
errs := strProc.Validate(&result, fakeOption)
if len(errs) > 0 {
t.Errorf("Unexpected errors: %v", errs)
}
if !contextPassed {
t.Error("Parse context was not passed to the parsing option")
}
})
}
func TestStringLikeValidateRequired(t *testing.T) {
tests := []struct {
name string
data CustomString
expectErr bool
expected CustomString
}{
{"Valid value", CustomString("abc"), false, CustomString("abc")},
{"Empty value", CustomString(""), true, CustomString("")},
}
strProc := StringLike[CustomString]().Required()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
errs := strProc.Validate(&test.data)
if test.expectErr {
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
} else {
assert.Empty(t, errs)
}
assert.Equal(t, test.data, test.expected)
})
}
}
func TestStringLikeValidateOptional(t *testing.T) {
tests := []struct {
name string
data CustomString
expected CustomString
proc *StringSchema[CustomString]
expectErr bool
}{
{"Optional by default", CustomString("abc"), CustomString("abc"), StringLike[CustomString](), false},
{"Optional overrides required", CustomString("abc"), CustomString("abc"), StringLike[CustomString]().Required().Optional(), false},
{"Required errors on empty", CustomString(""), CustomString(""), StringLike[CustomString]().Required(), true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
errs := test.proc.Validate(&test.data)
if test.expectErr {
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
}
assert.Equal(t, test.data, test.expected)
})
}
}
func TestStringLikeValidateDefault(t *testing.T) {
tests := []struct {
name string
data CustomString
default_ CustomString
expectErr bool
expected CustomString
}{
{"Valid value", CustomString("abc"), CustomString("def"), false, CustomString("abc")},
{"Empty value with default", CustomString(""), CustomString("def"), false, CustomString("def")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().Default(test.default_)
errs := strProc.Validate(&test.data)
if test.expectErr {
assert.NotEmpty(t, errs)
tutils.VerifyDefaultIssueMessages(t, errs)
} else {
assert.Empty(t, errs)
}
assert.Equal(t, test.data, test.expected)
})
}
}
func TestStringLikeValidateCatch(t *testing.T) {
tests := []struct {
name string
data CustomString
catch CustomString
expected CustomString
}{
{"Without catch", CustomString("abc"), CustomString("catch"), CustomString("abc")},
{"With catch", CustomString(""), CustomString("catch"), CustomString("catch")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().TestFunc(func(val *CustomString, ctx Ctx) bool {
return *val != ""
}).Catch(test.catch).Required()
errs := strProc.Validate(&test.data)
if len(errs) > 0 {
tutils.VerifyDefaultIssueMessages(t, errs)
}
if test.data != test.expected {
t.Errorf("%s -> Expected %v, but got %v", test.name, test.expected, test.data)
}
})
}
}
func TestStringLikeValidateTransform(t *testing.T) {
tests := []struct {
name string
data CustomString
transform p.Transform[*CustomString]
expectErr bool
expected CustomString
}{
{"To upper", CustomString("abc"), func(val *CustomString, ctx Ctx) error {
*val = CustomString(strings.ToUpper(string(*val)))
return nil
}, false, CustomString("ABC")},
{"No change", CustomString("abc"), func(val *CustomString, ctx Ctx) error { return nil }, false, CustomString("abc")},
{"Invalid transform", CustomString("abc"), func(val *CustomString, ctx Ctx) error { return fmt.Errorf("fail") }, true, CustomString("abc")},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
strProc := StringLike[CustomString]().Transform(test.transform)
errs := strProc.Validate(&test.data)
if (len(errs) > 0) != test.expectErr {
t.Errorf("Expected error: %v, got: %v", test.expectErr, errs)
}
if test.data != test.expected {
t.Errorf("Expected %v, but got %v", test.expected, test.data)
}
})
}
}
func TestStringLikeValidateCustomTest(t *testing.T) {
validator := StringLike[CustomString]().TestFunc(func(val *CustomString, ctx Ctx) bool {
assert.Equal(t, CustomString("abc"), *val)
return true
}, Message("custom"))
dest := CustomString("abc")
errs := validator.Validate(&dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, CustomString("abc"), dest)
}