-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_test.go
93 lines (83 loc) · 2.12 KB
/
field_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
93
package validator
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
)
func TestField_Value(t *testing.T) {
value := reflect.ValueOf("value")
field := Field{current: value}
if value != field.Value() {
t.Fatalf("invalid field value")
}
}
func TestField_Parent(t *testing.T) {
value := reflect.ValueOf("value")
parent := ParentField{value}
field := Field{parent: parent}
if parent != field.Parent() {
t.Fatal("invalid parent of field")
}
if parent.Interface() != value.Interface() {
t.Fatal("invalid parent field")
}
field = Field{parent: ParentField{reflect.ValueOf(nil)}}
if field.Parent().Interface() != nil {
t.Fatalf("want parent field nil, but got %v", field.Parent().Interface())
}
}
func TestField_ShortString(t *testing.T) {
testcases := []struct {
field Field
wantString string
}{
{
field: Field{current: reflect.ValueOf(strings.Repeat("a", 31))},
wantString: fmt.Sprintf("%s", strings.Repeat("a", 31)),
},
{
field: Field{current: reflect.ValueOf(strings.Repeat("a", 32))},
wantString: fmt.Sprintf("%s", strings.Repeat("a", 32)),
},
{
field: Field{current: reflect.ValueOf(strings.Repeat("a", 33))},
wantString: fmt.Sprintf("%s...", strings.Repeat("a", 32)),
},
{
field: Field{current: reflect.ValueOf(strings.Repeat("a", 64))},
wantString: fmt.Sprintf("%s...", strings.Repeat("a", 32)),
},
}
for _, tc := range testcases {
if tc.field.ShortString() != tc.wantString {
t.Errorf("want `%s`, but got `%s`", tc.wantString, tc.field.ShortString())
}
}
}
func TestField_String(t *testing.T) {
var unknown interface{}
testcases := []struct {
field Field
wantString string
}{
{
field: Field{current: reflect.ValueOf([]interface{}{"a"}).Index(0)},
wantString: "<Interface>",
},
{
field: Field{current: reflect.ValueOf(&bytes.Buffer{})},
wantString: "<Ptr>",
},
{
field: Field{current: reflect.ValueOf(unknown)},
wantString: "<Unknown>",
},
}
for _, tc := range testcases {
if tc.field.String() != tc.wantString {
t.Errorf("want %s, but got %s", tc.wantString, tc.field.String())
}
}
}