-
Notifications
You must be signed in to change notification settings - Fork 14
/
module_test.go
97 lines (80 loc) · 2.3 KB
/
module_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
package dingo
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type (
tryModuleOk struct{}
tryModuleFail struct{}
tryModulePanic struct{}
)
func (t *tryModuleOk) Configure(injector *Injector) {
injector.Bind(new(string)).ToInstance("test")
}
func (t *tryModuleFail) Configure(injector *Injector) {
injector.Bind(new(int)).ToInstance("test")
}
func (t *tryModulePanic) Configure(injector *Injector) {
injector.Bind(nil)
}
func TestTryModule(t *testing.T) {
assert.NoError(t, TryModule(new(tryModuleOk)))
assert.Error(t, TryModule(new(tryModuleFail)))
assert.Error(t, TryModule(new(tryModulePanic)))
}
func TestResolveDependenciesWithModuleFunc(t *testing.T) {
var countInline, countExtern int
ext := ModuleFunc(func(injector *Injector) {
countExtern++
})
injector, err := NewInjector(
new(tryModuleOk),
ModuleFunc(func(injector *Injector) {
countInline++
}),
ModuleFunc(func(injector *Injector) {
countInline++
}),
ext,
ext,
)
assert.NoError(t, err)
assert.NotNil(t, injector)
assert.Equal(t, 2, countInline, "inline modules should be called once (eventually twice for this test)")
assert.Equal(t, 1, countExtern, "variable defined modules should only be called once")
}
type (
resolveDependenciesModuleA struct{}
resolveDependenciesModuleB struct{}
resolveDependenciesModuleB2 struct{}
resolveDependenciesModuleC struct{}
)
func (*resolveDependenciesModuleA) Configure(*Injector) {}
func (*resolveDependenciesModuleA) Depends() []Module {
return []Module{
new(resolveDependenciesModuleA),
new(resolveDependenciesModuleB),
new(resolveDependenciesModuleB2),
}
}
func (*resolveDependenciesModuleB) Configure(*Injector) {}
func (*resolveDependenciesModuleB) Depends() []Module {
return []Module{
new(resolveDependenciesModuleC),
new(resolveDependenciesModuleB2),
}
}
func (*resolveDependenciesModuleB2) Configure(*Injector) {}
func (*resolveDependenciesModuleC) Configure(*Injector) {}
func Test_resolveDependencies(t *testing.T) {
resolved := resolveDependencies([]Module{new(resolveDependenciesModuleA)}, nil)
if !reflect.DeepEqual(resolved, []Module{
new(resolveDependenciesModuleC),
new(resolveDependenciesModuleB2),
new(resolveDependenciesModuleB),
new(resolveDependenciesModuleA),
}) {
t.Errorf("%#v not correctly resolved", resolved)
}
}