-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_test.go
121 lines (115 loc) · 2.18 KB
/
spec_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
package brief_test
import (
"strings"
"testing"
"github.com/robbyriverside/brief"
)
func TestChild(t *testing.T) {
tests := []struct {
Path []string
Found bool
}{
{
Path: []string{"head", "title"},
Found: true,
},
{
Path: []string{"body", "div:main"},
Found: true,
},
{
Path: []string{"body", "div"}, // div name is ignored
Found: true,
},
{
Path: []string{"body", "div:main", "p"},
Found: true,
},
{
Path: []string{"body", "div:"}, // div does have a name
Found: false,
},
{
Path: []string{"body", "div", "p:foo"}, // p does NOT have a name
Found: false,
},
}
t.Log(test1)
nodes, err := brief.Decode(strings.NewReader(test1), "tests")
if err != nil {
t.Fatal(err)
}
node := nodes[0]
for _, test := range tests {
found := node.Child(test.Path...)
if found == nil && test.Found {
t.Error("failed:", test.Found, test.Path)
}
if found != nil && !test.Found {
t.Error("failed:", test.Found, test.Path)
}
t.Logf("%s %t --> %s", test.Path, test.Found, found)
}
}
func TestFind(t *testing.T) {
tests := []struct {
Name string
Found bool
}{
{
Name: "head",
Found: true,
},
{
Name: "title",
Found: true,
},
{
Name: "p",
Found: true,
},
{
Name: "foo", // not found
Found: false,
},
{
Name: "div:", // div has a name
Found: false,
},
{
Name: "p:foo", // p has no name
Found: false,
},
}
t.Log(test1)
nodes, err := brief.Decode(strings.NewReader(test1), "tests")
if err != nil {
t.Fatal(err)
}
node := nodes[0]
for _, test := range tests {
found := node.Find(test.Name)
if found == nil && test.Found {
t.Error("failed:", test.Found, test.Name)
}
if found != nil && !test.Found {
t.Error("failed:", test.Found, test.Name)
}
t.Logf("%s %t --> %s", test.Name, test.Found, found)
}
}
func TestCollect(t *testing.T) {
t.Log(test5)
nodes, err := brief.Decode(strings.NewReader(test5), "tests")
if err != nil {
t.Fatal(err)
}
node := nodes[0]
child := node.Find("command:bottom")
if child == nil {
t.Fatal("bottom not found")
}
t.Logf("child %s", child)
names := child.Collect("command", "project")
t.Logf("names: %s", names)
}