-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
61 lines (53 loc) · 1.73 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package xop
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIntoContext(t *testing.T) {
log := NewSeed().Request("myLog")
ctx := context.Background()
ctxWithLog := log.IntoContext(ctx)
assert.Equal(t, log, ctxWithLog.Value(contextKey))
}
func TestFromContext(t *testing.T) {
log := NewSeed().Request("myLog")
ctx := context.Background()
ctxLog, ok := FromContext(ctx)
assert.False(t, ok)
assert.Nil(t, ctxLog)
ctxWithLog := log.IntoContext(ctx)
ctxLog, ok = FromContext(ctxWithLog)
assert.True(t, ok)
assert.Equal(t, log, ctxLog)
}
func TestFromContextOrDefault(t *testing.T) {
ctx := context.Background()
log := FromContextOrDefault(ctx)
assert.Equal(t, Default, log)
log = NewSeed().Request("myLog")
ctxLog := log.IntoContext(ctx)
assert.Equal(t, log, FromContextOrDefault(ctxLog))
}
func TestFromContextOrPanic(t *testing.T) {
ctx := context.Background()
assert.Panics(t, func() { _ = FromContextOrPanic(ctx) })
log := NewSeed().Request("myLog")
ctxLog := log.IntoContext(ctx)
assert.Equal(t, log, FromContextOrPanic(ctxLog))
}
func TestCustomFromContext(t *testing.T) {
adjustSettings := func(sub *Sub) *Sub {
return sub.PrefillText("banana")
}
customLogFun := CustomFromContext(FromContextOrDefault, adjustSettings)
customLog := customLogFun(context.Background())
defer customLog.Done()
assert.NotEqual(t, Default, customLog, "logs are not equal")
assert.NotEqual(t, Default.Settings(), customLog.Settings(), "default settings")
assert.Equal(t, noFilenameFunc(Default.Sub().PrefillText("banana").Logger().Settings()), noFilenameFunc(customLog.Settings()), "modified settings")
}
func noFilenameFunc(settings LogSettings) LogSettings {
settings.StackFilenameRewrite(nil)
return settings
}