-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathdocument_test.go
95 lines (85 loc) · 2.24 KB
/
document_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
package htmldoc
import (
"sync"
"testing"
"github.com/daviddengcn/go-assert"
)
func TestDocumentParse(t *testing.T) {
// parse a document and check we have valid nodes
doc := Document{
FilePath: "fixtures/documents/index.html",
}
doc.Init()
doc.Parse()
nodeElem := doc.htmlNode.FirstChild.FirstChild.NextSibling.FirstChild
assert.Equals(t, "document first body node", nodeElem.Data, "h1")
}
func TestDocumentParseOnce(t *testing.T) {
// Document.Parse should only parse once, subsequent calls should return quickly
doc := Document{
FilePath: "fixtures/documents/index.html",
}
doc.Init()
doc.Parse()
// Store copy of htmlNode
hN := doc.htmlNode
doc.Parse()
// and assert it's the same one
assert.Equals(t, "htmlNode", doc.htmlNode, hN)
}
func TestDocumentParseOnceConcurrent(t *testing.T) {
// Document.Parse should be thread safe
doc := Document{
FilePath: "fixtures/documents/index.html",
}
doc.Init()
// Parse many times
wg := sync.WaitGroup{}
for i := 0; i < 320; i++ {
wg.Add(1)
go func() {
defer wg.Done()
doc.Parse()
}()
}
// Wait until all jobs done
wg.Wait()
// Assert we have something sensible by the end of this
nodeElem := doc.htmlNode.FirstChild.FirstChild.NextSibling.FirstChild
assert.Equals(t, "document first body node", nodeElem.Data, "h1")
}
func TestDocumentNodesOfInterest(t *testing.T) {
doc := Document{
FilePath: "fixtures/documents/nodes.htm",
}
doc.Init()
doc.Parse()
assert.Equals(t, "nodes of interest", len(doc.NodesOfInterest), 12)
}
func TestDocumentBasePathDefault(t *testing.T) {
doc := Document{
FilePath: "fixtures/documents/index.html",
}
doc.Init()
doc.Parse()
assert.Equals(t, "BasePath", doc.BasePath, "")
}
func TestDocumentBasePathFromTag(t *testing.T) {
doc := Document{
FilePath: "fixtures/documents/dir2/base_tag.htm",
}
doc.Init()
doc.Parse()
assert.Equals(t, "BasePath", doc.BasePath, "/dir2")
}
func TestDocumentIsHashValid(t *testing.T) {
// parse a document and check we have valid nodes
doc := Document{
FilePath: "fixtures/documents/index.html",
}
doc.Init()
doc.Parse()
assert.IsTrue(t, "#xyz present", doc.IsHashValid("xyz"))
assert.IsTrue(t, "#prq present", doc.IsHashValid("prq"))
assert.IsFalse(t, "#abc present", doc.IsHashValid("abc"))
}