-
Notifications
You must be signed in to change notification settings - Fork 208
/
validation_test.go
92 lines (74 loc) · 1.95 KB
/
validation_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
package authboss
import (
"testing"
"github.com/friendsofgo/errors"
)
type mockFieldError struct {
name string
err error
}
func (m mockFieldError) Name() string {
return m.name
}
func (m mockFieldError) Err() error {
return m.err
}
func (m mockFieldError) Error() string {
return m.err.Error()
}
func TestErrorList_Error(t *testing.T) {
t.Parallel()
errList := ErrorList{errors.New("one"), errors.New("two")}
if e := errList.Error(); e != "one, two" {
t.Error("Wrong value for error:", e)
}
}
func TestErrorList_Map(t *testing.T) {
t.Parallel()
errNotLong := "not long enough"
errEmail := "should be an email"
errAsploded := "asploded"
errList := ErrorList{
mockFieldError{"username", errors.New(errNotLong)},
mockFieldError{"username", errors.New(errEmail)},
mockFieldError{"password", errors.New(errNotLong)},
errors.New(errAsploded),
}
m := errList.Map()
if len(m) != 3 {
t.Error("Wrong number of fields:", len(m))
}
usernameErrs := m["username"]
if len(usernameErrs) != 2 {
t.Error("Wrong number of username errors:", len(usernameErrs))
}
if usernameErrs[0] != errNotLong {
t.Error("Wrong username error at 0:", usernameErrs[0])
}
if usernameErrs[1] != errEmail {
t.Error("Wrong username error at 1:", usernameErrs[1])
}
passwordErrs := m["password"]
if len(passwordErrs) != 1 {
t.Error("Wrong number of password errors:", len(passwordErrs))
}
if passwordErrs[0] != errNotLong {
t.Error("Wrong password error at 0:", passwordErrs[0])
}
unknownErrs := m[""]
if len(unknownErrs) != 1 {
t.Error("Wrong number of unknown errors:", len(unknownErrs))
}
if unknownErrs[0] != errAsploded {
t.Error("Wrong unknown error at 0:", unknownErrs[0])
}
}
func TestErrorList_MapHelper(t *testing.T) {
t.Parallel()
errList := []error{
mockFieldError{"username", errors.New("")},
mockFieldError{"username", errors.New("")},
mockFieldError{"password", errors.New("")},
}
var _ map[string][]string = ErrorMap(errList)
}