-
Notifications
You must be signed in to change notification settings - Fork 4
/
field_test.go
321 lines (277 loc) · 5.88 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package golog_test
import (
"fmt"
"reflect"
"testing"
"time"
. "github.com/damianopetrungaro/golog"
)
func Test_Bool(t *testing.T) {
k := "key name"
v := true
f := Bool(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Bools(t *testing.T) {
k := "key name"
v := []bool{true, false, false, true}
f := Bools(k, v)
testFieldHelper(t, k, v, f)
}
func Test_String(t *testing.T) {
k := "key name"
v := "value name"
f := String(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Strings(t *testing.T) {
k := "key name"
v := []string{"name one", "name two", "name three"}
f := Strings(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Byte(t *testing.T) {
k := "key name"
v := byte('a')
f := Byte(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Bytes(t *testing.T) {
k := "key name"
v := []byte{'a', 'b', 'c'}
f := Bytes(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint(t *testing.T) {
k := "key name"
v := uint(101)
f := Uint(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uints(t *testing.T) {
k := "key name"
v := []uint{10, 0, 202}
f := Uints(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint8(t *testing.T) {
k := "key name"
v := uint8(101)
f := Uint8(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint8s(t *testing.T) {
k := "key name"
v := []uint8{10, 0, 202}
f := Uint8s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint16(t *testing.T) {
k := "key name"
v := uint16(101)
f := Uint16(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint16s(t *testing.T) {
k := "key name"
v := []uint16{10, 0, 202}
f := Uint16s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint32(t *testing.T) {
k := "key name"
v := uint32(101)
f := Uint32(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint32s(t *testing.T) {
k := "key name"
v := []uint32{10, 0, 202}
f := Uint32s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint64(t *testing.T) {
k := "key name"
v := uint64(101)
f := Uint64(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Uint64s(t *testing.T) {
k := "key name"
v := []uint64{10, 0, 202}
f := Uint64s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int(t *testing.T) {
k := "key name"
v := 12
f := Int(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Ints(t *testing.T) {
k := "key name"
v := []int{1, 2, 3, 4, 5}
f := Ints(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int8(t *testing.T) {
k := "key name"
v := int8(12)
f := Int8(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int8s(t *testing.T) {
k := "key name"
v := []int8{1, 2, 3, 4, 5}
f := Int8s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int16(t *testing.T) {
k := "key name"
v := int16(12)
f := Int16(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int16s(t *testing.T) {
k := "key name"
v := []int16{1, 2, 3, 4, 5}
f := Int16s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int32(t *testing.T) {
k := "key name"
v := int32(12)
f := Int32(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int32s(t *testing.T) {
k := "key name"
v := []int32{1, 2, 3, 4, 5}
f := Int32s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int64(t *testing.T) {
k := "key name"
v := int64(12)
f := Int64(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Int64s(t *testing.T) {
k := "key name"
v := []int64{1, 2, 3, 4, 5}
f := Int64s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Float64(t *testing.T) {
k := "key name"
v := 3.39
f := Float64(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Float64s(t *testing.T) {
k := "key name"
v := []float64{1.12, 21.12, 3.419}
f := Float64s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Float32(t *testing.T) {
k := "key name"
v := float32(9.01)
f := Float32(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Float32s(t *testing.T) {
k := "key name"
v := []float32{1.12, 21.12, 3.419}
f := Float32s(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Time(t *testing.T) {
k := "key name"
v := time.Now()
f := Time(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Times(t *testing.T) {
k := "key name"
v := []time.Time{time.Now(), time.Now(), time.Now()}
f := Times(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Err(t *testing.T) {
t.Run("test nil error", func(t *testing.T) {
k := "error"
errMsg := "<nil>"
f := Err(nil)
testFieldHelper(t, k, errMsg, f)
})
t.Run("test actual error", func(t *testing.T) {
k := "error"
errMsg := "an error"
v := fmt.Errorf(errMsg)
f := Err(v)
testFieldHelper(t, k, errMsg, f)
})
}
func Test_Errs(t *testing.T) {
k := "errors"
errMsgs := []string{"an error", "<nil>", "another error"}
v := []error{fmt.Errorf("an error"), nil, fmt.Errorf("another error")}
f := Errs(v)
testFieldHelper(t, k, errMsgs, f)
}
func Test_Mapper(t *testing.T) {
k := "key name"
v := user{ID: "ID", Reference: 123, Birthdate: time.Now()}
f := Mapper(k, v)
testFieldHelper(t, k, v, f)
}
func Test_Mappers(t *testing.T) {
k := "key name"
v1 := user{ID: "ID", Reference: 123, Birthdate: time.Now()}
v2 := user{ID: "DI", Reference: 321, Birthdate: time.Now()}
vs := []FieldMapper{v1, v2}
f := Mappers(k, vs)
testFieldHelper(t, k, vs, f)
}
// tests Key and Value methods as well implicitly
func testFieldHelper(t *testing.T, k string, v any, f Field) {
t.Helper()
if f.Key() != k {
t.Error("could not match key")
t.Errorf("got: %v", f.Key())
t.Errorf("want: %v", k)
}
if !reflect.DeepEqual(f.Value(), v) {
t.Error("could not match value")
t.Errorf("got: %v", f.Value())
t.Errorf("want: %v", v)
}
}
func FieldMatcher(t *testing.T, xs, ys Fields) {
t.Helper()
if len(xs) != len(ys) {
t.Error("could not match fields length")
t.Errorf("xs: %v", len(xs))
t.Errorf("ys: %v", len(ys))
}
for i, x := range xs {
if !reflect.DeepEqual(x, ys[i]) {
t.Errorf("could not match value at index %d", i)
t.Errorf("x: %v", x)
t.Errorf("y: %v", ys[i])
}
}
}
type user struct {
ID string
Reference int
Birthdate time.Time
}
func (u user) ToFields() Fields {
return Fields{
String("id", u.ID),
Int("ref", u.Reference),
Time("birthdate", u.Birthdate),
}
}