-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree_test.go
158 lines (128 loc) · 4.43 KB
/
tree_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package rmtool
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWalkTree(t *testing.T) {
assert := assert.New(t)
root := sampleTree()
actual := make([]string, 0)
root.Walk(func(n *Node) error {
actual = append(actual, n.ID())
return nil
})
expected := []string{"root", "a0", "a1", "a2", "b0", "c0", "c1", "c2"}
assert.ElementsMatch(expected, actual, "not every node was visited")
// error from visitor function should be returned
expectedErr := errors.New("some error")
// and walk should abort on error
counter := 0
actualErr := root.Walk(func(n *Node) error {
counter++
return expectedErr
})
assert.Equal(expectedErr, actualErr)
assert.Equal(1, counter)
}
func TestMatchName(t *testing.T) {
assert := assert.New(t)
n := node("foobar", "Foo Bar", DocumentType)
assert.True(MatchName("Foo Bar")(n), "exact match")
assert.True(MatchName("foo")(n), "partial match")
assert.True(MatchName("bar")(n), "partial match")
assert.True(MatchName("oo")(n), "partial match")
assert.False(MatchName("not foo")(n), "no match")
assert.True(MatchName("")(n), "empty string should match all")
}
func TestMatchType(t *testing.T) {
assert := assert.New(t)
doc := node("foo", "Foo", DocumentType)
assert.True(IsDocument(doc))
assert.False(IsFolder(doc))
folder := node("bar", "Bar", CollectionType)
assert.False(IsDocument(folder))
assert.True(IsFolder(folder))
}
func TestFilterTree(t *testing.T) {
assert := assert.New(t)
root := sampleTree()
// return matching docs AND folders on path up to root
f := root.Filtered(MatchName("c"))
assert.Equal("root", f.ID(), "root node was lost in filter")
assert.Equal(1, len(f.Children), "intermediate folder lost in filter")
assert.Equal(3, len(f.Children[0].Children), "matching nodes incomplete")
// multiple matches
f = root.Filtered(IsDocument, MatchName("0"))
assert.Equal(2, len(f.Children))
assert.Equal("a0", f.Children[0].ID())
assert.Equal("b0", f.Children[1].ID())
assert.Equal(1, len(f.Children[1].Children))
assert.Equal("c0", f.Children[1].Children[0].ID())
}
func TestSortTree(t *testing.T) {
assert := assert.New(t)
root := sampleTree()
assert.Equal(root.Children[0].ID(), "a0", "precondition failed")
assert.Equal(root.Children[3].ID(), "b0", "precondition failed")
root.Sort(DefaultSort)
assert.Equal(root.Children[0].ID(), "b0", "Folders must come before documents")
assert.Equal(root.Children[1].ID(), "a0", "documents by name")
}
func TestTreePath(t *testing.T) {
assert := assert.New(t)
root := sampleTree()
assert.Equal(root.Path(), []string{})
assert.Equal(root.Children[0].Path(), []string{"root"})
assert.Equal(root.Children[3].Children[0].Path(), []string{"root", "b0"})
// filter and sort should not disturb paths of remaining elements
root = root.Filtered(MatchName("c0"))
root.Sort(DefaultSort)
assert.Equal(root.Path(), []string{})
assert.Equal(root.Children[0].Path(), []string{"root"})
assert.Equal(root.Children[0].Children[0].Path(), []string{"root", "b0"})
}
func TestMatchPath(t *testing.T) {
assert := assert.New(t)
root := sampleTree()
assert.True(MatchPath("")(root), "empty path matches root folder")
assert.True(MatchPath("a0")(root.Children[0]), "plain name matches item in root")
assert.True(MatchPath("b0")(root.Children[3]), "match folder")
assert.True(MatchPath("b0/c1")(root.Children[3].Children[1]), "multi-level match")
assert.False(MatchPath("b0/c1/xx")(root.Children[3].Children[1]), "non-existing name")
assert.False(MatchPath("xx/c1")(root.Children[3].Children[1]), "non-existing first level")
assert.False(MatchPath("xx")(root.Children[0]), "non-existing name first level")
// normalize
assert.True(MatchPath("/b0/c1")(root.Children[3].Children[1]), "leading / ignored")
assert.True(MatchPath("b0/c1/")(root.Children[3].Children[1]), "trailing / ignored")
assert.True(MatchPath("b0///c1")(root.Children[3].Children[1]), "multiple // ignored")
}
// creates a tree like this:
//
// root
// |
// +- a0
// +- a1
// +- a2
// ´- b0
// |
// +- c0
// +- c1
// ´- c2
func sampleTree() *Node {
root := node("root", "root", CollectionType)
a := []string{"a0", "a1", "a2"}
for _, id := range a {
root.addChild(node(id, id, DocumentType))
}
b := node("b0", "b0", CollectionType)
root.addChild(b)
c := []string{"c0", "c1", "c2"}
for _, id := range c {
b.addChild(node(id, id, DocumentType))
}
return root
}
func node(id, name string, t NotebookType) *Node {
return newNode(&nodeMeta{id, "", name, t})
}