-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
153 lines (109 loc) · 2.95 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
package autoinject
import (
"testing"
)
type TestContainer_Service struct {
}
type TestContainer_InjectTest struct {
private string
OtherTag string `json:"other_tag"`
Foo string `autoinject:"test.string.foo"`
Service *TestContainer_Service `autoinject:"-"`
}
type TestContainer_Invalid struct {
Service TestContainer_Service `autoinject:"-"`
}
type TestContainer_MultiLevel struct {
Service *TestContainer_InjectTest `autoinject:"-"`
}
func TestContainer_AddParameter(t *testing.T) {
container := NewContainer()
container.AddParameter("test.randomInt", 42)
param, err := container.GetParameter("test.randomInt")
if 42 != param {
t.Error("Expected 42 got ", param)
}
if err != nil {
t.Error("No error expected")
}
}
func TestContainer_GetParameter(t *testing.T) {
container := NewContainer()
parameter, err := container.GetParameter("test.invalid")
if err == nil {
t.Error("Expected an error")
}
if parameter != nil {
t.Error("Expected nil got ", parameter)
}
}
func TestContainer_Register(t *testing.T) {
container := NewContainer()
container.Register("service", func() interface{} {
return &struct {}{}
})
service, err := container.Get("service")
if err != nil {
t.Error("Expected no error")
}
if service == nil {
t.Error("Expected not nil")
}
}
func TestContainer_Get(t *testing.T) {
container := NewContainer()
service, err := container.Get("invalid")
if err == nil {
t.Error("Expected error")
}
if service != nil {
t.Error("Expected nil service got ", service)
}
}
func TestContainer_AutoInject(t *testing.T) {
container := NewContainer()
container.AddParameter("test.string.foo", "Foo is a string")
container.Register("autoinject.TestContainer_Service", func() interface{} {
return new(TestContainer_Service)
})
testObj := new(TestContainer_InjectTest)
service, err := container.AutoInject(testObj)
if err != nil {
t.Error("Expected no error")
}
if service == nil {
t.Error("Expected service got nil")
}
}
func TestContainer_AutoInject2(t *testing.T) {
container := NewContainer()
container.AddParameter("test.string.foo", "Foo is a string")
container.Register("autoinject.TestContainer_Service", func() interface{} {
return new(TestContainer_Service)
})
container.Register("autoinject.TestContainer_InjectTest", func() interface{} {
return new(TestContainer_InjectTest)
})
testObj := new(TestContainer_MultiLevel)
service, err := container.AutoInject(testObj)
if err != nil {
t.Error("Expected no error")
}
if service == nil {
t.Error("Expected service to be not nil")
}
}
func TestContainer_AutoInject_ErrorOnNonPointer(t *testing.T) {
container := NewContainer()
container.Register("autoinject.TestContainer_Service", func() interface{} {
return new(TestContainer_Service)
})
testObj := new(TestContainer_Invalid)
service, err := container.AutoInject(testObj)
if err == nil {
t.Error("Expected an error")
}
if service != nil {
t.Error("Expected nil")
}
}