-
Notifications
You must be signed in to change notification settings - Fork 14
/
container_test.go
426 lines (368 loc) · 10.7 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package di
import (
"reflect"
"testing"
)
type Bar interface {
test(int) string
}
type Foo struct {
a string
}
func (f *Foo) test(b int) string {
return f.a
}
type Writer interface {
Write(string) string
}
type ResponseWriter struct {
Foo *Foo `inject:"true"`
t int
}
func (*ResponseWriter) Write(s string) string {
return s
}
type Request struct {
Bar Bar `inject:"true"`
}
type Context struct {
data string
}
type Controller struct {
*Context `inject:"true"`
Response Writer `inject:"true"`
response Writer `inject:"true"`
Response2 Writer `inject:"true"`
Response3 Writer
Request Request `inject:"true"`
action string
}
func TestRegister(t *testing.T) {
var foo1 Foo
foo2 := Foo{}
foo3 := &Foo{}
foo4 := make([]Foo, 0)
var foo5 [10]Foo
var foo6 chan Foo
var foo7 map[string]Foo
var bar1 Bar = &Foo{}
bar2 := (*Bar)(nil)
tests := []struct {
value interface{}
result reflect.Type
}{
{100, reflect.TypeOf(int(0))},
{"abc", reflect.TypeOf(string(""))},
{true, reflect.TypeOf(bool(false))},
{100, reflect.TypeOf(int(0))},
{foo1, reflect.TypeOf(Foo{})},
{foo2, reflect.TypeOf(Foo{})},
{foo3, reflect.TypeOf(&Foo{})},
{foo4, reflect.TypeOf(make([]Foo, 0))},
{foo5, reflect.TypeOf([10]Foo{})},
{foo6, reflect.TypeOf(make(chan Foo, 0))},
{foo7, reflect.TypeOf(make(map[string]Foo))},
{bar1, reflect.TypeOf(&Foo{})},
{bar2, reflect.TypeOf((*Bar)(nil))},
}
for _, test := range tests {
c := NewContainer()
c.Register(test.value)
if !c.HasRegistered(test.result) {
t.Errorf("Register(%v) failed, expected %v is registered", test.value, test.result)
}
}
}
func TestRegisterInterface(t *testing.T) {
var foo Foo
var bar *Bar = (*Bar)(nil)
c := NewContainer()
barType := InterfaceOf(bar)
c.RegisterAs(&foo, barType)
if !c.HasRegistered(barType) {
t.Errorf("RegisterAs(%v, %v) failed, expected %v is registered", &foo, barType, barType)
}
c.Unregister(barType)
if c.HasRegistered(barType) {
t.Errorf("barType is still registered, expected unregistered")
}
fooType := reflect.TypeOf(&foo)
if c.HasRegistered(fooType) {
t.Errorf("RegisterAs(%v, %v) failed, expected %v is NOT registered", &foo, barType, fooType)
}
defer func() {
if e := recover(); e == nil {
t.Errorf("Expected a panic when registering an incompatible object")
}
}()
writerType := InterfaceOf((*Writer)(nil))
c.RegisterAs(&foo, writerType)
}
func TestRegisterInterface2(t *testing.T) {
var foo Foo
fooType := reflect.TypeOf(&foo)
writerType := InterfaceOf((*Writer)(nil))
c := NewContainer()
defer func() {
if e := recover(); e == nil {
t.Errorf("Expected a panic when registering an incompatible type")
}
}()
c.RegisterAs(fooType, writerType)
}
func TestRegisterProvider(t *testing.T) {
var f = func(Container) reflect.Value {
return reflect.ValueOf(Foo{})
}
bar := (*Bar)(nil)
funcType := reflect.TypeOf(f)
barType := InterfaceOf(bar)
c := NewContainer()
c.RegisterProvider(f, barType, true)
if !c.HasRegistered(barType) {
t.Errorf("RegisterProvider(func, %v) failed, expected %v is registered", barType, barType)
}
if c.HasRegistered(funcType) {
t.Errorf("RegisterProvider(func, %v) failed, expected %v is NOT registered", barType, funcType)
}
}
func TestBuildBasic(t *testing.T) {
tests := []struct {
rt reflect.Type
result interface{}
}{
{reflect.TypeOf(int(0)), 0},
{reflect.TypeOf(string("abc")), ""},
{reflect.TypeOf(Foo{}), Foo{}},
}
c := NewContainer()
for _, test := range tests {
v := c.Make(test.rt)
if v != test.result {
t.Errorf("Make(%v): expected %#v, got %#v", test.rt, test.result, v)
}
}
v := c.Make(reflect.TypeOf(&Foo{}))
if reflect.ValueOf(v).IsNil() {
t.Errorf("Make(%v) = nil, expected not nil", &Foo{})
}
}
func TestBuildComplex(t *testing.T) {
c := NewContainer()
slice := c.Make(reflect.TypeOf([]int(nil))).([]int)
if slice == nil || len(slice) != 0 {
t.Errorf("Make([]int): expected []int{}, got %#v", slice)
}
m := c.Make(reflect.TypeOf(map[string]Foo{})).(map[string]Foo)
if m == nil || len(m) != 0 {
t.Errorf("Make(map[string]Foo): expected map[string]Foo{}, got %#v", slice)
}
ch := c.Make(reflect.TypeOf(make(chan Foo))).(chan Foo)
if ch == nil || len(ch) != 0 {
t.Errorf("Make(chan Foo): expected chan Foo{}, got %#v", ch)
}
}
func TestBuildStruct(t *testing.T) {
c := NewContainer()
foo := Foo{"abc"}
c.Register(foo)
foo1 := c.Make(reflect.TypeOf(Foo{})).(Foo)
if foo1.a != "abc" {
t.Errorf("Make(Foo): expected Foo.a=%q, got %q", "abc", foo1.a)
}
// the registered version should remain unchanged (registering by value)
foo1.a = "test"
foo2 := c.Make(reflect.TypeOf(Foo{})).(Foo)
if foo2.a != "abc" {
t.Errorf("Make(Foo) after change: expected Foo.a=%q, got %q", "abc", foo2.a)
}
// registering by value doesn't mean registering the corresponding pointer
foo3 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo3 != nil {
t.Errorf("Make(Foo*) != nil, expected nil")
}
}
func TestBuildStructPtr(t *testing.T) {
c := NewContainer()
foo := &Foo{"abc"}
c.Register(foo)
foo1 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo1.a != "abc" {
t.Errorf("Make(*Foo): expected Foo.a=%q, got %q", "abc", foo1.a)
}
// the registered version should be unchanged (registering by pointer)
foo1.a = "test"
foo2 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo2.a != "test" {
t.Errorf("Make(*Foo) after change: expected Foo.a=%q, got %q", "test", foo2.a)
}
// registering by value doesn't mean registering the corresponding pointer
foo3 := c.Make(reflect.TypeOf(Foo{})).(Foo)
if foo3.a != "test" {
t.Errorf("Make(Foo).a = %q, expected %q", foo3.a, "abc")
}
}
func TestBuildInterface(t *testing.T) {
c := NewContainer()
w := ResponseWriter{t: 1}
writer := InterfaceOf((*Writer)(nil))
c.RegisterAs(&w, writer)
// make interface
r := c.Make(writer).(*ResponseWriter)
if r.t != 1 {
t.Errorf("Make(Writer).t = %v, expected 1", r.t)
}
// make pointer
r2 := c.Make(reflect.TypeOf(&w)).(*ResponseWriter)
if r2 == nil {
t.Errorf("Make(*ResponseWriter) = nil, expected not nil")
}
// making an unregistered concrete struct should result in zero struct
r3 := c.Make(reflect.TypeOf(w)).(ResponseWriter)
if r3.t != 0 {
t.Errorf("Make(ResponseWriter).t = %v, expected 0", r3.t)
}
// making an unregistered interface should result in nil
r4 := c.Make(InterfaceOf((*Bar)(nil)))
if r4 != nil {
t.Errorf("Make(Bar) = %#v, expected nil", r4)
}
}
func TestBuildProvider(t *testing.T) {
// shared
c := NewContainer()
c.RegisterProvider(func(Container) reflect.Value {
return reflect.ValueOf(&Foo{"abc"})
}, reflect.TypeOf(&Foo{}), true)
foo1 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo1.a != "abc" {
t.Errorf("Make(*Foo).a = %q, expected %q", foo1.a, "abc")
}
foo1.a = "xyz"
foo2 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo1.a != foo2.a {
t.Errorf("Changed shared Foo, Make(*Foo).a = %q, expected %q", foo2.a, foo1.a)
}
// not shared
c = NewContainer()
c.RegisterProvider(func(Container) reflect.Value {
return reflect.ValueOf(&Foo{"abc"})
}, reflect.TypeOf(&Foo{}), false)
foo3 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo3.a != "abc" {
t.Errorf("Make(*Foo).a = %q, expected %q", foo3.a, "abc")
}
foo3.a = "xyz"
foo4 := c.Make(reflect.TypeOf(&Foo{})).(*Foo)
if foo3.a == foo4.a {
t.Errorf("Changed non-shared Foo, Make(*Foo).a = %q, expected %q", foo4.a, "abc")
}
}
func TestBuildInject(t *testing.T) {
barType := InterfaceOf((*Bar)(nil))
writerType := InterfaceOf((*Writer)(nil))
c := NewContainer()
c.Register(&Context{"abc"})
c.RegisterAs(&Foo{"123"}, barType)
c.RegisterAs(reflect.TypeOf(&ResponseWriter{}), writerType)
con := c.Make(reflect.TypeOf(Controller{})).(Controller)
if con.action != "" {
t.Errorf("Controller.action = %q, expected %q", con.action, "")
}
if con.data != "abc" {
t.Errorf("Controller.data = %q, expected %q", con.data, "abc")
}
if con.Request.Bar.test(0) != "123" {
t.Errorf("Controller.request.bar.test() = %q, expected %q", con.Request.Bar.test(0), "123")
}
}
func TestInject(t *testing.T) {
writerType := InterfaceOf((*Writer)(nil))
c := NewContainer()
c.Register(&Context{"abc"})
c.RegisterAs(&ResponseWriter{t: 123}, writerType)
c.RegisterAs(&Foo{"xyz"}, InterfaceOf((*Bar)(nil)))
con := &Controller{}
c.Inject(con)
// anonymous field injection
if con.data != "abc" {
t.Errorf("Controller.data = %q, expected %q", con.data, "abc")
}
// valid injection
if con.Response == nil {
t.Errorf("Controller.Response should not be nil")
}
if con.Response.(*ResponseWriter).t != 123 {
t.Errorf("Controller.Response.t = %q, expected %q", con.Response.(*ResponseWriter).t, 123)
}
// inject struct tag
if con.Response2 == nil {
t.Errorf("Controller.Response2 should not be nil")
}
// field not marked with "inject" should not be injected
if con.Response3 != nil {
t.Errorf("Controller.Response3 should be nil")
}
// unexported should not be injected
if con.response != nil {
t.Errorf("Controller.response should be nil")
}
// recursive injection
if con.Request.Bar == nil {
t.Errorf("Controller.Request.bar = nil, expected not nil")
}
if con.Request.Bar.(*Foo).a != "xyz" {
t.Errorf("Controller.Request.bar.(*Foo).a = %q, expected %q", con.Request.Bar.(*Foo).a, "xyz")
}
// nothing should happen for injecting a non-struct variable
c.Inject(1)
}
func TestCall(t *testing.T) {
writerType := InterfaceOf((*Writer)(nil))
c := NewContainer()
w := ResponseWriter{t: 1}
context := Context{"abc"}
c.RegisterAs(&w, writerType)
c.Register(&context)
f := func(cc *Context, w Writer, s string) (string, string, string) {
return cc.data, w.Write("test"), s
}
result := c.Call(f)
if len(result) != 3 {
t.Error("The return result should contain 3 values.")
}
if result[0] != "abc" || result[1] != "test" {
t.Errorf("The return result was (%q, %q, %q), expected (%q, %q, %q)", result[0], result[1], result[2], "abc", "test", "")
}
}
func TestParent(t *testing.T) {
c := NewContainer()
if c.ParentContainer() != nil {
t.Error("ParentContainer() != nil, expected nil")
}
f := c.Make(reflect.TypeOf(Foo{})).(Foo)
if f.a != "" {
t.Error("Build(Foo) returns non-zero Foo struct, expected zero")
}
p := NewContainer()
p.Register(Foo{"abc"})
c.SetParentContainer(p)
if c.ParentContainer() == nil {
t.Error("ParentContainer() == nil, expected not nil")
}
f = c.Make(reflect.TypeOf(Foo{})).(Foo)
if f.a != "abc" {
t.Errorf("Build(Foo).a=%q, expected %q", f.a, "abc")
}
}
func TestInterfaceOf(t *testing.T) {
defer func() {
if e := recover(); e == nil {
t.Errorf("Expected a panic when calling InterfaceOf() with a non-interface")
}
}()
InterfaceOf(reflect.TypeOf(Foo{}))
}