-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathptr_test.go
137 lines (114 loc) · 3.36 KB
/
ptr_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
package goval_test
import (
"context"
"errors"
"testing"
"github.com/pkg-id/goval"
)
func TestPtr(t *testing.T) {
ctx := context.Background()
err := goval.Ptr[string]().Validate(ctx, nil)
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
}
func TestPtrValidator_Required(t *testing.T) {
ctx := context.Background()
err := goval.Ptr[string]().Required().Validate(ctx, nil)
if err == nil {
t.Errorf("expect error; got no error")
}
var exp *goval.RuleError
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if !exp.Code.Equal(goval.PtrRequired) {
t.Errorf("expect the error code: %v; got error code: %v", goval.PtrRequired, exp.Code)
}
if exp.Args != nil {
t.Errorf("expect the error args is empty; got error args: %v", exp.Args)
}
err = goval.Ptr[string]().Required().Validate(ctx, new(string))
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
}
func TestPtrValidator_Optional(t *testing.T) {
ctx := context.Background()
sv := goval.String().Required()
err := goval.Ptr[string]().Optional(sv).Validate(ctx, nil)
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
err = goval.Ptr[string]().Optional(sv).Validate(ctx, new(string))
if err == nil {
t.Errorf("expect error; got no error")
}
var exp *goval.RuleError
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if !exp.Code.Equal(goval.StringRequired) {
t.Errorf("expect the error code: %v; got error code: %v", goval.StringRequired, exp.Code)
}
if exp.Args != nil {
t.Errorf("expect the error args is empty; got error args: %v", exp.Args)
}
}
func TestPtrValidator_Then(t *testing.T) {
ctx := context.Background()
sv := goval.String().Required()
err := goval.Ptr[string]().Then(sv).Validate(ctx, new(string))
if err == nil {
t.Errorf("expect error; got no error")
}
var exp *goval.RuleError
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if !exp.Code.Equal(goval.StringRequired) {
t.Errorf("expect the error code: %v; got error code: %v", goval.StringRequired, exp.Code)
}
if exp.Args != nil {
t.Errorf("expect the error args is empty; got error args: %v", exp.Args)
}
}
func TestPtrValidator_ThenPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expect panic; got no panic")
}
}()
sv := goval.String().Required()
_ = goval.Ptr[string]().Then(sv).Validate(context.Background(), nil)
}
func BenchmarkPtrValidator_Required(b *testing.B) {
b.Run("when rules violated", func(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
_ = goval.Ptr[int]().Required().Validate(ctx, nil)
}
})
b.Run("when no rules violated", func(b *testing.B) {
ctx := context.Background()
val := 1
for i := 0; i < b.N; i++ {
_ = goval.Ptr[int]().Required().Validate(ctx, &val)
}
})
}
func BenchmarkPtrValidator_Optional(b *testing.B) {
b.Run("when rules violated", func(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
_ = goval.Ptr[int]().Optional(goval.Number[int]().Required()).Validate(ctx, nil)
}
})
b.Run("when no rules violated", func(b *testing.B) {
ctx := context.Background()
val := 1
for i := 0; i < b.N; i++ {
_ = goval.Ptr[int]().Optional(goval.Number[int]().Required()).Validate(ctx, &val)
}
})
}