-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappcontext_test.go
More file actions
40 lines (31 loc) · 962 Bytes
/
appcontext_test.go
File metadata and controls
40 lines (31 loc) · 962 Bytes
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
package appcontext_test
import (
"context"
"testing"
"github.com/ituoga/appcontext"
)
func TestCore(t *testing.T) {
t.Run("With and Get", func(t *testing.T) {
var myKey = appcontext.Key[string]("myKey")
ctx := context.Background()
ctx = appcontext.With(ctx, myKey, "testValue")
var secondKey = appcontext.Key[string]("secondKey")
ctx = appcontext.With(ctx, secondKey, "anotherValue")
value, ok := appcontext.Get(ctx, myKey)
if !ok || value != "testValue" {
t.Errorf("Expected 'testValue', got '%v'", value)
}
secondValue, ok := appcontext.Get(ctx, secondKey)
if !ok || secondValue != "anotherValue" {
t.Errorf("Expected 'anotherValue', got '%v'", secondValue)
}
})
t.Run("Get non-existent key", func(t *testing.T) {
var anotherKey = appcontext.Key[string]("nonExistentKey")
ctx := context.Background()
_, ok := appcontext.Get(ctx, anotherKey)
if ok {
t.Error("Expected false for non-existent key")
}
})
}