-
Notifications
You must be signed in to change notification settings - Fork 89
/
xpath_expression_test.go
157 lines (139 loc) · 5.82 KB
/
xpath_expression_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
package xpath
import (
"testing"
)
func Test_descendant_issue(t *testing.T) {
// Issue #93 https://github.com/antchfx/xpath/issues/93
/*
<div id="wrapper">
<span>span one</span>
<div>
<span>span two</span>
</div>
</div>
*/
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.lines = 1
div.addAttribute("id", "wrapper")
span := div.createChildNode("span", ElementNode)
span.lines = 2
span.createChildNode("span one", TextNode)
div = div.createChildNode("div", ElementNode)
div.lines = 3
span = div.createChildNode("span", ElementNode)
span.lines = 4
span.createChildNode("span two", TextNode)
test_xpath_elements(t, doc, `//div[@id='wrapper']/descendant::span[1]`, 2)
test_xpath_elements(t, doc, `//div[@id='wrapper']//descendant::span[1]`, 2, 4)
}
// https://github.com/antchfx/htmlquery/issues/52
func TestRelativePaths(t *testing.T) {
test_xpath_elements(t, book_example, `//bookstore`, 2)
test_xpath_elements(t, book_example, `//book`, 3, 9, 15, 25)
test_xpath_elements(t, book_example, `//bookstore/book`, 3, 9, 15, 25)
test_xpath_tags(t, book_example, `//book/..`, "bookstore")
test_xpath_elements(t, book_example, `//book[@category="cooking"]/..`, 2)
test_xpath_elements(t, book_example, `//book/year[text() = 2005]/../..`, 2) // bookstore
test_xpath_elements(t, book_example, `//book/year/../following-sibling::*`, 9, 15, 25)
test_xpath_count(t, book_example, `//bookstore/book/*`, 20)
test_xpath_tags(t, html_example, "//title/../..", "html")
test_xpath_elements(t, html_example, "//ul/../p", 19)
}
func TestAbsolutePaths(t *testing.T) {
test_xpath_elements(t, book_example, `bookstore`, 2)
test_xpath_elements(t, book_example, `bookstore/book`, 3, 9, 15, 25)
test_xpath_elements(t, book_example, `(bookstore/book)`, 3, 9, 15, 25)
test_xpath_elements(t, book_example, `bookstore/book[2]`, 9)
test_xpath_elements(t, book_example, `bookstore/book[last()]`, 25)
test_xpath_elements(t, book_example, `bookstore/book[last()]/title`, 26)
test_xpath_values(t, book_example, `/bookstore/book[last()]/title/text()`, "Learning XML")
test_xpath_values(t, book_example, `/bookstore/book[@category = "children"]/year`, "2005")
test_xpath_elements(t, book_example, `bookstore/book/..`, 2)
test_xpath_elements(t, book_example, `/bookstore/*`, 3, 9, 15, 25)
test_xpath_elements(t, book_example, `/bookstore/*/title`, 4, 10, 16, 26)
}
func TestAttributes(t *testing.T) {
test_xpath_tags(t, html_example.FirstChild, "@*", "lang")
test_xpath_count(t, employee_example, `//@*`, 9)
test_xpath_values(t, employee_example, `//@discipline`, "web", "DBA", "appdev")
test_xpath_count(t, employee_example, `//employee/@id`, 3)
}
func TestExpressions(t *testing.T) {
test_xpath_elements(t, book_example, `//book[@category = "cooking"] | //book[@category = "children"]`, 3, 9)
test_xpath_elements(t, book_example, `//book[@category = "web"] and //book[price = "39.95"]`, 25)
test_xpath_count(t, html_example, `//ul/*`, 3)
test_xpath_count(t, html_example, `//ul/*/a`, 3)
// Sequence
//
// table/tbody/tr/td/(para, .[not(para)], ..)
}
func TestSequence(t *testing.T) {
// `//table/tbody/tr/td/(para, .[not(para)],..)`
test_xpath_count(t, html_example, `//body/(h1, h2, p)`, 2)
test_xpath_count(t, html_example, `//body/(h1, h2, p, ..)`, 3)
}
func TestLatinAttributesInXPath(t *testing.T) {
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.addAttribute("language", "english")
div.lines = 1
test_xpath_elements(t, doc, `//div[@language='english']`, 1)
}
func TestCyrillicAttributesInXPath(t *testing.T) {
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.addAttribute("язык", "русский")
div.lines = 1
test_xpath_elements(t, doc, `//div[@язык='русский']`, 1)
}
func TestGreekAttributesInXPath(t *testing.T) {
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.addAttribute("γλώσσα", "ελληνικά")
div.lines = 1
test_xpath_elements(t, doc, `//div[@γλώσσα='ελληνικά']`, 1)
}
func TestCyrillicAndGreekAttributesMixedInXPath(t *testing.T) {
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.addAttribute("язык", "русский")
div.addAttribute("γλώσσα", "ελληνικά")
div.lines = 1
test_xpath_elements(t, doc, `//div[@язык='русский' and @γλώσσα='ελληνικά']`, 1)
}
func TestCyrillicAttributesInXPath_NoMatch(t *testing.T) {
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.addAttribute("язык", "русский")
div.lines = 1
test_xpath_elements(t, doc, `//div[@язык='английский']`)
}
func TestGreekAttributesInXPath_NoMatch(t *testing.T) {
doc := createNode("", RootNode)
div := doc.createChildNode("div", ElementNode)
div.addAttribute("γλώσσα", "ελληνικά")
div.lines = 1
test_xpath_elements(t, doc, `//div[@γλώσσα='αγγλικά']`)
}
func TestNonEnglishExpression(t *testing.T) {
doc := createNode("", RootNode)
n_1 := doc.createChildNode("Σειρά", ElementNode)
n_1.lines = 1
n_2 := n_1.createChildNode("ελληνικά", ElementNode)
n_2.lines = 2
n_2.createChildNode("hello", TextNode)
test_xpath_elements(t, doc, "//Σειρά", 1)
test_xpath_values(t, doc, "//Σειρά/ελληνικά", "hello")
}
func TestChineseCharactersExpression(t *testing.T) {
doc := createNode("", RootNode)
n := doc.createChildNode("中文", ElementNode)
n.createChildNode("你好世界", TextNode)
test_xpath_values(t, doc, "//中文", "你好世界")
}
func TestBUG_104(t *testing.T) {
// BUG https://github.com/antchfx/xpath/issues/104
test_xpath_count(t, book_example, `//author[1]`, 4)
test_xpath_values(t, book_example, `//author[1]/text()`, "Giada De Laurentiis", "J K. Rowling", "James McGovern", "Erik T. Ray")
}