-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml_utils_test.go
More file actions
51 lines (43 loc) · 1.29 KB
/
Copy pathhtml_utils_test.go
File metadata and controls
51 lines (43 loc) · 1.29 KB
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
package epub
import (
"strings"
"testing"
"golang.org/x/net/html"
)
func TestFindNode(t *testing.T) {
doc, err := html.Parse(strings.NewReader(`<html><body><div id="a"><p class="target">Hi</p></div></body></html>`))
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
t.Run("matches node", func(t *testing.T) {
found := FindNode(doc, func(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == "p"
})
if found == nil || found.Data != "p" {
t.Errorf("expected to find p element, got %v", found)
}
})
t.Run("returns nil when no match", func(t *testing.T) {
found := FindNode(doc, func(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == "article"
})
if found != nil {
t.Errorf("expected nil, got %v", found)
}
})
}
func TestGetTextContent(t *testing.T) {
doc, err := html.Parse(strings.NewReader(`<html><body><div>Hello <span>nested <b>text</b></span> here</div></body></html>`))
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
div := FindNode(doc, func(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == "div"
})
if div == nil {
t.Fatal("expected to find div element")
}
if got := GetTextContent(div); got != "Hello nested text here" {
t.Errorf("expected concatenated text, got %q", got)
}
}