-
Notifications
You must be signed in to change notification settings - Fork 1
/
struct_test.go
58 lines (49 loc) · 1.1 KB
/
struct_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
package hvalid
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type Student struct {
Name string
Email string
Age int
Score int
}
func StudentValidator() ValidatorFunc[Student] {
return ValidatorFunc[Student](func(data Student) error {
if data.Age < 0 {
return errors.New("学生的年龄不能小于 0")
}
if data.Score > 100 {
return errors.New("课程成绩不能大于 100")
}
return Validate[string](data.Email, IsEmail("Not email address"))
})
}
func TestStruct(t *testing.T) {
var s1 = Student{
Name: "张三",
Email: "[email protected]",
Age: 18,
Score: 110,
}
err1 := Validate[Student](s1, StudentValidator())
assert.ErrorContains(t, err1, "课程成绩不能大于 100")
var s2 = Student{
Name: "李四",
Email: "lisi.gmail.com",
Age: 19,
Score: 59,
}
err2 := Validate[Student](s2, StudentValidator())
assert.ErrorContains(t, err2, "Not email address")
var s3 = Student{
Name: "lyonnee",
Email: "[email protected]",
Age: 20,
Score: 99,
}
err3 := Validate[Student](s3, StudentValidator())
assert.NoError(t, err3)
}