Debian Codegraph @

xpath_expression_test.go

raw
1 package xpath
2
3 import (
4 "testing"
5 )
6
7 func Test_descendant_issue(t *testing.T) {
8 // Issue #93 https://github.com/antchfx/xpath/issues/93
9 /*
10 <div id="wrapper">
11 <span>span one</span>
12 <div>
13 <span>span two</span>
14 </div>
15 </div>
16 */
17 doc := createNode("", RootNode)
18 div := doc.createChildNode("div", ElementNode)
19 div.lines = 1
20 div.addAttribute("id", "wrapper")
21 span := div.createChildNode("span", ElementNode)
22 span.lines = 2
23 span.createChildNode("span one", TextNode)
24 div = div.createChildNode("div", ElementNode)
25 div.lines = 3
26 span = div.createChildNode("span", ElementNode)
27 span.lines = 4
28 span.createChildNode("span two", TextNode)
29
30 test_xpath_elements(t, doc, `//div[@id='wrapper']/descendant::span[1]`, 2)
31 test_xpath_elements(t, doc, `//div[@id='wrapper']//descendant::span[1]`, 2, 4)
32 }
33
34 // https://github.com/antchfx/htmlquery/issues/52
35
36 func TestRelativePaths(t *testing.T) {
37 test_xpath_elements(t, book_example, `//bookstore`, 2)
38 test_xpath_elements(t, book_example, `//book`, 3, 9, 15, 25)
39 test_xpath_elements(t, book_example, `//bookstore/book`, 3, 9, 15, 25)
40 test_xpath_tags(t, book_example, `//book/..`, "bookstore")
41 test_xpath_elements(t, book_example, `//book[@category="cooking"]/..`, 2)
42 test_xpath_elements(t, book_example, `//book/year[text() = 2005]/../..`, 2) // bookstore
43 test_xpath_elements(t, book_example, `//book/year/../following-sibling::*`, 9, 15, 25)
44 test_xpath_count(t, book_example, `//bookstore/book/*`, 20)
45 test_xpath_tags(t, html_example, "//title/../..", "html")
46 test_xpath_elements(t, html_example, "//ul/../p", 19)
47 }
48
49 func TestAbsolutePaths(t *testing.T) {
50 test_xpath_elements(t, book_example, `bookstore`, 2)
51 test_xpath_elements(t, book_example, `bookstore/book`, 3, 9, 15, 25)
52 test_xpath_elements(t, book_example, `(bookstore/book)`, 3, 9, 15, 25)
53 test_xpath_elements(t, book_example, `bookstore/book[2]`, 9)
54 test_xpath_elements(t, book_example, `bookstore/book[last()]`, 25)
55 test_xpath_elements(t, book_example, `bookstore/book[last()]/title`, 26)
56 test_xpath_values(t, book_example, `/bookstore/book[last()]/title/text()`, "Learning XML")
57 test_xpath_values(t, book_example, `/bookstore/book[@category = "children"]/year`, "2005")
58 test_xpath_elements(t, book_example, `bookstore/book/..`, 2)
59 test_xpath_elements(t, book_example, `/bookstore/*`, 3, 9, 15, 25)
60 test_xpath_elements(t, book_example, `/bookstore/*/title`, 4, 10, 16, 26)
61 }
62
63 func TestAttributes(t *testing.T) {
64 test_xpath_tags(t, html_example.FirstChild, "@*", "lang")
65 test_xpath_count(t, employee_example, `//@*`, 9)
66 test_xpath_values(t, employee_example, `//@discipline`, "web", "DBA", "appdev")
67 test_xpath_count(t, employee_example, `//employee/@id`, 3)
68 }
69
70 func TestExpressions(t *testing.T) {
71 test_xpath_elements(t, book_example, `//book[@category = "cooking"] | //book[@category = "children"]`, 3, 9)
72 test_xpath_elements(t, book_example, `//book[@category = "web"] and //book[price = "39.95"]`, 25)
73 test_xpath_count(t, html_example, `//ul/*`, 3)
74 test_xpath_count(t, html_example, `//ul/*/a`, 3)
75 // Sequence
76 //
77 // table/tbody/tr/td/(para, .[not(para)], ..)
78 }
79
80 func TestSequence(t *testing.T) {
81 // `//table/tbody/tr/td/(para, .[not(para)],..)`
82 test_xpath_count(t, html_example, `//body/(h1, h2, p)`, 2)
83 test_xpath_count(t, html_example, `//body/(h1, h2, p, ..)`, 3)
84 }
85
86 func TestLatinAttributesInXPath(t *testing.T) {
87 doc := createNode("", RootNode)
88 div := doc.createChildNode("div", ElementNode)
89 div.addAttribute("language", "english")
90 div.lines = 1
91 test_xpath_elements(t, doc, `//div[@language='english']`, 1)
92 }
93
94 func TestCyrillicAttributesInXPath(t *testing.T) {
95 doc := createNode("", RootNode)
96 div := doc.createChildNode("div", ElementNode)
97 div.addAttribute("язык", "русский")
98 div.lines = 1
99 test_xpath_elements(t, doc, `//div[@язык='русский']`, 1)
100 }
101
102 func TestGreekAttributesInXPath(t *testing.T) {
103 doc := createNode("", RootNode)
104 div := doc.createChildNode("div", ElementNode)
105 div.addAttribute("γλώσσα", "ελληνικά")
106 div.lines = 1
107 test_xpath_elements(t, doc, `//div[@γλώσσα='ελληνικά']`, 1)
108 }
109
110 func TestCyrillicAndGreekAttributesMixedInXPath(t *testing.T) {
111 doc := createNode("", RootNode)
112 div := doc.createChildNode("div", ElementNode)
113 div.addAttribute("язык", "русский")
114 div.addAttribute("γλώσσα", "ελληνικά")
115 div.lines = 1
116 test_xpath_elements(t, doc, `//div[@язык='русский' and @γλώσσα='ελληνικά']`, 1)
117 }
118
119 func TestCyrillicAttributesInXPath_NoMatch(t *testing.T) {
120 doc := createNode("", RootNode)
121 div := doc.createChildNode("div", ElementNode)
122 div.addAttribute("язык", "русский")
123 div.lines = 1
124 test_xpath_elements(t, doc, `//div[@язык='английский']`)
125 }
126
127 func TestGreekAttributesInXPath_NoMatch(t *testing.T) {
128 doc := createNode("", RootNode)
129 div := doc.createChildNode("div", ElementNode)
130 div.addAttribute("γλώσσα", "ελληνικά")
131 div.lines = 1
132 test_xpath_elements(t, doc, `//div[@γλώσσα='αγγλικά']`)
133 }
134
135 func TestNonEnglishExpression(t *testing.T) {
136 doc := createNode("", RootNode)
137 n_1 := doc.createChildNode("Σειρά", ElementNode)
138 n_1.lines = 1
139 n_2 := n_1.createChildNode("ελληνικά", ElementNode)
140 n_2.lines = 2
141 n_2.createChildNode("hello", TextNode)
142 test_xpath_elements(t, doc, "//Σειρά", 1)
143 test_xpath_values(t, doc, "//Σειρά/ελληνικά", "hello")
144 }
145
146 func TestChineseCharactersExpression(t *testing.T) {
147 doc := createNode("", RootNode)
148 n := doc.createChildNode("中文", ElementNode)
149 n.createChildNode("你好世界", TextNode)
150 test_xpath_values(t, doc, "//中文", "你好世界")
151 }
152
153 func TestBUG_104(t *testing.T) {
154 // BUG https://github.com/antchfx/xpath/issues/104
155 test_xpath_count(t, book_example, `//author[1]`, 4)
156 test_xpath_values(t, book_example, `//author[1]/text()`, "Giada De Laurentiis", "J K. Rowling", "James McGovern", "Erik T. Ray")
157 }
158
159 func TestNonEnglishPredicateExpression(t *testing.T) {
160 // https://github.com/antchfx/xpath/issues/106
161 doc := createNode("", RootNode)
162 n := doc.createChildNode("h1", ElementNode)
163 n.addAttribute("id", "断点")
164 n.createChildNode("Hello,World!", TextNode)
165 test_xpath_count(t, doc, "//h1[@id='断点']", 1)
166 }