-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
117 lines (108 loc) · 3.17 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package pipeline
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContext(t *testing.T) {
tests := map[string]struct {
givenKey interface{}
givenValue interface{}
expectedValue interface{}
expectedFound bool
}{
"GivenNonExistentKey_ThenExpectNilAndFalse": {
givenKey: nil,
expectedValue: nil,
},
"GivenKeyWithNilValue_ThenExpectNilAndTrue": {
givenKey: "key",
givenValue: nil,
expectedValue: nil,
expectedFound: true,
},
"GivenKeyWithValue_ThenExpectValueAndTrue": {
givenKey: "key",
givenValue: "value",
expectedValue: "value",
expectedFound: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := MutableContext(context.Background())
if tc.givenKey != nil {
StoreInContext(ctx, tc.givenKey, tc.givenValue)
}
result, found := LoadFromContext(ctx, tc.givenKey)
assert.Equal(t, tc.expectedValue, result, "value")
assert.Equal(t, tc.expectedFound, found, "value found")
})
}
}
func TestContextPanics(t *testing.T) {
assert.PanicsWithError(t, "context was not set up with MutableContext()", func() {
StoreInContext(context.Background(), "key", "value")
}, "StoreInContext")
assert.PanicsWithError(t, "context was not set up with MutableContext()", func() {
LoadFromContext(context.Background(), "key")
}, "LoadFromContext")
}
func TestMutableContextRepeated(t *testing.T) {
parent := context.Background()
result := MutableContext(parent)
assert.NotEqual(t, parent, result)
repeated := MutableContext(result)
assert.Equal(t, result, repeated)
}
func TestMustLoadFromContext(t *testing.T) {
t.Run("KeyExistsWithNil", func(t *testing.T) {
ctx := MutableContext(context.Background())
StoreInContext(ctx, "key", nil)
result := MustLoadFromContext(ctx, "key")
assert.Nil(t, result)
})
t.Run("KeyDoesntExist", func(t *testing.T) {
assert.PanicsWithError(t, `key "key" was not found in context`, func() {
ctx := MutableContext(context.Background())
_ = MustLoadFromContext(ctx, "key")
})
})
t.Run("KeyExistsWithValue", func(t *testing.T) {
ctx := MutableContext(context.Background())
StoreInContext(ctx, "key", "value")
result := MustLoadFromContext(ctx, "key")
assert.Equal(t, "value", result)
})
}
func TestLoadFromContextOrDefault(t *testing.T) {
t.Run("KeyExists", func(t *testing.T) {
ctx := MutableContext(context.Background())
StoreInContext(ctx, "key", "value")
result := LoadFromContextOrDefault(ctx, "key", "default")
assert.Equal(t, "value", result)
})
t.Run("KeyDoesntExist", func(t *testing.T) {
ctx := MutableContext(context.Background())
result := LoadFromContextOrDefault(ctx, "key", "default")
assert.Equal(t, "default", result)
})
}
func ExampleMutableContext() {
type key struct{}
ctx := MutableContext(context.Background())
p := NewPipeline[context.Context]().WithSteps(
NewStep("store value", func(ctx context.Context) error {
StoreInContext(ctx, key{}, "value")
return nil
}),
NewStep("retrieve value", func(ctx context.Context) error {
value, _ := LoadFromContext(ctx, key{})
fmt.Println(value)
return nil
}),
)
_ = p.RunWithContext(ctx)
// Output: value
}