-
Notifications
You must be signed in to change notification settings - Fork 124
/
helper_test.go
489 lines (434 loc) · 10.2 KB
/
helper_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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package govalidator
import (
"testing"
)
type inputs map[string]bool
var (
_alpha = inputs{
"abcdefghijgklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ": true,
"7877**": false,
"abc": true,
")(^%&)": false,
}
_alphaDash = inputs{
"abcdefghijgklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-": true,
"John_Do-E": true,
"+=a(0)": false,
}
_alphaNumeric = inputs{
"abcdefghijgklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890": true,
"090a": true,
"*&*)": false,
}
_boolStringsList = inputs{
"0": true,
"1": true,
"true": true,
"false": true,
"o": false,
"a": false,
}
// Ref: https://www.freeformatter.com/credit-card-number-generator-validator.html
_creditCardList = inputs{
"4896644531043572": true,
"2221005631780408": true,
"349902515380498": true,
"6011843157272458": true,
"3543358904915048": true,
"5404269782892303": true,
"4508168417293390": true,
"0604595245598387": false,
"6388244169973297": false,
}
_coordinateList = inputs{
"30.297018,-78.486328": true,
"40.044438,-104.0625": true,
"58.068581,-99.580078": true,
"abc, xyz": false,
"0, 887": false,
}
_cssColorList = inputs{
"#000": true,
"#00aaff": true,
"rgb(123,32,12)": true,
"#0": false,
"#av": false,
}
_dateList = inputs{
"2016-10-14": true,
"2013/02/18": true,
"2020/12/30": true,
"0001/14/30": false,
}
_dateDDMMYYList = inputs{
"01-01-2000": true,
"28/02/2001": true,
"01/12/2000": true,
"2012/11/30": false,
"201/11/30": false,
}
_emailList = inputs{
"[email protected]": true,
"[email protected]": true,
"[email protected]": true,
"janeahoo.com": false,
"[email protected]": false,
}
_floatList = inputs{"123": true, "12.50": true, "33.07": true, "abc": false, "o0.45": false, "100000000000000": true, "100000000000000.189": true}
_roleList = []string{"admin", "manager", "supervisor"}
_validJSONString = `{"FirstName": "Bob", "LastName": "Smith"}`
_invalidJSONString = `{"invalid json"}`
_macaddressList = inputs{
"fc:40:2e:f1:d3:6f": true,
"87:7a:45:f6:8b:ed": true,
"a5:91:91:80:d2:fd": true,
"1f:ce:44:46:24:b4": true,
"00:02:x2:34:72:a5": false,
}
_numericStringList = inputs{"12": true, "09": true, "878": true, "100": true, "a": false, "xyz": false, "1000000000000": true}
_latList = inputs{"30.297018": true, "40.044438": true, "a": false, "xyz": false}
_lonList = inputs{"-78.486328": true, "-104.0625": true, "a": false, "xyz": false}
_ipList = inputs{"10.255.255.255": true, "172.31.255.255": true, "192.168.255.255": true, "a92.168.255.255": false, "172.31.255.25b": false}
_ipV6List = inputs{
"1200:0000:AB00:1234:0000:2552:7777:1313": true,
"21DA:D3:0:2F3B:2AA:FF:FE28:9C5A": true,
"10.255.255.255": false,
}
_urlList = inputs{
"http://www.google.com": true,
"https://www.google.com": true,
"https://facebook.com": true,
"yahoo.com": true,
"adca": false,
}
_uuidList = inputs{
"ee7cf0a0-1922-401b-a1ae-6ec9261484c0": true,
"ee7cf0a0-1922-401b-a1ae-6ec9261484c1": true,
"ee7cf0a0-1922-401b-a1ae-6ec9261484a0": true,
"39888f87-fb62-5988-a425-b2ea63f5b81e": false,
}
_uuidV3List = inputs{
"a987fbc9-4bed-3078-cf07-9141ba07c9f3": true,
"b987fbc9-4bed-3078-cf07-9141ba07c9f3": true,
"ee7cf0a0-1922-401b-a1ae-6ec9261484c0": false,
}
_uuidV4List = inputs{
"df7cca36-3d7a-40f4-8f06-ae03cc22f045": true,
"ef7cca36-3d7a-40f4-8f06-ae03cc22f048": true,
"b987fbc9-4bed-3078-cf07-9141ba07c9f3": false,
}
_uuidV5List = inputs{
"39888f87-fb62-5988-a425-b2ea63f5b81e": true,
"33388f87-fb62-5988-a425-b2ea63f5b81f": true,
"b987fbc9-4bed-3078-cf07-9141ba07c9f3": false,
}
)
func Test_IsAlpha(t *testing.T) {
for a, s := range _alpha {
if isAlpha(a) != s {
t.Error("IsAlpha failed to determine alpha!")
}
}
}
func Benchmark_IsAlpha(b *testing.B) {
for n := 0; n < b.N; n++ {
isAlpha("abcdAXZY")
}
}
func Test_IsAlphaDash(t *testing.T) {
for a, s := range _alphaDash {
if isAlphaDash(a) != s {
t.Error("IsAlphaDash failed to determine alpha dash!")
}
}
}
func Benchmark_IsAlphaDash(b *testing.B) {
for n := 0; n < b.N; n++ {
isAlphaDash("John_Do-E")
}
}
func Test_IsAlphaNumeric(t *testing.T) {
for a, s := range _alphaNumeric {
if isAlphaNumeric(a) != s {
t.Error("IsAlphaNumeric failed to determine alpha numeric!")
}
}
}
func Benchmark_IsAlphaNumeric(b *testing.B) {
for n := 0; n < b.N; n++ {
isAlphaNumeric("abc12AZ")
}
}
func Test_IsBoolean(t *testing.T) {
for b, s := range _boolStringsList {
if isBoolean(b) != s {
t.Error("IsBoolean failed to determine boolean!")
}
}
}
func Benchmark_IsBoolean(b *testing.B) {
for n := 0; n < b.N; n++ {
isBoolean("true")
}
}
func Test_IsCreditCard(t *testing.T) {
for card, state := range _creditCardList {
if isCreditCard(card) != state {
t.Error("IsCreditCard failed to determine credit card!")
}
}
}
func Benchmark_IsCreditCard(b *testing.B) {
for n := 0; n < b.N; n++ {
isCreditCard("2221005631780408")
}
}
func Test_IsCoordinate(t *testing.T) {
for c, s := range _coordinateList {
if isCoordinate(c) != s {
t.Error("IsCoordinate failed to determine coordinate!")
}
}
}
func Benchmark_IsCoordinate(b *testing.B) {
for n := 0; n < b.N; n++ {
isCoordinate("30.297018,-78.486328")
}
}
func Test_IsCSSColor(t *testing.T) {
for c, s := range _cssColorList {
if isCSSColor(c) != s {
t.Error("IsCSSColor failed to determine css color code!")
}
}
}
func Benchmark_IsCSSColor(b *testing.B) {
for n := 0; n < b.N; n++ {
isCSSColor("#00aaff")
}
}
func Test_IsDate(t *testing.T) {
for d, s := range _dateList {
if isDate(d) != s {
t.Error("IsDate failed to determine date!")
}
}
}
func Benchmark_IsDate(b *testing.B) {
for n := 0; n < b.N; n++ {
isDate("2016-10-14")
}
}
func Test_IsDateDDMMYY(t *testing.T) {
for d, s := range _dateDDMMYYList {
if isDateDDMMYY(d) != s {
t.Error("IsDateDDMMYY failed to determine date!")
}
}
}
func Benchmark_IsDateDDMMYY(b *testing.B) {
for n := 0; n < b.N; n++ {
isDateDDMMYY("23-10-2014")
}
}
func Test_IsEmail(t *testing.T) {
for e, s := range _emailList {
if isEmail(e) != s {
t.Error("IsEmail failed to determine email!")
}
}
}
func Benchmark_IsEmail(b *testing.B) {
for n := 0; n < b.N; n++ {
isEmail("[email protected]")
}
}
func Test_IsFloat(t *testing.T) {
for f, s := range _floatList {
if isFloat(f) != s {
t.Error("IsFloat failed to determine float value!")
}
}
}
func Benchmark_IsFloat(b *testing.B) {
for n := 0; n < b.N; n++ {
isFloat("123.001")
}
}
func Test_IsIn(t *testing.T) {
if !isIn(_roleList, "admin") {
t.Error("IsIn failed!")
}
}
func Benchmark_IsIn(b *testing.B) {
for n := 0; n < b.N; n++ {
isIn(_roleList, "maager")
}
}
func Test_IsJSON(t *testing.T) {
if !isJSON(_validJSONString) {
t.Error("IsJSON failed!")
}
if isJSON(_invalidJSONString) {
t.Error("IsJSON unable to detect invalid json!")
}
}
func Benchmark_IsJSON(b *testing.B) {
for n := 0; n < b.N; n++ {
isJSON(_validJSONString)
}
}
func Test_IsMacAddress(t *testing.T) {
for n, s := range _macaddressList {
if isMacAddress(n) != s {
t.Error("IsMacAddress failed!")
}
}
}
func Benchmark_IsMacAddress(b *testing.B) {
for n := 0; n < b.N; n++ {
isMacAddress("00:02:02:34:72:a5")
}
}
func Test_IsNumeric(t *testing.T) {
for n, s := range _numericStringList {
if isNumeric(n) != s {
t.Error("IsNumeric failed!")
}
}
}
func Benchmark_IsNumeric(b *testing.B) {
for n := 0; n < b.N; n++ {
isNumeric("123")
}
}
func Test_IsLatitude(t *testing.T) {
for n, s := range _latList {
if isLatitude(n) != s {
t.Error("IsLatitude failed!")
}
}
}
func Benchmark_IsLatitude(b *testing.B) {
for n := 0; n < b.N; n++ {
isLatitude("30.297018")
}
}
func Test_IsLongitude(t *testing.T) {
for n, s := range _lonList {
if isLongitude(n) != s {
t.Error("IsLongitude failed!")
}
}
}
func Benchmark_IsLongitude(b *testing.B) {
for n := 0; n < b.N; n++ {
isLongitude("-78.486328")
}
}
func Test_IsIP(t *testing.T) {
for i, s := range _ipList {
if isIP(i) != s {
t.Error("IsIP failed!")
}
}
}
func Benchmark_IsIP(b *testing.B) {
for n := 0; n < b.N; n++ {
isIP("10.255.255.255")
}
}
func Test_IsIPV4(t *testing.T) {
for i, s := range _ipList {
if isIPV4(i) != s {
t.Error("IsIPV4 failed!")
}
}
}
func Benchmark_IsIPV4(b *testing.B) {
for n := 0; n < b.N; n++ {
isIPV4("10.255.255.255")
}
}
func Test_IsIPV6(t *testing.T) {
for i, s := range _ipV6List {
if isIPV6(i) != s {
t.Error("IsIPV4 failed!")
}
}
}
func Benchmark_IsIPV6(b *testing.B) {
for n := 0; n < b.N; n++ {
isIPV6("10.255.255.255")
}
}
func Test_IsMatchedRegex(t *testing.T) {
if !isMatchedRegex("^(name|age)$", "name") {
t.Error("IsMatchedRegex failed!")
}
}
func Benchmark_IsMatchedRegex(b *testing.B) {
for n := 0; n < b.N; n++ {
isMatchedRegex("^(name|age)$", "name")
}
}
func Test_IsURL(t *testing.T) {
for u, s := range _urlList {
if isURL(u) != s {
t.Error("IsURL failed!")
}
}
}
func Benchmark_IsURL(b *testing.B) {
for n := 0; n < b.N; n++ {
isURL("https://www.facebook.com")
}
}
func Test_IsUUID(t *testing.T) {
for u, s := range _uuidList {
if isUUID(u) != s {
t.Error("IsUUID failed!")
}
}
}
func Benchmark_IsUUID(b *testing.B) {
for n := 0; n < b.N; n++ {
isUUID("ee7cf0a0-1922-401b-a1ae-6ec9261484c0")
}
}
func Test_IsUUID3(t *testing.T) {
for u, s := range _uuidV3List {
if isUUID3(u) != s {
t.Error("IsUUID3 failed!")
}
}
}
func Benchmark_IsUUID3(b *testing.B) {
for n := 0; n < b.N; n++ {
isUUID3("a987fbc9-4bed-3078-cf07-9141ba07c9f3")
}
}
func Test_IsUUID4(t *testing.T) {
for u, s := range _uuidV4List {
if isUUID4(u) != s {
t.Error("IsUUID4 failed!")
}
}
}
func Benchmark_IsUUID4(b *testing.B) {
for n := 0; n < b.N; n++ {
isUUID4("57b73598-8764-4ad0-a76a-679bb6640eb1")
}
}
func Test_IsUUID5(t *testing.T) {
for u, s := range _uuidV5List {
if isUUID5(u) != s {
t.Error("IsUUID5 failed!")
}
}
}
func Benchmark_IsUUID5(b *testing.B) {
for n := 0; n < b.N; n++ {
isUUID5("987fbc97-4bed-5078-9f07-9141ba07c9f3")
}
}