-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector_test.go
179 lines (142 loc) · 5.54 KB
/
selector_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package xpath
import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
// go test -v selector/*.go
// TestSelectorFromStr go test -v selector/*.go -run TestSelectorFromStr
func TestSelectorFromStr(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
assert.Equal(t, x.GetNode().FirstChild.Data, "html")
}
// TestSelectorFromReader go test -v selector/*.go -run TestSelectorFromReader
func TestSelectorFromReader(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromReader(strings.NewReader(html))
assert.Equal(t, x.GetNode().FirstChild.Data, "html")
}
// TestSelectorFromFile go test -v selector/*.go -run TestSelectorFromFile
func TestSelectorFromFile(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
file, _ := os.CreateTemp(os.TempDir(), "")
defer func(name string) {
err := os.Remove(name)
if err != nil {
t.Log(err)
}
}(file.Name())
_, _ = file.WriteString(html)
x, _ := NewSelectorFromFile(file.Name())
assert.Equal(t, x.GetNode().FirstChild.Data, "html")
}
// TestSelectorFindStrMany go test -v selector/*.go -run TestSelectorFindStrMany
func TestSelectorFindStrMany(t *testing.T) {
html := []byte(`<html class="123">....<div class="789">....</div><div class="456">....</div></html>`)
x, _ := NewSelectorFromBytes(html)
li := x.FindStrMany(`//div[@class]/@class`)
t.Log(li)
assert.Equal(t, li, []string{"789", "456"})
li = x.FindStrMany(`//div[@class]/@classs`)
t.Log(li)
assert.Equal(t, li, []string(nil))
}
// TestSelectorFindStrOne go test -v selector/*.go -run TestSelectorFindStrOne
func TestSelectorFindStrOne(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
str := x.FindStrOne(`//div[@class]/@class`)
t.Log(str)
assert.Equal(t, str, "789")
str = x.FindStrOne(`//div[@class]/@classs`)
t.Log(str)
assert.Equal(t, str, "")
}
// TestSelectorFindStrOneOr go test -v selector/*.go -run TestSelectorFindStrOneOr
func TestSelectorFindStrOneOr(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
str := x.FindStrOneOr(`//div[@class]/@class`, "999")
t.Log(str)
assert.Equal(t, str, "789")
str = x.FindStrOneOr(`//div[@class]/@classs`, "999")
t.Log(str)
assert.Equal(t, str, "999")
}
// TestSelectorFindIntMany go test -v selector/*.go -run TestSelectorFindIntMany
func TestSelectorFindIntMany(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
li := x.FindIntMany(`//div[@class]/@class`)
t.Log(li)
assert.Equal(t, li, []int{789, 456})
li = x.FindIntMany(`//div[@class]/@classs`)
t.Log(li)
assert.Equal(t, li, []int(nil))
}
// TestSelectorFindIntOne go test -v selector/*.go -run TestSelectorFindIntOne
func TestSelectorFindIntOne(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
i := x.FindIntOne(`//div[@class]/@class`)
t.Log(i)
assert.Equal(t, i, 789)
i = x.FindIntOne(`//div[@class]/@classs`)
t.Log(i)
assert.Equal(t, i, 0)
}
// TestSelectorFindIntOneOr go test -v selector/*.go -run TestSelectorFindIntOneOr
func TestSelectorFindIntOneOr(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
i := x.FindIntOneOr(`//div[@class]/@class`, 999)
t.Log(i)
assert.Equal(t, i, 789)
i = x.FindIntOneOr(`//div[@class]/@classs`, 999)
t.Log(i)
assert.Equal(t, i, 999)
}
// TestSelectorFindNodeManyFindStrMany go test -v selector/*.go -run TestSelectorFindNodeManyFindStrMany
func TestSelectorFindNodeManyFindStrMany(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
for k, i := range x.FindNodeMany(`//div[@class]`) {
li := i.FindStrMany(`./@class`)
t.Log(li)
if k == 0 {
assert.Equal(t, li, []string{"789"})
}
if k == 1 {
assert.Equal(t, li, []string{"456"})
}
}
for _, i := range x.FindNodeMany(`//div[@classs]`) {
li := i.FindStrMany(`./@class`)
t.Log(li)
assert.Equal(t, li, []string(nil))
}
}
// TestSelectorFindNodeOneFindStrMany go test -v selector/*.go -run TestSelectorFindNodeOneFindStrMany
func TestSelectorFindNodeOneFindStrMany(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
li := x.FindNodeOne(`//div[@class]`).FindStrMany(`./@class`)
t.Log(li)
assert.Equal(t, li, []string{"789"})
li = x.FindNodeOneOr(`//div[@classs]`).FindStrMany(`./@class`)
t.Log(li)
assert.Equal(t, li, []string(nil))
}
// TestSelectorOutHtml go test -v selector/*.go -run TestSelectorOutHtml
func TestSelectorOutHtml(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="456">....</div></html>`
x, _ := NewSelectorFromStr(html)
s := x.FindNodeOne(`//html[@class]`).OutHtml(true)
t.Log(s)
assert.Equal(t, s, "<html class=\"123\"><head></head><body>....<div class=\"789\">....</div><div class=\"456\">....</div></body></html>")
s = x.FindNodeOne(`//html[@class]`).OutHtml(false)
t.Log(s)
assert.Equal(t, s, "<head></head><body>....<div class=\"789\">....</div><div class=\"456\">....</div></body>")
}