-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_test.go
77 lines (59 loc) · 1.51 KB
/
container_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
package di
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type (
Config struct {
A string
B string
}
A struct {
AName string
}
B struct {
BName string
}
)
func TestContainerRegister(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
cb := New(&Config{A: "a", B: "b"})
sidA := ServiceID[*A]("service.a")
sidB := ServiceID[*B]("service.b")
var err error
err = Register(cb, sidA, func(ctx context.Context, cc *Container[*Config]) (*A, error) {
return &A{AName: cc.Config().A}, nil
})
assert.NoError(t, err)
err = Register(cb, sidB, func(ctx context.Context, cc *Container[*Config]) (*B, error) {
return &B{BName: cc.Config().B}, nil
})
assert.NoError(t, err)
c, err := cb.Build()
assert.NoError(t, err)
a, err := Get(ctx, c, sidA)
assert.NoError(t, err)
b, err := Get(ctx, c, sidB)
assert.NoError(t, err)
assert.Equal(t, c.Config().A, a.AName)
assert.Equal(t, c.Config().B, b.BName)
}
func TestContainerSeal(t *testing.T) {
cb := New(&Config{A: "a", B: "b"})
sidA := ServiceID[*A]("service.a")
sidB := ServiceID[*B]("service.b")
var err error
err = Register(cb, sidA, func(ctx context.Context, cc *Container[*Config]) (*A, error) {
return &A{AName: cc.Config().A}, nil
})
assert.NoError(t, err)
cb.Build()
err = Register(cb, sidB, func(ctx context.Context, cc *Container[*Config]) (*B, error) {
return &B{BName: cc.Config().B}, nil
})
assert.Error(t, err)
assert.True(t, errors.Is(err, ErrContainerSealed))
}