-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjection_test.go
262 lines (232 loc) · 5.22 KB
/
injection_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
package genjector
import (
"errors"
"reflect"
"testing"
)
func TestKey_Generate(t *testing.T) {
key := Key{}
generated := key.Generate()
if generated != nil {
t.Errorf("expected nil, got %v", generated)
}
key = Key{
Annotation: "annotation",
}
generated = key.Generate()
if !reflect.DeepEqual(generated, [2]interface{}{"annotation", nil}) {
t.Errorf("expected concrete value, got %v", generated)
}
key = Key{
Annotation: "annotation",
Value: "value",
}
generated = key.Generate()
if !reflect.DeepEqual(generated, [2]interface{}{"annotation", "value"}) {
t.Errorf("expected concrete value, got %v", generated)
}
}
func TestNewContainer(t *testing.T) {
container := NewContainer()
if !reflect.DeepEqual(container, Container{}) {
t.Errorf("expected concrete value, got %v", container)
}
}
func TestBind_bindingError(t *testing.T) {
err := Bind[int](&testBindingSource{
binding: func() (Binding, error) {
return nil, errors.New("error")
},
key: func() Key {
return Key{}
},
})
if err == nil {
t.Error("expected error, got nil")
}
}
func TestBind_bindingOptionError(t *testing.T) {
err := Bind[int](&testBindingSource{
binding: func() (Binding, error) {
return &testBinding{}, nil
},
key: func() Key {
return Key{}
},
}, &testBindingOption{
binding: func(binding Binding) (Binding, error) {
return nil, errors.New("error")
},
key: func(key Key) Key {
return key
},
container: func(container Container) Container {
return NewContainer()
},
})
if err == nil {
t.Error("expected error, got nil")
}
}
func TestBind_binding_success(t *testing.T) {
inner := NewContainer()
if !reflect.DeepEqual(inner, Container{}) {
t.Errorf("expected concrete value, got %v", inner)
}
err := Bind[int](&testBindingSource{
binding: func() (Binding, error) {
return &testBinding{}, nil
},
key: func() Key {
return Key{
Annotation: "first",
Value: "1",
}
},
}, &testBindingOption{
binding: func(binding Binding) (Binding, error) {
binding.(*testBinding).value = "value"
return binding, nil
},
key: func(key Key) Key {
return Key{
Annotation: key.Annotation + "second",
Value: key.Value.(string) + "2",
}
},
container: func(Container) Container {
return inner
},
})
if err != nil {
t.Errorf("expected nil, got error %v", err)
}
if !reflect.DeepEqual(inner, Container{
[2]interface{}{
"firstsecond",
"12",
}: &testBinding{
value: "value",
},
}) {
t.Errorf("expected concrete value, got %v", inner)
}
}
func TestMustBind(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("the code did not panic")
}
}()
MustBind[int](&testBindingSource{
binding: func() (Binding, error) {
return nil, errors.New("error")
},
key: func() Key {
return Key{}
},
})
}
func TestNewInstance_defaultValue(t *testing.T) {
instance, err := NewInstance[testStruct]()
if err != nil {
t.Errorf("expected nil, got error %s", err)
}
if !reflect.DeepEqual(instance, testStruct{
a: "test",
b: 10,
}) {
t.Errorf("expected concrete value, got %v", instance)
}
}
func TestNewInstance_defaultPointer(t *testing.T) {
instance, err := NewInstance[*testStruct]()
if err != nil {
t.Errorf("expected nil, got error %s", err)
}
if instance != nil {
t.Errorf("expected nil, got %v", instance)
}
}
func TestNewInstance_defaultError(t *testing.T) {
instance, err := NewInstance[interface{}]()
if err == nil {
t.Error("expected error, got nil")
}
if instance != nil {
t.Errorf("expected nil, got %v", instance)
}
}
func TestNewInstance_success(t *testing.T) {
inner := Container{
(*int)(nil): &testBinding{
instance: func(initialize bool) (interface{}, error) {
return nil, errors.New("error")
},
},
}
instance, err := NewInstance[int](WithContainer(inner))
if err == nil {
t.Error("expected error, got nil")
}
if instance != 0 {
t.Errorf("expected 0, got %v", instance)
}
}
func TestNewInstance_invalid(t *testing.T) {
inner := Container{
(*int)(nil): &testBinding{
instance: func(initialize bool) (interface{}, error) {
return "value", nil
},
},
}
instance, err := NewInstance[int](WithContainer(inner))
if err == nil {
t.Error("expected error, got nil")
}
if instance != 0 {
t.Errorf("expected 0, got %v", instance)
}
}
func TestNewInstance_simple_success(t *testing.T) {
inner := Container{
(*int)(nil): &testBinding{
instance: func(initialize bool) (interface{}, error) {
return 10, nil
},
},
}
instance, err := NewInstance[int](WithContainer(inner))
if err != nil {
t.Errorf("expected nil, got error %s", err)
}
if instance != 10 {
t.Errorf("expected 10, got %v", instance)
}
}
func TestNewInstance_complex_success(t *testing.T) {
inner := Container{
[2]interface{}{"annotation", nil}: &testBinding{
instance: func(initialize bool) (interface{}, error) {
return 10, nil
},
},
}
instance, err := NewInstance[int](&testKeyOption{
key: func(key Key) Key {
return Key{
Annotation: "annotation",
}
},
container: func(container Container) Container {
return inner
},
})
if err != nil {
t.Errorf("expected nil, got error %s", err)
}
if instance != 10 {
t.Errorf("expected 10, got %v", instance)
}
}