-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_test.go
46 lines (44 loc) · 1.5 KB
/
class_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
package node
import (
"regexp"
"testing"
)
func TestClass(t *testing.T) {
if nodes := soup.FindAll(0, A, 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.FindAll(0, nil, Class(regexp.MustCompile("itl"))); 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, Class(func(class string, node Node) bool {
return node.HasAttr("class") && len(class) == 6
})); 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, A, Attr("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)
}
}
}
}