-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_test.go
More file actions
156 lines (142 loc) · 3.31 KB
/
Copy pathvalidator_test.go
File metadata and controls
156 lines (142 loc) · 3.31 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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package validate
import (
"context"
"fmt"
"testing"
)
func TestValidator_Register(t *testing.T) {
v := Global()
if err := v.Register(Rule{}); err == nil {
t.Fatalf("Register(): empty rule")
}
if err := v.Register(
Rule{
Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil }),
}); err == nil {
t.Fatalf("Register(): rule without name")
}
if err := v.Register(
Rule{
Name: "1",
Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil }),
},
); err != nil {
t.Fatalf("Register(): error = %v", err)
}
if err := v.Register(
Rule{
Name: "1",
Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error { return nil }),
},
); err == nil {
t.Fatalf("Register(): duplicate rule name")
}
}
/*
goos: linux
goarch: amd64
pkg: go.osspkg.com/validate
cpu: 12th Gen Intel(R) Core(TM) i9-12900KF
Benchmark_Validate
Benchmark_Validate-24 7162855 165.8 ns/op 48 B/op 3 allocs/op
*/
func Benchmark_Validate(b *testing.B) {
v := New()
err := v.Register(
Rule{
Name: "eq",
Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
intVal, ok := value.(int)
if !ok {
return fmt.Errorf("expected int, got %T", value)
}
if len(opts) != 1 {
return fmt.Errorf("expected 1 options, got %d", len(opts))
}
argVal, ok := opts[0].(int)
if !ok {
return fmt.Errorf("expected int, got %T", opts[0])
}
if intVal != argVal {
return fmt.Errorf("expected %d, got %d", intVal, argVal)
}
return nil
}),
},
)
if err != nil {
b.Fatal(err)
}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := v.Validate(ctx, func(cb Callback) {
cb.Require("eq", 1, 1)
cb.Require("eq", 2, 2)
cb.Optional("eq", 0, 2)
})
if err != nil {
b.Fatal(err)
}
}
})
}
/*
goos: linux
goarch: amd64
pkg: go.osspkg.com/validate
cpu: 12th Gen Intel(R) Core(TM) i9-12900KF
Benchmark_ValidateStruct
Benchmark_ValidateStruct-24 10004199 118.1 ns/op 16 B/op 2 allocs/op
*/
func Benchmark_ValidateStruct(b *testing.B) {
v := New()
v.Register(
Rule{
Name: "eq",
Handle: HandlerFunc(func(ctx context.Context, value any, opts ...any) error {
intVal, ok := value.(int)
if !ok {
return fmt.Errorf("expected int, got %T", value)
}
if len(opts) != 1 {
return fmt.Errorf("expected 1 options, got %d", len(opts))
}
var argVal int
if err := StringDecode(&argVal, opts[0].(string)); err != nil {
return err
}
if intVal != argVal {
return fmt.Errorf("expected %d, got %d", argVal, intVal)
}
return nil
}),
},
)
type mock struct {
Id1 int `validate:"required;eq=1"`
Id2 int `validate:"required;eq=2"`
Id3 int `validate:"eq=2"`
}
mod := &mock{
Id1: 1,
Id2: 2,
Id3: 0,
}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := v.ValidateStruct(ctx, mod); err != nil {
b.Fatal(err)
}
}
})
}