-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
60 lines (55 loc) · 1.54 KB
/
errors_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
package validator
import (
"reflect"
"testing"
)
func TestFieldError_Error(t *testing.T) {
testcase := []struct {
name string
err Error
wantMessage string
}{
{
name: "error",
err: &fieldError{
field: Field{name: "field", current: reflect.ValueOf("text")},
tag: Tag{name: "tag"},
},
wantMessage: "field: 'text' does validate as 'tag'",
},
{
name: "error suppress field value",
err: &fieldError{
field: Field{name: "field", current: reflect.ValueOf("text")},
tag: Tag{name: "tag"},
suppressErrorFieldValue: true,
},
wantMessage: "field: The value does validate as 'tag'",
},
}
for _, tc := range testcase {
t.Run(tc.name, func(t *testing.T) {
if tc.err.Error() != tc.wantMessage {
t.Fatalf("error want `%v`, got `%v`", tc.wantMessage, tc.err.Error())
}
})
}
}
func TestFieldError_Field(t *testing.T) {
err := &fieldError{field: Field{name: "field", current: reflect.ValueOf("text")}}
if want, got := "field", err.Field().Name(); want != got {
t.Errorf("want %v, but got %v", want, got)
}
if want, got := "text", err.Field().String(); want != got {
t.Errorf("want %v, but got %v", want, got)
}
}
func TestFieldError_Tag(t *testing.T) {
err := &fieldError{tag: Tag{name: "tmp", params: []string{"1", "2", "3"}}}
if want, got := "tmp", err.Tag().name; want != got {
t.Errorf("want %v, but got %v", want, got)
}
if want, got := "tmp(1|2|3)", err.Tag().Fullname(); want != got {
t.Errorf("want %v, but got %v", want, got)
}
}