-
Notifications
You must be signed in to change notification settings - Fork 11
/
field_test.go
166 lines (154 loc) · 4.63 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package logr
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
/*
UnknownType FieldType = iota
StringType
StringerType
StructType
ErrorType
BoolType
TimestampMillisType
TimeType
DurationType
Int64Type
Int32Type
IntType
Uint64Type
Uint32Type
UintType
Float64Type
Float32Type
BinaryType
ArrayType
MapType
*/
func TestField_ValueString(t *testing.T) {
tests := []struct {
name string
field Field
wantW string
wantErr bool
}{
{name: "StringType", field: String("str", "test"), wantW: "test", wantErr: false},
{name: "StringerType", field: Stringer("strgr", newTestStringer("Hello")), wantW: "Hello", wantErr: false},
{name: "StringerType with nil", field: Stringer("nilstrgr", nil), wantW: "", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := tt.field.ValueString(w, nil); (err != nil) != tt.wantErr {
t.Errorf("Field.ValueString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("Field.ValueString() = %v, want %v", gotW, tt.wantW)
}
})
}
}
func TestFieldForAny(t *testing.T) {
testString := "hello"
var nilPointer *string
tests := []struct {
name string
field Field
wantW string
wantErr bool
}{
{name: "StringType", field: Any("str", "test"), wantW: "test", wantErr: false},
{name: "StringerType", field: Any("strgr", newTestStringer("Hello")), wantW: "Hello", wantErr: false},
{name: "String pointer", field: Any("strptr", &testString), wantW: testString, wantErr: false},
{name: "String pointer with nil", field: Any("nilptr", nilPointer), wantW: "", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := tt.field.ValueString(w, nil); (err != nil) != tt.wantErr {
t.Errorf("Field.ValueString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("Field.ValueString() = %v, want %v", gotW, tt.wantW)
}
})
}
}
func TestField_Array(t *testing.T) {
tests := []struct {
name string
field Field
wantW string
wantErr bool
}{
{name: "nil", field: Array[[]any]("array", nil), wantW: "", wantErr: false},
{name: "empty", field: Array("array", []string{}), wantW: "", wantErr: false},
{name: "one elements", field: Array("array", []string{"foo"}), wantW: "foo", wantErr: false},
{name: "two elements", field: Array("array", []string{"foo", "bar"}), wantW: "foo,bar", wantErr: false},
{name: "three elements", field: Array("array", []string{"foo", "bar", "xyz"}), wantW: "foo,bar,xyz", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := tt.field.ValueString(w, nil); (err != nil) != tt.wantErr {
t.Errorf("Field.ValueString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("Field.ValueString() = %v, want %v", gotW, tt.wantW)
}
})
}
}
func TestField_Map(t *testing.T) {
tests := []struct {
name string
field Field
wantW string
wantCommas int
wantErr bool
}{
{name: "nil", field: Map[map[string]any]("map", nil), wantW: "", wantErr: false},
{name: "empty", field: Map("map", map[string]any{}), wantW: "", wantErr: false},
{name: "one elements", field: Map("map", map[string]int{"foo": 0}), wantW: "foo=0", wantErr: false},
{name: "two elements", field: Map("map", map[string]int{"foo": 0, "bar": 1}), wantW: "foo=0,bar=1", wantCommas: 1, wantErr: false},
{name: "three elements", field: Map("map", map[string]int{"foo": 0, "bar": 1, "xyz": 2}), wantW: "foo=0,bar=1,xyz=2", wantCommas: 2, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := tt.field.ValueString(w, nil); (err != nil) != tt.wantErr {
t.Errorf("Field.ValueString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantCommas == 0 {
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("Field.ValueString() = %v, want %v", gotW, tt.wantW)
}
} else {
gotW := w.String()
if commas := strings.Count(gotW, ","); commas != tt.wantCommas {
t.Errorf("Got %d commas in %s, want %d", commas, gotW, tt.wantCommas)
}
gotElems := strings.Split(gotW, ",")
wantElems := strings.Split(tt.wantW, ",")
assert.ElementsMatch(t, gotElems, wantElems)
}
})
}
}
type testStringer struct {
s string
}
func newTestStringer(s string) *testStringer {
return &testStringer{
s: s,
}
}
func (ts *testStringer) String() string {
return ts.s
}