-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcore_coffin_test.go
153 lines (145 loc) · 3.75 KB
/
core_coffin_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
package gone
import (
"reflect"
"testing"
)
// TestCore_GetCoffinsByType tests the getCoffinsByType method
func TestCore_GetCoffinsByType(t *testing.T) {
tests := []struct {
name string
setup func(*Core)
targetType reflect.Type
wantCount int
}{
{
name: "No matching coffins",
setup: func(core *Core) {
// Load components that don't match the target type
_ = core.Load(&MockNamed{})
},
targetType: reflect.TypeOf(&MockDependency{}),
wantCount: 0,
},
{
name: "Single matching coffin",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
},
targetType: reflect.TypeOf(&MockDependency{}),
wantCount: 1,
},
{
name: "Multiple matching coffins",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
_ = core.Load(&MockDependency{})
_ = core.Load(&MockDependency{}, Name("named-dep"))
},
targetType: reflect.TypeOf(&MockDependency{}),
wantCount: 3,
},
{
name: "OnlyForName flag excludes coffin",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
_ = core.Load(&MockDependency{}, OnlyForName())
},
targetType: reflect.TypeOf(&MockDependency{}),
wantCount: 1,
},
{
name: "Interface type matching",
setup: func(core *Core) {
_ = core.Load(&MockInitiator{})
_ = core.Load(&MockInitCombinations{})
},
targetType: reflect.TypeOf((*Initiator)(nil)).Elem(),
wantCount: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := NewCore()
tt.setup(core)
coffins := core.getCoffinsByType(tt.targetType)
if len(coffins) != tt.wantCount {
t.Errorf("getCoffinsByType() returned %d coffins, want %d", len(coffins), tt.wantCount)
}
})
}
}
// TestCore_GetDefaultCoffinByType tests the getDefaultCoffinByType method
func TestCore_GetDefaultCoffinByType(t *testing.T) {
tests := []struct {
name string
setup func(*Core)
targetType reflect.Type
wantNil bool
wantDefault bool
}{
{
name: "No matching coffins",
setup: func(core *Core) {
// Load components that don't match the target type
_ = core.Load(&MockNamed{})
},
targetType: reflect.TypeOf(&MockDependency{}),
wantNil: true,
wantDefault: false,
},
{
name: "Single matching coffin",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
},
targetType: reflect.TypeOf(&MockDependency{}),
wantNil: false,
wantDefault: false,
},
{
name: "Multiple coffins with default",
setup: func(core *Core) {
_ = core.Load(&MockDependency{})
_ = core.Load(&MockDependency{}, IsDefault())
},
targetType: reflect.TypeOf(&MockDependency{}),
wantNil: false,
wantDefault: true,
},
{
name: "Multiple coffins without default",
setup: func(core *Core) {
_ = core.Load(&MockDependency{}, Name("dep1"))
_ = core.Load(&MockDependency{}, Name("dep2"))
},
targetType: reflect.TypeOf(&MockDependency{}),
wantNil: false,
wantDefault: false,
},
{
name: "Interface type with default implementation",
setup: func(core *Core) {
_ = core.Load(&MockInitiator{})
_ = core.Load(&MockInitCombinations{}, IsDefault((*Initiator)(nil)))
},
targetType: reflect.TypeOf((*Initiator)(nil)).Elem(),
wantNil: false,
wantDefault: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core := NewCore()
tt.setup(core)
coffin := core.getDefaultCoffinByType(tt.targetType)
if (coffin == nil) != tt.wantNil {
t.Errorf("getDefaultCoffinByType() returned nil: %v, want nil: %v", coffin == nil, tt.wantNil)
}
if !tt.wantNil && tt.wantDefault {
if !coffin.isDefault(tt.targetType) {
t.Errorf("getDefaultCoffinByType() did not return the default coffin")
}
}
})
}
}