-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder_test.go
350 lines (341 loc) · 13.7 KB
/
finder_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package node
import (
"reflect"
"regexp"
"strings"
"testing"
)
func TestFindAll(t *testing.T) {
if nodes := soup.FindAll(0, Title); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if nodes := soup.FindAll(0, P, Class("title")); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != `<p class="title"><b>The Dormouse's story</b></p>` {
t.Errorf("expected html %q; got %q", `<p class="title"><b>The Dormouse's story</b></p>`, html)
}
if nodes := soup.FindAll(0, A); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.FindAll(0, nil, Id("link2")); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != lacie {
t.Errorf("expected html %q; got %q", lacie, html)
}
if nodes := soup.FindAll(0, nil, String(regexp.MustCompile("sisters"))); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if text := nodes[0].Readable(); text != "Once upon a time there were three little sisters; and their names were\n" {
t.Errorf("expected text %q; got %q", "Once upon a time there were three little sisters; and their names were\n", text)
}
if nodes := soup.FindAll(0, nil, Attr("href", regexp.MustCompile("elsie"))); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
if nodes := soup.FindAll(0, nil, Id(True)); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.FindAll(0, nil, Attr("href", regexp.MustCompile("elsie")), Id("link1")); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
}
func TestFindN(t *testing.T) {
if nodes := soup.FindN(0, 2, A); len(nodes) != 2 {
t.Errorf("expected nodes %d; got %d", 2, len(nodes))
} else {
expected := []string{elsie, lacie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
}
func TestFind(t *testing.T) {
if nodes := soup.FindN(0, 1, Title); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if html := soup.Find(0, Title).Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if node := soup.Find(0, Tag("nosuchtag")); node != nil {
t.Errorf("expected node nil; got %q", node.Readable())
}
if html := soup.Find(0, Head).Find(0, Title).Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
}
func TestFindMethod(t *testing.T) {
aString := soup.FindString(0, String("Lacie"))
if text := aString.String(); text != "Lacie" {
t.Errorf("expected string %q; got %q", "Lacie", text)
}
if nodes := aString.FindAll(Parent, A); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != lacie {
t.Errorf("expected html %q; got %q", lacie, html)
}
if html := aString.Find(Parent, P).Readable(); !strings.HasPrefix(html, `<p class="story">Once upon a time there were`) {
t.Errorf("expected html %q; got %q", `<p class="story">Once upon a time there were`, html)
}
if nodes := aString.FindAll(Parent, P, Class("title")); len(nodes) != 0 {
t.Errorf("expected nodes %d; got %d", 0, len(nodes))
}
firstLink := soup.Find(0, A)
if nodes := firstLink.FindAll(NextSibling, A); len(nodes) != 2 {
t.Errorf("expected nodes %d; got %d", 2, len(nodes))
} else {
expected := []string{lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if html := soup.Find(0, P, Class("story")).Find(NextSibling, P).Readable(); html != `<p class="story">...</p>` {
t.Errorf("expected html %q; got %q", `<p class="story">...</p>`, html)
}
lastLink := soup.Find(0, A, Id("link3"))
if nodes := lastLink.FindAll(PrevSibling, A); len(nodes) != 2 {
t.Errorf("expected nodes %d; got %d", 2, len(nodes))
} else {
expected := []string{lacie, elsie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if html := soup.Find(0, P, Class("story")).Find(PrevSibling, P).Readable(); html != `<p class="title"><b>The Dormouse's story</b></p>` {
t.Errorf("expected html %q; got %q", `<p class="title"><b>The Dormouse's story</b></p>`, html)
}
if strings := firstLink.FindAllString(Next, True); len(strings) != 9 {
t.Errorf("expected nodes %d; got %d", 9, len(strings))
} else {
expected := []string{"Elsie", ",\n", "Lacie", " and\n", "Tillie",
";\nand they lived at the bottom of a well.", "\n\n", "...", "\n"}
for i, node := range strings {
if html := node.String(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if html := firstLink.Find(Next, P).Readable(); html != `<p class="story">...</p>` {
t.Errorf("expected html %q; got %q", `<p class="story">...</p>`, html)
}
if nodes := firstLink.FindAll(Previous, P); len(nodes) != 2 {
t.Errorf("expected nodes %d; got %d", 2, len(nodes))
} else {
expected := []string{
`<p class="story">Once upon a time there were three little sisters;`,
`<p class="title"><b>The Dormouse's story</b></p>`,
}
for i, node := range nodes {
if html := node.Readable(); !strings.HasPrefix(html, expected[i]) {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if html := firstLink.Find(Previous, Title).Readable(); html != `<title>The Dormouse's story</title>` {
t.Errorf("expected html %q; got %q", `<title>The Dormouse's story</title>`, html)
}
if nodes := soup.Find(0, Tag("html")).FindAll(NoRecursive, Title); len(nodes) != 0 {
t.Errorf("expected nodes %d; got %d", 0, len(nodes))
}
}
func TestSelectAll(t *testing.T) {
if nodes := soup.SelectAll("title"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if nodes := soup.SelectAll("p:nth-of-type(3)"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != `<p class="story">...</p>` {
t.Errorf("expected html %q; got %q", `<p class="story">...</p>`, html)
}
if nodes := soup.SelectAll("body a"); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll("html head title"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if nodes := soup.SelectAll("head > title"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if nodes := soup.SelectAll("p > a"); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll("p > a:nth-of-type(2)"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != lacie {
t.Errorf("expected html %q; got %q", lacie, html)
}
if nodes := soup.SelectAll("p > #link1"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
if nodes := soup.SelectAll("body > a"); len(nodes) != 0 {
t.Errorf("expected nodes %d; got %d", 0, len(nodes))
}
if nodes := soup.SelectAll(".sister"); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll("[class~=sister]"); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll("#link1"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
if nodes := soup.SelectAll("a#link2"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != lacie {
t.Errorf("expected html %q; got %q", lacie, html)
}
if nodes := soup.SelectAll("#link1,#link2"); len(nodes) != 2 {
t.Errorf("expected nodes %d; got %d", 2, len(nodes))
} else {
expected := []string{elsie, lacie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll("a[href]"); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll(`a[href="http://example.com/elsie"]`); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
if nodes := soup.SelectAll(`a[href^="http://example.com/"]`); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{elsie, lacie, tillie}
for i, node := range nodes {
if html := node.Readable(); html != expected[i] {
t.Errorf("expected html #%d %q; got %q", i, expected[i], html)
}
}
}
if nodes := soup.SelectAll(`a[href$="tillie"]`); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != tillie {
t.Errorf("expected html %q; got %q", tillie, html)
}
if nodes := soup.SelectAll(`a[href*=".com/el"]`); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
}
func TestSelect(t *testing.T) {
if html := soup.Select(".sister").Readable(); html != elsie {
t.Errorf("expected html %q; got %q", elsie, html)
}
}
func TestXPath(t *testing.T) {
if nodes := soup.XPath("//title[1]"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
if node := soup.XPath("//nosuchtag"); len(node) != 0 {
t.Errorf("expected node nil; got %q", node[0].Readable())
}
if nodes := soup.XPath("//head/title"); len(nodes) != 1 {
t.Errorf("expected nodes %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<title>The Dormouse's story</title>" {
t.Errorf("expected html %q; got %q", "<title>The Dormouse's story</title>", html)
}
}
func TestEvaluate(t *testing.T) {
if _, err := soup.Evaluate("$test"); err == nil {
t.Errorf("expected error; got nil")
}
if res, err := soup.Evaluate("count(//a)"); err != nil {
t.Error(err)
} else if v, ok := res.(float64); !ok {
t.Errorf("expect type float64; got %s", reflect.TypeOf(res))
} else if v != 3 {
t.Errorf("expected count 3; got %g", v)
}
if res, err := soup.Evaluate(`local-name(//a)`); err != nil {
t.Error(err)
} else if v, ok := res.(string); !ok {
t.Errorf("expect type string; got %s", reflect.TypeOf(res))
} else if v != "a" {
t.Errorf("expected %q; got %q", "a", v)
}
if res, err := soup.Evaluate(`boolean(//table)`); err != nil {
t.Error(err)
} else if v, ok := res.(bool); !ok {
t.Errorf("expect type bool; got %s", reflect.TypeOf(res))
} else if v {
t.Error("expected false; got true")
}
}