This repository was archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_test.go
More file actions
107 lines (103 loc) · 2.3 KB
/
validate_test.go
File metadata and controls
107 lines (103 loc) · 2.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
package proto_validate_reflect
import (
"strings"
"testing"
"github.com/protoconf/proto-validate-reflect/cases"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
func TestValidate(t *testing.T) {
type args struct {
msg proto.Message
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "test email",
args: args{msg: &cases.StringEmail{Val: "myemail.com"}},
wantErr: true,
},
{
name: "test email is valid",
args: args{msg: &cases.StringEmail{Val: "my@email.com"}},
wantErr: false,
},
{
name: "test float",
args: args{msg: &cases.FloatConst{Val: 99.99}},
wantErr: true,
},
{
name: "test double",
args: args{msg: &cases.DoubleConst{Val: 99.99}},
wantErr: true,
},
{
name: "test int32",
args: args{msg: &cases.Int32Const{Val: 99}},
wantErr: true,
},
{
name: "test int64",
args: args{msg: &cases.Int64Const{Val: 99}},
wantErr: true,
},
{
name: "test uint32",
args: args{msg: &cases.UInt32Const{Val: 99}},
wantErr: true,
},
{
name: "test uint64",
args: args{msg: &cases.UInt64Const{Val: 99}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := Validate(tt.args.msg); (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateError_Error(t *testing.T) {
type fields struct {
name protoreflect.Name
errors map[protoreflect.Name][]error
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "test",
fields: fields{
name: protoreflect.Name("HelloWorldMessage"),
errors: map[protoreflect.Name][]error{
protoreflect.Name("test_field"): {ErrorConst},
},
},
want: strings.Join([]string{
`Message "HelloWorldMessage" has errors:`,
`field "test_field" has the following errors:`,
"\tvalue does not match the const rule"}, "\n"),
},
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := &ValidateError{
name: tt.fields.name,
errors: tt.fields.errors,
}
if got := v.Error(); got != tt.want {
t.Errorf("ValidateError.Error() = %v, want %v", got, tt.want)
}
})
}
}