-
Notifications
You must be signed in to change notification settings - Fork 16
/
graph_test.go
144 lines (115 loc) · 3.03 KB
/
graph_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
package main
import (
"strings"
"testing"
)
func TestSingleNode(t *testing.T) {
graph := NewGraph()
dep := &Dep{Import: "github.com"}
graph.Insert(dep)
root := graph.Nodes["github.com"]
if root == nil {
t.Error("Expected root not to be nil")
}
if root.Dependency != dep {
t.Errorf("Expected Node.Dependency to be %v", dep)
}
if root.Leaf == false {
t.Errorf("Expected root to be also a leaf")
}
}
func TestGraphWithSeveralRoots(t *testing.T) {
graph := NewGraph()
dep1 := &Dep{Import: "github.com"}
dep2 := &Dep{Import: "code.google.com"}
graph.Insert(dep1)
graph.Insert(dep2)
root := graph.Nodes["github.com"]
if root == nil {
t.Error("Expected root not to be nil")
}
root = graph.Nodes["code.google.com"]
if root == nil {
t.Error("Expected root not to be nil")
}
}
func TestDeepGraph(t *testing.T) {
graph := NewGraph()
dep1 := &Dep{Import: "github.com/d2fn/gopack"}
dep2 := &Dep{Import: "code.google.com/p/go.net"}
graph.Insert(dep1)
graph.Insert(dep2)
testTree(dep1, graph, t)
testTree(dep2, graph, t)
}
func testTree(dep *Dep, graph *Graph, t *testing.T) {
nodes := graph.Nodes
keys := strings.Split(dep.Import, "/")
for idx, key := range keys {
node := nodes[key]
if node == nil {
t.Error("Expected node to not be nil")
}
if idx < len(keys)-1 {
if node.Leaf == true {
t.Error("Expected leaf to not be a leaf")
}
if node.Dependency != nil {
t.Errorf("Expected node to not store the dependency")
}
nodes = node.Nodes
} else {
if node.Leaf == false {
t.Error("Expected node to be a leaf")
}
if node.Dependency != dep {
t.Errorf("Expected node to store the dependency")
}
}
}
}
func TestSearchFailsWithNoNodes(t *testing.T) {
graph := NewGraph()
node := graph.Search("github.com/d2fn/gopack")
if node != nil {
t.Error("Expected search to fail when there are no nodes")
}
}
func TestSearchFailsWithDifferentNodes(t *testing.T) {
graph := NewGraph()
dep := &Dep{Import: "github.com/d2fn/gopack"}
graph.Insert(dep)
node := graph.Search("github.com/dotcloud/docker")
if node != nil {
t.Error("Expected search to fail when the dependency doesn't exist")
}
}
func TestSearchWorksWithBareNames(t *testing.T) {
graph := NewGraph()
dep := &Dep{Import: "github.com/d2fn/gopack"}
graph.Insert(dep)
node := graph.Search("github.com/d2fn/gopack")
if node == nil {
t.Error("Expected search to succeed importing bare repos")
}
}
func TestSearchWorksWithExtendedNames(t *testing.T) {
graph := NewGraph()
dep := &Dep{Import: "github.com/d2fn/gopack"}
graph.Insert(dep)
node := graph.Search("github.com/d2fn/gopack/graph")
if node.Dependency != dep {
t.Error("Expected search to succeed importing extended repos")
}
}
func TestInsertingLeafs(t *testing.T) {
graph := NewGraph()
dep := &Dep{Import: "github.com/d2fn/gopack"}
graph.Insert(dep)
if graph.Leafs.Len() == 0 {
t.Fatal("Expected to have one leaf")
}
if graph.Leafs.Front().Value != "github.com/d2fn/gopack" {
t.Fatal("Expected to have github.com/d2fn/gopack in the list of leafs")
}
}