-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcore_dep_test.go
212 lines (202 loc) · 5.24 KB
/
core_dep_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
package gone
import (
"reflect"
"testing"
)
// TestCore_GetDepByName tests the getDepByName method
func TestCore_GetDepByName(t *testing.T) {
tests := []struct {
name string
setup func(*Core)
depName string
wantNil bool
wantType reflect.Type
}{
{
name: "Find existing named component",
setup: func(core *Core) {
_ = core.Load(&MockNamed{})
},
depName: "named-dep",
wantNil: false,
wantType: reflect.TypeOf(&MockNamed{}),
},
{
name: "Find default provider",
setup: func(core *Core) {
// Core itself is loaded as default provider
},
depName: DefaultProviderName,
wantNil: false,
wantType: reflect.TypeOf(&Core{}),
},
{
name: "Component not found",
setup: func(core *Core) {
_ = core.Load(&MockComponent{})
},
depName: "non-existent",
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := NewCore()
tt.setup(core)
coffin, err := core.getDepByName(tt.depName)
if tt.wantNil {
if err == nil {
t.Errorf("getDepByName(%q) expected error, got nil", tt.depName)
}
if coffin != nil {
t.Errorf("getDepByName(%q) expected nil coffin, got %v", tt.depName, coffin)
}
} else {
if err != nil {
t.Errorf("getDepByName(%q) unexpected error: %v", tt.depName, err)
}
if coffin == nil {
t.Errorf("getDepByName(%q) unexpected nil coffin", tt.depName)
} else if reflect.TypeOf(coffin.goner) != tt.wantType {
t.Errorf("getDepByName(%q) got type %v, want %v", tt.depName,
reflect.TypeOf(coffin.goner), tt.wantType)
}
}
})
}
}
// TestCore_GetDepByType tests the getDepByType method
func TestCore_GetDepByType(t *testing.T) {
tests := []struct {
name string
setup func(*Core)
targetType reflect.Type
wantNil bool
wantType reflect.Type
}{
{
name: "Find existing type",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
},
targetType: reflect.TypeOf(&MockDependency{}),
wantNil: false,
wantType: reflect.TypeOf(&MockDependency{}),
},
{
name: "Multiple implementations with default",
setup: func(core *Core) {
_ = core.Load(&MockDependency{}, Name("dep1"))
_ = core.Load(&MockDependency{}, IsDefault(), Name("dep2"))
},
targetType: reflect.TypeOf(&MockDependency{}),
wantNil: false,
wantType: reflect.TypeOf(&MockDependency{}),
},
{
name: "Interface type with implementation",
setup: func(core *Core) {
_ = core.Load(&MockInitiator{})
},
targetType: reflect.TypeOf((*Initiator)(nil)).Elem(),
wantNil: false,
wantType: reflect.TypeOf(&MockInitiator{}),
},
{
name: "Type not found",
setup: func(core *Core) {},
targetType: reflect.TypeOf(&MockComponent{}),
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := NewCore()
tt.setup(core)
coffin, err := core.getDepByType(tt.targetType)
if tt.wantNil {
if err == nil {
t.Errorf("getDepByType(%v) expected error, got nil", tt.targetType)
}
if coffin != nil {
t.Errorf("getDepByType(%v) expected nil coffin, got %v", tt.targetType, coffin)
}
} else {
if err != nil {
t.Errorf("getDepByType(%v) unexpected error: %v", tt.targetType, err)
}
if coffin == nil {
t.Errorf("getDepByType(%v) unexpected nil coffin", tt.targetType)
} else if reflect.TypeOf(coffin.goner) != tt.wantType {
t.Errorf("getDepByType(%v) got type %v, want %v", tt.targetType,
reflect.TypeOf(coffin.goner), tt.wantType)
}
}
})
}
}
// TestCore_GetSliceDepsByType tests the getSliceDepsByType method
func TestCore_GetSliceDepsByType(t *testing.T) {
tests := []struct {
name string
setup func(*Core)
targetType reflect.Type
wantCount int
}{
{
name: "No matching components",
setup: func(core *Core) {
_ = core.Load(&MockNamed{})
},
targetType: reflect.TypeOf([]*MockDependency{}),
wantCount: 0,
},
{
name: "Single matching component",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
},
targetType: reflect.TypeOf([]*MockDependency{}),
wantCount: 1,
},
{
name: "Multiple matching components",
setup: func(core *Core) {
_ = core.Load(&MockDependency{}, Name("dep1"))
_ = core.Load(&MockDependency{}, Name("dep2"))
_ = core.Load(&MockDependency{}, Name("dep3"))
},
targetType: reflect.TypeOf([]*MockDependency{}),
wantCount: 3,
},
{
name: "Interface type with multiple implementations",
setup: func(core *Core) {
_ = core.Load(&MockInitiator{})
_ = core.Load(&MockInitCombinations{})
},
targetType: reflect.TypeOf([]Initiator{}),
wantCount: 2,
},
{
name: "OnlyForName flag excludes component",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
_ = core.Load(&MockDependency{}, OnlyForName())
},
targetType: reflect.TypeOf([]*MockDependency{}),
wantCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := NewCore()
tt.setup(core)
deps := core.getSliceDepsByType(tt.targetType)
if len(deps) != tt.wantCount {
t.Errorf("getSliceDepsByType(%v) returned %d coffins, want %d",
tt.targetType, len(deps), tt.wantCount)
}
})
}
}