forked from PuerkitoBio/goquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_test.go
150 lines (137 loc) · 2.76 KB
/
type_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package goquery
import (
"bytes"
"code.google.com/p/go.net/html"
"fmt"
"os"
"testing"
)
// Test helper functions and members
var doc *Document
var doc2 *Document
var docB *Document
var docW *Document
func Doc() *Document {
if doc == nil {
doc = LoadDoc("page.html")
}
return doc
}
func Doc2() *Document {
if doc2 == nil {
doc2 = LoadDoc("page2.html")
}
return doc2
}
func DocB() *Document {
if docB == nil {
docB = LoadDoc("gotesting.html")
}
return docB
}
func DocW() *Document {
if docW == nil {
docW = LoadDoc("gowiki.html")
}
return docW
}
func AssertLength(t *testing.T, nodes []*html.Node, length int) {
if len(nodes) != length {
t.Errorf("Expected %d nodes, found %d.", length, len(nodes))
for i, n := range nodes {
t.Logf("Node %d: %+v.", i, n)
}
}
}
func AssertClass(t *testing.T, sel *Selection, class string) {
if !sel.HasClass(class) {
t.Errorf("Expected node to have class %s, found %+v.", class, sel.Get(0))
}
}
func AssertPanic(t *testing.T) {
if e := recover(); e == nil {
t.Error("Expected a panic.")
}
}
func AssertEqual(t *testing.T, s1 *Selection, s2 *Selection) {
if s1 != s2 {
t.Error("Expected selection objects to be the same.")
}
}
func AssertSelectionIs(t *testing.T, sel *Selection, is ...string) {
for i := 0; i < sel.Length(); i++ {
if !sel.Eq(i).Is(is[i]) {
t.Errorf("Expected node %d to be %s, found %+v", i, is[i], sel.Get(i))
}
}
}
func LoadDoc(page string) *Document {
if f, e := os.Open(fmt.Sprintf("./testdata/%s", page)); e != nil {
panic(e.Error())
} else {
defer f.Close()
if node, e := html.Parse(f); e != nil {
panic(e.Error())
} else {
return NewDocumentFromNode(node)
}
}
return nil
}
func TestNewDocument(t *testing.T) {
if f, e := os.Open("./testdata/page.html"); e != nil {
t.Error(e.Error())
} else {
defer f.Close()
if node, e := html.Parse(f); e != nil {
t.Error(e.Error())
} else {
doc = NewDocumentFromNode(node)
}
}
}
func TestNewDocumentFromReader(t *testing.T) {
cases := []struct {
src string
err bool
sel string
cnt int
}{
0: {
src: `
<html>
<head>
<title>Test</title>
<body>
<h1>Hi</h1>
</body>
</html>`,
sel: "h1",
cnt: 1,
},
1: {
// Actually pretty hard to make html.Parse return an error
// based on content...
src: `<html><body><aef<eqf>>>qq></body></ht>`,
},
}
buf := bytes.NewBuffer(nil)
for i, c := range cases {
buf.Reset()
buf.WriteString(c.src)
d, e := NewDocumentFromReader(buf)
if (e != nil) != c.err {
if c.err {
t.Errorf("[%d] - expected error, got none", i)
} else {
t.Errorf("[%d] - expected no error, got %s", i, e)
}
}
if c.sel != "" {
s := d.Find(c.sel)
if s.Length() != c.cnt {
t.Errorf("[%d] - expected %d nodes, found %d", i, c.cnt, s.Length())
}
}
}
}