-
Notifications
You must be signed in to change notification settings - Fork 2
/
notempty_test.go
84 lines (81 loc) · 1.85 KB
/
notempty_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
package validator
import "testing"
func TestNotEmpty(t *testing.T) {
type s1 struct {
A string `valid:"notempty"`
Z string `valid:"notempty"`
B int `valid:"notempty"`
Y int `valid:"notempty"`
C bool `valid:"notempty"`
X bool `valid:"notempty"`
D *testing.T `valid:"notempty"`
W *testing.T `valid:"notempty"`
E []byte `valid:"notempty"`
V []byte `valid:"notempty"`
F interface{} `valid:"notempty"`
U interface{} `valid:"notempty"`
}
s := s1{
A: "",
Z: "a",
B: 0,
Y: 1,
C: false,
X: true,
D: nil,
W: t,
E: []byte{},
V: []byte("a"),
F: nil,
U: t,
}
v := New(WithFunc("notempty", notEmpty))
err := v.Validate(&s)
if err == nil {
t.Fatal("error expected")
}
errs := err.(Errors)
if len(errs) != 6 {
t.Fatalf("unexpected errors length: %+v; want: %d", err, 6)
}
if errs[0].Error() != "A must not be empty" {
t.Fatalf("unexpected error: %+v", errs[0])
}
if errs[1].Error() != "B must not be zero" {
t.Fatalf("unexpected error: %+v", errs[1])
}
if errs[2].Error() != "C must not be false" {
t.Fatalf("unexpected error: %+v", errs[2])
}
if errs[3].Error() != "D must not be nil" {
t.Fatalf("unexpected error: %+v", errs[3])
}
if errs[4].Error() != "E must not be empty" {
t.Fatalf("unexpected error: %+v", errs[4])
}
if errs[5].Error() != "F must not be nil" {
t.Fatalf("unexpected error: %+v", errs[5])
}
}
func TestNotEmptyUnsupported(t *testing.T) {
type s1 struct {
A int
}
s := struct {
A s1 `valid:"notempty"`
}{
s1{0},
}
v := New(WithFunc("notempty", notEmpty))
err := v.Validate(&s)
if err == nil {
t.Fatal("error expected")
}
errs := err.(Errors)
if len(errs) != 1 {
t.Fatalf("unexpected errors length: %+v; want: %d", errs, 1)
}
if errs[0].Error() != "validator: unsupported: A" {
t.Fatalf("unexpected error: %+v", errs[0])
}
}