-
Notifications
You must be signed in to change notification settings - Fork 4
/
max_test.go
193 lines (176 loc) · 4.45 KB
/
max_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
package govalidator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_MaxInt(t *testing.T) {
tests := []struct {
name string
field string
value int
max int
isPassed bool
msg string
expectedMsg string
}{
{
name: "test integer value of `10` will pass if the validator defined maximum acceptable value to 10",
field: "score",
value: 10,
max: 10,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test integer value of `71` won't pass because the validator defined maximum acceptable value to 50",
field: "age",
value: 71,
max: 50,
isPassed: false,
msg: "",
expectedMsg: "age should be less than 50",
},
{
name: "test integer value of `141` won't pass because the validator defined maximum acceptable value to 20 with custom msg",
field: "goal",
value: 141,
max: 20,
isPassed: false,
msg: "goal have to be less than 20",
expectedMsg: "goal have to be less than 20",
},
}
for _, test := range tests {
v := New()
v.MaxInt(test.value, test.max, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
func Test_MaxFloat64(t *testing.T) {
tests := []struct {
name string
field string
value float64
max float64
isPassed bool
msg string
expectedMsg string
}{
{
name: "test float value of `20.4` will pass if the validator defined maximum acceptable value to 110.3",
field: "score",
value: 20.4,
max: 110.3,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test float value of `0.1` won't pass because the validator defined maximum acceptable value to 0.01",
field: "score",
value: 0.1,
max: 0.01,
isPassed: false,
msg: "",
expectedMsg: "score should be less than 0.01",
},
{
name: "test float value of `141` won't pass because the validator defined maximum acceptable value to 20 with custom msg",
field: "goal",
value: 122.23,
max: 20,
isPassed: false,
msg: "goal have to be less than 20",
expectedMsg: "goal have to be less than 20",
},
}
for _, test := range tests {
v := New()
v.MaxFloat(test.value, test.max, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
func Test_MaxString(t *testing.T) {
tests := []struct {
name string
field string
value string
max int
isPassed bool
msg string
expectedMsg string
}{
{
name: "test `rey` will pass validation when maximum valid length is 5",
field: "name",
value: "rey",
max: 5,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test empty string will pass validation when maximum valid length is 2",
field: "username",
value: "",
max: 2,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test empty space string won't pass validation when maximum valid length is 0",
field: "username",
value: " ",
max: -1,
isPassed: false,
msg: "",
expectedMsg: "username should have less than -1 characters",
},
{
name: "test `abcd` won't pass validation when maximum valid length is 3",
field: "alphabet",
value: "abcd",
max: 3,
isPassed: false,
msg: "alphabet should have less than 3 characters",
expectedMsg: "alphabet should have less than 3 characters",
},
}
for _, test := range tests {
v := New()
v.MaxString(test.value, test.max, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}