-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_test.go
151 lines (147 loc) · 4.75 KB
/
path_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
package config
import (
"testing"
)
func TestPath(t *testing.T) {
t.Run("Elements", func(t *testing.T) {
root := NewRootPath("")
node := root.AddNodePath(root.NewNodePath("a"))
node = node.AddNodePath(node.NewNodePath("b"))
p := node.AddPath(node.NewPath("c"))
elements := p.Elements()
switch {
case len(elements) != 3,
elements[0] != "a",
elements[1] != "b",
elements[2] != "c":
t.Errorf("path with anonymous root returned elements %v", elements)
}
root = NewRootPath("a")
node = root.AddNodePath(root.NewNodePath("b"))
node = node.AddNodePath(node.NewNodePath("c"))
p = node.AddPath(node.NewPath("d"))
elements = p.Elements()
switch {
case len(elements) != 4,
elements[0] != "a",
elements[1] != "b",
elements[2] != "c",
elements[3] != "d":
t.Errorf("path with named root returned elements %v", elements)
}
})
t.Run("Cmp", func(t *testing.T) {
root := NewRootPath("")
a := root.AddNodePath(root.NewNodePath("a"))
b := a.AddNodePath(a.NewNodePath("b"))
c := b.AddPath(b.NewPath("c"))
d := b.AddPath(b.NewPath("d"))
if i := PathCmp(c, d); i != -1 {
t.Errorf("comparing %s with %s returned %d", c, d, i)
}
if i := PathCmp(d, c); i != 1 {
t.Errorf("comparing %s with %s returned %d", d, c, i)
}
if i := PathCmp(d, d); i != 0 {
t.Errorf("comparing %s with %s returned %d", d, d, i)
}
aa := a.AddPath(a.NewPath("a"))
if i := PathCmp(aa, d); i != -1 {
t.Errorf("comparing %s with %s returned %d", aa, d, i)
}
if i := PathCmp(d, aa); i != 1 {
t.Errorf("comparing %s with %s returned %d", d, aa, i)
}
})
}
func TestNodePath(t *testing.T) {
t.Run("root", func(t *testing.T) {
root := NewRootPath("")
elements := root.Elements()
if len(elements) != 0 {
t.Errorf("anonymous root path returned elements %v", elements)
}
})
t.Run("NewPath", func(t *testing.T) {
root := NewRootPath("root")
p := root.NewPath("test")
elements := p.Elements()
if len(elements) != 2 || elements[0] != "root" || elements[1] != "test" {
t.Errorf("path root->test returned elements %v", elements)
}
if found := root.FindPath("test"); found != nil {
t.Errorf("finding unadded path returned %s", found)
}
if found := root.FindNodePath("test"); found != nil {
t.Errorf("finding unadded node path returned %s", found)
}
})
t.Run("AddPath", func(t *testing.T) {
root := NewRootPath("root")
p := root.AddPath(root.NewPath("test"))
elements := p.Elements()
if len(elements) != 2 || elements[0] != "root" || elements[1] != "test" {
t.Errorf("path root->test returned elements %v", elements)
}
if found := root.FindPath("test"); found == nil {
t.Errorf("finding added path returned nil")
} else if found != p {
t.Errorf("finding added path returned %s", found)
}
if found := root.FindNodePath("test"); found != nil {
t.Errorf("finding unadded node path returned %s", found)
}
})
t.Run("NewNodePath", func(t *testing.T) {
root := NewRootPath("root")
p := root.NewNodePath("test")
elements := p.Elements()
if len(elements) != 2 || elements[0] != "root" || elements[1] != "test" {
t.Errorf("path root->test returned elements %v", elements)
}
if found := root.FindNodePath("test"); found != nil {
t.Errorf("finding unadded node path returned %s", found)
}
if found := root.FindPath("test"); found != nil {
t.Errorf("finding unadded path returned %s", found)
}
})
t.Run("AddNodePath", func(t *testing.T) {
root := NewRootPath("root")
p := root.AddNodePath(root.NewNodePath("test"))
elements := p.Elements()
if len(elements) != 2 || elements[0] != "root" || elements[1] != "test" {
t.Errorf("path root->test returned elements %v", elements)
}
if found := root.FindNodePath("test"); found == nil {
t.Errorf("finding added node path returned nil")
} else if found != p {
t.Errorf("finding added node path returned %s", found)
}
if found := root.FindPath("test"); found != nil {
t.Errorf("finding unadded path returned %s", found)
}
})
t.Run("FindPath", func(t *testing.T) {
root := NewRootPath("")
node := root.AddNodePath(root.NewNodePath("a"))
node = node.AddNodePath(node.NewNodePath("b"))
p := node.AddPath(node.NewPath("c"))
if found := root.FindPath("a", "b", "c"); found == nil {
t.Errorf("finding added path returned nil")
} else if found != p {
t.Errorf("finding added path returned %s", found)
}
})
t.Run("FindNodePath", func(t *testing.T) {
root := NewRootPath("root")
node := root.AddNodePath(root.NewNodePath("a"))
node = node.AddNodePath(node.NewNodePath("b"))
node = node.AddNodePath(node.NewNodePath("c"))
if found := root.FindNodePath("a", "b", "c"); found == nil {
t.Errorf("finding added node path returned nil")
} else if found != node {
t.Errorf("finding added node path returned %s", found)
}
})
}