-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_test.go
85 lines (67 loc) · 1.63 KB
/
value_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
79
80
81
82
83
84
85
package contextz
import (
"context"
"errors"
"testing"
)
func TestWithValue(t *testing.T) {
t.Parallel()
t.Run("success", func(t *testing.T) {
t.Parallel()
const expected = 1
ctx := WithValue(context.Background(), expected)
actual, err := Value[int](ctx)
if err != nil {
t.Errorf("❌: err != nil: %+v", err)
}
if expected != actual {
t.Errorf("❌: expected(%v) != actual(%v)", expected, actual)
}
})
}
func TestValue(t *testing.T) {
t.Parallel()
t.Run("error,ErrNilContext", func(t *testing.T) {
t.Parallel()
ctx := (context.Context)(nil)
_, err := Value[int](ctx)
if !errors.Is(err, ErrNilContext) {
t.Errorf("❌: !errors.Is(err, ErrNilContext): %+v", err)
}
})
t.Run("error,ErrNotFoundInContext", func(t *testing.T) {
t.Parallel()
ctx := context.Background()
_, err := Value[int](ctx)
if !errors.Is(err, ErrNotFoundInContext) {
t.Errorf("❌: !errors.Is(err, ErrNotFoundInContext): %+v", err)
}
})
}
func TestMustValue(t *testing.T) {
t.Parallel()
t.Run("success", func(t *testing.T) {
t.Parallel()
const expected = 1
ctx := WithValue(context.Background(), expected)
actual := MustValue[int](ctx)
if expected != actual {
t.Errorf("❌: expected(%v) != actual(%v)", expected, actual)
}
})
t.Run("error,panic", func(t *testing.T) {
t.Parallel()
defer func() {
if r := recover(); r == nil {
t.Errorf("❌: panic did not occur")
if err, ok := r.(error); ok {
t.Errorf("❌: err: %+v", err)
if !errors.Is(err, ErrNilContext) {
t.Errorf("❌: !errors.Is(err, ErrNilContext): %+v", err)
}
}
}
}()
MustValue[int](nil)
})
}