-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
validate_test.go
55 lines (42 loc) · 1.17 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
package validate
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/require"
)
type v1 struct{}
func (v *v1) IsValid(errors *Errors) {
errors.Add("v1", "there's an error with v1")
}
type v2 struct{}
func (v *v2) IsValid(errors *Errors) {
errors.Add("v2", "there's an error with v2")
}
func TestValidate(t *testing.T) {
r := require.New(t)
errors := Validate(&v1{}, &v2{})
r.Equal(errors.Count(), 2)
r.Equal(errors.HasAny(), true)
r.Equal(errors.Errors["v1"], []string{"there's an error with v1"})
r.Equal(errors.Errors["v2"], []string{"there's an error with v2"})
r.Equal(errors.String(), `{"errors":{"v1":["there's an error with v1"],"v2":["there's an error with v2"]}}`)
}
func TestErrorsKeys(t *testing.T) {
r := require.New(t)
errors := Validate(&v1{}, &v2{})
r.Contains(errors.Keys(), "v1")
r.Contains(errors.Keys(), "v2")
}
func Test_ErrorsXML(t *testing.T) {
r := require.New(t)
errors := Errors{
Errors: map[string][]string{
"name": []string{"name1", "name2"},
"email": []string{"emailA", "emailB"},
},
}
x, err := xml.Marshal(errors)
r.NoError(err)
r.Contains(string(x), "<errors>")
r.Contains(string(x), "<email><message>emailA")
}