-
Notifications
You must be signed in to change notification settings - Fork 0
/
corners_test.go
115 lines (94 loc) · 2.66 KB
/
corners_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
package module
import (
"context"
"testing"
)
func TestProviderReturnNilInterfaceWithoutError(t *testing.T) {
type Interface interface{}
newNilInterface := func(context.Context) (Interface, error) {
return nil, nil
}
moduleInterface := New[Interface]()
provideNil := moduleInterface.ProvideWithFunc(newNilInterface)
repo := NewRepo()
repo.Add(provideNil)
ctx, err := repo.InjectTo(context.Background())
if err != nil {
t.Fatal("inject error:", err)
}
var got Interface = moduleInterface.Value(ctx)
if got != nil {
t.Errorf("moduleInterface.Value(ctx) = %v, want: nil", got)
}
}
func TestProviderReturnNilPointerWithoutError(t *testing.T) {
type Instance struct{}
newNilInstance := func(context.Context) (*Instance, error) {
return nil, nil
}
moduleInstance := New[*Instance]()
provideNil := moduleInstance.ProvideWithFunc(newNilInstance)
repo := NewRepo()
repo.Add(provideNil)
ctx, err := repo.InjectTo(context.Background())
if err != nil {
t.Fatal("inject error:", err)
}
var got *Instance = moduleInstance.Value(ctx)
if got != nil {
t.Errorf("moduleInstance.Value(ctx) = %v, want: nil", got)
}
}
func TestModuleKeyIsNotString(t *testing.T) {
type Instance struct{}
newInstance := func(context.Context) (*Instance, error) {
return &Instance{}, nil
}
moduleInstance := New[*Instance]()
provider := moduleInstance.ProvideWithFunc(newInstance)
repo := NewRepo()
repo.Add(provider)
ctx, err := repo.InjectTo(context.Background())
if err != nil {
t.Fatal("inject error:", err)
}
if got := moduleInstance.Value(ctx); got == nil {
t.Errorf("moduleInstance.Value(ctx) = nil, want: not nil")
}
key := moduleInstance.moduleKey
if got := ctx.Value(key); got == nil {
t.Errorf("moduleInstance.Value(ctx) = nil, want: not nil")
}
keyAsStr := string(key)
if got := ctx.Value(keyAsStr); got != nil {
t.Errorf("ctx.Value(string(%q)) = %v, want: nil", keyAsStr, got)
}
}
func TestSameInstanceBetweenInject(t *testing.T) {
type Instance struct{}
newCount := 0
newInstance := func(context.Context) (*Instance, error) {
newCount++
return &Instance{}, nil
}
moduleInstance := New[*Instance]()
provider := moduleInstance.ProvideWithFunc(newInstance)
repo := NewRepo()
repo.Add(provider)
ctx1, err := repo.InjectTo(context.Background())
if err != nil {
t.Fatal("inject error:", err)
}
instance1 := moduleInstance.Value(ctx1)
ctx2, err := repo.InjectTo(context.Background())
if err != nil {
t.Fatal("inject error:", err)
}
instance2 := moduleInstance.Value(ctx2)
if instance1 != instance2 {
t.Fatal("different instances.")
}
if newCount != 1 {
t.Fatalf("newInstance was run %d times, which should be only 1.", newCount)
}
}