-
Notifications
You must be signed in to change notification settings - Fork 2
/
minmax_test.go
197 lines (190 loc) · 4.51 KB
/
minmax_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
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
package validator
import "testing"
func mIntVal(v int) *int {
return &v
}
func mUintVal(v uint) *uint {
return &v
}
func TestMin(t *testing.T) {
type s1 struct {
A string `valid:"min=3"`
Z string `valid:"min=1"`
B uint `valid:"min=100"`
Y int `valid:"min=-0xf"`
C float64 `valid:"min=3.21"`
X float32 `valid:"min=-1.1"`
D *uint `valid:"min=22222"`
W *int `valid:"min=077"`
E []byte `valid:"min=2"`
V []byte `valid:"min=1"`
F map[int]uint `valid:"min=5"`
U map[string]int `valid:"min=2"`
}
s := s1{
A: "ab",
Z: "a",
B: 99,
Y: -10,
C: 3.2099999,
X: -1.0,
D: mUintVal(22221),
W: mIntVal(11112),
E: []byte{1},
V: []byte{0},
F: map[int]uint{
1: 2,
3: 4,
5: 6,
},
U: map[string]int{
"a": 1,
"b": 2,
},
}
v := New(WithFunc("min", min))
err := v.Validate(&s)
if err == nil {
t.Fatal("error expected")
}
errs := err.(Errors)
if len(errs) != 6 {
t.Fatalf("unexpected errors length: %+v; want: %d", err, 6)
}
if errs[0].Error() != "A must have length not less than 3 (was 2)" {
t.Fatalf("unexpected error: %+v", errs[0])
}
if errs[1].Error() != "B must not be less than 100 (was 99)" {
t.Fatalf("unexpected error: %+v", errs[1])
}
if errs[2].Error() != "C must not be less than 3.21 (was 3.2099999)" {
t.Fatalf("unexpected error: %+v", errs[2])
}
if errs[3].Error() != "D must not be less than 22222 (was 22221)" {
t.Fatalf("unexpected error: %+v", errs[3])
}
if errs[4].Error() != "E must have length not less than 2 (was 1)" {
t.Fatalf("unexpected error: %+v", errs[4])
}
if errs[5].Error() != "F must have length not less than 5 (was 3)" {
t.Fatalf("unexpected error: %+v", errs[5])
}
}
func TestMinUnsupported(t *testing.T) {
type s1 struct {
A int
}
s := struct {
A s1 `valid:"min=1"`
B bool `valid:"min=0"`
}{
s1{0},
false,
}
v := New(WithFunc("min", min))
err := v.Validate(&s)
if err == nil {
t.Fatal("error expected")
}
errs := err.(Errors)
if len(errs) != 2 {
t.Fatalf("unexpected errors length: %+v; want: %d", errs, 2)
}
if errs[0].Error() != "validator: unsupported: A" {
t.Fatalf("unexpected error: %+v", errs[0])
}
if errs[1].Error() != "validator: unsupported: B" {
t.Fatalf("unexpected error: %+v", errs[1])
}
}
func TestMax(t *testing.T) {
type s1 struct {
A string `valid:"max=3"`
Z string `valid:"max=0"`
B int `valid:"max=-1"`
Y uint `valid:"max=99"`
C float64 `valid:"max=-3.211984"`
X float32 `valid:"max=19.54"`
D *uint `valid:"max=0xFF"`
W *int `valid:"max=19780601"`
E []byte `valid:"max=3"`
V []byte `valid:"max=1"`
F map[int]uint `valid:"max=2"`
U map[string]int `valid:"max=2"`
}
s := s1{
A: "abcd",
Z: "",
B: 0,
Y: 99,
C: -3.21,
X: 19.53,
D: mUintVal(0x100),
W: mIntVal(19780601),
E: []byte{1, 2, 3, 4},
V: nil,
F: map[int]uint{
1: 2,
3: 4,
5: 6,
},
U: map[string]int{
"a": 1,
"b": 2,
},
}
v := New(WithFunc("max", max))
err := v.Validate(&s)
if err == nil {
t.Fatal("error expected")
}
errs := err.(Errors)
if len(errs) != 6 {
t.Fatalf("unexpected errors length: %+v; want: %d", err, 6)
}
if errs[0].Error() != "A must have length not greater than 3 (was 4)" {
t.Fatalf("unexpected error: %+v", errs[0])
}
if errs[1].Error() != "B must not be greater than -1 (was 0)" {
t.Fatalf("unexpected error: %+v", errs[1])
}
if errs[2].Error() != "C must not be greater than -3.211984 (was -3.21)" {
t.Fatalf("unexpected error: %+v", errs[2])
}
if errs[3].Error() != "D must not be greater than 0xFF (was 256)" {
t.Fatalf("unexpected error: %+v", errs[3])
}
if errs[4].Error() != "E must have length not greater than 3 (was 4)" {
t.Fatalf("unexpected error: %+v", errs[4])
}
if errs[5].Error() != "F must have length not greater than 2 (was 3)" {
t.Fatalf("unexpected error: %+v", errs[5])
}
}
func TestMaxUnsupported(t *testing.T) {
type s1 struct {
A int
}
s := struct {
A s1 `valid:"max=-1"`
B bool `valid:"max=99"`
}{
s1{0},
false,
}
v := New(WithFunc("max", max))
err := v.Validate(&s)
if err == nil {
t.Fatal("error expected")
}
errs := err.(Errors)
if len(errs) != 2 {
t.Fatalf("unexpected errors length: %+v; want: %d", errs, 2)
}
if errs[0].Error() != "validator: unsupported: A" {
t.Fatalf("unexpected error: %+v", errs[0])
}
if errs[1].Error() != "validator: unsupported: B" {
t.Fatalf("unexpected error: %+v", errs[1])
}
}