-
Notifications
You must be signed in to change notification settings - Fork 0
/
katana_test.go
362 lines (288 loc) · 9.64 KB
/
katana_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
package katana_test
import (
"github.com/drborges/katana"
"github.com/smartystreets/assertions/should"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
type Dependency struct {
Field string
}
type DependencyA struct {
Dep *Dependency
}
type DependencyB struct {
Dep *DependencyA
}
type DependencyC struct {
Dep *DependencyD
}
type DependencyD struct {
Dep *DependencyC
}
type InterfaceDependency interface {
DoStuff()
}
type InterfaceDependencyImpl struct {
Field string
}
func (dep *InterfaceDependencyImpl) DoStuff() {}
func TestKatanaProvide(t *testing.T) {
Convey("Given I have an instance of katana injector with a few value providers", t, func() {
depA := &Dependency{}
depB := Dependency{}
injector := katana.New().Provide(depA, depB)
Convey("When I resolve instances of the provided values", func() {
var depA1, depA2 *Dependency
var depB1, depB2 Dependency
injector.Resolve(&depA1, &depA2, &depB1, &depB2)
Convey("Then instances of the same type are the same", func() {
So(depA1, should.NotBeNil)
So(depA2, should.NotBeNil)
So(depA1, should.Equal, depA2)
So(depB1, should.NotBeNil)
So(depB2, should.NotBeNil)
So(depB1, should.NotEqual, depB2)
So(depB1, should.Resemble, depB2)
})
})
})
}
func TestKatanaProvideAs(t *testing.T) {
Convey("Given I have an instance of katana injector with an instance provided as an interface type", t, func() {
dep := &InterfaceDependencyImpl{}
injector := katana.New().ProvideAs((*InterfaceDependency)(nil), dep)
Convey("When I resolve a reference to the provided dependency", func() {
var depA InterfaceDependency
injector.Resolve(&depA)
Convey("Then the reference is successfully resolved", func() {
So(depA, should.NotBeNil)
So(depA, should.Equal, dep)
})
})
})
}
func TestKatanaProvideNewInstance(t *testing.T) {
Convey("Given I have an instance of katana injector with a new instance provider of a dependency", t, func() {
injector := katana.New().ProvideNew(&Dependency{}, func() *Dependency {
return &Dependency{}
})
Convey("When I resolve multiple instances of the provided dependency", func() {
var dep1, dep2 *Dependency
injector.Resolve(&dep1, &dep2)
Convey("Then the resolved dependnecies points to different memory address", func() {
So(dep1, should.NotEqual, dep2)
})
})
})
Convey("Given I have two new instance providers one for *Dependency and another one for *DependencyB", t, func() {
injector := katana.New().ProvideNew(&Dependency{}, func() *Dependency {
return &Dependency{}
})
injector.ProvideNew(&DependencyA{}, func(dep *Dependency) *DependencyA {
return &DependencyA{dep}
})
Convey("When I resolve multiple instances of *DependencyB which depends on *Dependency", func() {
var dep1, dep2 *DependencyA
injector.Resolve(&dep1, &dep2)
Convey("Then the resolved dependencies point to different memory addresses", func() {
So(dep1, should.NotEqual, dep2)
Convey("And its dependencies also point to different memory addresses", func() {
So(dep1.Dep, should.NotBeNil)
So(dep2.Dep, should.NotBeNil)
So(dep1.Dep, should.NotEqual, dep2.Dep)
})
})
})
})
}
func TestKatanaProvidesSingletonInstance(t *testing.T) {
Convey("Given I have an instance of katana injector with a singleton dependency provider", t, func() {
injector := katana.New().ProvideSingleton(&Dependency{}, func() *Dependency {
return &Dependency{}
})
Convey("When I resolve multiple instances of the provided dependency", func() {
var dep1, dep2 *Dependency
injector.Resolve(&dep1, &dep2)
Convey("Then the resolved dependencies points to the same memory address", func() {
So(dep1, should.Equal, dep2)
})
})
})
}
func TestKatanaProvidesSingletonInstanceOfInterfaceType(t *testing.T) {
Convey("Given I have a provider of an interface dependency type", t, func() {
injector := katana.New().ProvideSingleton((*InterfaceDependency)(nil), func() InterfaceDependency {
return &InterfaceDependencyImpl{}
})
Convey("When I resolve multiple instances of the provided dependency", func() {
var dep1, dep2 InterfaceDependency
injector.Resolve(&dep1, &dep2)
Convey("Then the dependencies are resolved", func() {
So(dep1, should.Equal, dep2)
So(dep1, should.HaveSameTypeAs, &InterfaceDependencyImpl{})
So(dep2, should.HaveSameTypeAs, &InterfaceDependencyImpl{})
})
})
})
}
func TestKatanaProvidesNewInstanceOfInterfaceType(t *testing.T) {
Convey("Given I have a provider of an interface dependency type", t, func() {
injector := katana.New().ProvideNew((*InterfaceDependency)(nil), func() InterfaceDependency {
return &InterfaceDependencyImpl{}
})
Convey("When I resolve multiple instances of the provided dependency", func() {
var dep1, dep2 InterfaceDependency
injector.Resolve(&dep1, &dep2)
Convey("Then the dependencies are resolved", func() {
So(dep1, should.NotEqual, dep2)
So(dep1, should.HaveSameTypeAs, &InterfaceDependencyImpl{})
So(dep2, should.HaveSameTypeAs, &InterfaceDependencyImpl{})
})
})
})
}
func TestKatanaResolvesTransitiveDependencies(t *testing.T) {
Convey("Given I have transitive dependencies", t, func() {
injector := katana.New().ProvideNew(&DependencyB{}, func(dep *DependencyA) *DependencyB {
return &DependencyB{dep}
})
injector.ProvideNew(&DependencyA{}, func(dep *Dependency) *DependencyA {
return &DependencyA{dep}
})
injector.Provide(&Dependency{})
Convey("When I resolve the root dep", func() {
var depA *DependencyB
injector.Resolve(&depA)
Convey("Then all dependencies are resolved recursively", func() {
So(depA, should.NotBeNil)
So(depA.Dep, should.NotBeNil)
So(depA.Dep.Dep, should.NotBeNil)
})
})
})
}
type DepA struct {
depB *DepB
depD *DepD
}
type DepB struct {
field string
}
type DepC struct {
dep *DepA
}
type DepD struct {
dep *DepC
}
func TestErrCyclicDependency(t *testing.T) {
Convey("Given I have cyclic dependencies", t, func() {
injector := katana.New().ProvideNew(&DepA{}, func(depB *DepB, depD *DepD) *DepA {
return &DepA{depB, depD}
})
injector.ProvideNew(&DepB{}, func() *DepB {
return &DepB{}
})
injector.ProvideNew(&DepC{}, func(dep *DepA) *DepC {
return &DepC{dep}
})
injector.ProvideNew(&DepD{}, func(dep *DepC) *DepD {
return &DepD{dep}
})
Convey("When I resolve the cyclic dependency", func() {
var dep *DepA
resolveWithCyclicDependency := func() {
injector.Resolve(&dep)
}
Convey("Then all dependencies are resolved recursively", func() {
So(resolveWithCyclicDependency, should.Panic)
})
})
})
}
func TestErrInvalidProvider(t *testing.T) {
// TODO write test case to catch whether the error is properly built
Convey("Given I have a provider function with no return value for a given dependency", t, func() {
invalidProvider := func() {
katana.New().ProvideNew(&DependencyC{}, func() {})
}
Convey("Then it fails with an invalid provider error", func() {
So(invalidProvider, should.Panic)
})
})
Convey("Given I have a provider function with multiple return values for a given dependency", t, func() {
invalidProvider := func() {
katana.New().ProvideNew(&DependencyC{}, func() (*DependencyC, error) {
return nil, nil
})
}
Convey("Then it fails with an invalid provider error", func() {
So(invalidProvider, should.Panic)
})
})
}
func TestErrProviderAlreadyRegistered(t *testing.T) {
Convey("Given I have a provider registered for a given dependency", t, func() {
injector := katana.New().ProvideNew(&DependencyC{}, func() *DependencyC {
return &DependencyC{}
})
Convey("When I register another provider for that same dependency type", func() {
alreadyRegisteredProvider := func() {
injector.ProvideNew(&DependencyC{}, func() *DependencyC {
return &DependencyC{}
})
}
Convey("Then it fails with an already registered provider", func() {
So(alreadyRegisteredProvider, should.Panic)
})
})
})
}
func TestErrInvalidReference(t *testing.T) {
Convey("Given I have a provider registered for a given dependency", t, func() {
injector := katana.New().ProvideNew(Dependency{}, func() *Dependency {
return &Dependency{}
})
Convey("Then it panics resolving a reference that is a zero value", func() {
var ref *Dependency
resolvingZeroValue := func() { injector.Resolve(ref) }
So(resolvingZeroValue, should.Panic)
})
})
}
func TestInjectorClone(t *testing.T) {
Convey("Given I have an injector with a few injectables and cached instances", t, func() {
injector := katana.New()
injector.Provide(&DependencyB{})
injector.ProvideNew(&Dependency{}, func() *Dependency {
return &Dependency{}
})
injector.ProvideSingleton(&DependencyA{}, func(dep *Dependency) *DependencyA {
return &DependencyA{dep}
})
Convey("When I clone the injector", func() {
newInjector := injector.Clone()
Convey("Then the new injector inherits the original injector providers", func() {
var dep *Dependency
var depA *DependencyA
var depB *DependencyB
newInjector.Resolve(&dep, &depA, &depB)
So(dep, should.NotBeNil)
So(depA, should.NotBeNil)
So(depB, should.NotBeNil)
})
Convey("And I register new providers with the new injector", func() {
newInjector.ProvideSingleton((*InterfaceDependency)(nil), func() InterfaceDependency {
return &InterfaceDependencyImpl{}
})
Convey("Then the provider is available only in the cloned injector", func() {
var dep1, dep2 InterfaceDependency
newInjector.Resolve(&dep1)
So(dep1, should.NotBeNil)
resolveWithOriginalInjector := func () { injector.Resolve(&dep2) }
So(resolveWithOriginalInjector, should.Panic)
})
})
})
})
}