-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_test.go
98 lines (95 loc) · 3.24 KB
/
filter_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
package node
import (
"regexp"
"strings"
"testing"
"golang.org/x/net/html"
)
func TestFilter(t *testing.T) {
if nodes := soup.FindAll(0, B); len(nodes) != 1 {
t.Errorf("expected b %d; got %d", 1, len(nodes))
} else if html := nodes[0].Readable(); html != "<b>The Dormouse's story</b>" {
t.Errorf("expected html %q; got %q", "<b>The Dormouse's story</b>", html)
}
if nodes := soup.FindAll(0, Tag(regexp.MustCompile("^b"))); len(nodes) != 2 {
t.Errorf("expected ^b %d; got %d", 2, len(nodes))
} else {
expected := []string{"body", "b"}
for i, node := range nodes {
if name := node.Data(); name != expected[i] {
t.Errorf("expected name #%d %q; got %q", i, expected[i], name)
}
}
}
if nodes := soup.FindAll(0, Tag(regexp.MustCompile("t"))); len(nodes) != 2 {
t.Errorf("expected t %d; got %d", 2, len(nodes))
} else {
expected := []string{"html", "title"}
for i, node := range nodes {
if name := node.Data(); name != expected[i] {
t.Errorf("expected name #%d %q; got %q", i, expected[i], name)
}
}
}
if nodes := soup.FindAll(0, Tags("a", "b")); len(nodes) != 4 {
t.Errorf("expected nodes %d; got %d", 4, len(nodes))
} else {
expected := []string{"<b>The Dormouse's story</b>", 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, True); len(nodes) != 11 {
t.Errorf("expected nodes %d; got %d", 11, len(nodes))
} else {
expected := []string{"html", "head", "title", "body", "p", "b", "p", "a", "a", "a", "p"}
for i, node := range nodes {
if name := node.Data(); name != expected[i] {
t.Errorf("expected name #%d %q; got %q", i, expected[i], name)
}
}
}
if nodes := soup.FindAll(0, Tag(func(tag string, node Node) bool {
return node.HasAttr("class") && !node.HasAttr("id")
})); len(nodes) != 3 {
t.Errorf("expected nodes %d; got %d", 3, len(nodes))
} else {
expected := []string{
`<p class="title"><b>The Dormouse's story</b></p>`,
`<p class="story">Once upon a time there were`,
`<p class="story">...</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 nodes := soup.FindAll(0, nil, Attr("href", func(value string, node Node) bool {
return !regexp.MustCompile("lacie").MatchString(value)
})); len(nodes) != 2 {
t.Errorf("expected nodes %d; got %d", 2, len(nodes))
} else {
expected := []string{elsie, tillie}
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 nodes := soup.FindAll(0, Tag(func(tag string, node Node) bool {
return node.PrevNode() != nil && node.PrevNode().Type() == html.TextNode &&
node.NextNode() != nil && node.NextNode().Type() == html.TextNode
})); len(nodes) != 6 {
t.Errorf("expected nodes %d; got %d", 6, len(nodes))
} else {
expected := []string{"body", "p", "a", "a", "a", "p"}
for i, node := range nodes {
if name := node.Data(); name != expected[i] {
t.Errorf("expected name #%d %q; got %q", i, expected[i], name)
}
}
}
}