forked from moovweb/gokogiri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gokogiri_test.go
77 lines (68 loc) · 2.06 KB
/
gokogiri_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
package gokogiri
import (
"github.com/moovweb/gokogiri/help"
"testing"
)
func TestParseHtml(t *testing.T) {
input := "<html><body><div><h1></div>"
expected := `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div><h1></h1></div></body></html>
`
doc, err := ParseHtml([]byte(input))
if err != nil {
t.Error("Parsing has error:", err)
return
}
if doc.String() != expected {
t.Error("the output of the html doc does not match the expected")
}
expected = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body><div><h1></h1></div></body>
</html>
`
doc.Root().FirstChild().AddPreviousSibling("<head></head>")
if doc.String() != expected {
println(doc.String())
t.Error("the output of the html doc does not match the expected")
}
doc.Free()
CheckXmlMemoryLeaks(t)
}
func TestParseXml(t *testing.T) {
input := "<foo></foo>"
expected := `<?xml version="1.0" encoding="utf-8"?>
<foo/>
`
doc, err := ParseXml([]byte(input))
if err != nil {
t.Error("Parsing has error:", err)
return
}
if doc.String() != expected {
t.Error("the output of the xml doc does not match the expected")
}
expected = `<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar/>
</foo>
`
doc.Root().AddChild("<bar/>")
if doc.String() != expected {
t.Error("the output of the xml doc does not match the expected")
}
doc.Free()
CheckXmlMemoryLeaks(t)
}
func CheckXmlMemoryLeaks(t *testing.T) {
// LibxmlCleanUpParser() should only be called once during the lifetime of the
// program, but because there's no way to know when the last test of the suite
// runs in go, we can't accurately call it strictly once, so just avoid calling
// it for now because it's known to cause crashes if called multiple times.
//help.LibxmlCleanUpParser()
if !help.LibxmlCheckMemoryLeak() {
t.Errorf("Memory leaks: %d!!!", help.LibxmlGetMemoryAllocation())
help.LibxmlReportMemoryLeak()
}
}