-
Notifications
You must be signed in to change notification settings - Fork 8
/
context_test.go
41 lines (34 loc) · 1.03 KB
/
context_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
package logging
import (
"context"
"log/slog"
"testing"
)
func TestContextWithLogger(t *testing.T) {
ctx := context.Background()
logger := NewLogger()
ctxWithLogger := ContextWithLogger(ctx, logger)
// Extract logger from new context
extractedLogger, ok := ctxWithLogger.Value(ctxLogger{}).(*slog.Logger)
if !ok || extractedLogger != logger {
t.Errorf("Logger was not properly added to context")
}
}
func TestLoggerFromContext_WithLogger(t *testing.T) {
ctx := context.Background()
logger := NewLogger()
ctxWithLogger := ContextWithLogger(ctx, logger)
// Extract logger using loggerFromContext
extractedLogger := loggerFromContext(ctxWithLogger)
if extractedLogger != logger {
t.Errorf("Did not retrieve correct logger from context")
}
}
func TestLoggerFromContext_NoLogger(t *testing.T) {
ctx := context.Background()
// Extract logger from a context without a logger
extractedLogger := loggerFromContext(ctx)
if extractedLogger != Default() {
t.Errorf("Did not retrieve default logger when context has no logger")
}
}