-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
78 lines (67 loc) · 1.57 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package log
import (
"bytes"
"context"
"reflect"
"testing"
"github.com/zalgonoise/x/log/handlers/jsonh"
)
func TestInContext(t *testing.T) {
t.Run("Success", func(t *testing.T) {
b := &bytes.Buffer{}
wants := New(jsonh.New(b))
input := InContext(context.Background(), wants)
v := input.Value(StandardCtxKey)
if v == nil {
t.Error("output is unexpectedly nil")
}
var (
out Logger
ok bool
)
if out, ok = v.(Logger); !ok {
t.Errorf("output is not a Logger interface")
}
if !reflect.DeepEqual(wants, out) {
t.Errorf("output mismatch error: wanted %v ; got %v", wants, out)
}
})
t.Run("FailNoContext", func(t *testing.T) {
b := &bytes.Buffer{}
l := New(jsonh.New(b))
out := InContext(nil, l)
if out != nil {
t.Errorf("output is not nil")
}
})
t.Run("FailNoLogger", func(t *testing.T) {
out := InContext(context.Background(), nil)
if out != nil {
t.Errorf("output is not nil")
}
})
t.Run("FailNoInput", func(t *testing.T) {
out := InContext(nil, nil)
if out != nil {
t.Errorf("output is not nil")
}
})
}
func TestFrom(t *testing.T) {
t.Run("Success", func(t *testing.T) {
b := &bytes.Buffer{}
wants := New(jsonh.New(b))
input := context.WithValue(context.Background(), StandardCtxKey, wants)
out := From(input)
if !reflect.DeepEqual(wants, out) {
t.Errorf("output mismatch error: wanted %v ; got %v", wants, out)
}
})
t.Run("Fail", func(t *testing.T) {
input := context.Background()
out := From(input)
if out != nil {
t.Errorf("output mismatch error: wanted %v ; got %v", nil, out)
}
})
}