Repository navigation
Expand file tree
/
Copy pathtree_test.go
More file actions
248 lines (212 loc) · 4.79 KB
/
Copy pathtree_test.go
File metadata and controls
248 lines (212 loc) · 4.79 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package tree
import (
"errors"
"testing"
"github.com/verless/verless/test"
)
type testNode struct {
children map[string]Node
}
// Children simply returns the entire children map.
func (t *testNode) Children() map[string]Node {
return t.children
}
// InitChild initializes a new child node on its own and registers
// it as a child linked to the current node by the given edge.
func (t *testNode) InitChild(edge string) {
child := &testNode{
children: make(map[string]Node),
}
t.children[edge] = child
}
// CreateChild registers a node as a child linked to the current
// node by the given edge.
func (t *testNode) CreateChild(edge string, node Node) {
t.children[edge] = node
}
var root = testNode{
children: map[string]Node{
"blog": &testNode{
children: map[string]Node{
"coffee": &testNode{},
},
},
"tags": &testNode{
children: map[string]Node{
"coffee": &testNode{},
},
},
},
}
// TestCreateNode checks if the CreateNode registers all nodes
// under the given tree paths correctly.
func TestCreateNode(t *testing.T) {
tests := map[string]struct {
path string
}{
"root path": {
path: RootPath,
},
"path with depth 1": {
path: "/blog",
},
"path with depth 2": {
path: "/blog/coffee",
},
}
for name, testCase := range tests {
t.Log(name)
root := &testNode{
children: make(map[string]Node),
}
err := CreateNode(testCase.path, root, &testNode{})
test.Ok(t, err)
var n Node = root
for _, edge := range Edges(testCase.path) {
_, exists := n.Children()[edge]
test.Equals(t, true, exists)
n = n.Children()[edge]
}
}
}
// TestResolveOrInitNode tests if the ResolveOrInitNode function
// correctly initializes all nodes in a given path.
func TestResolveOrInitNode(t *testing.T) {
tests := map[string]struct {
path string
}{
"existing path with depth 1": {
path: "/blog",
},
"existing path with depth 2": {
path: "/blog/coffee",
},
"not existing path": {
path: "/blog/espresso",
},
}
for name, testCase := range tests {
t.Log(name)
root := &testNode{
children: make(map[string]Node),
}
node, err := ResolveOrInitNode(testCase.path, root)
test.Ok(t, err)
test.NotEquals(t, nil, node)
var n Node = root
for _, edge := range Edges(testCase.path) {
_, exists := n.Children()[edge]
test.Equals(t, true, exists)
n = n.Children()[edge]
}
}
}
// TestResolveNode checks if the ResolveNode function can resolve
// nodes in a pre-defined tree correctly and returns the expected
// errors for non-existing paths.
func TestResolveNode(t *testing.T) {
tests := map[string]struct {
path string
expectedError error
}{
"root path": {
path: RootPath,
},
"path with depth 1": {
path: "/blog",
},
"path with depth 2": {
path: "/blog/coffee",
},
"not existing path": {
path: "/coffee",
expectedError: ErrEdgeNotFound,
},
}
for name, testCase := range tests {
t.Log(name)
_, err := ResolveNode(testCase.path, &root)
if test.ExpectedError(t, testCase.expectedError, err) != test.IsCorrectNil {
continue
}
}
}
// TestWalk checks if the Walk function invokes the walkFn for as
// many nodes as expected, depending on a given maxWidth.
func TestWalk(t *testing.T) {
testErr := errors.New("this is a test error")
tests := map[string]struct {
depth int
count int
expectedError error
}{
"nodes with max depth 1": {
depth: 1,
count: 3,
},
"nodes with max depth 2": {
depth: 2,
count: 5,
},
"all nodes": {
depth: -1,
count: 5,
},
"with error": {
depth: -1,
count: 0,
expectedError: testErr,
},
}
for name, testCase := range tests {
t.Log(name)
count := 0
err := Walk(&root, func(_ string, node Node) error {
count++
return testCase.expectedError
}, testCase.depth)
if test.ExpectedError(t, testCase.expectedError, err) != test.IsCorrectNil {
continue
}
test.Equals(t, testCase.count, count)
}
}
// TestWalkPath tests if the WalkPath correctly invokes the
// walkFn for each node in a given path.
func TestWalkPath(t *testing.T) {
testErr := errors.New("this is a test error")
tests := map[string]struct {
path string
count int
expectedError error
}{
"root path": {
path: RootPath,
count: 1,
},
"path with depth 1": {
path: "/blog",
count: 2,
},
"path with depth 2": {
path: "/blog/coffee",
count: 3,
},
"not existing path": {
path: "/blog/espresso",
expectedError: testErr,
},
}
for name, testCase := range tests {
t.Log(name)
count := 0
err := WalkPath(testCase.path, &root, func(node Node) error {
count++
return testCase.expectedError
})
if test.ExpectedError(t, testCase.expectedError, err) != test.IsCorrectNil {
continue
}
test.Equals(t, testCase.count, count)
}
}