-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder_dependency_test.go
More file actions
62 lines (52 loc) · 1.42 KB
/
builder_dependency_test.go
File metadata and controls
62 lines (52 loc) · 1.42 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
package modular
import (
"testing"
)
type testDepModule struct {
name string
initSeq *[]string
}
func (m *testDepModule) Name() string { return m.name }
func (m *testDepModule) Init(app Application) error {
*m.initSeq = append(*m.initSeq, m.name)
return nil
}
func TestWithModuleDependency_OrdersModulesCorrectly(t *testing.T) {
seq := make([]string, 0)
modA := &testDepModule{name: "alpha", initSeq: &seq}
modB := &testDepModule{name: "beta", initSeq: &seq}
app, err := NewApplication(
WithLogger(nopLogger{}),
WithModules(modA, modB),
WithModuleDependency("alpha", "beta"),
)
if err != nil {
t.Fatalf("NewApplication: %v", err)
}
if err := app.Init(); err != nil {
t.Fatalf("Init: %v", err)
}
if len(seq) != 2 || seq[0] != "beta" || seq[1] != "alpha" {
t.Errorf("expected init order [beta, alpha], got %v", seq)
}
}
func TestWithModuleDependency_DetectsCycle(t *testing.T) {
modA := &testDepModule{name: "alpha", initSeq: new([]string)}
modB := &testDepModule{name: "beta", initSeq: new([]string)}
app, err := NewApplication(
WithLogger(nopLogger{}),
WithModules(modA, modB),
WithModuleDependency("alpha", "beta"),
WithModuleDependency("beta", "alpha"),
)
if err != nil {
t.Fatalf("NewApplication: %v", err)
}
err = app.Init()
if err == nil {
t.Fatal("expected circular dependency error")
}
if !IsErrCircularDependency(err) {
t.Errorf("expected ErrCircularDependency, got: %v", err)
}
}