This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_testing_test.go
152 lines (125 loc) · 3.39 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
package logur_test
import (
"strings"
"testing"
. "github.com/goph/logur"
"github.com/goph/logur/logtesting"
)
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.Replace(err.Error(), "\n", `\n`, -1))
}
}
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.Replace(err.Error(), "\n", `\n`, -1)
expectedError := strings.Replace(test.expectedError, "\n", `\n`, -1)
t.Errorf("expected log levels to be equal\ngot: %s\nwant: %s", actualError, expectedError)
}
})
}
}
func newTestLoggerSuite() *logtesting.LoggerTestSuite {
return &logtesting.LoggerTestSuite{
LoggerFactory: func(_ Level) (Logger, func() []LogEvent) {
logger := NewTestLogger()
return logger, func() []LogEvent { // nolint: gocritic
return logger.Events()
}
},
}
}
func TestTestLogger_Levels(t *testing.T) {
newTestLoggerSuite().TestLevels(t)
}
func TestTestLogger_Count(t *testing.T) {
logger := NewTestLogger()
logger.Debug("message")
if got, want := logger.Count(), 1; got != want {
t.Errorf("expected log event count to be %d, got %d", want, got)
}
}
func TestTestLogger_Events(t *testing.T) {
logger := NewTestLogger()
logger.Debug("message")
events := logger.Events()
if got, want := len(events), 1; got != want {
t.Fatalf("expected log event count to be %d, got %d", want, got)
}
event := LogEvent{
Level: Debug,
Line: "message",
}
logtesting.AssertLogEventsEqual(t, event, events[0])
}
func TestTestLogger_LastEvent(t *testing.T) {
logger := NewTestLogger()
logger.Debug("message")
logger.Info("another message")
lastEvent := logger.LastEvent()
if lastEvent == nil {
t.Fatal("failed to get last event")
}
event := LogEvent{
Level: Info,
Line: "another message",
}
logtesting.AssertLogEventsEqual(t, event, *lastEvent)
}