-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_test.go
95 lines (86 loc) · 1.31 KB
/
validate_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
package validate
import (
"fmt"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
type animalType string
const (
Cat animalType = "CAT"
Dog animalType = "DOG"
Other animalType = "OTHER"
)
var nameRegex = regexp.MustCompile("^[a-zA-Z]+$")
type animal struct {
Name string
Age int
Type animalType
}
func (v animal) Validate() error {
return Validate(
New(v.Name, StringRange(3, 10), StringMatchRegex(nameRegex)),
New(v.Age, NumberRange(0, 100)),
New(v.Type, Required[animalType], Enum(Cat, Dog, Other)),
)
}
func TestValidate(t *testing.T) {
v := []struct {
v animal
e error
}{
{
v: animal{
Name: "test",
Age: 10,
Type: Cat,
},
e: nil,
},
{
v: animal{
Name: "t",
Age: 10,
Type: Dog,
},
e: ErrStringRange(3, 10),
},
{
v: animal{
Name: "test",
Age: -1,
Type: Other,
},
e: ErrNumberRange(0, 100),
},
{
v: animal{
Name: "test",
Age: 10,
Type: "fish",
},
e: ErrEnum(Cat, Dog, Other),
},
{
v: animal{
Name: "####",
Age: 10,
Type: "fish",
},
e: ErrStringMatchRegex(nameRegex),
},
{
v: animal{
Name: "test",
Age: 10,
Type: "",
},
e: ErrRequired,
},
}
for _, tt := range v {
err := tt.v.Validate()
assert.Equal(t, tt.e, err)
fmt.Println(err)
}
}