This repository was archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexValidator_test.go
56 lines (52 loc) · 2.06 KB
/
RegexValidator_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
package ecms_validator
import (
"regexp"
"testing"
)
type RegexValidatorTestCase struct {
Name string
PatternStr string
Value interface{}
Expected bool
ExpectedMsgsLen int
}
func TestNewRegexValidatorOptions(t *testing.T) {
result := NewRegexValidatorOptions()
if result.Pattern != nil {
t.Errorf("Expected `*.PatternStr` to equal `nil`; Received: %v", result.Pattern)
}
}
func TestRegexValidator(t *testing.T) {
for _, tc := range []RegexValidatorTestCase{
{Name: "//_$_true", PatternStr: "", Value: "$", Expected: false, ExpectedMsgsLen: 1},
{Name: "//_''_true", PatternStr: "", Value: "", Expected: false, ExpectedMsgsLen: 1},
{Name: "/./_$_true", PatternStr: ".", Value: "$", Expected: true, ExpectedMsgsLen: 0},
{Name: "/./_''_false", PatternStr: ".", Value: "", Expected: false, ExpectedMsgsLen: 1},
{Name: "/./_nil_false", PatternStr: ".", Value: nil, Expected: false, ExpectedMsgsLen: 1},
{Name: "nil_nil_true", Value: nil, Expected: true, ExpectedMsgsLen: 0},
{Name: "/^\\d+$/_99_true", PatternStr: "^\\d+$", Value: "99", Expected: true, ExpectedMsgsLen: 0},
{Name: "/\\d/_99_true", PatternStr: "\\d", Value: "99", Expected: true, ExpectedMsgsLen: 0},
{Name: "/\\d/_abc_false", PatternStr: "\\d", Value: "abc", Expected: false, ExpectedMsgsLen: 1},
{Name: "/^[a-z]{5}$/_aeiou_true", PatternStr: "^[a-z]{5}$", Value: "aeiou", Expected: true, ExpectedMsgsLen: 0},
{Name: "/^[a-z]{5}$/_aeiouy_false", PatternStr: "^[a-z]{5}$", Value: "aeiouy", Expected: false, ExpectedMsgsLen: 1},
} {
t.Run(tc.Name, func(t2 *testing.T) {
vOptions := NewRegexValidatorOptions()
if len(tc.PatternStr) > 0 {
regex := regexp.MustCompile(tc.PatternStr)
vOptions.Pattern = regex
}
validator := RegexValidator(vOptions)
result, msgs := validator(tc.Value)
msgsLen := len(msgs)
if result != tc.Expected {
t2.Errorf("Expected %v for `result` boolean but got %v",
tc.Expected, result)
}
if msgsLen != tc.ExpectedMsgsLen {
t2.Errorf("Expected %d messages. Got %d",
tc.ExpectedMsgsLen, msgsLen)
}
})
}
}