-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_validaktor_test.go
More file actions
52 lines (46 loc) · 1.43 KB
/
regex_validaktor_test.go
File metadata and controls
52 lines (46 loc) · 1.43 KB
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
package validaktor
import (
"regexp"
"testing"
)
type testRegex struct {
exp string
isValid bool
err error
data interface{}
}
func TestRegexValidate(t *testing.T) {
testData := []testRegex{
{exp: "[A-Z]+", isValid: true, err: nil, data: "HELLO"},
{exp: "[0-9]{4,6}", isValid: true, err: nil, data: "12345"},
{exp: "\\w+", isValid: true, err: nil, data: "whatever24"},
{exp: `\w+`, isValid: true, err: nil, data: "iamgood"},
{exp: "[^A-Z]+", isValid: true, err: nil, data: "123456asdf"},
}
for _, v := range testData {
validator := ®exValidator{regex: regexp.MustCompile(v.exp)}
isValid, err := validator.validate(v.data)
if v.isValid != isValid {
t.Errorf("%+v != %+v it should be valid with data %+v", v.isValid, isValid, v.data)
}
if err != v.err {
t.Errorf("there was an error %s", err)
}
}
}
func TestRegexValidateKo(t *testing.T) {
testData := []testRegex{
{exp: "[A-Z]+", isValid: false, data: "1234"},
{exp: "^[0-9]{4,6}$", isValid: false, data: "123456789"},
{exp: "^\\w+$", isValid: false, data: "whate ver24"},
{exp: `^\w+$`, isValid: false, data: " iamg ood"},
{exp: "[^A-Z]+", isValid: false, data: "ASDFQWER"},
}
for _, v := range testData {
validator := ®exValidator{regex: regexp.MustCompile(v.exp)}
isValid, _ := validator.validate(v.data)
if v.isValid != isValid {
t.Errorf("%+v != %+v it should not be valid with data %+v", v.isValid, isValid, v.data)
}
}
}