-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidator_test.go
More file actions
77 lines (71 loc) · 1.75 KB
/
validator_test.go
File metadata and controls
77 lines (71 loc) · 1.75 KB
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
package validator
import (
"errors"
"fmt"
"testing"
"github.com/matryer/is"
)
func Test_NewSingleError(t *testing.T) {
t.Parallel()
is := is.New(t)
tests := map[string]struct {
fieldName string
errors []string
}{
"empty string should return error with empty string": {
fieldName: "test",
errors: []string{},
}, "single string should return error with single string": {
fieldName: "test",
errors: []string{"i failed"},
}, "multiple string should return error with multiple string": {
fieldName: "test",
errors: []string{"i failed", "i failed because of another thing"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := NewSingleError(test.fieldName, test.errors)
var val ErrValidation
ok := errors.As(err, &val)
is = is.NewRelaxed(t)
is.True(ok)
is.Equal(val[test.fieldName], test.errors)
})
}
}
func Test_NewFromError(t *testing.T) {
t.Parallel()
is := is.New(t)
tests := map[string]struct {
fieldName string
error error
exp ErrValidation
}{
"nil error string should return error with empty string": {
fieldName: "test",
error: nil,
exp: nil,
}, "error should be returned in validator": {
fieldName: "test",
error: errors.New("I failed"),
exp: map[string][]string{
"test": {"I failed"},
},
}, "wrapped error should be returned in validator": {
fieldName: "test",
error: fmt.Errorf("i wrap %w", errors.New("I failed")),
exp: map[string][]string{
"test": {"i wrap I failed"},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := NewFromError(test.fieldName, test.error)
//val, ok := err.(ErrValidation)
is = is.NewRelaxed(t)
is.Equal(test.exp, err)
})
}
}