-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_test.go
More file actions
260 lines (218 loc) · 6.59 KB
/
global_test.go
File metadata and controls
260 lines (218 loc) · 6.59 KB
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
package log
import (
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
// setupGlobalLoggerTest creates an observer logger and sets it as the global logger
func setupGlobalLoggerTest() (*observer.ObservedLogs, func()) {
// Create an observer core and logs
core, observedLogs := observer.New(zapcore.DebugLevel)
// Replace the global logger with our observer logger
originalLogger := zap.L()
zap.ReplaceGlobals(zap.New(core))
// Return the observed logs and a cleanup function
cleanup := func() {
zap.ReplaceGlobals(originalLogger)
}
return observedLogs, cleanup
}
// TestDebug tests the Debug global function
func TestDebug(t *testing.T) {
// Setup test
observedLogs, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Call the function being tested
Debug("debug message", zap.String("key", "value"))
// Verify the log entry
logs := observedLogs.All()
if len(logs) != 1 {
t.Fatalf("Expected 1 log entry, got %d", len(logs))
}
entry := logs[0]
if entry.Level != zapcore.DebugLevel {
t.Errorf("Expected level DebugLevel, got %v", entry.Level)
}
if entry.Message != "debug message" {
t.Errorf("Expected message 'debug message', got '%s'", entry.Message)
}
// Check for the field
contextMap := entry.ContextMap()
fieldValue, ok := contextMap["key"]
if !ok {
t.Error("Field 'key' not found in log entry")
} else if fieldValue != "value" {
t.Errorf("Expected 'key' to be 'value', got '%v'", fieldValue)
}
}
// TestInfo tests the Info global function
func TestInfo(t *testing.T) {
// Setup test
observedLogs, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Call the function being tested
Info("info message", zap.Int("count", 42))
// Verify the log entry
logs := observedLogs.All()
if len(logs) != 1 {
t.Fatalf("Expected 1 log entry, got %d", len(logs))
}
entry := logs[0]
if entry.Level != zapcore.InfoLevel {
t.Errorf("Expected level InfoLevel, got %v", entry.Level)
}
if entry.Message != "info message" {
t.Errorf("Expected message 'info message', got '%s'", entry.Message)
}
// Check for the field
contextMap := entry.ContextMap()
fieldValue, ok := contextMap["count"]
if !ok {
t.Error("Field 'count' not found in log entry")
} else {
// The number could be either int64 or float64 depending on the implementation
switch v := fieldValue.(type) {
case float64:
if v != float64(42) {
t.Errorf("Expected 'count' to be 42, got '%v'", v)
}
case int64:
if v != int64(42) {
t.Errorf("Expected 'count' to be 42, got '%v'", v)
}
default:
t.Errorf("Expected 'count' to be a number, got type %T", fieldValue)
}
}
}
// TestWarn tests the Warn global function
func TestWarn(t *testing.T) {
// Setup test
observedLogs, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Call the function being tested
Warn("warn message", zap.Bool("enabled", true))
// Verify the log entry
logs := observedLogs.All()
if len(logs) != 1 {
t.Fatalf("Expected 1 log entry, got %d", len(logs))
}
entry := logs[0]
if entry.Level != zapcore.WarnLevel {
t.Errorf("Expected level WarnLevel, got %v", entry.Level)
}
if entry.Message != "warn message" {
t.Errorf("Expected message 'warn message', got '%s'", entry.Message)
}
// Check for the field
contextMap := entry.ContextMap()
fieldValue, ok := contextMap["enabled"]
if !ok {
t.Error("Field 'enabled' not found in log entry")
} else if fieldValue != true {
t.Errorf("Expected 'enabled' to be true, got '%v'", fieldValue)
}
}
// TestError tests the Error global function
func TestError(t *testing.T) {
// Setup test
observedLogs, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Create an error
err := &testError{"test error"}
// Call the function being tested
Error("error message", zap.Error(err))
// Verify the log entry
logs := observedLogs.All()
if len(logs) != 1 {
t.Fatalf("Expected 1 log entry, got %d", len(logs))
}
entry := logs[0]
if entry.Level != zapcore.ErrorLevel {
t.Errorf("Expected level ErrorLevel, got %v", entry.Level)
}
if entry.Message != "error message" {
t.Errorf("Expected message 'error message', got '%s'", entry.Message)
}
// Check for the error field
contextMap := entry.ContextMap()
fieldValue, ok := contextMap["error"]
if !ok {
t.Error("Field 'error' not found in log entry")
} else if fieldValue != "test error" {
t.Errorf("Expected 'error' to be 'test error', got '%v'", fieldValue)
}
}
// TestDPanic tests the DPanic global function
func TestDPanic(t *testing.T) {
// Setup test
observedLogs, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Call the function being tested
DPanic("dpanic message", zap.String("key", "value"))
// Verify the log entry
logs := observedLogs.All()
if len(logs) != 1 {
t.Fatalf("Expected 1 log entry, got %d", len(logs))
}
entry := logs[0]
if entry.Level != zapcore.DPanicLevel {
t.Errorf("Expected level DPanicLevel, got %v", entry.Level)
}
if entry.Message != "dpanic message" {
t.Errorf("Expected message 'dpanic message', got '%s'", entry.Message)
}
}
// We don't test Panic and Fatal because they would terminate the test process
// TestSync tests the Sync global function
func TestSync(t *testing.T) {
// Setup test
_, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Call the function being tested
// This just tests that Sync doesn't panic
err := Sync()
if err != nil {
// Some implementations of zap.Logger.Sync() return an error
// on certain platforms, so we don't fail the test if it returns an error
t.Logf("Sync() returned error: %v", err)
}
}
// TestWith tests the With global function
func TestWith(t *testing.T) {
// Setup test
observedLogs, cleanup := setupGlobalLoggerTest()
defer cleanup()
// Call the function being tested
logger := With(zap.String("component", "test"))
// Use the returned logger
logger.Info("test message")
// Verify the log entry
logs := observedLogs.All()
if len(logs) != 1 {
t.Fatalf("Expected 1 log entry, got %d", len(logs))
}
entry := logs[0]
if entry.Level != zapcore.InfoLevel {
t.Errorf("Expected level InfoLevel, got %v", entry.Level)
}
if entry.Message != "test message" {
t.Errorf("Expected message 'test message', got '%s'", entry.Message)
}
// Check for the component field
contextMap := entry.ContextMap()
fieldValue, ok := contextMap["component"]
if !ok {
t.Error("Field 'component' not found in log entry")
} else if fieldValue != "test" {
t.Errorf("Expected 'component' to be 'test', got '%v'", fieldValue)
}
}
// testError is a simple error implementation for testing
type testError struct {
message string
}
func (e *testError) Error() string {
return e.message
}