-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator_common_test.go
106 lines (84 loc) · 2.81 KB
/
validator_common_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
package validation
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tiendc/gofn"
)
var ctxBg = context.Background()
func Test_Nil_NotNil(t *testing.T) {
// Success cases
errsNil := Nil[any](nil).Validate(ctxBg)
errsNotNil := NotNil[any](nil).Validate(ctxBg)
assert.True(t, len(errsNil) == 0 && len(errsNotNil) == 1)
var pStr *string
errsNil = Nil(pStr).Validate(ctxBg)
errsNotNil = NotNil(pStr).Validate(ctxBg)
assert.True(t, len(errsNil) == 0 && len(errsNotNil) == 1)
var pSlice *[]string
errsNil = Nil(pSlice).Validate(ctxBg)
errsNotNil = NotNil(pSlice).Validate(ctxBg)
assert.True(t, len(errsNil) == 0 && len(errsNotNil) == 1)
var pMap *map[int]bool
errsNil = Nil(pMap).Validate(ctxBg)
errsNotNil = NotNil(pMap).Validate(ctxBg)
assert.True(t, len(errsNil) == 0 && len(errsNotNil) == 1)
// Failure cases
errsNil = Nil(gofn.ToPtr[any]("")).Validate(ctxBg)
errsNotNil = NotNil(gofn.ToPtr[any]("")).Validate(ctxBg)
assert.False(t, len(errsNil) == 0 && len(errsNotNil) == 1)
errsNil = Nil(gofn.ToPtr[int](0)).Validate(ctxBg)
errsNotNil = NotNil(gofn.ToPtr[int](0)).Validate(ctxBg)
assert.False(t, len(errsNil) == 0 && len(errsNotNil) == 1)
var aSlice []string
errsNil = Nil(&aSlice).Validate(ctxBg)
errsNotNil = NotNil(&aSlice).Validate(ctxBg)
assert.False(t, len(errsNil) == 0 && len(errsNotNil) == 1)
var aMap map[int]bool
errsNil = Nil(&aMap).Validate(ctxBg)
errsNotNil = NotNil(&aMap).Validate(ctxBg)
assert.False(t, len(errsNil) == 0 && len(errsNotNil) == 1)
}
func Test_Required(t *testing.T) {
// Failure cases
errs := Required(nil).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "required", errs[0].Type())
aInt := int32(0)
errs = Required(&aInt).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "required", errs[0].Type())
aStr := ""
errs = Required(&aStr).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "required", errs[0].Type())
aSlice := []string{}
errs = Required(&aSlice).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "required", errs[0].Type())
aMap := map[int]bool{}
errs = Required(&aMap).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "required", errs[0].Type())
var aAny any
aAny = aMap
errs = Required(&aAny).Validate(ctxBg)
assert.Equal(t, 1, len(errs))
assert.Equal(t, "required", errs[0].Type())
// Success cases
aInt = 1
errs = Required(&aInt).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
aStr = "a"
errs = Required(&aStr).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
aSlice = []string{"a", "b"}
errs = Required(&aSlice).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
aMap = map[int]bool{0: false}
errs = Required(&aMap).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
aAny = aSlice
errs = Required(&aAny).Validate(ctxBg)
assert.Equal(t, 0, len(errs))
}