-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom_test.go
70 lines (64 loc) · 1.44 KB
/
custom_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
package govalidator
import (
"github.com/stretchr/testify/assert"
"testing"
)
func Test_CustomRule(t *testing.T) {
tests := []struct {
name string
field string
value bool
isPassed bool
msg string
expectedMsg string
}{
{
name: "test CustomRule with true condition",
value: true,
field: "username",
msg: "",
isPassed: true,
expectedMsg: "",
},
{
name: "test CustomRule with false condition and custom message",
value: false,
field: "username",
msg: "username must be unique",
isPassed: false,
expectedMsg: "username must be unique",
},
{
name: "test CustomRule with false condition and empty message",
value: false,
field: "email",
msg: "",
isPassed: false,
expectedMsg: "",
},
{
name: "test CustomRule with true condition and custom message",
value: true,
field: "email",
msg: "email must be valid",
isPassed: true,
expectedMsg: "",
},
}
for _, test := range tests {
v := New()
v.CustomRule(test.value, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed(), test.name)
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed, expected: %s, got: %s",
test.name,
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}