-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
46 lines (41 loc) · 992 Bytes
/
Copy pathcontext_test.go
File metadata and controls
46 lines (41 loc) · 992 Bytes
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
package cannon
import (
"context"
"testing"
"go.uber.org/zap"
)
func TestContext(t *testing.T) {
newLogger := func() *zap.Logger {
logger, err := zap.NewProduction(Core())
if err != nil {
t.Fatalf("failed to create logger: %s", err)
}
return logger
}
nilLogger := func() *zap.Logger { return nil }
tt := []struct {
Name string
Logger func() *zap.Logger
ShouldErr bool
}{
{"embed in context", newLogger, false},
{"no logger defined", nilLogger, true},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
factory = nil
logger := tc.Logger()
ctx := CtxLogger(context.Background(), logger)
receivedLogger, err := LoggerFromCtx(ctx)
switch {
case tc.ShouldErr && err == nil:
t.Fatalf("logger should have errored but did not, instead got logger: %v", receivedLogger)
return
case receivedLogger != logger:
t.Fatalf("expected logger %v but got %v", logger, receivedLogger)
return
default:
}
})
}
}