-
Notifications
You must be signed in to change notification settings - Fork 0
/
fender_test.go
142 lines (130 loc) · 3.05 KB
/
fender_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
138
139
140
141
142
package fender_test
import (
"context"
"testing"
"github.com/foomo/fender"
"github.com/foomo/fender/fend"
"github.com/foomo/fender/rule"
"github.com/stretchr/testify/assert"
)
type Gender string
const (
GenderMale Gender = "male"
GenderFemmale Gender = "female"
)
func (g Gender) String() string {
return string(g)
}
func (g Gender) Valid() bool {
return g == GenderMale || g == GenderFemmale
}
var (
email = fend.NewRules(
rule.StringMin(6),
rule.StringMax(30),
rule.Email,
)
title = fend.NewRules(
rule.StringMin(2),
rule.NotEqual("dr."),
)
gender = fend.NewRules(
rule.Valid[Gender],
)
dynamic = fend.DynamicVar(
func(ctx context.Context) error {
return fend.NewRuleError("dynamic", "foo")
},
)
firstName = fend.NewRules(
rule.StringMin(6),
rule.StringMax(30),
rule.Alpha,
)
streetNumber = fend.NewRules(
rule.NumberMin(1),
rule.NumberMax(10),
)
)
func TestAll(t *testing.T) {
tests := []struct {
name string
fends fend.Fends
wantAllErr string
wantFirstErr string
wantAllFirstErr string
}{
{
name: "success values",
fends: fend.Fends{
title.Var("dr"),
gender.Var(GenderMale),
firstName.Var("foobar"),
streetNumber.Var(5),
email.Var("[email protected]"),
},
},
{
name: "failure values",
fends: fend.Fends{
title.Var("dr."),
gender.Var("divers"),
firstName.Var("foo"),
streetNumber.Var(15),
email.Var("foo"),
dynamic,
},
wantAllErr: "equal=dr.;valid;min=6;max=10;min=6:email=parse;dynamic:foo",
wantFirstErr: "equal=dr.",
wantAllFirstErr: "equal=dr.",
},
{
name: "success fields",
fends: fend.Fends{
title.Field("title", "dr"),
gender.Field("gender", GenderMale),
firstName.Field("firstName", "foobar"),
streetNumber.Field("firstName", 5),
email.Field("email", "[email protected]"),
},
},
{
name: "failure fields",
fends: fend.Fends{
title.Field("title", "dr."),
gender.Field("gender", "divers"),
firstName.Field("firstName", "foo"),
streetNumber.Field("firstName", 15),
email.Field("email", "foo"),
dynamic,
},
wantAllErr: "title:equal=dr.;gender:valid;firstName:min=6;firstName:max=10;email:min=6:email=parse;dynamic:foo",
wantFirstErr: "title:equal=dr.",
wantAllFirstErr: "title:equal=dr.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := fender.All(context.TODO(), tt.fends...); tt.wantAllErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantAllErr)
}
if err := fender.All(context.TODO(), tt.fends...); tt.wantAllErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantAllErr)
}
if err := fender.First(context.TODO(), tt.fends...); tt.wantFirstErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantFirstErr)
}
if err := fender.AllFirst(context.TODO(), tt.fends...); tt.wantAllFirstErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantAllFirstErr)
}
})
}
}