-
Notifications
You must be signed in to change notification settings - Fork 7
/
pagser_test.go
111 lines (100 loc) · 2.23 KB
/
pagser_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
package pagser
import (
"fmt"
"testing"
)
const rawPagserHtml = `
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pagser Example</title>
<meta name="keywords" content="golang,pagser,goquery,html,page,parser,colly">
</head>
<body>
<div class="navlink">
<div class="container">
<ul class="clearfix">
<li id=''><a href="/">Index</a></li>
<li id='2'><a href="/list/web" title="web site">Web page</a></li>
<li id='3'><a href="/list/pc" title="pc page">Pc Page</a></li>
<li id='4'><a href="/list/mobile" title="mobile page">Mobile Page</a></li>
</ul>
</div>
</div>
</body>
</html>
`
type PagserData struct {
Title string `pagser:"title"`
Keywords []string `pagser:"meta[name='keywords']->attrSplit(content)"`
Navs []struct {
ID int `pagser:"->attrEmpty(id, -1)"`
Name string `pagser:"a->text()"`
Url string `pagser:"a->attr(href)"`
} `pagser:".navlink li"`
}
type ConfigData struct {
Title string `query:"title"`
Keywords []string `query:"meta[name='keywords']@attrSplit(content)"`
Navs []struct {
ID int `query:"@attrEmpty(id, -1)"`
Name string `query:"a@text()"`
Url string `query:"a@attr(href)"`
} `query:".navlink li"`
}
func TestNew(t *testing.T) {
p := New()
var data PagserData
err := p.Parse(&data, rawPagserHtml)
if err != nil {
t.Fatal(err)
}
fmt.Printf("json: %v\n", prettyJson(data))
}
func TestNewWithConfig(t *testing.T) {
cfg := Config{
TagName: "query",
FuncSymbol: "@",
CastError: true,
Debug: true,
}
p, err := NewWithConfig(cfg)
if err != nil {
t.Fatal(err)
}
var data ConfigData
err = p.Parse(&data, rawPagserHtml)
if err != nil {
t.Fatal(err)
}
fmt.Printf("json: %v\n", prettyJson(data))
}
func TestNewWithConfigTagNameError(t *testing.T) {
cfg := Config{
TagName: "",
FuncSymbol: "->",
CastError: true,
Debug: true,
}
_, err := NewWithConfig(cfg)
if err != nil {
t.Log(err)
} else {
t.Fatal("Result must return error")
}
}
func TestNewWithConfigFuncSymbolError(t *testing.T) {
cfg := Config{
TagName: "pagser",
FuncSymbol: "",
CastError: true,
Debug: true,
}
_, err := NewWithConfig(cfg)
if err != nil {
t.Log(err)
} else {
t.Fatal("Result must return error")
}
}