-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_test.go
183 lines (136 loc) · 4.09 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package tests
import (
"fmt"
"github.com/goal-web/container"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/utils"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
type DemoParam struct {
Id string
}
type DemoComponent struct {
Param DemoParam
}
func TestArgumentsTypeMap(t *testing.T) {
args := container.NewArgumentsTypeMap([]interface{}{"啦啦啦", DemoParam{Id: "111"}})
str := args.Pull("string")
fmt.Println(str)
assert.True(t, str == "啦啦啦")
args = container.NewArgumentsTypeMap([]interface{}{})
assert.True(t, args.Pull("string") == nil)
}
func TestBaseContainer(t *testing.T) {
app := container.New()
app.Instance("a", "a")
assert.True(t, app.HasBound("a"))
assert.True(t, app.Get("a") == "a")
app.Alias("a", "A")
assert.True(t, app.Get("A") == "a")
assert.True(t, app.HasBound("A"))
app.Bind("DemoParam", func() DemoParam {
return DemoParam{Id: "测试一下"}
})
assert.True(t, app.Get(utils.GetTypeKey(reflect.TypeOf(DemoParam{}))).(DemoParam).Id == "测试一下")
app.Call(container.NewMagicalFunc(func(param DemoParam) {
assert.True(t, param.Id == "测试一下")
}))
}
func TestContainer(t *testing.T) {
app := container.New()
app.Bind("DemoParam", func() DemoParam {
return DemoParam{Id: "没有外部参数的话,从容器中获取"}
})
fn := container.NewMagicalFunc(func(param DemoParam) string {
return param.Id
})
// 自己传参
assert.True(t, app.Call(fn, DemoParam{Id: "优先使用外部参数"})[0] == "优先使用外部参数")
// 不传参,使用容器中的实例
assert.True(t, app.Call(fn)[0] == "没有外部参数的话,从容器中获取")
}
type DemoStruct struct {
Param DemoParam `di:""` // 注入对应类型的实例
Config string `di:"config"` // 注入指定 key 的实例
}
func TestContainerMake(t *testing.T) {
app := container.New()
app.Instance("config", "通过容器设置的配置")
app.Bind("DemoParam", func() DemoParam {
return DemoParam{Id: "没有外部参数的话,从容器中获取"}
})
demo := &DemoStruct{}
app.DI(demo)
fmt.Println(demo)
}
func TestAliasType(t *testing.T) {
app := container.New()
app.Singleton("param", func() DemoParam {
return DemoParam{
Id: "a",
}
})
type AliasParam DemoParam
app.Call(container.NewMagicalFunc(func(param AliasParam) {
fmt.Println(param)
}), app.Get("param"))
}
type DemoStruct2 struct {
DemoStruct
}
func (d *DemoStruct2) Construct(container2 contracts.Container) {
d.DemoStruct = container2.Get("struct").(DemoStruct)
}
// 调用方法支持注入自定义类
func TestAutoContainer(t *testing.T) {
app := container.New()
app.Singleton("struct", func() DemoStruct {
return DemoStruct{
Param: DemoParam{Id: "id"},
Config: "config",
}
})
//struct2Type := reflect.TypeOf(DemoStruct2{})
//struct2Value := reflect.New(struct2Type).Interface()
struct2Value := &DemoStruct2{}
app.DI(struct2Value)
app.Call(container.NewMagicalFunc(func(struct2 DemoStruct2) {
assert.True(t, struct2.Config == "config" && struct2.Param.Id == "id")
}))
app.Call(container.NewMagicalFunc(func(struct2 DemoStruct2, struct1 DemoStruct) { // 因为 DemoStruct2 实现了 contracts.Component 所以不会使用自定义参数
assert.True(t, struct2.Config == "config" && struct2.Param.Id == "id")
assert.True(t, struct1.Config == "config22" && struct1.Param.Id == "custom")
}), DemoStruct{
Param: DemoParam{Id: "custom"},
Config: "config22",
})
}
// 测试控制器执行
type DemoDependent struct {
Id string
}
type DemoController struct {
Dep DemoDependent `di:""` // 表示需要注入
}
func (demo *DemoController) PrintDep() {
fmt.Println(demo.Dep)
}
func TestControllerCall(t *testing.T) {
app := container.New()
app.Singleton("DemoDependent", func() DemoDependent {
return DemoDependent{
Id: "id ddd",
}
})
controller := &DemoController{}
app.DI(controller)
app.Call(container.NewMagicalFunc(controller.PrintDep))
}
func TestCallAndDIContainer(t *testing.T) {
app := container.New()
app.Call(container.NewMagicalFunc(func(container2 contracts.Container) {
fmt.Println(container2)
}))
}