-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfield_test.go
526 lines (446 loc) · 13.8 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// SPDX-FileCopyrightText: 2023-present Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package dazl
import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
type testWriter struct {
Writer
T *testing.T
}
type testStringWriter struct {
testWriter
Value string
}
func (w *testStringWriter) WithStringField(name string, value string) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, w.Value, value)
return w
}
func TestStringField(t *testing.T) {
writer := testWriter{T: t}
_, err := String("foo", "bar")(&testStringWriter{testWriter: writer, Value: "bar"})
assert.NoError(t, err)
_, err = String("foo", "bar")(writer)
assert.Error(t, err)
}
type testBoolWriter struct {
testWriter
}
func (w *testBoolWriter) WithBoolField(name string, value bool) Writer {
assert.Equal(w.T, "foo", name)
assert.True(w.T, value)
return w
}
func TestBoolField(t *testing.T) {
writer := testWriter{T: t}
_, err := Bool("foo", true)(&testBoolWriter{writer})
assert.NoError(t, err)
_, err = Bool("foo", true)(&testStringWriter{testWriter: writer, Value: "true"})
assert.NoError(t, err)
_, err = Bool("foo", true)(writer)
assert.Error(t, err)
}
type testTimeWriter struct {
testWriter
}
func (w *testTimeWriter) WithTimeField(name string, value time.Time) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, time.Unix(0, 0), value)
return w
}
func TestTimeField(t *testing.T) {
writer := testWriter{T: t}
_, err := Time("foo", time.Unix(0, 0))(&testTimeWriter{writer})
assert.NoError(t, err)
_, err = Time("foo", time.Unix(0, 0))(&testStringWriter{testWriter: writer, Value: time.Unix(0, 0).String()})
assert.NoError(t, err)
_, err = Time("foo", time.Unix(0, 0))(writer)
assert.Error(t, err)
}
type testDurationWriter struct {
testWriter
}
func (w *testDurationWriter) WithDurationField(name string, value time.Duration) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, time.Second, value)
return w
}
func TestDurationField(t *testing.T) {
writer := testWriter{T: t}
_, err := Duration("foo", time.Second)(&testDurationWriter{writer})
assert.NoError(t, err)
_, err = Duration("foo", time.Second)(&testStringWriter{testWriter: writer, Value: "1s"})
assert.NoError(t, err)
_, err = Duration("foo", time.Second)(writer)
assert.Error(t, err)
}
type testIntWriter struct {
testWriter
}
func (w *testIntWriter) WithIntField(name string, value int) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, 1, value)
return w
}
func TestIntField(t *testing.T) {
writer := testWriter{T: t}
_, err := Int("foo", 1)(&testIntWriter{writer})
assert.NoError(t, err)
_, err = Int("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Int("foo", 1)(writer)
assert.Error(t, err)
}
type testInt32Writer struct {
testWriter
}
func (w *testInt32Writer) WithInt32Field(name string, value int32) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, int32(1), value)
return w
}
func TestInt32Field(t *testing.T) {
writer := testWriter{T: t}
_, err := Int32("foo", 1)(&testInt32Writer{writer})
assert.NoError(t, err)
_, err = Int32("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Int32("foo", 1)(writer)
assert.Error(t, err)
}
type testInt64Writer struct {
testWriter
}
func (w *testInt64Writer) WithInt64Field(name string, value int64) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, int64(1), value)
return w
}
func TestInt64Field(t *testing.T) {
writer := testWriter{T: t}
_, err := Int64("foo", 1)(&testInt64Writer{writer})
assert.NoError(t, err)
_, err = Int64("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Int64("foo", 1)(writer)
assert.Error(t, err)
}
type testUintWriter struct {
testWriter
}
func (w *testUintWriter) WithUintField(name string, value uint) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, uint(1), value)
return w
}
func TestUintField(t *testing.T) {
writer := testWriter{T: t}
_, err := Uint("foo", 1)(&testUintWriter{writer})
assert.NoError(t, err)
_, err = Uint("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Uint("foo", 1)(writer)
assert.Error(t, err)
}
type testUint32Writer struct {
testWriter
}
func (w *testUint32Writer) WithUint32Field(name string, value uint32) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, uint32(1), value)
return w
}
func TestUint32Field(t *testing.T) {
writer := testWriter{T: t}
_, err := Uint32("foo", 1)(&testUint32Writer{writer})
assert.NoError(t, err)
_, err = Uint32("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Uint32("foo", 1)(writer)
assert.Error(t, err)
}
type testUint64Writer struct {
testWriter
}
func (w *testUint64Writer) WithUint64Field(name string, value uint64) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, uint64(1), value)
return w
}
func TestUint64Field(t *testing.T) {
writer := testWriter{T: t}
_, err := Uint64("foo", 1)(&testUint64Writer{writer})
assert.NoError(t, err)
_, err = Uint64("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Uint64("foo", 1)(writer)
assert.Error(t, err)
}
type testFloat32Writer struct {
testWriter
}
func (w *testFloat32Writer) WithFloat32Field(name string, value float32) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, float32(1), value)
return w
}
func TestFloat32Field(t *testing.T) {
writer := testWriter{T: t}
_, err := Float32("foo", 1)(&testFloat32Writer{writer})
assert.NoError(t, err)
_, err = Float32("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Float32("foo", 1)(writer)
assert.Error(t, err)
}
type testFloat64Writer struct {
testWriter
}
func (w *testFloat64Writer) WithFloat64Field(name string, value float64) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, float64(1), value)
return w
}
func TestFloat64Field(t *testing.T) {
writer := testWriter{T: t}
_, err := Float64("foo", 1)(&testFloat64Writer{writer})
assert.NoError(t, err)
_, err = Float64("foo", 1)(&testStringWriter{testWriter: writer, Value: "1"})
assert.NoError(t, err)
_, err = Float64("foo", 1)(writer)
assert.Error(t, err)
}
type testTimeSliceWriter struct {
testWriter
}
func (w *testTimeSliceWriter) WithTimeSliceField(name string, value []time.Time) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []time.Time{time.Unix(0, 0)}, value)
return w
}
func TestTimeSliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Times("foo", []time.Time{time.Unix(0, 0)})(&testTimeSliceWriter{writer})
assert.NoError(t, err)
_, err = Times("foo", []time.Time{time.Unix(0, 0)})(&testStringWriter{testWriter: writer, Value: "[" + time.Unix(0, 0).String() + "]"})
assert.NoError(t, err)
_, err = Times("foo", []time.Time{time.Unix(0, 0)})(writer)
assert.Error(t, err)
}
type testDurationSliceWriter struct {
testWriter
}
func (w *testDurationSliceWriter) WithDurationSliceField(name string, value []time.Duration) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []time.Duration{time.Second}, value)
return w
}
func TestDurationSliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Durations("foo", []time.Duration{time.Second})(&testDurationSliceWriter{writer})
assert.NoError(t, err)
_, err = Durations("foo", []time.Duration{time.Second})(&testStringWriter{testWriter: writer, Value: "[1s]"})
assert.NoError(t, err)
_, err = Durations("foo", []time.Duration{time.Second})(writer)
assert.Error(t, err)
}
type testIntSliceWriter struct {
testWriter
}
func (w *testIntSliceWriter) WithIntSliceField(name string, value []int) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []int{1}, value)
return w
}
func TestIntSliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Ints("foo", []int{1})(&testIntSliceWriter{writer})
assert.NoError(t, err)
_, err = Ints("foo", []int{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Ints("foo", []int{1})(writer)
assert.Error(t, err)
}
type testBoolSliceWriter struct {
testWriter
}
func (w *testBoolSliceWriter) WithBoolSliceField(name string, value []bool) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []bool{true}, value)
return w
}
func TestBoolSliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Bools("foo", []bool{true})(&testBoolSliceWriter{writer})
assert.NoError(t, err)
_, err = Bools("foo", []bool{true})(&testStringWriter{testWriter: writer, Value: "[true]"})
assert.NoError(t, err)
_, err = Bools("foo", []bool{true})(writer)
assert.Error(t, err)
}
type testInt32SliceWriter struct {
testWriter
}
func (w *testInt32SliceWriter) WithInt32SliceField(name string, value []int32) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []int32{1}, value)
return w
}
func TestInt32SliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Int32s("foo", []int32{1})(&testInt32SliceWriter{writer})
assert.NoError(t, err)
_, err = Int32s("foo", []int32{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Int32s("foo", []int32{1})(writer)
assert.Error(t, err)
}
type testInt64SliceWriter struct {
testWriter
}
func (w *testInt64SliceWriter) WithInt64SliceField(name string, value []int64) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []int64{1}, value)
return w
}
func TestInt64SliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Int64s("foo", []int64{1})(&testInt64SliceWriter{writer})
assert.NoError(t, err)
_, err = Int64s("foo", []int64{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Int64s("foo", []int64{1})(writer)
assert.Error(t, err)
}
type testUintSliceWriter struct {
testWriter
}
func (w *testUintSliceWriter) WithUintSliceField(name string, value []uint) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []uint{1}, value)
return w
}
func TestUintSliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Uints("foo", []uint{1})(&testUintSliceWriter{writer})
assert.NoError(t, err)
_, err = Uints("foo", []uint{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Uints("foo", []uint{1})(writer)
assert.Error(t, err)
}
type testUint32SliceWriter struct {
testWriter
}
func (w *testUint32SliceWriter) WithUint32SliceField(name string, value []uint32) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []uint32{1}, value)
return w
}
func TestUint32SliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Uint32s("foo", []uint32{1})(&testUint32SliceWriter{writer})
assert.NoError(t, err)
_, err = Uint32s("foo", []uint32{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Uint32s("foo", []uint32{1})(writer)
assert.Error(t, err)
}
type testUint64SliceWriter struct {
testWriter
}
func (w *testUint64SliceWriter) WithUint64SliceField(name string, value []uint64) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []uint64{1}, value)
return w
}
func TestUint64SliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Uint64s("foo", []uint64{1})(&testUint64SliceWriter{writer})
assert.NoError(t, err)
_, err = Uint64s("foo", []uint64{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Uint64s("foo", []uint64{1})(writer)
assert.Error(t, err)
}
type testFloat32SliceWriter struct {
testWriter
}
func (w *testFloat32SliceWriter) WithFloat32SliceField(name string, value []float32) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []float32{1}, value)
return w
}
func TestFloat32SliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Float32s("foo", []float32{1})(&testFloat32SliceWriter{writer})
assert.NoError(t, err)
_, err = Float32s("foo", []float32{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Float32s("foo", []float32{1})(writer)
assert.Error(t, err)
}
type testFloat64SliceWriter struct {
testWriter
}
func (w *testFloat64SliceWriter) WithFloat64SliceField(name string, value []float64) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []float64{1}, value)
return w
}
func TestFloat64SliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Float64s("foo", []float64{1})(&testFloat64SliceWriter{writer})
assert.NoError(t, err)
_, err = Float64s("foo", []float64{1})(&testStringWriter{testWriter: writer, Value: "[1]"})
assert.NoError(t, err)
_, err = Float64s("foo", []float64{1})(writer)
assert.Error(t, err)
}
type testStringSliceWriter struct {
testWriter
}
func (w *testStringSliceWriter) WithStringSliceField(name string, value []string) Writer {
assert.Equal(w.T, "foo", name)
assert.Equal(w.T, []string{"test"}, value)
return w
}
func TestStringSliceField(t *testing.T) {
writer := testWriter{T: t}
_, err := Strings("foo", []string{"test"})(&testStringSliceWriter{writer})
assert.NoError(t, err)
_, err = Strings("foo", []string{"test"})(&testStringWriter{testWriter: writer, Value: "[test]"})
assert.NoError(t, err)
_, err = Strings("foo", []string{"test"})(writer)
assert.Error(t, err)
}
type testErrorWriter struct {
testWriter
}
func (w *testErrorWriter) WithErrorField(value error) Writer {
assert.Equal(w.T, errors.New("test"), value)
return w
}
type testErrorStringWriter struct {
testWriter
Value string
}
func (w *testErrorStringWriter) WithStringField(name string, value string) Writer {
assert.Equal(w.T, "error", name)
assert.Equal(w.T, w.Value, value)
return w
}
func TestErrorField(t *testing.T) {
writer := testWriter{T: t}
_, err := Error(errors.New("test"))(&testErrorWriter{writer})
assert.NoError(t, err)
_, err = Error(errors.New("test"))(&testErrorStringWriter{testWriter: writer, Value: "test"})
assert.NoError(t, err)
_, err = Error(errors.New("test"))(writer)
assert.Error(t, err)
}