forked from gookit/validate
/
validate_test.go
162 lines (135 loc) · 3.4 KB
/
validate_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
package validate
import (
"reflect"
"testing"
"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
)
func TestUtil_Func_valueToInt64(t *testing.T) {
noErrTests := []struct {
val interface{}
strict bool
want int64
}{
{" 12", false, 12},
{float32(12.23), false, 12},
{12.23, false, 12},
}
for _, item := range noErrTests {
i64, err := valueToInt64(item.val, item.strict)
assert.NoError(t, err)
assert.Equal(t, item.want, i64)
}
}
func TestUtil_Func_getVariadicKind(t *testing.T) {
noErrTests := []struct {
val interface{}
want reflect.Kind
}{
{"invalid", reflect.Invalid},
{[]int{1, 2}, reflect.Int},
{[]int8{1, 2}, reflect.Int8},
{[]int16{1, 2}, reflect.Int16},
{[]int32{1, 2}, reflect.Int32},
{[]int64{1, 2}, reflect.Int64},
{[]uint{1, 2}, reflect.Uint},
{[]uint8{1, 2}, reflect.Uint8},
{[]uint16{1, 2}, reflect.Uint16},
{[]uint32{1, 2}, reflect.Uint32},
{[]uint64{1, 2}, reflect.Uint64},
{[]string{"a", "b"}, reflect.String},
}
for _, item := range noErrTests {
vt := reflect.TypeOf(item.val)
eleType := getVariadicKind(vt.String())
assert.Equal(t, item.want, eleType)
}
}
func TestUtil_Func_goodName(t *testing.T) {
tests := []struct {
give string
want bool
}{
{"ab", true},
{"1234", false},
{"01234", false},
{"abc123", true},
}
for _, item := range tests {
assert.Equal(t, item.want, goodName(item.give))
}
}
func Test_Util_Func_convertType(t *testing.T) {
nVal, err := convertType(23, intKind, reflect.String)
assert.NoError(t, err)
assert.Equal(t, "23", nVal)
nVal, err = convertType(uint(23), uintKind, reflect.String)
assert.NoError(t, err)
assert.Equal(t, "23", nVal)
}
func Test_IsZero(t *testing.T) {
assert.True(t, IsZero(reflect.ValueOf([2]int{})))
assert.True(t, IsZero(reflect.ValueOf(false)))
}
func TestMS_String(t *testing.T) {
ms := MS{}
assert.Equal(t, "", ms.One())
assert.Equal(t, "", ms.String())
ms["key"] = "val"
assert.Equal(t, "val", ms.One())
assert.Equal(t, " key: val", ms.String())
}
func TestOption(t *testing.T) {
opt := Option()
assert.Equal(t, "json", opt.FieldTag)
assert.Equal(t, "validate", opt.ValidateTag)
Config(func(opt *GlobalOption) {
opt.ValidateTag = "valid"
})
opt = Option()
assert.Equal(t, "valid", opt.ValidateTag)
ResetOption()
}
func TestStruct_nilPtr_field2(t *testing.T) {
type UserDto struct {
Name string `validate:"required"`
Sex *bool `validate:"required" json:"sex"`
}
sex := true
u := UserDto{
Name: "abc",
Sex: nil,
}
v := Struct(&u)
assert.False(t, v.Validate())
assert.True(t, v.Errors.HasField("Sex"))
assert.Contains(t, v.Errors.FieldOne("Sex"), "sex is required")
dump.Println(v.Errors)
u.Sex = &sex
v = Struct(&u)
assert.True(t, v.Validate())
}
func TestStruct_nexted_anonymity_struct(t *testing.T) {
type UserDto struct {
Name string `validate:"required"`
Sex *bool `validate:"required" json:"sex"`
ExtInfo struct{
Homepage string `validate:"required"`
CityName string
}
}
sex := true
u := &UserDto{
Name: "abc",
Sex: &sex,
}
v := Struct(u)
assert.False(t, v.Validate())
dump.Println(v.Errors)
assert.True(t, v.Errors.HasField("ExtInfo.Homepage"))
assert.Contains(t, v.Errors, "ExtInfo.Homepage")
assert.Equal(t, "ExtInfo.Homepage is required and not empty", v.Errors.FieldOne("ExtInfo.Homepage"))
u.ExtInfo.Homepage = "https://github.com/inhere"
v = Struct(u)
assert.True(t, v.Validate())
}