forked from logur/logur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_testing_test.go
324 lines (269 loc) · 8.06 KB
/
logger_testing_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
package logur_test
import (
"context"
"fmt"
"strings"
"testing"
. "logur.dev/logur"
"logur.dev/logur/conformance"
"logur.dev/logur/logtesting"
)
func TestLogEvents_Equals(t *testing.T) {
tests := map[string]struct {
expected LogEvent
actual LogEvent
}{
"level": {
expected: LogEvent{
Level: Trace,
},
actual: LogEvent{
Level: Debug,
},
},
"line": {
expected: LogEvent{
Line: "message",
},
actual: LogEvent{
Line: "other message",
},
},
"fields length": {
expected: LogEvent{
Fields: Fields{},
},
actual: LogEvent{
Fields: Fields{"key1": "value1"},
},
},
"fields value": {
expected: LogEvent{
Fields: Fields{"key1": "value1"},
},
actual: LogEvent{
Fields: Fields{"key1": "value2"},
},
},
}
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
if test.actual.Equals(test.expected) {
t.Errorf(
"log events should not be equal\nexpected: %+v\nactual: %+v",
test.expected,
test.actual,
)
}
})
}
}
func TestLogEvents_AssertEquals(t *testing.T) {
actual := LogEvent{
Line: "something happened",
Level: Trace,
Fields: Fields{"key": "value"},
}
expected := LogEvent{
Line: "something else happened",
Level: Info,
Fields: Fields{"key2": "value2"},
}
const (
expectedMessage = "failed to assert that log events are equal"
expectedVerboseMessage = `failed to assert that log events are equal
expected:
line: something else happened
level: info
fields: map[key2:value2]
actual:
line: something happened
level: trace
fields: map[key:value]
`
)
err := actual.AssertEquals(expected)
if err == nil {
t.Fatal("assertion is expected to fail")
}
if want, have := expectedMessage, fmt.Sprintf("%s", err); want != have {
t.Errorf("unexpexted error message: %v", have)
}
if want, have := expectedVerboseMessage, fmt.Sprintf("%+v", err); want != have {
t.Errorf("unexpexted error message: %v", have)
}
}
func TestAssertLogEventsEqual(t *testing.T) {
event1 := LogEvent{
Level: Trace,
Line: "message",
Fields: map[string]interface{}{"key1": "value1", "key2": "value2"},
}
event2 := LogEvent{
Level: Trace,
Line: "message",
Fields: map[string]interface{}{"key2": "value2", "key1": "value1"},
}
err := LogEventsEqual(event1, event2)
if err != nil {
t.Errorf("failed to assert that two identical event are equal: %s", strings.ReplaceAll(err.Error(), "\n", `\n`))
}
}
func TestAssertLogEventsEqual_Errors(t *testing.T) {
tests := map[string]struct {
expected LogEvent
actual LogEvent
expectedError string
}{
"level": {
expected: LogEvent{
Level: Trace,
},
actual: LogEvent{
Level: Debug,
},
expectedError: "expected log levels to be equal\ngot: debug\nwant: trace",
},
"line": {
expected: LogEvent{
Line: "message",
},
actual: LogEvent{
Line: "other message",
},
expectedError: "expected log lines to be equal\ngot: \"other message\"\nwant: \"message\"",
},
"fields length": {
expected: LogEvent{
Fields: map[string]interface{}{},
},
actual: LogEvent{
Fields: map[string]interface{}{"key1": "value1"},
},
expectedError: "expected log fields to be equal\ngot: map[key1:value1]\nwant: map[]",
},
"fields value": {
expected: LogEvent{
Fields: map[string]interface{}{"key1": "value1"},
},
actual: LogEvent{
Fields: map[string]interface{}{"key1": "value2"},
},
expectedError: "expected log fields to be equal\ngot: map[key1:value2]\nwant: map[key1:value1]",
},
}
for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
err := LogEventsEqual(test.expected, test.actual)
if err.Error() != test.expectedError {
actualError := strings.ReplaceAll(err.Error(), "\n", `\n`)
expectedError := strings.ReplaceAll(test.expectedError, "\n", `\n`)
t.Errorf("expected log levels to be equal\ngot: %s\nwant: %s", actualError, expectedError)
}
})
}
}
func TestTestLogger(t *testing.T) {
logger := &TestLogger{}
logger.Debug("debug message")
logger.Info("info message", map[string]interface{}{"key": "value"})
if want, have := 2, logger.Count(); want != have {
t.Errorf("unexpexted log event count\nexpected: %v\nactual: %v", want, have)
}
events := logger.Events()
if want, have := 2, len(events); want != have {
t.Errorf("unexpexted returned log event count\nexpected: %v\nactual: %v", want, have)
}
lastEvent := LogEvent{
Level: Info,
Line: "info message",
Fields: map[string]interface{}{"key": "value"},
}
logtesting.AssertLogEventsEqual(t, LogEvent{Level: Debug, Line: "debug message"}, events[0])
logtesting.AssertLogEventsEqual(t, lastEvent, events[1])
logtesting.AssertLogEventsEqual(t, lastEvent, *logger.LastEvent())
suite := conformance.TestSuite{
LoggerFactory: func(_ Level) (Logger, conformance.TestLogger) {
logger := &TestLogger{}
return logger, logger
},
}
t.Run("Conformance", suite.Run)
}
func TestTestLoggerContext(t *testing.T) {
logger := &TestLoggerContext{}
logger.DebugContext(context.Background(), "debug message")
logger.InfoContext(context.Background(), "info message", map[string]interface{}{"key": "value"})
if want, have := 2, logger.Count(); want != have {
t.Errorf("unexpexted log event count\nexpected: %v\nactual: %v", want, have)
}
events := logger.Events()
if want, have := 2, len(events); want != have {
t.Errorf("unexpexted returned log event count\nexpected: %v\nactual: %v", want, have)
}
lastEvent := LogEvent{
Level: Info,
Line: "info message",
Fields: map[string]interface{}{"key": "value"},
}
logtesting.AssertLogEventsEqual(t, LogEvent{Level: Debug, Line: "debug message"}, events[0])
logtesting.AssertLogEventsEqual(t, lastEvent, events[1])
logtesting.AssertLogEventsEqual(t, lastEvent, *logger.LastEvent())
// T O D O: Conformance tests
suite := conformance.TestSuite{
LoggerFactory: func(_ Level) (Logger, conformance.TestLogger) {
logger := &TestLoggerContext{}
return loggerContextWrapper{logger}, logger
},
}
t.Run("Conformance", suite.Run)
}
type loggerContextWrapper struct {
LoggerContext
}
func (l loggerContextWrapper) Trace(msg string, fields ...map[string]interface{}) {
l.TraceContext(context.Background(), msg, fields...)
}
func (l loggerContextWrapper) Debug(msg string, fields ...map[string]interface{}) {
l.DebugContext(context.Background(), msg, fields...)
}
func (l loggerContextWrapper) Info(msg string, fields ...map[string]interface{}) {
l.InfoContext(context.Background(), msg, fields...)
}
func (l loggerContextWrapper) Warn(msg string, fields ...map[string]interface{}) {
l.WarnContext(context.Background(), msg, fields...)
}
func (l loggerContextWrapper) Error(msg string, fields ...map[string]interface{}) {
l.ErrorContext(context.Background(), msg, fields...)
}
func TestTestLoggerFacade(t *testing.T) {
logger := &TestLoggerFacade{}
logger.Debug("debug message")
logger.DebugContext(context.Background(), "another debug message")
logger.InfoContext(context.Background(), "another info message", map[string]interface{}{"key": "value"})
if want, have := 3, logger.Count(); want != have {
t.Errorf("unexpexted log event count\nexpected: %v\nactual: %v", want, have)
}
events := logger.Events()
if want, have := 3, len(events); want != have {
t.Errorf("unexpexted returned log event count\nexpected: %v\nactual: %v", want, have)
}
lastEvent := LogEvent{
Level: Info,
Line: "another info message",
Fields: map[string]interface{}{"key": "value"},
}
logtesting.AssertLogEventsEqual(t, LogEvent{Level: Debug, Line: "debug message"}, events[0])
logtesting.AssertLogEventsEqual(t, LogEvent{Level: Debug, Line: "another debug message"}, events[1])
logtesting.AssertLogEventsEqual(t, lastEvent, events[2])
logtesting.AssertLogEventsEqual(t, lastEvent, *logger.LastEvent())
suite := conformance.TestSuite{
LoggerFactory: func(_ Level) (Logger, conformance.TestLogger) {
logger := &TestLoggerFacade{}
return logger, logger
},
}
t.Run("Conformance", suite.Run)
}