-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
322 lines (290 loc) · 10.6 KB
/
string_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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package vld
import (
"regexp"
"testing"
)
func ptrStr(val string) *string {
return &val
}
type testString struct {
name string
s *stringVld
wantErr bool
}
func Test_String_Required(t *testing.T) {
testStringRequired(t, "String", []testString{
{"1", String(""), true},
{"2", String("x"), false},
})
testStringRequired(t, "StrPtr", []testString{
{"1", StrPtr(nil), true},
{"2", StrPtr(ptrStr("x")), false},
})
}
func testStringRequired(t *testing.T, detail string, tests []testString) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.Required().Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.Required() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
type testValueStringLength struct {
name string
s *stringVld
length int
wantErr bool
}
func Test_String_GT(t *testing.T) {
testStringGT(t, "String", []testValueStringLength{
{"1", String("a"), 0, false}, // test zero length
{"2", String(""), 10, false}, // not required
{"3", String("").Required(), 10, true}, // required
{"4", String("a"), 1, true}, // greater than 1
{"5", String("ab"), 1, false},
})
testStringGT(t, "StrPtr", []testValueStringLength{
{"1", StrPtr(ptrStr("a")), 0, false}, // test zero length
{"2", StrPtr(nil), 10, false}, // not required
{"3", StrPtr(nil).Required(), 10, true}, // required
{"4", StrPtr(ptrStr("a")), 1, true}, // greater than 1
{"5", StrPtr(ptrStr("ab")), 1, false},
})
}
func testStringGT(t *testing.T, detail string, tests []testValueStringLength) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.GT(tt.length).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.GT() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
func Test_String_GTE(t *testing.T) {
testStringGTE(t, "String", []testValueStringLength{
{"1", String("a"), 0, false}, // test zero length
{"2", String(""), 10, false}, // not required
{"3", String("").Required(), 10, true}, // required
{"4", String("a"), 1, false}, // equal 1
{"5", String("ab"), 1, false},
{"6", String("ab"), 10, true},
})
testStringGTE(t, "StrPtr", []testValueStringLength{
{"1", StrPtr(ptrStr("a")), 0, false}, // test zero length
{"2", StrPtr(nil), 10, false}, // not required
{"3", StrPtr(nil).Required(), 10, true}, // required
{"4", StrPtr(ptrStr("a")), 1, false}, // equal 1
{"5", StrPtr(ptrStr("ab")), 1, false},
{"6", StrPtr(ptrStr("ab")), 10, true},
})
}
func testStringGTE(t *testing.T, detail string, tests []testValueStringLength) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.GTE(tt.length).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.GTE() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
func Test_String_LT(t *testing.T) {
testStringLT(t, "String", []testValueStringLength{
{"1", String("a"), 0, true}, // test zero length
{"2", String(""), 0, false}, // not required
{"3", String("").Required(), 0, true}, // required
{"4", String("a"), 1, true}, // equal
{"5", String("a"), 2, false}, // smaller than 2
{"6", String("ab"), 1, true},
})
testStringLT(t, "StrPtr", []testValueStringLength{
{"1", StrPtr(ptrStr("a")), 0, true}, // test zero length
{"2", StrPtr(nil), 0, false}, // not required
{"3", StrPtr(nil).Required(), 0, true}, // required
{"4", StrPtr(ptrStr("a")), 1, true}, // equal
{"5", StrPtr(ptrStr("a")), 2, false}, // smaller than 2
{"6", StrPtr(ptrStr("ab")), 1, true},
})
}
func testStringLT(t *testing.T, detail string, tests []testValueStringLength) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.LT(tt.length).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.LT() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
func Test_String_LTE(t *testing.T) {
testStringLTE(t, "String", []testValueStringLength{
{"1", String("a"), 0, true}, // test zero length
{"2", String(""), 0, false}, // not required
{"3", String("").Required(), 0, true}, // required
{"4", String("a"), 1, false}, // equal
{"5", String("a"), 2, false}, // smaller than 2
{"6", String("ab"), 1, true},
})
testStringLTE(t, "StrPtr", []testValueStringLength{
{"1", StrPtr(ptrStr("a")), 0, true}, // test zero length
{"2", StrPtr(nil), 0, false}, // not required
{"3", StrPtr(nil).Required(), 0, true}, // required
{"4", StrPtr(ptrStr("a")), 1, false}, // equal
{"5", StrPtr(ptrStr("a")), 2, false}, // smaller than 2
{"6", StrPtr(ptrStr("ab")), 1, true},
})
}
func testStringLTE(t *testing.T, detail string, tests []testValueStringLength) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.LTE(tt.length).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.LTE() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
type testValueStringMinMax struct {
name string
s *stringVld
min int
max int
wantErr bool
}
func Test_String_Length(t *testing.T) {
testStringLength(t, "String", []testValueStringMinMax{
{"1", String("a"), 0, 0, true}, // test zero
{"2", String(""), 5, 10, false}, // not required
{"3", String("").Required(), 5, 10, true}, // required
{"4", String("a"), 1, 3, false}, // equal min
{"5", String("ab"), 1, 3, false}, // mid
{"6", String("abc"), 1, 3, false}, // equal min
{"7", String("abc"), 1, 2, true}, // upper
{"8", String("abc"), 4, 5, true}, // smaller
})
testStringLength(t, "StrPtr", []testValueStringMinMax{
{"1", StrPtr(ptrStr("a")), 0, 0, true}, // test zero
{"2", StrPtr(nil), 5, 10, false}, // not required
{"3", StrPtr(nil).Required(), 5, 10, true}, // required
{"4", StrPtr(ptrStr("a")), 1, 3, false}, // equal min
{"5", StrPtr(ptrStr("ab")), 1, 3, false}, // mid
{"6", StrPtr(ptrStr("abc")), 1, 3, false}, // equal min
{"7", StrPtr(ptrStr("abc")), 1, 2, true}, // upper
{"8", StrPtr(ptrStr("abc")), 4, 5, true}, // smaller
})
}
func testStringLength(t *testing.T, detail string, tests []testValueStringMinMax) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.Length(tt.min, tt.max).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.Length() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
func Test_String_Len(t *testing.T) {
testStringLen(t, "String", []testValueStringLength{
{"1", String("a"), 0, true}, // test zero
{"2", String(""), 5, false}, // not required
{"3", String("").Required(), 5, true}, // required
{"4", String("ab"), 2, false}, // equal
{"5", String("ab"), 3, true}, // different
})
testStringLen(t, "StrPtr", []testValueStringLength{
{"1", StrPtr(ptrStr("a")), 0, true}, // test zero
{"2", StrPtr(nil), 5, false}, // not required
{"3", StrPtr(nil).Required(), 5, true}, // required
{"4", StrPtr(ptrStr("ab")), 2, false}, // equal
{"5", StrPtr(ptrStr("ab")), 3, true}, // different
})
}
func testStringLen(t *testing.T, detail string, tests []testValueStringLength) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.Len(tt.length).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.Len() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
type testValueStringMatch struct {
name string
s *stringVld
pattern string
wantErr bool
}
func Test_String_Match(t *testing.T) {
testStringMatch(t, "String", []testValueStringMatch{
{"1", String(""), "[a-z]", false}, // not required
{"2", String("").Required(), "[a-z]", true}, // required
{"3", String("ab"), "[a-z]", false}, // match
{"4", String("ab"), "[0-9]", true}, // not match
})
testStringMatch(t, "StrPtr", []testValueStringMatch{
{"1", StrPtr(nil), "[a-z]", false}, // not required
{"2", StrPtr(nil).Required(), "[a-z]", true}, // required
{"3", StrPtr(ptrStr("ab")), "[a-z]", false}, // match
{"4", StrPtr(ptrStr("ab")), "[0-9]", true}, // not match
})
}
func testStringMatch(t *testing.T, detail string, tests []testValueStringMatch) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.Match(regexp.MustCompile(tt.pattern)).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.Match() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
type testValueStringOneOf struct {
name string
s *stringVld
values []string
wantErr bool
}
func Test_String_OneOf(t *testing.T) {
testStringOneOf(t, "String", []testValueStringOneOf{
{"1", String(""), []string{"ab"}, false}, // not required
{"2", String("").Required(), []string{"ab"}, true}, // required
{"3", String("ab"), []string{"ab"}, false}, // one
{"4", String("ab"), []string{"abc"}, true}, // not match
{"5", String("ab"), []string{"de", "fg"}, true}, // not match
})
testStringOneOf(t, "StrPtr", []testValueStringOneOf{
{"1", StrPtr(nil), []string{"ab"}, false}, // not required
{"2", StrPtr(nil).Required(), []string{"ab"}, true}, // required
{"3", StrPtr(ptrStr("ab")), []string{"ab"}, false}, // one
{"4", StrPtr(ptrStr("ab")), []string{"abc"}, true}, // not match
{"5", StrPtr(ptrStr("ab")), []string{"de", "fg"}, true}, // not match
})
}
func testStringOneOf(t *testing.T, detail string, tests []testValueStringOneOf) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.OneOf(tt.values).Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.OneOf() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}
func Test_String_IsEmail(t *testing.T) {
testStringIsEmail(t, "String", []testString{
{"1", String(""), false}, // not required
{"2", String("").Required(), true}, // required
{"3", String("foo@bah.com"), false}, // match
{"4", String("not email"), true}, // not match
})
testStringIsEmail(t, "StrPtr", []testString{
{"1", StrPtr(nil), false}, // not required
{"2", StrPtr(nil).Required(), true}, // required
{"3", StrPtr(ptrStr("foo@bah.com")), false}, // match
{"4", StrPtr(ptrStr("not email")), true}, // not match
})
}
func testStringIsEmail(t *testing.T, detail string, tests []testString) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.s.IsEmail().Error(); (err != nil) != tt.wantErr {
t.Errorf("%v.IsEmail() error = %v, wantErr %v", detail, err, tt.wantErr)
}
})
}
}