-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiletree_test.go
136 lines (104 loc) · 3.48 KB
/
filetree_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
package resonatefuse
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateChild(t *testing.T) {
childName := "joe"
root := NewDirectory("root", nil)
// Add child that does not prevously exist
assert.Nil(t, root.children[childName])
assert.Nil(t, root.CreateChild(childName))
assert.NotNil(t, root.children[childName])
assert.Equal(t, root, root.children[childName].parent)
// Add child that does already exist
assert.NotNil(t, root.children[childName])
assert.Nil(t, root.CreateChild(childName))
assert.NotNil(t, root.children[childName])
assert.Equal(t, root, root.children[childName].parent)
}
func TestCreateDirChild(t *testing.T) {
childName := "joe"
root := NewDirectory("root", nil)
// Add child that does not prevously exist
assert.Nil(t, root.children[childName])
assert.Nil(t, root.CreateDirChild(childName))
assert.NotNil(t, root.children[childName])
assert.Equal(t, root, root.children[childName].parent)
// Add child that does already exist
assert.NotNil(t, root.children[childName])
assert.Nil(t, root.CreateDirChild(childName))
assert.NotNil(t, root.children[childName])
assert.Equal(t, root, root.children[childName].parent)
// Add child via path syntax (unix: joe/ali)
childMultiName := []string{childName, "ali"}
assert.Nil(t, root.CreateDirChild(filepath.Join(childMultiName...)))
assert.NotNil(t, root.children[childMultiName[0]].children[childMultiName[1]])
assert.Equal(t, root.children[childMultiName[0]], root.children[childMultiName[0]].children[childMultiName[1]].parent)
}
func TestRemoveChild(t *testing.T) {
tt := []struct {
state []string
target string
}{
{state: []string{"leo"}, target: "leo"},
{state: []string{"muhammad", "muhammad/ali"}, target: "muhammad/ali"},
}
for _, tc := range tt {
root := NewDirectory("root", nil)
for _, file := range tc.state {
assert.Nil(t, root.CreateDirChild(file))
}
// remove a child that exists
assert.Nil(t, root.RemoveChild(tc.target))
assert.Nil(t, root.Child(tc.target))
}
}
func TestRename(t *testing.T) {
tt := []struct {
state []string
source string
target string
}{
{state: []string{"joe"}, source: "joe", target: "leo"},
{state: []string{"muhammad", "muhammad/ali"}, source: "muhammad/ali", target: "ali"},
}
for _, tc := range tt {
root := NewDirectory("root", nil)
for _, file := range tc.state {
assert.Nil(t, root.CreateDirChild(file))
}
assert.Nil(t, root.Rename(tc.source, tc.target, root))
assert.Nil(t, root.Child(tc.source))
assert.NotNil(t, root.Child(tc.target))
}
}
func TestChild(t *testing.T) {
childrenNames := []string{"jeo", "ali", "leo"}
root := NewDirectory("root", nil)
for _, childName := range childrenNames {
root.children[childName] = NewDirectory(childName, root)
}
for _, name := range childrenNames {
assert.Equal(t, root.Child(name), root.children[name])
}
}
func TestChildren(t *testing.T) {
root := NewDirectory("root", nil)
for _, childName := range []string{"jeo", "ali", "leo"} {
root.children[childName] = NewDirectory(childName, root)
}
childrenList := make([]*FileTree, 0, len(root.children))
for _, child := range root.children {
childrenList = append(childrenList, child)
}
assert.ElementsMatch(t, root.Children(), childrenList)
}
func TestPath(t *testing.T) {
root := NewDirectory("root", nil)
childName := "joe"
child := NewDirectory(childName, root)
assert.Equal(t, root.Path(), filepath.Join("."))
assert.Equal(t, child.Path(), filepath.Join(".", childName))
}