-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
59 lines (47 loc) · 1.07 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
package cliz
import (
"context"
"strings"
"testing"
)
func TestFromContext(t *testing.T) {
t.Parallel()
t.Run("success", func(t *testing.T) {
t.Parallel()
cmd, ok := FromContext(WithContext(context.Background(), &Command{}))
if !ok {
t.Fatalf("❌: !ok")
}
if cmd == nil {
t.Errorf("❌: cmd == nil")
}
})
}
func TestMustFromContext(t *testing.T) {
t.Parallel()
t.Run("success", func(t *testing.T) {
t.Parallel()
ctx := WithContext(context.Background(), &Command{})
if c := MustFromContext(ctx); c == nil {
t.Fatal("❌: c == nil")
}
})
t.Run("error,panic", func(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r == nil {
t.Fatalf("❌: panic did not occur")
}
err, ok := r.(error)
if !ok {
t.Fatalf("❌: err: %+v", err)
}
const expected = "runtime error: invalid memory address or nil pointer dereference"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("❌: err == nil || !strings.Contains(err.Error(), %q): %+v", expected, err)
}
}()
MustFromContext(nil)
})
}