forked from lestrrat-go/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_test.go
37 lines (31 loc) · 848 Bytes
/
html_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
package libxml2_test
import (
"testing"
"github.com/lestrrat/go-libxml2"
"github.com/lestrrat/go-libxml2/xpath"
"github.com/stretchr/testify/assert"
)
func TestParseHTML(t *testing.T) {
doc, err := libxml2.ParseHTMLString(`<html><body><h1>Hello, World!</h1><p>Lorem Ipsum</p></body></html>`)
if err != nil {
t.Errorf("Failed to parse: %s", err)
return
}
defer doc.Free()
root, err := doc.DocumentElement()
if !assert.NoError(t, err, "DocumentElement() should succeed") {
return
}
if !assert.True(t, root.IsSameNode(root), "root == root") {
return
}
nodes := xpath.NodeList(doc.Find("/html/body/h1"))
if len(nodes) != 1 {
t.Errorf("Could not find matching nodes")
return
}
if nodes[0].TextContent() != "Hello, World!" {
t.Errorf("h1 content is not 'Hello, World!', got %s", nodes[0].TextContent())
return
}
}