-
Notifications
You must be signed in to change notification settings - Fork 1
/
all_test.go
254 lines (234 loc) · 7.38 KB
/
all_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package scrappy
import (
"golang.org/x/net/html"
"strings"
"testing"
)
func TestAll_Child(t *testing.T) {
s := New()
doc, err := html.Parse(strings.NewReader(document))
if err != nil {
t.Fatal("Unexpected error", err)
}
node := doc.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling
result := s.A.Child(node, Tag("div"))
if len(result) != 3 {
t.Fatal("Unexpected error, expected three div node instead", len(result))
}
}
func TestAll_Depth(t *testing.T) {
s := New()
doc, err := html.Parse(strings.NewReader(document))
if err != nil {
t.Fatal("Unexpected error", err)
}
// test with content
result := s.A.Depth(doc, ContainsText("more"))
if len(result) != 4 {
t.Fatal("Expected 4 nodes 'more' instead", len(result))
}
result = s.A.Depth(doc, Text("content"))
if len(result) != 3 {
t.Fatal("Expected 3 nodes 'content' instead", len(result))
}
result = s.A.Depth(doc, Text("href"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes 'content' instead", len(result))
}
// test with tag
result = s.A.Depth(doc, Tag("a"))
if len(result) != 4 {
t.Fatal("Expected 4 nodes 'a' instead", len(result))
}
result = s.A.Depth(doc, Tag("div"))
if len(result) != 3 {
t.Fatal("Expected 3 nodes 'div' instead", len(result))
}
result = s.A.Depth(doc, Tag("href"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes 'href' instead", len(result))
}
// test with attr
result = s.A.Depth(doc, Attr("href"))
if len(result) != 4 {
t.Fatal("Expected 4 nodes 'href' instead", len(result))
}
result = s.A.Depth(doc, Attr("onload"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes 'onload' instead", len(result))
}
result = s.A.Depth(doc, Attr("div"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes 'div' instead", len(result))
}
// test with attr value
result = s.A.Depth(doc, Value("myFunc()"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes 'myFunc' instead", len(result))
}
result = s.A.Depth(doc, Value("scrappy"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes 'scrappy' instead", len(result))
}
// mixing
result = s.A.Depth(doc, Attr("onload"), Value("myFunc()"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
result = s.A.Depth(doc, Tag("a"), Attr("href"), Value("scrappy"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
// mixing with custom filter
custom := func(node *html.Node) bool {
result := s.A.Depth(node, Text("content"))
return len(result) > 0
}
s.A.Depth(doc, Tag("a"), Attr("href"), Value("scrappy"), custom)
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
// nested filters enabled
s.nested = true
result = s.A.Depth(doc, Tag("body"), Tag("div"), Tag("section"), Attr("href"), Text("more"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
result = s.A.Depth(doc, Tag("body"), Tag("div"), Tag("section"), Attr("p"), Text("more"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes instead", len(result))
}
result = s.A.Depth(doc, Tag("html"), Attr("onload"), Value("myFunc()"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes instead", len(result))
}
// nested filters disabled
s.nested = false
result = s.A.Depth(doc, Tag("html"), Attr("onload"), Value("myFunc()"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
result = s.A.Depth(doc, Tag("body"), Tag("div"), Tag("section"), Attr("href"), Text("more"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes instead", len(result))
}
}
func TestAll_Parent(t *testing.T) {
s := New()
doc, err := html.Parse(strings.NewReader(document))
if err != nil {
t.Fatal("Unexpected error", err)
}
node := doc.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling.FirstChild.NextSibling.FirstChild.NextSibling
result := s.A.Parent(node, Tag("body"))
if len(result) != 1 {
t.Fatal("Unexpected error, there is 1 parent body instead", len(result))
}
}
func TestAll_Breadth(t *testing.T) {
s := New()
doc, err := html.Parse(strings.NewReader(document))
if err != nil {
t.Fatal("Unexpected error", err)
}
// test with content
result := s.A.Breadth(doc, ContainsText("more"))
if len(result) != 4 {
t.Fatal("Expected 4 nodes 'more' instead", len(result))
}
result = s.A.Breadth(doc, Text("content"))
if len(result) != 3 {
t.Fatal("Expected 3 nodes 'content' instead", len(result))
}
result = s.A.Breadth(doc, Text("href"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes 'content' instead", len(result))
}
// test with tag
result = s.A.Breadth(doc, Tag("a"))
if len(result) != 4 {
t.Fatal("Expected 4 nodes 'a' instead", len(result))
}
result = s.A.Breadth(doc, Tag("div"))
if len(result) != 3 {
t.Fatal("Expected 3 nodes 'div' instead", len(result))
}
result = s.A.Breadth(doc, Tag("href"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes 'href' instead", len(result))
}
// test with attr
result = s.A.Breadth(doc, Attr("href"))
if len(result) != 4 {
t.Fatal("Expected 4 nodes 'href' instead", len(result))
}
result = s.A.Breadth(doc, Attr("onload"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes 'onload' instead", len(result))
}
result = s.A.Breadth(doc, Attr("div"))
if len(result) != 0 {
t.Fatal("Expected 0 nodes 'div' instead", len(result))
}
// test with attr value
result = s.A.Breadth(doc, Value("myFunc()"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes 'myFunc' instead", len(result))
}
result = s.A.Breadth(doc, Value("scrappy"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes 'scrappy' instead", len(result))
}
// mixing
result = s.A.Breadth(doc, Attr("onload"), Value("myFunc()"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
result = s.A.Breadth(doc, Tag("a"), Attr("href"), Value("scrappy"))
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
// mixing with custom filter
custom := func(node *html.Node) bool {
result := s.A.Breadth(node, Text("content"))
return len(result) > 0
}
s.A.Breadth(doc, Tag("a"), Attr("href"), Value("scrappy"), custom)
if len(result) != 1 {
t.Fatal("Expected 1 nodes instead", len(result))
}
}
func TestAll_NextSibling(t *testing.T) {
s := New()
doc, err := html.Parse(strings.NewReader(document))
if err != nil {
t.Fatal("Unexpected error", err)
}
node := doc.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling.FirstChild.NextSibling.FirstChild.NextSibling
result := s.A.NextSibling(node, Tag("section"))
if len(result) != 3 {
t.Fatal("Unexpected error, expected three section node instead", len(result))
}
node = doc.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling.FirstChild.NextSibling
result = s.A.NextSibling(node, Tag("div"))
if len(result) != 2 {
t.Fatal("Unexpected error, expected 2 section node instead", len(result))
}
}
func TestAll_PrevSibling(t *testing.T) {
s := New()
doc, err := html.Parse(strings.NewReader(document))
if err != nil {
t.Fatal("Unexpected error", err)
}
node := doc.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling.FirstChild.NextSibling.LastChild.PrevSibling
result := s.A.PrevSibling(node, Tag("section"))
if len(result) != 3 {
t.Fatal("Unexpected error, expected only three section node instead", len(result))
}
node = doc.FirstChild.NextSibling.FirstChild.NextSibling.NextSibling.LastChild.PrevSibling
result = s.A.PrevSibling(node, Tag("div"))
if len(result) != 2 {
t.Fatal("Unexpected error, expected there are 2 div node instead", len(result))
}
}