-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
89 lines (78 loc) · 1.79 KB
/
parser_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
package csv
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/motemen/go-testutil/dataloc"
)
func Test_parseValidateTag(t *testing.T) {
t.Parallel()
type args struct {
tags string
}
tests := []struct {
name string
args args
want validators
}{
{
name: "should return a validationRule with all fields set to false",
args: args{tags: ""},
want: validators{},
},
{
name: "should return a validationRule with shouldBool set to true",
args: args{tags: "boolean,alpha"},
want: validators{
newBooleanValidator(),
newAlphaValidator(),
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := &CSV{}
got, err := c.parseValidateTag(tt.args.tags)
if err != nil {
t.Errorf("parseValidateTag() error = %v, test case at %s", err, dataloc.L(tt.name))
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("parseValidateTage() mismatch (-got +want):\n%s", diff)
}
})
}
}
func TestCSV_parseStructTag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
arg any
want ruleSet
wantErr bool
}{
{
name: "should return an error if the struct is not a pointer",
arg: &[]struct {
Name string `validate:"boolean"`
}{},
want: ruleSet{
validators{newBooleanValidator()},
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
csv := &CSV{}
if err := csv.parseStructTag(tt.arg); (err != nil) != tt.wantErr {
t.Errorf("CSV.parseStructTag() error = %v, wantErr %v, test case at %s", err, tt.wantErr, dataloc.L(tt.name))
}
if diff := cmp.Diff(csv.ruleSet, tt.want); diff != "" {
t.Errorf("CSV.parseStructTag() mismatch (-got +want):\n%s", diff)
}
})
}
}