-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_test.go
197 lines (161 loc) · 3.38 KB
/
dom_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
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
package dom
import (
"testing"
"golang.org/x/net/html"
)
func TestAllNodes(t *testing.T) {
child0 := &html.Node{
Type: html.ElementNode,
}
child0.AppendChild(&html.Node{
Type: html.TextNode,
})
child1 := &html.Node{
Type: html.ElementNode,
}
doc := &html.Node{}
doc.AppendChild(child0)
doc.AppendChild(child1)
nodes := AllNodes(doc)
if len(nodes) != 4 {
t.Errorf("expected different length, but got %d", len(nodes))
}
}
// - - - - - - - - - - - - - - - //
func TestAllChildNodes(t *testing.T) {
child0 := &html.Node{
Type: html.ElementNode,
}
child1 := &html.Node{
Type: html.TextNode,
}
child2 := &html.Node{
Type: html.ElementNode,
}
doc := &html.Node{}
doc.AppendChild(child0)
doc.AppendChild(child1)
doc.AppendChild(child2)
nodes := AllChildNodes(doc)
if len(nodes) != 3 {
t.Error("expected different length")
}
if nodes[0] != child0 || nodes[1] != child1 || nodes[2] != child2 {
t.Error("expected different nodes")
}
}
func TestAllChildElements(t *testing.T) {
firstChild := &html.Node{
Type: html.ElementNode,
}
middleChild := &html.Node{
Type: html.TextNode,
}
lastChild := &html.Node{
Type: html.ElementNode,
}
doc := &html.Node{}
doc.AppendChild(firstChild)
doc.AppendChild(middleChild)
doc.AppendChild(lastChild)
nodes := AllChildElements(doc)
if len(nodes) != 2 {
t.Error("expected different length")
}
if nodes[0] != firstChild || nodes[1] != lastChild {
t.Error("expected different nodes")
}
}
// - - - - - - - - - - - - - - - //
func TestFirstChildNode(t *testing.T) {
node := &html.Node{
Type: html.ElementNode,
}
child := &html.Node{
Type: html.TextNode,
}
node.AppendChild(child)
res := FirstChildNode(node)
if res != child {
t.Error("expected the first child node")
}
res = FirstChildNode(child)
if res != nil {
t.Error("expected the first child to be nil")
}
}
func TestFirstChildElement(t *testing.T) {
node := &html.Node{
Type: html.ElementNode,
}
child1 := &html.Node{
Type: html.TextNode,
}
child2 := &html.Node{
Type: html.ElementNode,
}
node.AppendChild(child1)
node.AppendChild(child2)
res := FirstChildElement(node)
if res != child2 {
t.Error("expected the first child node to be child2")
}
}
// - - - - - - - - - - - - - - - //
func TestPrevElementSibling(t *testing.T) {
first := &html.Node{
Type: html.ElementNode,
Data: "first",
}
text := &html.Node{
Type: html.TextNode,
Data: "between",
PrevSibling: first,
}
last := &html.Node{
Type: html.ElementNode,
Data: "last",
PrevSibling: text,
}
output := PrevSiblingElement(last)
if output != first {
t.Error("expected 'start' node")
}
output = PrevSiblingElement(text)
if output != first {
t.Error("expected 'end' node")
}
output = PrevSiblingElement(first)
if output != nil {
t.Error("expected nil node")
}
}
// - - - - - - - - - - - - - - - //
func TestNextElementSibling(t *testing.T) {
end := &html.Node{
Type: html.ElementNode,
Data: "end",
}
text := &html.Node{
Type: html.TextNode,
Data: "between",
NextSibling: end,
}
start := &html.Node{
Type: html.ElementNode,
Data: "start",
NextSibling: text,
}
output := NextSiblingElement(start)
if output != end {
t.Error("expected 'end' node")
}
output = NextSiblingElement(text)
if output != end {
t.Error("expected 'end' node")
}
output = NextSiblingElement(end)
if output != nil {
t.Error("expected nil node")
}
}