Repository navigation
Expand file tree
/
Copy pathgraph_test.go
More file actions
98 lines (81 loc) · 1.96 KB
/
Copy pathgraph_test.go
File metadata and controls
98 lines (81 loc) · 1.96 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
94
95
96
97
98
package terraform
import (
"reflect"
"strings"
"testing"
)
func TestGraphAdd(t *testing.T) {
// Test Add since we override it and want to make sure we don't break it.
var g Graph
g.Add(42)
g.Add(84)
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphAddStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphConnectDependent(t *testing.T) {
var g Graph
g.Add(&testGraphDependable{VertexName: "a"})
b := g.Add(&testGraphDependable{
VertexName: "b",
DependentOnMock: []string{"a"},
})
if missing := g.ConnectDependent(b); len(missing) > 0 {
t.Fatalf("bad: %#v", missing)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testGraphConnectDepsStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}
func TestGraphReplace_DependableWithNonDependable(t *testing.T) {
var g Graph
a := g.Add(&testGraphDependable{VertexName: "a"})
b := g.Add(&testGraphDependable{
VertexName: "b",
DependentOnMock: []string{"a"},
})
newA := "non-dependable-a"
if missing := g.ConnectDependent(b); len(missing) > 0 {
t.Fatalf("bad: %#v", missing)
}
if !g.Replace(a, newA) {
t.Fatalf("failed to replace")
}
c := g.Add(&testGraphDependable{
VertexName: "c",
DependentOnMock: []string{"a"},
})
// This should fail by reporting missing, since a node with dependable
// name "a" is no longer in the graph.
missing := g.ConnectDependent(c)
expected := []string{"a"}
if !reflect.DeepEqual(expected, missing) {
t.Fatalf("expected: %#v, got: %#v", expected, missing)
}
}
type testGraphDependable struct {
VertexName string
DependentOnMock []string
}
func (v *testGraphDependable) Name() string {
return v.VertexName
}
func (v *testGraphDependable) DependableName() []string {
return []string{v.VertexName}
}
func (v *testGraphDependable) DependentOn() []string {
return v.DependentOnMock
}
const testGraphAddStr = `
42
84
`
const testGraphConnectDepsStr = `
a
b
a
`