Debian Codegraph @

xpath_axes_test.go

raw
1 package xpath
2
3 import "testing"
4
5 func Test_self(t *testing.T) {
6 test_xpath_elements(t, employee_example, `//name/self::*`, 4, 9, 14)
7 }
8
9 func Test_child(t *testing.T) {
10 test_xpath_elements(t, employee_example, `//child::employee/child::email`, 6, 11, 16)
11 test_xpath_elements(t, employee_example, `/empinfo/child::*`, 3, 8, 13)
12 test_xpath_elements(t, employee_example, `/empinfo/child::node()`, 3, 8, 13)
13 test_xpath_values(t, employee_example, `//name/child::text()`, "Opal Kole", "Max Miller", "Beccaa Moss")
14 }
15
16 func Test_descendant(t *testing.T) {
17 test_xpath_elements(t, employee_example, `//employee/descendant::*`, 4, 5, 6, 9, 10, 11, 14, 15, 16)
18 test_xpath_count(t, employee_example, `//descendant::employee`, 3)
19
20 }
21
22 func Test_descendant_or_self(t *testing.T) {
23 test_xpath_tags(t, employee_example.FirstChild, `self::*`, "empinfo")
24 test_xpath_elements(t, employee_example, `//employee/descendant-or-self::*`, 3, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15, 16)
25 test_xpath_count(t, employee_example, `//descendant-or-self::employee`, 3)
26 }
27
28 func Test_ancestor(t *testing.T) {
29 test_xpath_tags(t, employee_example, `//employee/ancestor::*`, "empinfo")
30 test_xpath_tags(t, employee_example, `//employee/ancestor::empinfo`, "empinfo")
31 // Test Panic
32 //test_xpath_elements(t, employee_example, `//ancestor::name`, 4, 9, 14)
33 }
34
35 func Test_ancestor_predicate(t *testing.T) {
36 doc := createElement(0, "",
37 createElement(1, "html",
38 createElement(2, "body",
39 createElement(3, "h1"),
40 createElement(4, "section",
41 createElement(5, "div",
42 createElement(6, "section",
43 createElement(7, "div",
44 createElement(8, "span"),
45 ),
46 ),
47 ),
48 ),
49 createElement(9, "section",
50 createElement(10, "div",
51 createElement(11, "section",
52 createElement(12, "div",
53 createElement(13, "span"),
54 ),
55 ),
56 ),
57 ),
58 ),
59 ),
60 )
61
62 test_xpath_elements(t, doc, `//span/ancestor::*`, 7, 6, 5, 4, 2, 1, 12, 11, 10, 9)
63 test_xpath_elements(t, doc, `//span/ancestor::section`, 6, 4, 11, 9)
64 test_xpath_elements(t, doc, `//span/ancestor::section[1]`, 6, 11)
65 test_xpath_elements(t, doc, `//span/ancestor::section[2]`, 4, 9)
66 }
67
68 func Test_ancestor_predicate_chain(t *testing.T) {
69 doc := createElement(0, "",
70 createElement(1, "html",
71 createElementAttr(2, "body", map[string]string{"itemscope": "", "itemtype": "Article"},
72 createElement(3, "section",
73 createElementAttr(4, "span", map[string]string{"itemprop": "author"}),
74 createElementAttr(5, "div", map[string]string{"itemscope": "", "itemtype": "Comment"},
75 createElementAttr(6, "span", map[string]string{"itemprop": "author"}),
76 createElement(7, "div",
77 createElementAttr(8, "span", map[string]string{"itemprop": "author"}),
78 ),
79 ),
80 ),
81 ),
82 ),
83 )
84
85 // Find elements marked as "author" property whose closest "itemscope" ancestor is of "Comment" type.
86 // This should find "span" elements on lines 6 and 8, but not line 4 since that one is under "Article".
87 test_xpath_elements(t, doc, `//*[@itemprop="author"][ancestor::*[@itemscope][1][@itemtype="Comment"]]`, 6, 8)
88 }
89
90 func Test_ancestor_or_self(t *testing.T) {
91 // Expected the value is [2, 3, 8, 13], but got [3, 2, 8, 13]
92 test_xpath_elements(t, employee_example, `//employee/ancestor-or-self::*`, 3, 2, 8, 13)
93 test_xpath_elements(t, employee_example, `//name/ancestor-or-self::employee`, 3, 8, 13)
94 }
95
96 func Test_parent(t *testing.T) {
97 test_xpath_elements(t, employee_example, `//name/parent::*`, 3, 8, 13)
98 test_xpath_elements(t, employee_example, `//name/parent::employee`, 3, 8, 13)
99 }
100
101 func Test_attribute(t *testing.T) {
102 test_xpath_values(t, employee_example, `//attribute::id`, "1", "2", "3")
103 test_xpath_count(t, employee_example, `//attribute::*`, 9)
104
105 // test failed
106 //test_xpath_tags(t, employee_example, `//attribute::*[1]`, "id", "discipline", "id", "from", "discipline", "id", "discipline")
107 // test failed(random): the return values is expected but the order of value is random.
108 //test_xpath_tags(t, employee_example, `//attribute::*`, "id", "discipline", "experience", "id", "from", "discipline", "experience", "id", "discipline")
109
110 }
111
112 func Test_following(t *testing.T) {
113 test_xpath_elements(t, employee_example, `//employee[@id=1]/following::*`, 8, 9, 10, 11, 13, 14, 15, 16)
114 }
115
116 func Test_following_sibling(t *testing.T) {
117 test_xpath_elements(t, employee_example, `//employee[@id=1]/following-sibling::*`, 8, 13)
118 test_xpath_elements(t, employee_example, `//employee[@id=1]/following-sibling::employee`, 8, 13)
119 }
120
121 func Test_preceding(t *testing.T) {
122 //testXPath3(t, html, "//li[last()]/preceding-sibling::*[2]", selectNode(html, "//li[position()=2]"))
123 //testXPath3(t, html, "//li/preceding::*[1]", selectNode(html, "//h1"))
124 test_xpath_elements(t, employee_example, `//employee[@id=3]/preceding::*`, 8, 9, 10, 11, 3, 4, 5, 6)
125 }
126
127 func Test_preceding_sibling(t *testing.T) {
128 test_xpath_elements(t, employee_example, `//employee[@id=3]/preceding-sibling::*`, 8, 3)
129 }
130
131 func Test_namespace(t *testing.T) {
132 // TODO
133 }