-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlist_test.go
162 lines (125 loc) · 3.31 KB
/
list_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
151
152
153
154
155
156
157
158
159
160
161
162
package ant
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
)
func TestList(t *testing.T) {
t.Run("query", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `<title>title</title>`)
var list = List{root}
list = list.Query(`title`)
assert.Equal(1, len(list))
assert.Equal("title", list[0].DataAtom.String())
})
t.Run("is", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `<a class="item">anchor</a><li class="item">item</li>`)
var list = List{root}.Query(`.item`)
assert.True(list.Is("li"))
assert.True(list.Is("a"))
assert.False(list.Is("div"))
})
t.Run("at", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `
<li>1</li>
<li>2</li>
<li>3</li>
`)
lis := List{root}.Query(`li`)
assert.Equal("1", lis.At(0).Text())
assert.Equal("2", lis.At(1).Text())
assert.Equal("3", lis.At(2).Text())
assert.Equal("", lis.At(3).Text())
assert.Equal("3", lis.At(-1).Text())
assert.Equal("2", lis.At(-2).Text())
assert.Equal("1", lis.At(-3).Text())
assert.Equal("", lis.At(-4).Text())
})
t.Run("text", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `<title>title</title>`)
var list = List{root}.Query("title")
assert.Equal("title", list.Text())
})
t.Run("text multi", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `<li>a</li><li><a>b</a></li>`)
var list = List{root}.Query("li")
assert.Equal("ab", list.Text())
})
t.Run("text empty", func(t *testing.T) {
var assert = require.New(t)
assert.Equal("", List{}.Text())
})
t.Run("attr", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `<title key=val></title>`)
var list = List{root}.Query("title")
v, ok := list.Attr("key")
assert.True(ok)
assert.Equal("val", v)
})
t.Run("attr empty", func(t *testing.T) {
var assert = require.New(t)
v, ok := List{}.Attr("foo")
assert.False(ok)
assert.Equal("", v)
})
t.Run("scan struct", func(t *testing.T) {
var assert = require.New(t)
var root = parse(t, `<title key=val>title</title>`)
var list = List{root}.Query("title")
var data struct {
Title string `css:"title"`
Key string `css:"title@key"`
}
err := list.Scan(&data)
assert.NoError(err)
assert.Equal("title", data.Title)
assert.Equal("val", data.Key)
})
t.Run("scan empty", func(t *testing.T) {
var assert = require.New(t)
var data struct {
Title string `css:"title"`
Key string `css:"title@key"`
}
err := List{}.Scan(&data)
assert.NoError(err)
})
}
func BenchmarkList(b *testing.B) {
b.Run("query", func(b *testing.B) {
var root = parse(b, `<title>title</title>`)
var list = List{root}
for i := 0; i < b.N; i++ {
list = list.Query(`title`)
}
})
b.Run("text", func(b *testing.B) {
var root = parse(b, `<title>title</title>`)
var list = List{root}.Query("title")
for i := 0; i < b.N; i++ {
list.Text()
}
})
b.Run("attr", func(b *testing.B) {
var root = parse(b, `<title key=val></title>`)
var list = List{root}.Query("title")
for i := 0; i < b.N; i++ {
list.Attr(`title`)
}
})
}
func parse(t testing.TB, s string) *html.Node {
t.Helper()
root, err := html.Parse(strings.NewReader(s))
if err != nil {
t.Fatalf("parse: %s", err)
}
return root
}