-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_test.go
More file actions
590 lines (560 loc) · 17.3 KB
/
Copy pathnumeric_test.go
File metadata and controls
590 lines (560 loc) · 17.3 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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package validator
import (
"context"
"testing"
)
func TestNumericNonFinite(t *testing.T) {
num := &numericRule{}
for _, s := range []string{"Inf", "+Inf", "-Inf", "Infinity", "NaN", "nan"} {
if num.Passes(&fakeField{val: rvOf(s)}) {
t.Errorf("numeric %q should be false", s)
}
}
if !num.Passes(&fakeField{val: rvOf("3.14")}) {
t.Error("numeric 3.14 should pass")
}
}
func TestSizeBlankThreshold(t *testing.T) {
if (&minRule{}).Passes(&fakeField{val: rvOf("ab"), attrs: []string{""}}) {
t.Error("min with blank threshold should fail")
}
if (&betweenRule{}).Passes(&fakeField{val: rvOf(5), attrs: []string{"", "10"}}) {
t.Error("between with blank lower bound should fail")
}
}
func TestBooleanRejectsNonBinary(t *testing.T) {
b := &booleanRule{}
for _, v := range []any{2, -5, 0.5, 2.0} {
if b.Passes(&fakeField{val: rvOf(v)}) {
t.Errorf("boolean %v should fail", v)
}
}
if !b.Passes(&fakeField{val: rvOf(1)}) {
t.Error("boolean 1 should pass")
}
if !b.Passes(&fakeField{val: rvOf("true")}) {
t.Error(`boolean "true" should pass`)
}
if b.Passes(&fakeField{val: rvOf("maybe")}) {
t.Error(`boolean "maybe" should fail`)
}
// blank is not a boolean, matching numeric/date
for _, v := range []any{" ", "\t\n", []byte(" ")} {
if b.Passes(&fakeField{val: rvOf(v)}) {
t.Errorf("boolean %q should fail: blank is not a boolean", v)
}
}
}
// number matches numeric's trimming and accepts uint64-range strings like the
// native uint64 branch does.
func TestNumberStringConsistency(t *testing.T) {
n := &numberRule{}
if !n.Passes(&fakeField{val: rvOf(" 42 ")}) {
t.Error(`number " 42 " should pass: trimmed like numeric`)
}
if !n.Passes(&fakeField{val: rvOf("18446744073709551615")}) {
t.Error("number accepts uint64-range strings like native uint64 values")
}
if n.Passes(&fakeField{val: rvOf("18446744073709551616")}) {
t.Error("number rejects beyond-uint64 strings")
}
}
// numeric is strict decimal: Go literal syntax is not form data.
func TestNumericStrictDecimal(t *testing.T) {
num := &numericRule{}
for _, s := range []string{"1_000", "0x1F", "0x1p4", "1e3", "1E-2", ".", "5.", ".5", "+"} {
if num.Passes(&fakeField{val: rvOf(s)}) {
t.Errorf("numeric %q should fail under strict decimal parsing", s)
}
}
for _, s := range []string{"42", "-7", "+3", "3.14", " 10 ", "-0.5"} {
if !num.Passes(&fakeField{val: rvOf(s)}) {
t.Errorf("numeric %q should pass", s)
}
}
}
func TestNumberRejectsNonInteger(t *testing.T) {
n := &numberRule{}
for _, v := range []any{" ", "1e3", "3.0", "0x1F", 3.5} {
if n.Passes(&fakeField{val: rvOf(v)}) {
t.Errorf("number %v should fail", v)
}
}
for _, v := range []any{float64(3.0), 42, "42", "-7"} {
if !n.Passes(&fakeField{val: rvOf(v)}) {
t.Errorf("number %v should pass", v)
}
}
}
func TestMin(t *testing.T) {
r := &minRule{}
if r.Signature() != "min" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"number equal", 3, []string{"3"}, true},
{"number above", 5, []string{"3"}, true},
{"number below", 2, []string{"3"}, false},
{"float below", 2.5, []string{"3"}, false},
{"numeric string counts runes by default", "10", []string{"3"}, false},
{"string length equal", "abc", []string{"3"}, true},
{"string length below", "ab", []string{"3"}, false},
{"slice length above", []int{1, 2, 3, 4}, []string{"3"}, true},
{"empty value passes", "", []string{"3"}, true},
{"nil passes", nil, []string{"3"}, true},
{"missing arg", "abc", []string{}, false},
{"bad arg", "abc", []string{"x"}, false},
{"unmeasurable struct", struct{}{}, []string{"3"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestMax(t *testing.T) {
r := &maxRule{}
if r.Signature() != "max" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"number equal", 10, []string{"10"}, true},
{"number below", 5, []string{"10"}, true},
{"number above", 11, []string{"10"}, false},
{"numeric string counts runes by default", "20", []string{"10"}, true},
{"string length equal", "abcde", []string{"5"}, true},
{"string length above", "abcdef", []string{"5"}, false},
{"empty passes", "", []string{"5"}, true},
{"missing arg", "abc", []string{}, false},
{"bad arg", "abc", []string{"x"}, false},
{"unmeasurable struct", struct{}{}, []string{"5"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestBetween(t *testing.T) {
r := &betweenRule{}
if r.Signature() != "between" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"in range", 5, []string{"1", "10"}, true},
{"lower bound", 1, []string{"1", "10"}, true},
{"upper bound", 10, []string{"1", "10"}, true},
{"below", 0, []string{"1", "10"}, true}, // 0 is empty -> passes
{"above", 11, []string{"1", "10"}, false},
{"string length in range", "abc", []string{"2", "5"}, true},
{"string length below", "a", []string{"2", "5"}, false},
{"empty passes", "", []string{"1", "10"}, true},
{"missing args", 5, []string{"1"}, false},
{"bad first arg", 5, []string{"x", "10"}, false},
{"bad second arg", 5, []string{"1", "y"}, false},
{"unmeasurable struct", struct{}{}, []string{"1", "10"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestGt(t *testing.T) {
r := >Rule{}
if r.Signature() != "gt" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"above", 5, []string{"3"}, true},
{"equal fails", 3, []string{"3"}, false},
{"below fails", 2, []string{"3"}, false},
{"string length above", "abcd", []string{"3"}, true},
{"string length equal fails", "abc", []string{"3"}, false},
{"empty passes", "", []string{"3"}, true},
{"missing arg", 5, []string{}, false},
{"bad arg", 5, []string{"x"}, false},
{"unmeasurable struct", struct{}{}, []string{"3"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestGte(t *testing.T) {
r := >eRule{}
if r.Signature() != "gte" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"above", 5, []string{"3"}, true},
{"equal", 3, []string{"3"}, true},
{"below fails", 2, []string{"3"}, false},
{"string length equal", "abc", []string{"3"}, true},
{"empty passes", "", []string{"3"}, true},
{"missing arg", 5, []string{}, false},
{"bad arg", 5, []string{"x"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestLt(t *testing.T) {
r := <Rule{}
if r.Signature() != "lt" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"below", 2, []string{"3"}, true},
{"equal fails", 3, []string{"3"}, false},
{"above fails", 5, []string{"3"}, false},
{"string length below", "ab", []string{"3"}, true},
{"empty passes", "", []string{"3"}, true},
{"missing arg", 5, []string{}, false},
{"bad arg", 5, []string{"x"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestLte(t *testing.T) {
r := <eRule{}
if r.Signature() != "lte" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"below", 2, []string{"3"}, true},
{"equal", 3, []string{"3"}, true},
{"above fails", 5, []string{"3"}, false},
{"string length equal", "abc", []string{"3"}, true},
{"empty passes", "", []string{"3"}, true},
{"missing arg", 5, []string{}, false},
{"bad arg", 5, []string{"x"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestLen(t *testing.T) {
r := &lenRule{}
if r.Signature() != "len" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"number equal", 6, []string{"6"}, true},
{"number not equal", 5, []string{"6"}, false},
{"string length equal", "abcdef", []string{"6"}, true},
{"string length not equal", "abc", []string{"6"}, false},
{"slice length equal", []int{1, 2}, []string{"2"}, true},
{"empty passes", "", []string{"6"}, true},
{"missing arg", "abc", []string{}, false},
{"bad arg", "abc", []string{"x"}, false},
{"unmeasurable struct", struct{}{}, []string{"6"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestSize(t *testing.T) {
r := &sizeRule{}
if r.Signature() != "size" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"number equal", 6, []string{"6"}, true},
{"number not equal", 5, []string{"6"}, false},
{"string length equal", "abcdef", []string{"6"}, true},
{"empty passes", "", []string{"6"}, true},
{"missing arg", "abc", []string{}, false},
{"bad arg", "abc", []string{"x"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestDigits(t *testing.T) {
r := &digitsRule{}
if r.Signature() != "digits" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
attrs []string
want bool
}{
{"all digits exact", "1234", []string{"4"}, true},
{"all digits wrong length", "123", []string{"4"}, false},
{"contains letter", "12a4", []string{"4"}, false},
{"contains dot", "12.4", []string{"4"}, false},
{"negative sign fails", "-123", []string{"4"}, false},
{"int value as string", 1234, []string{"4"}, true},
{"empty passes", "", []string{"4"}, true},
{"missing arg", "1234", []string{}, false},
{"bad arg", "1234", []string{"x"}, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val), attrs: c.attrs})
if got != c.want {
t.Errorf("Passes(%v, %v) = %v, want %v", c.val, c.attrs, got, c.want)
}
})
}
}
func TestNumeric(t *testing.T) {
r := &numericRule{}
if r.Signature() != "numeric" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
want bool
}{
{"int", 5, true},
{"float", 3.14, true},
{"numeric string", "12.5", true},
{"negative numeric string", "-7", true},
{"non-numeric string", "abc", false},
{"slice not numeric", []int{1, 2}, false},
{"empty passes", "", true},
{"nil passes", nil, true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val)})
if got != c.want {
t.Errorf("Passes(%v) = %v, want %v", c.val, got, c.want)
}
})
}
}
func TestNumber(t *testing.T) {
r := &numberRule{}
if r.Signature() != "number" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
want bool
}{
{"int", 5, true},
{"int string", "42", true},
{"float string fails", "3.14", false},
{"non-numeric string fails", "abc", false},
{"empty passes", "", true},
{"nil passes", nil, true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val)})
if got != c.want {
t.Errorf("Passes(%v) = %v, want %v", c.val, got, c.want)
}
})
}
}
func TestBoolean(t *testing.T) {
r := &booleanRule{}
if r.Signature() != "boolean" {
t.Fatalf("signature = %q", r.Signature())
}
cases := []struct {
name string
val any
want bool
}{
{"bool true", true, true},
{"int one", 1, true},
{"string true", "true", true},
{"string yes", "yes", true},
{"string on", "on", true},
{"string off", "off", true},
{"invalid string fails", "maybe", false},
{"non-convertible struct fails", struct{}{}, false},
{"empty passes", "", true},
{"nil passes", nil, true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := r.Passes(&fakeField{name: "x", val: rvOf(c.val)})
if got != c.want {
t.Errorf("Passes(%v) = %v, want %v", c.val, got, c.want)
}
})
}
}
func TestNumericMessages(t *testing.T) {
msgs := map[string]string{
"min": (&minRule{}).Message(),
"max": (&maxRule{}).Message(),
"between": (&betweenRule{}).Message(),
"gt": (>Rule{}).Message(),
"gte": (>eRule{}).Message(),
"lt": (<Rule{}).Message(),
"lte": (<eRule{}).Message(),
"len": (&lenRule{}).Message(),
"size": (&sizeRule{}).Message(),
"digits": (&digitsRule{}).Message(),
"numeric": (&numericRule{}).Message(),
"number": (&numberRule{}).Message(),
"boolean": (&booleanRule{}).Message(),
}
for sig, msg := range msgs {
if msg == "" {
t.Errorf("rule %q has empty message", sig)
}
}
}
// Size rules on strings count runes by default; a numeric/number assertion on
// the expression's AND spine flips them to numeric comparison (the compiler
// swaps in the hinted variant).
func TestSizeStringSemantics(t *testing.T) {
ctx := context.Background()
// the README password shape: min:8 must reject a short numeric password
pw := MustMap(map[string]any{"pwd": "999"}, map[string]string{"pwd": "required && min:8"})
_ = pw.Validate(ctx)
if !pw.Errors().Has("pwd") {
t.Error(`min:8 on "999" must fail by rune count — numeric content must not bypass a length constraint`)
}
// form-style numeric field: numeric && gte:18 compares by value
age := MustMap(map[string]any{"age": "30"}, map[string]string{"age": "numeric && gte:18"})
_ = age.Validate(ctx)
if age.Fails() {
t.Errorf(`numeric && gte:18 on "30" must compare numerically, got %v`, age.Errors().All())
}
young := MustMap(map[string]any{"age": "9"}, map[string]string{"age": "numeric && gte:18"})
_ = young.Validate(ctx)
if !young.Errors().Has("age") {
t.Error(`numeric && gte:18 on "9" must fail numerically (9 < 18), not pass by length`)
}
// the hint applies only on the AND spine, not inside || or !
or := MustMap(map[string]any{"v": "999"}, map[string]string{"v": "(numeric || alpha) && min:8"})
_ = or.Validate(ctx)
if !or.Errors().Has("v") {
t.Error("a numeric leaf inside || must not flip min to numeric comparison")
}
// multibyte strings count runes, not bytes
uni := MustMap(map[string]any{"name": "héllo"}, map[string]string{"name": "len:5"})
_ = uni.Validate(ctx)
if uni.Fails() {
t.Errorf("len counts runes: héllo is 5, got %v", uni.Errors().All())
}
}
// Integer comparisons stay exact above 2^53 (no float64 rounding).
func TestSizeIntegerPrecision(t *testing.T) {
ctx := context.Background()
const big = int64(9007199254740993) // 2^53 + 1: rounds to 2^53 in float64
eq := MustMap(map[string]any{"n": big}, map[string]string{"n": "gte:9007199254740993"})
_ = eq.Validate(ctx)
if eq.Fails() {
t.Errorf("gte at 2^53+1 must compare exactly, got %v", eq.Errors().All())
}
lt := MustMap(map[string]any{"n": big - 1}, map[string]string{"n": "lt:9007199254740993"})
_ = lt.Validate(ctx)
if lt.Fails() {
t.Errorf("lt at 2^53 boundary must compare exactly, got %v", lt.Errors().All())
}
gt := MustMap(map[string]any{"n": big}, map[string]string{"n": "gt:9007199254740992"})
_ = gt.Validate(ctx)
if gt.Fails() {
t.Errorf("gt at 2^53 boundary must compare exactly, got %v", gt.Errors().All())
}
// uint64 above MaxInt64 with an exact threshold
u := MustMap(map[string]any{"n": uint64(18446744073709551615)}, map[string]string{"n": "gte:18446744073709551615"})
_ = u.Validate(ctx)
if u.Fails() {
t.Errorf("MaxUint64 gte:MaxUint64 must pass exactly, got %v", u.Errors().All())
}
// numeric strings under the hint also compare exactly
s := MustMap(map[string]any{"n": "9007199254740993"}, map[string]string{"n": "numeric && gt:9007199254740992"})
_ = s.Validate(ctx)
if s.Fails() {
t.Errorf("hinted numeric string must compare exactly above 2^53, got %v", s.Errors().All())
}
}