-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_test.go
More file actions
124 lines (117 loc) · 2.82 KB
/
Copy pathstr_test.go
File metadata and controls
124 lines (117 loc) · 2.82 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
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
package vaddie
import (
"regexp"
"testing"
)
var strTests = []TestCase[string]{
{
Name: "empty",
ValidValues: []string{""},
InvalidValues: []string{"ab"},
Validation: StrEmpty(),
},
{
Name: "space",
ValidValues: []string{" \t\n"},
InvalidValues: []string{"ab"},
Validation: StrSpace(),
},
{
Name: "min",
ValidValues: []string{"abcdefg", "🐈🐕🐢⏰🐦"},
InvalidValues: []string{"ab"},
Validation: StrMin(5),
},
{
Name: "max",
ValidValues: []string{"abc"},
InvalidValues: []string{"abcdefg", "🐈🐕🐢⏰🐦"},
Validation: StrMax(5),
},
{
Name: "unicode min",
ValidValues: []string{"abcdefg", "🐈🐕🐢⏰🐦🐈🐕🐢⏰🐦"},
InvalidValues: []string{"ab", "🐈"},
Validation: StrUnicodeMin(5),
},
{
Name: "unicode max",
ValidValues: []string{"🐈🐕🐢⏰🐦"},
InvalidValues: []string{"🐈🐕🐢⏰🐦🐈🐕🐢⏰🐦"},
Validation: StrUnicodeMax(8),
},
{
Name: "letters",
ValidValues: []string{"abcd"},
InvalidValues: []string{"abcd1"},
Validation: StrLetters(),
},
{
Name: "ascii",
ValidValues: []string{"abcd"},
InvalidValues: []string{"abcd😎"},
Validation: StrAscii(),
},
{
Name: "has prefix",
ValidValues: []string{"abcd"},
InvalidValues: []string{"def"},
Validation: StrHasPrefix("abc"),
},
{
Name: "has suffix",
ValidValues: []string{"aaawxyz"},
InvalidValues: []string{"aaabcde"},
Validation: StrHasSuffix("xyz"),
},
{
Name: "contains",
ValidValues: []string{"things.com"},
InvalidValues: []string{"without a dot"},
Validation: StrContains("."),
},
{
Name: "contains any",
ValidValues: []string{"A", "ABCD", "alskdjflkasdfA"},
InvalidValues: []string{"B", "UIOUP"},
Validation: StrContainsAny("A"),
},
{
Name: "match",
ValidValues: []string{"abc", "abcdefg"},
InvalidValues: []string{"def", "ABCD", "01234"},
Validation: StrMatch("abc.*"),
},
{
Name: "match w/ invalid regex",
InvalidValues: []string{"def", "ABCD", "01234"},
Validation: StrMatch("..**\\("),
},
{
Name: "email",
ValidValues: []string{
"thing@gmail.com",
"test.withdot@hotmail.com",
"test+withplus@yahoo.com",
},
InvalidValues: []string{"def", "ABCD", "01234"},
Validation: StrEmail(),
},
}
func Test_Strings(t *testing.T) {
for _, tc := range strTests {
tc.Run(t)
}
}
func Test_Regexp(t *testing.T) {
rg := regexp.MustCompile("ab?")
if err := StrRegexp(rg)("abz"); err != nil {
t.Errorf("unexpected validation error")
}
if err := StrRegexp(rg)("abd"); err != nil {
t.Errorf("unexpected validation error")
}
if err := StrRegexp(rg)("zdf"); err == nil {
t.Errorf("expected validation error")
}
}