-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
44 lines (33 loc) · 1.01 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
42
43
44
package grip
import (
"context"
"testing"
"github.com/tychoish/fun/assert/check"
"github.com/tychoish/grip/send"
)
func TestContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if Context(ctx) != std {
t.Fatal("context does not default to standard")
}
if Context(nil) != std { //nolint:staticcheck
t.Fatal("context does not default to standard")
}
logger := NewLogger(send.MakeStdOutput())
ctx = WithLogger(ctx, logger)
if !HasContextLogger(ctx, string(defaultContextKey)) {
t.Error(ctx)
}
if Context(ctx) == std {
t.Fatal("context logger should not return standard if set")
}
if Context(ctx) != logger {
t.Fatal("context should return expected value")
}
ctx = WithNewContextLogger(ctx, string(defaultContextKey), func() send.Sender { panic("should not panic") })
check.True(t, HasContextLogger(ctx, string(defaultContextKey)))
check.Panic(t, func() {
ctx = WithNewContextLogger(ctx, "novel-key", func() send.Sender { panic("should not panic") })
})
}