-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathxpath_expression_test.go
83 lines (74 loc) · 3.17 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
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_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)
}