-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontainer_test.go
384 lines (312 loc) · 6.85 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package alice
import (
"reflect"
"testing"
)
//***********************************************************
// Definitions of dependencies and modules used for testing
//***********************************************************
type D1 interface {
D1()
}
type D1Impl struct{}
func (d *D1Impl) D1() {}
type D2 interface {
D2()
}
type D2Impl struct{}
func (d *D2Impl) D2() {}
type D3 interface {
D3()
}
type D3Impl struct{}
func (d *D3Impl) D3() {}
type D4 interface {
D4()
}
type D4Impl struct{}
func (d *D4Impl) D4() {}
type D5 interface {
D5()
}
type D5Impl struct{}
func (d *D5Impl) D5() {}
type D5Impl2 struct{}
func (d *D5Impl2) D5() {}
type M1 struct {
BaseModule
}
func (m *M1) D1() D1 {
return &D1Impl{}
}
func (m *M1) D2() D2 {
return &D2Impl{}
}
type M2 struct {
BaseModule
D1 D1 `alice:"D1"`
D2 D2 `alice:"D2"`
D3 D3 `alice:""`
D4 D4 `alice:""`
}
func (m *M2) D5() *D5Impl {
return &D5Impl{}
}
type M3 struct {
BaseModule
D5 D5 `alice:""`
}
func (m *M3) DM3() D1 {
return &D1Impl{}
}
type M4 struct {
BaseModule
D1 D1 `alice:"D1"`
}
func (m *M4) D3() D3 {
return &D3Impl{}
}
func (m *M4) D4() D4 {
return &D4Impl{}
}
type M5 struct {
BaseModule
}
type M6 struct {
BaseModule
D1 D1 `alice:"DM3"`
}
func (m *M6) D3() D3 {
return &D3Impl{}
}
func (m *M6) D4() D4 {
return &D4Impl{}
}
type M1Duplicated struct {
BaseModule
}
func (m *M1Duplicated) D1() D1 {
return &D1Impl{}
}
type ModuleWithD51 struct {
BaseModule
}
func (m *ModuleWithD51) D5_1() D5 {
return &D5Impl{}
}
type ModuleWithD52 struct {
BaseModule
}
func (m *ModuleWithD52) D5_2() D5 {
return &D5Impl2{}
}
type ModuleWithD5Impl1 struct {
BaseModule
}
func (m *ModuleWithD5Impl1) D5_1() *D5Impl {
return &D5Impl{}
}
type ModuleWithD5Impl2 struct {
BaseModule
}
func (m *ModuleWithD5Impl2) D5_2() *D5Impl2 {
return &D5Impl2{}
}
type SelfDependModule struct {
BaseModule
D D1 `alice:"D1"`
}
func (m *SelfDependModule) D1() D1 {
return &D1Impl{}
}
//***********************************************************
func TestPopulate(t *testing.T) {
var (
m1 = &M1{}
m2 = &M2{}
m3 = &M3{}
m4 = &M4{}
m5 = &M5{}
)
c := &container{modules: []Module{m1, m2, m3, m4, m5}}
c.populate()
expectedM2 := &M2{
D1: &D1Impl{},
D2: &D2Impl{},
D3: &D3Impl{},
D4: &D4Impl{},
}
if !reflect.DeepEqual(m2, expectedM2) {
t.Errorf("bad m2 after populate(): got %v, expected %v", m2, expectedM2)
}
expectedM3 := &M3{
D5: &D5Impl{},
}
if !reflect.DeepEqual(m3, expectedM3) {
t.Errorf("bad m3 after populate(): got %v, expected %v", m3, expectedM3)
}
expectedM4 := &M4{
D1: &D1Impl{},
}
if !reflect.DeepEqual(m4, expectedM4) {
t.Errorf("bad m4 after populate(): got %v, expected %v", m4, expectedM4)
}
expectedInstanceByName := map[string]interface{}{
"D1": &D1Impl{},
"D2": &D2Impl{},
"D5": &D5Impl{},
"DM3": &D1Impl{},
"D3": &D3Impl{},
"D4": &D4Impl{},
}
if !reflect.DeepEqual(c.instanceByName, expectedInstanceByName) {
t.Errorf("bad instanceByName after populate(): got %v, expected %v", c.instanceByName, expectedInstanceByName)
}
expectedInstanceByType := map[reflect.Type][]interface{}{
reflect.TypeOf((*D1)(nil)).Elem(): {
&D1Impl{},
&D1Impl{},
},
reflect.TypeOf((*D2)(nil)).Elem(): {
&D2Impl{},
},
reflect.TypeOf((*D5Impl)(nil)): {
&D5Impl{},
},
reflect.TypeOf((*D3)(nil)).Elem(): {
&D3Impl{},
},
reflect.TypeOf((*D4)(nil)).Elem(): {
&D4Impl{},
},
}
if !reflect.DeepEqual(c.instanceByType, expectedInstanceByType) {
t.Errorf("bad instanceByType after populate(): got %v, expected %v", c.instanceByType, expectedInstanceByType)
}
}
func TestPopulate_PanicOnInvalidModule(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic after populate() on invalid module")
} else {
t.Log(r)
}
}()
c := &container{modules: []Module{nonPointerModule{}}}
c.populate()
}
func TestPopulate_PanicOnCreateGraphError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic after populate() on create graph error")
} else {
t.Log(r)
}
}()
c := &container{modules: []Module{&M1{}, &M1Duplicated{}}}
c.populate()
}
func TestPopulate_PanicOnInstantiationOrderError(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic after populate() on instantiation order error")
} else {
t.Log(r)
}
}()
c := &container{modules: []Module{&M1{}, &M2{}, &M3{}, &M6{}}}
c.populate()
}
func TestInstance(t *testing.T) {
var (
m1 = &M1{}
m2 = &M2{}
m3 = &M3{}
m4 = &M4{}
m5 = &M5{}
)
c := &container{modules: []Module{m1, m2, m3, m4, m5}}
c.populate()
d2 := c.Instance(reflect.TypeOf((*D2)(nil)).Elem()).(D2)
expectedD2 := &D2Impl{}
if !reflect.DeepEqual(d2, expectedD2) {
t.Errorf("bad instance from Instance(): got %v, expected %v", d2, expectedD2)
}
d5 := c.Instance(reflect.TypeOf((*D5Impl)(nil))).(*D5Impl)
expectedD5 := &D5Impl{}
if !reflect.DeepEqual(d5, expectedD5) {
t.Errorf("bad instance from Instance(): got %v, expected %v", d5, expectedD5)
}
}
func TestInstance_PanicOnTypeNotFound(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for Instance() on type not found")
} else {
t.Log(r)
}
}()
c := &container{modules: []Module{&M1{}}}
c.populate()
c.Instance(reflect.TypeOf((*D3)(nil)).Elem())
}
func TestInstance_PanicOnMultipleMatchedType(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for Instance() on multiple matched type")
} else {
t.Log(r)
}
}()
c := &container{modules: []Module{&M1{}, &M2{}, &M3{}, &M4{}}}
c.populate()
c.Instance(reflect.TypeOf((*D1)(nil)).Elem())
}
func TestInstanceByName(t *testing.T) {
var (
m1 = &M1{}
m2 = &M2{}
m3 = &M3{}
m4 = &M4{}
m5 = &M5{}
)
c := &container{modules: []Module{m1, m2, m3, m4, m5}}
c.populate()
d1 := c.InstanceByName("D1").(D1)
expectedD1 := &D1Impl{}
if !reflect.DeepEqual(d1, expectedD1) {
t.Errorf("bad instance from InstanceByName(): got %v, expected %v", d1, expectedD1)
}
dm3 := c.InstanceByName("DM3").(D1)
expectedDM3 := &D1Impl{}
if !reflect.DeepEqual(dm3, expectedDM3) {
t.Errorf("bad instance from InstanceByName(): got %v, expected %v", dm3, expectedDM3)
}
}
func TestInstanceByName_PanicOnNameNotFound(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for InstanceByName() on name not found")
} else {
t.Log(r)
}
}()
c := &container{modules: []Module{&M1{}}}
c.populate()
c.InstanceByName("D3")
}
func TestCreateContainer(t *testing.T) {
var (
m1 = &M1{}
m2 = &M2{}
m3 = &M3{}
m4 = &M4{}
m5 = &M5{}
)
c := CreateContainer(m1, m2, m3, m4, m5)
d1 := c.InstanceByName("D1").(D1)
expectedD1 := &D1Impl{}
if !reflect.DeepEqual(d1, expectedD1) {
t.Errorf("bad instance after CreateContainer(): got %v, expected %v", d1, expectedD1)
}
}