-
Notifications
You must be signed in to change notification settings - Fork 4
/
regex_test.go
84 lines (77 loc) · 1.91 KB
/
regex_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
package govalidator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_RegexMatches(t *testing.T) {
tests := []struct {
name string
field string
value string
pattern string
isPassed bool
message string
expectedMsg string
}{
{
name: "test an empty string value will fail regex validation",
field: "code",
value: "",
pattern: "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
isPassed: false,
message: "code is not valid",
expectedMsg: "code is not valid",
},
{
name: "test an empty space string value will fail regex validation",
field: "name",
value: " ",
pattern: "^[0-9]{10}$",
isPassed: false,
message: "name is not valid",
expectedMsg: "name is not valid",
},
{
name: "test a wrong string value will fail regex validation",
field: "id",
value: "09377475856",
pattern: "^[0-9]{10}$",
isPassed: false,
message: "id is not valid",
expectedMsg: "id is not valid",
},
{
name: "test a correct string value will pass validation",
field: "id",
value: "1160277052",
pattern: "^[0-9]{10}$",
isPassed: true,
message: "",
expectedMsg: "",
},
{
name: "test a correct email string value will pass validation",
field: "email",
value: "[email protected]",
pattern: "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
isPassed: true,
message: "",
expectedMsg: "",
},
}
for _, test := range tests {
v := New()
v.RegexMatches(test.value, test.pattern, test.field, test.message)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equal(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}