-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication_module_mgmt_test.go
More file actions
93 lines (85 loc) · 2.34 KB
/
application_module_mgmt_test.go
File metadata and controls
93 lines (85 loc) · 2.34 KB
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
package modular
import (
"testing"
)
// Test_ResolveDependencies tests module dependency resolution
func Test_ResolveDependencies(t *testing.T) {
tests := []struct {
name string
modules []Module
wantErr bool
errCheck func(error) bool
checkOrder func([]string) bool
}{
{
name: "Simple dependency chain",
modules: []Module{
&testModule{name: "module-c", dependencies: []string{"module-b"}},
&testModule{name: "module-b", dependencies: []string{"module-a"}},
&testModule{name: "module-a", dependencies: []string{}},
},
wantErr: false,
checkOrder: func(order []string) bool {
// Ensure module-a comes before module-b and module-b before module-c
aIdx := -1
bIdx := -1
cIdx := -1
for i, name := range order {
switch name {
case "module-a":
aIdx = i
case "module-b":
bIdx = i
case "module-c":
cIdx = i
}
}
return aIdx < bIdx && bIdx < cIdx
},
},
{
name: "Circular dependency",
modules: []Module{
&testModule{name: "module-a", dependencies: []string{"module-b"}},
&testModule{name: "module-b", dependencies: []string{"module-a"}},
},
wantErr: true,
errCheck: IsCircularDependencyError,
},
{
name: "Missing dependency",
modules: []Module{
&testModule{name: "module-a", dependencies: []string{"non-existent"}},
},
wantErr: true,
errCheck: IsModuleDependencyMissingError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := &StdApplication{
cfgProvider: NewStdConfigProvider(testCfg{Str: "test"}),
cfgSections: make(map[string]ConfigProvider),
svcRegistry: make(ServiceRegistry),
moduleRegistry: make(ModuleRegistry),
logger: &logger{t},
}
// Register modules
for _, module := range tt.modules {
app.RegisterModule(module)
}
// Resolve dependencies
order, _, err := app.resolveDependencies()
if (err != nil) != tt.wantErr {
t.Errorf("resolveDependencies() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && tt.errCheck != nil && !tt.errCheck(err) {
t.Errorf("resolveDependencies() expected specific error, got %v", err)
}
if !tt.wantErr && tt.checkOrder != nil && !tt.checkOrder(order) {
t.Errorf("resolveDependencies() returned incorrect order: %v", order)
}
})
}
}