-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector_test.go
194 lines (156 loc) · 5.97 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package css
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, "html", x.GetNode().Get(0).FirstChild.Data)
}
// 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, "html", x.GetNode().Get(0).FirstChild.Data)
}
// 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, "html", x.GetNode().Get(0).FirstChild.Data)
}
// TestSelector_Remove go test -v selector/*.go -run TestSelector_Remove
func TestSelector_Remove(t *testing.T) {
html := []byte(`<html class="123">....<div class="789">....</div><div class="456">....</div></html>`)
x, _ := NewSelectorFromBytes(html)
x.Remove("div")
assert.Equal(t, "<html class=\"123\"><head></head><body>....</body></html>", x.OutHtml(false))
}
// 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`)
t.Log(li)
assert.Equal(t, []string{"789", "456"}, li)
li = x.FindStrMany(`div@classs`)
t.Log(li)
assert.Equal(t, []string(nil), li)
}
// 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`)
t.Log(str)
assert.Equal(t, "789", str)
str = x.FindStrOne(`div@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`, "999")
t.Log(str)
assert.Equal(t, "789", str)
str = x.FindStrOneOr(`div@classs`, "999")
t.Log(str)
assert.Equal(t, "999", str)
}
// 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`)
t.Log(li)
assert.Equal(t, []int{789, 456}, li)
li = x.FindIntMany(`div@classs`)
t.Log(li)
assert.Equal(t, []int(nil), li)
}
// 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`)
t.Log(i)
assert.Equal(t, 789, i)
i = x.FindIntOne(`div@classs`)
t.Log(i)
assert.Equal(t, 0, i)
}
// 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`, 999)
t.Log(i)
assert.Equal(t, 789, i)
i = x.FindIntOneOr(`div@classs`, 999)
t.Log(i)
assert.Equal(t, 999, i)
}
// TestSelectorFindNodeManyFindStrMany go test -v selector/*.go -run TestSelectorFindNodeManyFindStrMany
func TestSelectorFindNodeManyFindStrMany(t *testing.T) {
html := `<div class="abc">1<div class="789">2</div><div class="456">3</div></div><div class="abc">4<div classs="789">5</div><div classs="jqk">6</div></div>`
x, _ := NewSelectorFromStr(html)
//for k, i := range x.FindNodeMany(`div.abc div`) {
// li := i.FindStrMany(`@class`)
// if k == 0 {
// assert.Equal(t, []string{"789"}, li)
// }
// if k == 1 {
// assert.Equal(t, []string{"456"}, li)
// }
//}
for k, i := range x.FindNodeMany(`div.abc div`) {
s := i.FindStrOne(`@classs`)
if k == 0 {
assert.Equal(t, "", s)
}
if k == 2 {
t.Log("s", s, i.OutHtml(false))
assert.Equal(t, "789", s)
}
}
}
// TestSelectorFindNodeOneFindStrMany go test -v selector/*.go -run TestSelectorFindNodeOneFindStrMany
func TestSelectorFindNodeOneFindStrMany(t *testing.T) {
html := `<div class="123">....<div class="789">....</div><div class="456">....</div></div><div class="123">....<div class="789">....</div><div class="457">....</div></div>`
x, _ := NewSelectorFromStr(html)
li := x.FindNodeOne(`div[class="123"]`).FindStrMany(`div@class`)
t.Log(li)
assert.Equal(t, []string{"789", "456"}, li)
li = x.FindNodeOneOr(`div[class="123"]`).FindStrMany(`@class`)
t.Log(li)
assert.Equal(t, []string{"123"}, li)
li = x.FindNodeMany(`div[class="123"]`)[1].FindStrMany(`@class`)
t.Log(li)
assert.Equal(t, []string{"123"}, li)
}
// TestSelectorOutHtml go test -v selector/*.go -run TestSelectorOutHtml
func TestSelectorOutHtml(t *testing.T) {
html := `<html class="123">....<div class="789">....</div><div class="jqk">...</div></html>`
x, _ := NewSelectorFromStr(html)
s := x.FindNodeOne(`div`).OutHtml(false)
t.Log(s)
assert.Equal(t, "....", s)
s = x.FindNodeOne(`div.jqk`).OutHtml(true)
t.Log(s)
assert.Equal(t, "....<div class=\"789\">....</div><div class=\"jqk\">...</div>", s)
}