-
Notifications
You must be signed in to change notification settings - Fork 3
/
field_test.go
244 lines (225 loc) · 14.3 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
package logger_test
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/gol4ng/logger"
)
// Create a stringer object
// StringerMock with mockery does'nt work properly
type MyStringer struct{}
func (s MyStringer) String() string {
return "my_stringer"
}
func TestField(t *testing.T) {
now := time.Now()
err := errors.New("my_value")
tests := []struct {
name string
field logger.Field
expectedValue interface{}
expectedType logger.FieldType
}{
{field: logger.Skip("Skip field", "my_value"), expectedValue: "my_value", expectedType: logger.SkipType},
{field: logger.Bool("Bool field", true), expectedValue: true, expectedType: logger.BoolType},
{field: logger.Bool("Bool field", false), expectedValue: false, expectedType: logger.BoolType},
{field: logger.Int8("Int8 field", 123), expectedValue: int8(123), expectedType: logger.Int8Type},
{field: logger.Int16("Int16 field", 123), expectedValue: int16(123), expectedType: logger.Int16Type},
{field: logger.Int32("Int32 field", 123), expectedValue: int32(123), expectedType: logger.Int32Type},
{field: logger.Int64("Int64 field", 123), expectedValue: int64(123), expectedType: logger.Int64Type},
{field: logger.Uint8("Uint8 field", 123), expectedValue: uint8(123), expectedType: logger.Uint8Type},
{field: logger.Uint16("Uint16 field", 123), expectedValue: uint16(123), expectedType: logger.Uint16Type},
{field: logger.Uint32("Uint32 field", 123), expectedValue: uint32(123), expectedType: logger.Uint32Type},
{field: logger.Uint64("Uint64 field", 123), expectedValue: uint64(123), expectedType: logger.Uint64Type},
{field: logger.Uintptr("Uintptr field", 123), expectedValue: uintptr(123), expectedType: logger.UintptrType},
{field: logger.Float32("Float32 field", 123), expectedValue: float32(123), expectedType: logger.Float32Type},
{field: logger.Float64("Float64 field", 123), expectedValue: float64(123), expectedType: logger.Float64Type},
{field: logger.Complex64("Complex64 field", 123), expectedValue: complex64(123), expectedType: logger.Complex64Type},
{field: logger.Complex128("Complex128 field", 123), expectedValue: complex128(123), expectedType: logger.Complex128Type},
{field: logger.String("String field", "my_value"), expectedValue: "my_value", expectedType: logger.StringType},
{field: logger.Binary("Binary field null", nil), expectedValue: []byte(nil), expectedType: logger.BinaryType},
{field: logger.Binary("Binary field", []byte("my_value")), expectedValue: []byte("my_value"), expectedType: logger.BinaryType},
{field: logger.ByteString("ByteString field null", nil), expectedValue: []byte(nil), expectedType: logger.ByteStringType},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedValue: []byte("my_value"), expectedType: logger.ByteStringType},
{field: logger.Error("Error nil field", nil), expectedValue: nil, expectedType: logger.ErrorType},
{field: logger.Error("Error field", err), expectedValue: err, expectedType: logger.ErrorType},
{field: logger.Time("Time field", now), expectedValue: now, expectedType: logger.TimeType},
{field: logger.Duration("Duration field", 5*time.Second), expectedValue: 5 * time.Second, expectedType: logger.DurationType},
{field: logger.Stringer("Stringer nil field", nil), expectedValue: nil, expectedType: logger.StringerType},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedValue: MyStringer{}, expectedType: logger.StringerType},
{field: logger.Reflect("Reflect nil field", nil), expectedValue: nil, expectedType: logger.ReflectType},
{field: logger.Reflect("Reflect field", struct{}{}), expectedValue: struct{}{}, expectedType: logger.ReflectType},
}
for _, tt := range tests {
t.Run(tt.field.Name, func(t *testing.T) {
assert.Equal(t, tt.expectedType, tt.field.Type)
assert.Equal(t, tt.expectedValue, tt.field.Value)
})
}
}
func TestField_Any(t *testing.T) {
tests := []struct {
name string
value interface{}
expectedType logger.FieldType
}{
{name: "my bool", value: true, expectedType: logger.BoolType},
{name: "my bool", value: false, expectedType: logger.BoolType},
{name: "my int", value: 123, expectedType: logger.Int64Type},
{name: "my int8", value: int8(123), expectedType: logger.Int8Type},
{name: "my int16", value: int16(123), expectedType: logger.Int16Type},
{name: "my int32", value: int32(123), expectedType: logger.Int32Type},
{name: "my int64", value: int64(123), expectedType: logger.Int64Type},
{name: "my uint8", value: uint8(123), expectedType: logger.Uint8Type},
{name: "my uint16", value: uint16(123), expectedType: logger.Uint16Type},
{name: "my uint32", value: uint32(123), expectedType: logger.Uint32Type},
{name: "my uint64", value: uint64(123), expectedType: logger.Uint64Type},
{name: "my uintptr", value: uintptr(123), expectedType: logger.UintptrType},
{name: "my float32", value: float32(123), expectedType: logger.Float32Type},
{name: "my float64", value: float64(123), expectedType: logger.Float64Type},
{name: "my complex64", value: complex64(123), expectedType: logger.Complex64Type},
{name: "my complex128", value: complex128(123), expectedType: logger.Complex128Type},
{name: "my strings", value: "my strings", expectedType: logger.StringType},
{name: "my binary", value: []byte{1, 2, 3}, expectedType: logger.BinaryType},
{name: "my error", value: errors.New("my error message"), expectedType: logger.ErrorType},
{name: "my time", value: time.Now(), expectedType: logger.TimeType},
{name: "my duration", value: time.Second, expectedType: logger.DurationType},
{name: "my stringer", value: MyStringer{}, expectedType: logger.StringerType},
{name: "my reflect", value: struct{}{}, expectedType: logger.ReflectType},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
field := logger.Any(tt.name, tt.value)
assert.Equal(t, tt.expectedType, field.Type)
})
}
}
func TestField_String(t *testing.T) {
now := time.Now()
err := errors.New("my_error_value")
tests := []struct {
name string
field logger.Field
expectedString string
}{
{field: logger.Skip("Skip field", "my_value"), expectedString: "<skipped>"},
{field: logger.Bool("Bool field", true), expectedString: "true"},
{field: logger.Bool("Bool field", false), expectedString: "false"},
{field: logger.Int8("Int8 field", 123), expectedString: "123"},
{field: logger.Int16("Int16 field", 123), expectedString: "123"},
{field: logger.Int32("Int32 field", 123), expectedString: "123"},
{field: logger.Int64("Int64 field", 123), expectedString: "123"},
{field: logger.Uint8("Uint8 field", 123), expectedString: "123"},
{field: logger.Uint16("Uint16 field", 123), expectedString: "123"},
{field: logger.Uint32("Uint32 field", 123), expectedString: "123"},
{field: logger.Uint64("Uint64 field", 123), expectedString: "123"},
{field: logger.Uintptr("Uintptr field", 123), expectedString: "123"},
{field: logger.Float32("Float32 field", 1.23456789), expectedString: "1.234567881"},
{field: logger.Float64("Float64 field", 123.4567891011), expectedString: "123.4567891"},
{field: logger.Complex64("Complex64 field", 6+7i), expectedString: "(6+7i)"},
{field: logger.Complex128("Complex128 field", 6+7i), expectedString: "(6+7i)"},
{field: logger.String("String field", "my_value"), expectedString: "my_value"},
{field: logger.Binary("Binary nil field", nil), expectedString: ""},
{field: logger.Binary("Binary field", []byte{1, 2, 3}), expectedString: "\x01\x02\x03"},
{field: logger.ByteString("ByteString nil field", nil), expectedString: ""},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedString: "my_value"},
{field: logger.Error("Error nil field", nil), expectedString: "<nil>"},
{field: logger.Error("Error field", err), expectedString: "my_error_value"},
{field: logger.Time("Time field", now), expectedString: now.String()},
{field: logger.Duration("Duration field", 5*time.Second), expectedString: "5s"},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedString: "my_stringer"},
{field: logger.Reflect("Reflect nil field", nil), expectedString: "<nil>"},
{field: logger.Reflect("Reflect field", struct{}{}), expectedString: "{}"},
}
for _, tt := range tests {
t.Run(tt.field.Name, func(t *testing.T) {
assert.Equal(t, tt.expectedString, tt.field.String())
})
}
}
func TestField_MarshalJSON(t *testing.T) {
now := time.Now()
nowMarshalled, _ := now.MarshalJSON()
err := errors.New("my_error_value")
tests := []struct {
name string
field logger.Field
expectedString string
}{
{field: logger.Skip("Skip field", "my_value"), expectedString: `"<skipped>"`},
{field: logger.Bool("Bool field", true), expectedString: "true"},
{field: logger.Bool("Bool field", false), expectedString: "false"},
{field: logger.Int8("Int8 field", 123), expectedString: "123"},
{field: logger.Int16("Int16 field", 123), expectedString: "123"},
{field: logger.Int32("Int32 field", 123), expectedString: "123"},
{field: logger.Int64("Int64 field", 123), expectedString: "123"},
{field: logger.Uint8("Uint8 field", 123), expectedString: "123"},
{field: logger.Uint16("Uint16 field", 123), expectedString: "123"},
{field: logger.Uint32("Uint32 field", 123), expectedString: "123"},
{field: logger.Uint64("Uint64 field", 123), expectedString: "123"},
{field: logger.Uintptr("Uintptr field", 123), expectedString: "123"},
{field: logger.Float32("Float32 field", 1.23456789), expectedString: "1.234567881"},
{field: logger.Float64("Float64 field", 123.4567891011), expectedString: "123.4567891"},
{field: logger.Complex64("Complex64 field", 6+7i), expectedString: `"(6+7i)"`},
{field: logger.Complex128("Complex128 field", 6+7i), expectedString: `"(6+7i)"`},
{field: logger.String("String field", "my_value"), expectedString: `"my_value"`},
{field: logger.Binary("Binary nil field", nil), expectedString: `""`},
{field: logger.Binary("Binary field", []byte{1, 2, 3}), expectedString: `"\x01\x02\x03"`},
{field: logger.ByteString("ByteString nil field", nil), expectedString: `""`},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedString: `"my_value"`},
{field: logger.Error("Error nil field", nil), expectedString: "null"},
{field: logger.Error("Error field", err), expectedString: `"my_error_value"`},
{field: logger.Time("Time field", now), expectedString: string(nowMarshalled)},
{field: logger.Duration("Duration field", 5*time.Second), expectedString: `"5s"`},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedString: `"my_stringer"`},
{field: logger.Reflect("Reflect nil field", nil), expectedString: "null"},
{field: logger.Reflect("Reflect field", struct{}{}), expectedString: "{}"},
}
for _, tt := range tests {
t.Run(tt.field.Name, func(t *testing.T) {
result, err := tt.field.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, tt.expectedString, string(result))
})
}
}
func TestField_GoString(t *testing.T) {
now := time.Now()
error := errors.New("my_error_value")
tests := []struct {
name string
field logger.Field
expectedGoString string
}{
{field: logger.Skip("Skip field", "my_value"), expectedGoString: "logger.Field{Name: Skip field, Value: <skipped>, Type: 1}"},
{field: logger.Bool("Bool field", true), expectedGoString: "logger.Field{Name: Bool field, Value: true, Type: 2}"},
{field: logger.Bool("Bool field", false), expectedGoString: "logger.Field{Name: Bool field, Value: false, Type: 2}"},
{field: logger.Int8("Int8 field", 123), expectedGoString: "logger.Field{Name: Int8 field, Value: 123, Type: 3}"},
{field: logger.Int16("Int16 field", 123), expectedGoString: "logger.Field{Name: Int16 field, Value: 123, Type: 4}"},
{field: logger.Int32("Int32 field", 123), expectedGoString: "logger.Field{Name: Int32 field, Value: 123, Type: 5}"},
{field: logger.Int64("Int64 field", 123), expectedGoString: "logger.Field{Name: Int64 field, Value: 123, Type: 6}"},
{field: logger.Uint8("Uint8 field", 123), expectedGoString: "logger.Field{Name: Uint8 field, Value: 123, Type: 7}"},
{field: logger.Uint16("Uint16 field", 123), expectedGoString: "logger.Field{Name: Uint16 field, Value: 123, Type: 8}"},
{field: logger.Uint32("Uint32 field", 123), expectedGoString: "logger.Field{Name: Uint32 field, Value: 123, Type: 9}"},
{field: logger.Uint64("Uint64 field", 123), expectedGoString: "logger.Field{Name: Uint64 field, Value: 123, Type: 10}"},
{field: logger.Uintptr("Uintptr field", 123), expectedGoString: "logger.Field{Name: Uintptr field, Value: 123, Type: 11}"},
{field: logger.Float32("Float32 field", 1.23456789), expectedGoString: "logger.Field{Name: Float32 field, Value: 1.234567881, Type: 12}"},
{field: logger.Float64("Float64 field", 123.4567891011), expectedGoString: "logger.Field{Name: Float64 field, Value: 123.4567891, Type: 13}"},
{field: logger.Complex64("Complex64 field", 6+7i), expectedGoString: "logger.Field{Name: Complex64 field, Value: (6+7i), Type: 14}"},
{field: logger.Complex128("Complex128 field", 6+7i), expectedGoString: "logger.Field{Name: Complex128 field, Value: (6+7i), Type: 15}"},
{field: logger.String("String field", "my_value"), expectedGoString: "logger.Field{Name: String field, Value: my_value, Type: 16}"},
{field: logger.Binary("Binary field", []byte{1, 2, 3}), expectedGoString: "logger.Field{Name: Binary field, Value: \x01\x02\x03, Type: 17}"},
{field: logger.ByteString("ByteString field", []byte("my_value")), expectedGoString: "logger.Field{Name: ByteString field, Value: my_value, Type: 18}"},
{field: logger.Error("Error field", error), expectedGoString: "logger.Field{Name: Error field, Value: my_error_value, Type: 19}"},
{field: logger.Time("Time field", now), expectedGoString: "logger.Field{Name: Time field, Value: " + now.String() + ", Type: 20}"},
{field: logger.Duration("Duration field", 5*time.Second), expectedGoString: "logger.Field{Name: Duration field, Value: 5s, Type: 21}"},
{field: logger.Stringer("Stringer field", MyStringer{}), expectedGoString: "logger.Field{Name: Stringer field, Value: my_stringer, Type: 22}"},
{field: logger.Reflect("Reflect field", struct{}{}), expectedGoString: "logger.Field{Name: Reflect field, Value: {}, Type: 23}"},
}
for _, tt := range tests {
t.Run(tt.field.Name, func(t *testing.T) {
assert.Equal(t, tt.expectedGoString, tt.field.GoString())
})
}
}