Mercurial > genshi > mirror
annotate markup/tests/path.py @ 179:13909179e5e1 trunk
Implemented support for XPath variables in predicates (#31).
author | cmlenz |
---|---|
date | Mon, 21 Aug 2006 17:25:19 +0000 |
parents | 1f6cb675a66c |
children | 94120e6b0ce4 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
38
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
38
diff
changeset
|
8 # are also available at http://markup.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
38
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
17 from markup.input import XML |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
18 from markup.path import Path, PathSyntaxError |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
19 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
20 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
21 class PathTestCase(unittest.TestCase): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
22 |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
23 def test_error_no_absolute_path(self): |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
24 self.assertRaises(PathSyntaxError, Path, '/root') |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
25 |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
26 def test_error_unsupported_axis(self): |
137 | 27 self.assertRaises(PathSyntaxError, Path, '..') |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
28 self.assertRaises(PathSyntaxError, Path, 'parent::ma') |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
29 |
164
1f6cb675a66c
Report error when position predicates are used in XPath expressions (which is NYI).
cmlenz
parents:
163
diff
changeset
|
30 def test_error_position_predicate(self): |
1f6cb675a66c
Report error when position predicates are used in XPath expressions (which is NYI).
cmlenz
parents:
163
diff
changeset
|
31 self.assertRaises(PathSyntaxError, Path, 'item[0]') |
1f6cb675a66c
Report error when position predicates are used in XPath expressions (which is NYI).
cmlenz
parents:
163
diff
changeset
|
32 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
33 def test_1step(self): |
38
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
34 xml = XML('<root><elem/></root>') |
137 | 35 |
36 path = Path('elem') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
37 self.assertEqual('<Path "child::elem">', repr(path)) |
137 | 38 self.assertEqual('<elem/>', path.select(xml).render()) |
39 | |
40 path = Path('child::elem') | |
41 self.assertEqual('<Path "child::elem">', repr(path)) | |
42 self.assertEqual('<elem/>', path.select(xml).render()) | |
43 | |
44 path = Path('//elem') | |
45 self.assertEqual('<Path "descendant-or-self::node()/child::elem">', | |
46 repr(path)) | |
47 self.assertEqual('<elem/>', path.select(xml).render()) | |
48 | |
49 path = Path('descendant::elem') | |
50 self.assertEqual('<Path "descendant::elem">', repr(path)) | |
51 self.assertEqual('<elem/>', path.select(xml).render()) | |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
52 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
53 def test_1step_self(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
54 xml = XML('<root><elem/></root>') |
137 | 55 |
56 path = Path('.') | |
57 self.assertEqual('<Path "self::node()">', repr(path)) | |
58 self.assertEqual('<root><elem/></root>', path.select(xml).render()) | |
59 | |
60 path = Path('self::node()') | |
61 self.assertEqual('<Path "self::node()">', repr(path)) | |
62 self.assertEqual('<root><elem/></root>', path.select(xml).render()) | |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
63 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
64 def test_1step_wildcard(self): |
38
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
65 xml = XML('<root><elem/></root>') |
137 | 66 |
67 path = Path('*') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
68 self.assertEqual('<Path "child::*">', repr(path)) |
137 | 69 self.assertEqual('<elem/>', path.select(xml).render()) |
70 | |
71 path = Path('child::*') | |
72 self.assertEqual('<Path "child::*">', repr(path)) | |
73 self.assertEqual('<elem/>', path.select(xml).render()) | |
74 | |
75 path = Path('child::node()') | |
76 self.assertEqual('<Path "child::node()">', repr(path)) | |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
77 self.assertEqual('<elem/>', Path('child::node()').select(xml).render()) |
137 | 78 |
79 path = Path('//*') | |
80 self.assertEqual('<Path "descendant-or-self::node()/child::*">', | |
81 repr(path)) | |
82 self.assertEqual('<elem/>', path.select(xml).render()) | |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
83 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
84 def test_1step_attribute(self): |
138 | 85 path = Path('@foo') |
86 self.assertEqual('<Path "attribute::foo">', repr(path)) | |
87 self.assertEqual('', path.select(XML('<root/>')).render()) | |
88 | |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
89 xml = XML('<root foo="bar"/>') |
138 | 90 self.assertEqual('bar', path.select(xml).render()) |
91 | |
92 path = Path('./@foo') | |
93 self.assertEqual('<Path "self::node()/attribute::foo">', repr(path)) | |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
94 self.assertEqual('bar', Path('./@foo').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
95 |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
96 def test_1step_text(self): |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
97 xml = XML('<root>Hey</root>') |
138 | 98 |
99 path = Path('text()') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
100 self.assertEqual('<Path "child::text()">', repr(path)) |
138 | 101 self.assertEqual('Hey', path.select(xml).render()) |
102 | |
103 path = Path('./text()') | |
104 self.assertEqual('<Path "self::node()/child::text()">', repr(path)) | |
105 self.assertEqual('Hey', path.select(xml).render()) | |
106 | |
107 path = Path('//text()') | |
108 self.assertEqual('<Path "descendant-or-self::node()/child::text()">', | |
109 repr(path)) | |
110 self.assertEqual('Hey', path.select(xml).render()) | |
111 | |
112 path = Path('.//text()') | |
113 self.assertEqual('<Path "self::node()/child::text()">', repr(path)) | |
114 self.assertEqual('Hey', path.select(xml).render()) | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
115 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
116 def test_2step(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
117 xml = XML('<root><foo/><bar/></root>') |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
118 self.assertEqual('<foo/><bar/>', Path('*').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
119 self.assertEqual('<bar/>', Path('bar').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
120 self.assertEqual('', Path('baz').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
121 |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
122 def test_2step_attribute(self): |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
123 xml = XML('<elem class="x"><span id="joe">Hey Joe</span></elem>') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
124 #self.assertEqual('x', Path('@*').select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
125 #self.assertEqual('x', Path('./@*').select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
126 #self.assertEqual('xjoe', Path('//@*').select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
127 self.assertEqual('joe', Path('*/@*').select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
128 |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
129 xml = XML('<elem><foo id="1"/>foo id="2"/></elem>') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
130 #self.assertEqual('', Path('@*').select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
131 #self.assertEqual('12', Path('foo/@*').select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
132 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
133 def test_2step_complex(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
134 xml = XML('<root><foo><bar/></foo></root>') |
138 | 135 |
136 path = Path('foo/bar') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
137 self.assertEqual('<Path "child::foo/child::bar">', repr(path)) |
138 | 138 self.assertEqual('<bar/>', path.select(xml).render()) |
139 | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
140 path = Path('./bar') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
141 self.assertEqual('<Path "self::node()/child::bar">', repr(path)) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
142 #self.assertEqual('', path.select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
143 |
138 | 144 path = Path('foo/*') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
145 self.assertEqual('<Path "child::foo/child::*">', repr(path)) |
138 | 146 self.assertEqual('<bar/>', path.select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
147 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
148 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
149 path = Path('./bar') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
150 self.assertEqual('<Path "self::node()/child::bar">', repr(path)) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
151 #self.assertEqual('<bar id="2"/>', path.select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
152 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
153 def test_2step_text(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
154 xml = XML('<root><item>Foo</item></root>') |
138 | 155 |
156 path = Path('item/text()') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
157 self.assertEqual('<Path "child::item/child::text()">', repr(path)) |
138 | 158 self.assertEqual('Foo', path.select(xml).render()) |
159 | |
160 path = Path('*/text()') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
161 self.assertEqual('<Path "child::*/child::text()">', repr(path)) |
138 | 162 self.assertEqual('Foo', path.select(xml).render()) |
163 | |
164 path = Path('//text()') | |
165 self.assertEqual('<Path "descendant-or-self::node()/child::text()">', | |
166 repr(path)) | |
167 self.assertEqual('Foo', path.select(xml).render()) | |
168 | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
169 path = Path('./text()') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
170 self.assertEqual('<Path "self::node()/child::text()">', repr(path)) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
171 #self.assertEqual('', path.select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
172 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
173 xml = XML('<root><item>Foo</item><item>Bar</item></root>') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
174 path = Path('item/text()') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
175 self.assertEqual('<Path "child::item/child::text()">', repr(path)) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
176 self.assertEqual('FooBar', path.select(xml).render()) |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
177 |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
178 xml = XML('<root><item>Foo</item><item>Bar</item></root>') |
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
179 self.assertEqual('FooBar', path.select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
180 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
181 def test_3step(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
182 xml = XML('<root><foo><bar/></foo></root>') |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
183 path = Path('foo/*') |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
184 self.assertEqual('<Path "child::foo/child::*">', |
138 | 185 repr(path)) |
186 self.assertEqual('<bar/>', path.select(xml).render()) | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
187 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
188 def test_3step_complex(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
189 xml = XML('<root><foo><bar/></foo></root>') |
138 | 190 path = Path('*/bar') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
191 self.assertEqual('<Path "child::*/child::bar">', repr(path)) |
138 | 192 self.assertEqual('<bar/>', path.select(xml).render()) |
193 | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
194 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
138 | 195 path = Path('//bar') |
196 self.assertEqual('<Path "descendant-or-self::node()/child::bar">', | |
197 repr(path)) | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
198 self.assertEqual('<bar id="1"/><bar id="2"/>', |
138 | 199 path.select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
200 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
201 def test_node_type_comment(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
202 xml = XML('<root><!-- commented --></root>') |
138 | 203 path = Path('comment()') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
204 self.assertEqual('<Path "child::comment()">', repr(path)) |
138 | 205 self.assertEqual('<!-- commented -->', path.select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
206 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
207 def test_node_type_text(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
208 xml = XML('<root>Some text <br/>in here.</root>') |
138 | 209 path = Path('text()') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
210 self.assertEqual('<Path "child::text()">', repr(path)) |
138 | 211 self.assertEqual('Some text in here.', path.select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
212 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
213 def test_node_type_node(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
214 xml = XML('<root>Some text <br/>in here.</root>') |
138 | 215 path = Path('node()') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
216 self.assertEqual('<Path "child::node()">', repr(path)) |
138 | 217 self.assertEqual('Some text <br/>in here.', path.select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
218 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
219 def test_node_type_processing_instruction(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
220 xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>') |
138 | 221 |
222 path = Path('processing-instruction()') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
223 self.assertEqual('<Path "child::processing-instruction()">', |
138 | 224 repr(path)) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
225 self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>', |
138 | 226 path.select(xml).render()) |
227 | |
228 path = Path('processing-instruction("php")') | |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
229 self.assertEqual('<Path "child::processing-instruction(\"php\")">', |
138 | 230 repr(path)) |
231 self.assertEqual('<?php echo("x") ?>', path.select(xml).render()) | |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
232 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
233 def test_simple_union(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
234 xml = XML('<root>Oh <foo>my</foo></root>') |
138 | 235 path = Path('*|text()') |
145
47bbd9d2a5af
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
236 self.assertEqual('<Path "child::*|child::text()">', |
138 | 237 repr(path)) |
238 self.assertEqual('Oh <foo>my</foo>', path.select(xml).render()) | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
239 |
121
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
240 def test_predicate_name(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
241 xml = XML('<root><foo/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
242 self.assertEqual('<foo/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
243 Path('*[name()="foo"]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
244 |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
245 def test_predicate_localname(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
246 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
247 self.assertEqual('<foo xmlns="NS"/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
248 Path('*[local-name()="foo"]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
249 |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
250 def test_predicate_namespace(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
251 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
252 self.assertEqual('<foo xmlns="NS"/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
253 Path('*[namespace-uri()="NS"]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
254 |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
255 def test_predicate_not_name(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
256 xml = XML('<root><foo/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
257 self.assertEqual('<bar/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
258 Path('*[not(name()="foo")]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
259 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
260 def test_predicate_attr(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
261 xml = XML('<root><item/><item important="very"/></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
262 self.assertEqual('<item important="very"/>', |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
263 Path('item[@important]').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
264 self.assertEqual('<item important="very"/>', |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
265 Path('item[@important="very"]').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
266 |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
267 def test_predicate_attr_equality(self): |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
268 xml = XML('<root><item/><item important="notso"/></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
269 self.assertEqual('', |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
270 Path('item[@important="very"]').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
271 self.assertEqual('<item/><item important="notso"/>', |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
272 Path('item[@important!="very"]').select(xml).render()) |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
273 |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
274 def test_predicate_attr_greater_than(self): |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
275 xml = XML('<root><item priority="3"/></root>') |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
276 self.assertEqual('', |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
277 Path('item[@priority>3]').select(xml).render()) |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
278 self.assertEqual('<item priority="3"/>', |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
279 Path('item[@priority>2]').select(xml).render()) |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
280 |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
281 def test_predicate_attr_less_than(self): |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
282 xml = XML('<root><item priority="3"/></root>') |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
283 self.assertEqual('', |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
284 Path('item[@priority<3]').select(xml).render()) |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
285 self.assertEqual('<item priority="3"/>', |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
286 Path('item[@priority<4]').select(xml).render()) |
1 | 287 |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
288 def test_predicate_attr_and(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
289 xml = XML('<root><item/><item important="very"/></root>') |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
290 path = Path('item[@important and @important="very"]') |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
291 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
292 path = Path('item[@important and @important="notso"]') |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
293 self.assertEqual('', path.select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
294 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
295 def test_predicate_attr_or(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
296 xml = XML('<root><item/><item important="very"/></root>') |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
297 path = Path('item[@urgent or @important]') |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
298 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
299 path = Path('item[@urgent or @notso]') |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
300 self.assertEqual('', path.select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
301 |
155
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
302 def test_predicate_boolean_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
303 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
304 path = Path('*[boolean("")]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
305 self.assertEqual('', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
306 path = Path('*[boolean("yo")]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
307 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
308 path = Path('*[boolean(0)]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
309 self.assertEqual('', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
310 path = Path('*[boolean(42)]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
311 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
312 path = Path('*[boolean(false())]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
313 self.assertEqual('', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
314 path = Path('*[boolean(true())]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
315 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
316 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
317 def test_predicate_ceil_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
318 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
319 path = Path('*[ceiling("4.5")=5]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
320 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
321 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
322 def test_predicate_concat_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
323 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
324 path = Path('*[name()=concat("f", "oo")]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
325 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
326 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
327 def test_predicate_contains_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
328 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
329 path = Path('*[contains(name(), "oo")]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
330 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
331 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
332 def test_predicate_false_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
333 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
334 path = Path('*[false()]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
335 self.assertEqual('', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
336 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
337 def test_predicate_floor_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
338 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
339 path = Path('*[floor("4.5")=4]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
340 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
341 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
342 def test_predicate_normalize_space_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
343 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
344 path = Path('*[normalize-space(" foo bar ")="foo bar"]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
345 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
346 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
347 def test_predicate_number_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
348 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
349 path = Path('*[number("3.0")=3]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
350 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
163
9c023c395e44
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
351 path = Path('*[number("3.0")=3.0]') |
9c023c395e44
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
352 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9c023c395e44
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
353 path = Path('*[number("0.1")=.1]') |
9c023c395e44
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
354 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
155
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
355 |
162
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
356 def test_predicate_round_function(self): |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
357 xml = XML('<root><foo>bar</foo></root>') |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
358 path = Path('*[round("4.4")=4]') |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
359 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
360 path = Path('*[round("4.6")=5]') |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
361 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
456039594db9
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
362 |
155
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
363 def test_predicate_starts_with_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
364 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
365 path = Path('*[starts-with(name(), "f")]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
366 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
367 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
368 def test_predicate_string_length_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
369 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
370 path = Path('*[string-length(name())=3]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
371 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
372 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
373 def test_predicate_substring_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
374 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
375 path = Path('*[substring(name(), 1)="oo"]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
376 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
377 path = Path('*[substring(name(), 1, 1)="o"]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
378 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
379 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
380 def test_predicate_substring_after_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
381 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
382 path = Path('*[substring-after(name(), "f")="oo"]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
383 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
384 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
385 def test_predicate_substring_before_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
386 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
387 path = Path('*[substring-before(name(), "oo")="f"]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
388 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
389 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
390 def test_predicate_translate_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
391 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
392 path = Path('*[translate(name(), "fo", "ba")="baa"]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
393 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
394 |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
395 def test_predicate_true_function(self): |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
396 xml = XML('<root><foo>bar</foo></root>') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
397 path = Path('*[true()]') |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
398 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9a5aedda1099
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
399 |
179
13909179e5e1
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
400 def test_predicate_variable(self): |
13909179e5e1
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
401 xml = XML('<root><foo>bar</foo></root>') |
13909179e5e1
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
402 path = Path('*[name()=$bar]') |
13909179e5e1
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
403 variables = {'bar': 'foo'} |
13909179e5e1
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
404 self.assertEqual('<foo>bar</foo>', path.select(xml, variables).render()) |
13909179e5e1
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
405 |
1 | 406 |
407 def suite(): | |
408 suite = unittest.TestSuite() | |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
409 suite.addTest(doctest.DocTestSuite(Path.__module__)) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
410 suite.addTest(unittest.makeSuite(PathTestCase, 'test')) |
1 | 411 return suite |
412 | |
413 if __name__ == '__main__': | |
414 unittest.main(defaultTest='suite') |