-
Notifications
You must be signed in to change notification settings - Fork 7
/
validator_number_test.go
573 lines (459 loc) · 15 KB
/
validator_number_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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package valgo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidatorNumberNot(t *testing.T) {
v := Is(Number(1).Not().EqualTo(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberEqualToValid(t *testing.T) {
var v *Validation
v = Is(Number(2).EqualTo(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).EqualTo(myNumber2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberEqualToInvalid(t *testing.T) {
var v *Validation
v = Is(Number(1).EqualTo(2))
assert.False(t, v.Valid())
assert.NotEmpty(t, v.Errors())
assert.Equal(t,
"Value 0 must be equal to \"2\"",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 1
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).EqualTo(myNumber2))
assert.False(t, v.Valid())
assert.NotEmpty(t, v.Errors())
assert.Equal(t,
"Value 0 must be equal to \"2\"",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberGreaterThanValid(t *testing.T) {
var v *Validation
v = Is(Number(3).GreaterThan(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 3
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).GreaterThan(myNumber2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberGreaterThanInvalid(t *testing.T) {
var v *Validation
v = Is(Number(2).GreaterThan(2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be greater than \"2\"",
v.Errors()["value_0"].Messages()[0])
v = Is(Number(2).GreaterThan(3))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be greater than \"3\"",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).GreaterThan(myNumber2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be greater than \"2\"",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberGreaterOrEqualToValid(t *testing.T) {
var v *Validation
v = Is(Number(2).GreaterOrEqualTo(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
v = Is(Number(3).GreaterOrEqualTo(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).GreaterOrEqualTo(myNumber2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberGreaterOrEqualToInvalid(t *testing.T) {
var v *Validation
v = Is(Number(2).GreaterOrEqualTo(3))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be greater than or equal to \"3\"",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 3
v = Is(Number(myNumber1).GreaterOrEqualTo(myNumber2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be greater than or equal to \"3\"",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberLessThanValid(t *testing.T) {
var v *Validation
v = Is(Number(2).LessThan(3))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 3
v = Is(Number(myNumber1).LessThan(myNumber2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberLessThanInvalid(t *testing.T) {
var v *Validation
v = Is(Number(2).LessThan(2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be less than \"2\"",
v.Errors()["value_0"].Messages()[0])
v = Is(Number(3).LessThan(2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be less than \"2\"",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).LessThan(myNumber2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be less than \"2\"",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberLessOrEqualToValid(t *testing.T) {
var v *Validation
v = Is(Number(2).LessOrEqualTo(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
v = Is(Number(1).LessOrEqualTo(2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).LessOrEqualTo(myNumber2))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberLessOrEqualToInvalid(t *testing.T) {
var v *Validation
v = Is(Number(3).LessOrEqualTo(2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be less than or equal to \"2\"",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 3
var myNumber2 MyNumber = 2
v = Is(Number(myNumber1).LessOrEqualTo(myNumber2))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be less than or equal to \"2\"",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberBetweenValid(t *testing.T) {
var v *Validation
v = Is(Number(2).Between(2, 6))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
v = Is(Number(4).Between(2, 6))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
v = Is(Number(6).Between(2, 6))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 2
var myNumber3 MyNumber = 6
v = Is(Number(myNumber1).Between(myNumber2, myNumber3))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberBetweenInvalid(t *testing.T) {
var v *Validation
v = Is(Number(2).Between(3, 6))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be between \"3\" and \"6\"",
v.Errors()["value_0"].Messages()[0])
v = Is(Number(7).Between(3, 6))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be between \"3\" and \"6\"",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
var myNumber2 MyNumber = 3
var myNumber3 MyNumber = 6
v = Is(Number(myNumber1).Between(myNumber2, myNumber3))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be between \"3\" and \"6\"",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberZeroValid(t *testing.T) {
var v *Validation
v = Is(Number(0).Zero())
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 0
v = Is(Number(myNumber1).Zero())
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberZeroInvalid(t *testing.T) {
var v *Validation
v = Is(Number(1).Zero())
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be zero",
v.Errors()["value_0"].Messages()[0])
v = Is(Number(0.1).Zero())
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be zero",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 1
v = Is(Number(myNumber1).Zero())
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 must be zero",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberPassingValid(t *testing.T) {
var v *Validation
v = Is(Number(1).Passing(func(val int) bool {
return val == 1
}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 1
v = Is(Number(myNumber1).Passing(func(val MyNumber) bool {
return val == 1
}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberPassingInvalid(t *testing.T) {
var v *Validation
v = Is(Number(1).Passing(func(val int) bool {
return val == 2
}))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 is not valid",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 1
v = Is(Number(myNumber1).Passing(func(val MyNumber) bool {
return val == 2
}))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 is not valid",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberInSliceValid(t *testing.T) {
var v *Validation
v = Is(Number(2).InSlice([]int{1, 2, 3}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 2
v = Is(Number(myNumber1).InSlice([]MyNumber{1, 2, 3}))
assert.True(t, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberInSliceInvalid(t *testing.T) {
var v *Validation
v = Is(Number(4).InSlice([]int{1, 2, 3}))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 is not valid",
v.Errors()["value_0"].Messages()[0])
// Custom Type
type MyNumber int
var myNumber1 MyNumber = 4
v = Is(Number(myNumber1).InSlice([]MyNumber{1, 2, 3}))
assert.False(t, v.Valid())
assert.Equal(t,
"Value 0 is not valid",
v.Errors()["value_0"].Messages()[0])
}
func TestValidatorNumberOrOperatorWithIs(t *testing.T) {
var v *Validation
var _true = true
var _false = false
// Testing Or operation with two valid conditions
v = Is(Number(1).EqualTo(1).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, _true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left invalid and right valid conditions
v = Is(Number(1).EqualTo(0).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left valid and right invalid conditions
v = Is(Number(1).EqualTo(1).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with two invalid conditions
v = Is(Number(1).EqualTo(0).Or().EqualTo(0))
assert.False(t, v.Valid())
assert.Equal(t, _false || false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing And operation (default when no Or() function is used) with left valid and right invalid conditions
v = Is(Number(1).EqualTo(1).EqualTo(0))
assert.False(t, v.Valid())
assert.Equal(t, true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing combination of Not and Or operators with left valid and right invalid conditions
v = Is(Number(1).Not().EqualTo(0).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, !false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing combination of Not and Or operators with left invalid and right valid conditions
v = Is(Number(1).Not().EqualTo(1).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, !true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the first condition being valid
v = Is(Number(1).EqualTo(1).Or().EqualTo(0).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, true || _false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the last condition being valid
v = Is(Number(1).EqualTo(0).Or().EqualTo(0).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, _false || false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid Or operation then valid And operation
v = Is(Number(1).EqualTo(0).Or().EqualTo(1).EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, false || _true && true, v.Valid())
assert.Empty(t, v.Errors())
// Testing valid Or operation then invalid And operation
v = Is(Number(1).EqualTo(0).Or().EqualTo(1).EqualTo(0))
assert.False(t, v.Valid())
assert.Equal(t, false || true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing valid And operation then invalid Or operation
v = Is(Number(1).EqualTo(1).EqualTo(1).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, _true && true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid And operation then valid Or operation
v = Is(Number(1).EqualTo(1).EqualTo(0).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, true && false || true, v.Valid())
assert.Empty(t, v.Errors())
}
func TestValidatorNumberOrOperatorWithCheck(t *testing.T) {
var v *Validation
// Check are Non-Short-circuited operations
var _true = true
var _false = false
// Testing Or operation with two valid conditions
v = Check(Number(1).EqualTo(1).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, _true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left invalid and right valid conditions
v = Check(Number(1).EqualTo(0).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with left valid and right invalid conditions
v = Check(Number(1).EqualTo(1).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing Or operation with two invalid conditions
v = Check(Number(1).EqualTo(0).Or().EqualTo(0))
assert.False(t, v.Valid())
assert.Equal(t, _false || false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing And operation (default when no Or() function is used) with left valid and right invalid conditions
v = Check(Number(1).EqualTo(1).EqualTo(0))
assert.False(t, v.Valid())
assert.Equal(t, true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing combination of Not and Or operators with left valid and right invalid conditions
v = Check(Number(1).Not().EqualTo(0).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, !false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing combination of Not and Or operators with left invalid and right valid conditions
v = Check(Number(1).Not().EqualTo(1).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, !true || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the first condition being valid
v = Check(Number(1).EqualTo(1).Or().EqualTo(0).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, true || _false || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing multiple Or operations in sequence with the last condition being valid
v = Check(Number(1).EqualTo(0).Or().EqualTo(0).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, _false || false || true, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid Or operation then valid And operation
v = Check(Number(1).EqualTo(0).Or().EqualTo(1).EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, false || _true && true, v.Valid())
assert.Empty(t, v.Errors())
// Testing valid Or operation then invalid And operation
v = Check(Number(1).EqualTo(0).Or().EqualTo(1).EqualTo(0))
assert.False(t, v.Valid())
assert.Equal(t, false || true && false, v.Valid())
assert.NotEmpty(t, v.Errors())
// Testing valid And operation then invalid Or operation
v = Check(Number(1).EqualTo(1).EqualTo(1).Or().EqualTo(0))
assert.True(t, v.Valid())
assert.Equal(t, _true && true || false, v.Valid())
assert.Empty(t, v.Errors())
// Testing invalid And operation then valid Or operation
v = Check(Number(1).EqualTo(1).EqualTo(0).Or().EqualTo(1))
assert.True(t, v.Valid())
assert.Equal(t, true && false || true, v.Valid())
assert.Empty(t, v.Errors())
}