Mercurial > genshi > genshi-test
annotate genshi/tests/path.py @ 410:3460b04daeac
Improve the handling of namespaces in serialization.
author | cmlenz |
---|---|
date | Mon, 26 Feb 2007 18:26:59 +0000 |
parents | 1596045ff0f3 |
children | 41f29cbbf1c8 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
822089ae65ce
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 | |
230 | 8 # are also available at http://genshi.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 | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
230 | 17 from genshi.input import XML |
18 from genshi.path import Path, PathSyntaxError | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
19 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
20 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
21 class PathTestCase(unittest.TestCase): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
22 |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
23 def test_error_no_absolute_path(self): |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
24 self.assertRaises(PathSyntaxError, Path, '/root') |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
25 |
8a4d9064f363
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
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
28 self.assertRaises(PathSyntaxError, Path, 'parent::ma') |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
29 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
30 def test_1step(self): |
38
fec9f4897415
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
|
31 xml = XML('<root><elem/></root>') |
137 | 32 |
33 path = Path('elem') | |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
34 self.assertEqual('<Path "child::elem">', repr(path)) |
137 | 35 self.assertEqual('<elem/>', path.select(xml).render()) |
36 | |
37 path = Path('child::elem') | |
38 self.assertEqual('<Path "child::elem">', repr(path)) | |
39 self.assertEqual('<elem/>', path.select(xml).render()) | |
40 | |
41 path = Path('//elem') | |
42 self.assertEqual('<Path "descendant-or-self::node()/child::elem">', | |
43 repr(path)) | |
44 self.assertEqual('<elem/>', path.select(xml).render()) | |
45 | |
46 path = Path('descendant::elem') | |
47 self.assertEqual('<Path "descendant::elem">', repr(path)) | |
48 self.assertEqual('<elem/>', path.select(xml).render()) | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
49 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
50 def test_1step_self(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
51 xml = XML('<root><elem/></root>') |
137 | 52 |
53 path = Path('.') | |
54 self.assertEqual('<Path "self::node()">', repr(path)) | |
55 self.assertEqual('<root><elem/></root>', path.select(xml).render()) | |
56 | |
57 path = Path('self::node()') | |
58 self.assertEqual('<Path "self::node()">', repr(path)) | |
59 self.assertEqual('<root><elem/></root>', path.select(xml).render()) | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
60 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
61 def test_1step_wildcard(self): |
38
fec9f4897415
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
|
62 xml = XML('<root><elem/></root>') |
137 | 63 |
64 path = Path('*') | |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
65 self.assertEqual('<Path "child::*">', repr(path)) |
137 | 66 self.assertEqual('<elem/>', path.select(xml).render()) |
67 | |
68 path = Path('child::*') | |
69 self.assertEqual('<Path "child::*">', repr(path)) | |
70 self.assertEqual('<elem/>', path.select(xml).render()) | |
71 | |
72 path = Path('child::node()') | |
73 self.assertEqual('<Path "child::node()">', repr(path)) | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
74 self.assertEqual('<elem/>', Path('child::node()').select(xml).render()) |
137 | 75 |
76 path = Path('//*') | |
77 self.assertEqual('<Path "descendant-or-self::node()/child::*">', | |
78 repr(path)) | |
79 self.assertEqual('<elem/>', path.select(xml).render()) | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
80 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
81 def test_1step_attribute(self): |
138 | 82 path = Path('@foo') |
83 self.assertEqual('<Path "attribute::foo">', repr(path)) | |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
84 |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
85 xml = XML('<root/>') |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
86 self.assertEqual('', path.select(xml).render()) |
138 | 87 |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
88 xml = XML('<root foo="bar"/>') |
138 | 89 self.assertEqual('bar', path.select(xml).render()) |
90 | |
91 path = Path('./@foo') | |
92 self.assertEqual('<Path "self::node()/attribute::foo">', repr(path)) | |
234 | 93 self.assertEqual('bar', path.select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
94 |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
95 def test_1step_text(self): |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
96 xml = XML('<root>Hey</root>') |
138 | 97 |
98 path = Path('text()') | |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
99 self.assertEqual('<Path "child::text()">', repr(path)) |
138 | 100 self.assertEqual('Hey', path.select(xml).render()) |
101 | |
102 path = Path('./text()') | |
103 self.assertEqual('<Path "self::node()/child::text()">', repr(path)) | |
104 self.assertEqual('Hey', path.select(xml).render()) | |
105 | |
106 path = Path('//text()') | |
107 self.assertEqual('<Path "descendant-or-self::node()/child::text()">', | |
108 repr(path)) | |
109 self.assertEqual('Hey', path.select(xml).render()) | |
110 | |
111 path = Path('.//text()') | |
215 | 112 self.assertEqual('<Path "self::node()/descendant-or-self::node()/child::text()">', |
113 repr(path)) | |
138 | 114 self.assertEqual('Hey', path.select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
115 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
116 def test_2step(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
117 xml = XML('<root><foo/><bar/></root>') |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
118 self.assertEqual('<foo/><bar/>', Path('*').select(xml).render()) |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
119 self.assertEqual('<bar/>', Path('bar').select(xml).render()) |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
120 self.assertEqual('', Path('baz').select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
121 |
145
56d534eb53f9
* 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): |
56d534eb53f9
* 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>') |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
124 self.assertEqual('x', Path('@*').select(xml).render()) |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
125 self.assertEqual('x', Path('./@*').select(xml).render()) |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
126 self.assertEqual('xjoe', Path('.//@*').select(xml).render()) |
145
56d534eb53f9
* 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()) |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
128 |
215 | 129 xml = XML('<elem><foo id="1"/><foo id="2"/></elem>') |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
130 self.assertEqual('', Path('@*').select(xml).render()) |
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
131 self.assertEqual('12', Path('foo/@*').select(xml).render()) |
145
56d534eb53f9
* 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
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
133 def test_2step_complex(self): |
039fc5b87405
* 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
56d534eb53f9
* 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
56d534eb53f9
* 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') |
56d534eb53f9
* 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)) |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
142 self.assertEqual('', path.select(xml).render()) |
145
56d534eb53f9
* 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
56d534eb53f9
* 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
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
147 |
039fc5b87405
* 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
56d534eb53f9
* 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') |
56d534eb53f9
* 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)) |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
151 self.assertEqual('<bar id="2"/>', path.select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
152 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
153 def test_2step_text(self): |
039fc5b87405
* 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
56d534eb53f9
* 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
56d534eb53f9
* 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
56d534eb53f9
* 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()') |
56d534eb53f9
* 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)) |
216
0a01371cecbc
Many fixes to XPath evaluation. Among other things, this should get rid of the bug that attributes were getting ?pulled up? by `py:match` directives using `py:attrs="select('@*')"` (see #50).
cmlenz
parents:
215
diff
changeset
|
171 self.assertEqual('', path.select(xml).render()) |
145
56d534eb53f9
* 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
039fc5b87405
* 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
56d534eb53f9
* 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()') |
56d534eb53f9
* 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)) |
56d534eb53f9
* 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()) |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
177 |
56d534eb53f9
* 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>') |
56d534eb53f9
* 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
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
180 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
181 def test_3step(self): |
039fc5b87405
* 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
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
183 path = Path('foo/*') |
234 | 184 self.assertEqual('<Path "child::foo/child::*">', repr(path)) |
138 | 185 self.assertEqual('<bar/>', path.select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
186 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
187 def test_3step_complex(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
188 xml = XML('<root><foo><bar/></foo></root>') |
138 | 189 path = Path('*/bar') |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
190 self.assertEqual('<Path "child::*/child::bar">', repr(path)) |
138 | 191 self.assertEqual('<bar/>', path.select(xml).render()) |
192 | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
193 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
138 | 194 path = Path('//bar') |
195 self.assertEqual('<Path "descendant-or-self::node()/child::bar">', | |
196 repr(path)) | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
197 self.assertEqual('<bar id="1"/><bar id="2"/>', |
138 | 198 path.select(xml).render()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
199 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
200 def test_node_type_comment(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
201 xml = XML('<root><!-- commented --></root>') |
138 | 202 path = Path('comment()') |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
203 self.assertEqual('<Path "child::comment()">', repr(path)) |
138 | 204 self.assertEqual('<!-- commented -->', path.select(xml).render()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
205 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
206 def test_node_type_text(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
207 xml = XML('<root>Some text <br/>in here.</root>') |
138 | 208 path = Path('text()') |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
209 self.assertEqual('<Path "child::text()">', repr(path)) |
138 | 210 self.assertEqual('Some text in here.', path.select(xml).render()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
211 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
212 def test_node_type_node(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
213 xml = XML('<root>Some text <br/>in here.</root>') |
138 | 214 path = Path('node()') |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
215 self.assertEqual('<Path "child::node()">', repr(path)) |
138 | 216 self.assertEqual('Some text <br/>in here.', path.select(xml).render()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
217 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
218 def test_node_type_processing_instruction(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
219 xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>') |
138 | 220 |
221 path = Path('processing-instruction()') | |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
222 self.assertEqual('<Path "child::processing-instruction()">', |
138 | 223 repr(path)) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
224 self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>', |
138 | 225 path.select(xml).render()) |
226 | |
227 path = Path('processing-instruction("php")') | |
145
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
228 self.assertEqual('<Path "child::processing-instruction(\"php\")">', |
138 | 229 repr(path)) |
230 self.assertEqual('<?php echo("x") ?>', path.select(xml).render()) | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
231 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
232 def test_simple_union(self): |
259
6f11ad260890
Fix bug in evaluating XPath expressions using the union operator `|`, which caused any path but the first to get out of sync with the event stream, and the whole thing returning too few results.
cmlenz
parents:
234
diff
changeset
|
233 xml = XML("""<body>1<br />2<br />3<br /></body>""") |
138 | 234 path = Path('*|text()') |
234 | 235 self.assertEqual('<Path "child::*|child::text()">', repr(path)) |
259
6f11ad260890
Fix bug in evaluating XPath expressions using the union operator `|`, which caused any path but the first to get out of sync with the event stream, and the whole thing returning too few results.
cmlenz
parents:
234
diff
changeset
|
236 self.assertEqual('1<br/>2<br/>3<br/>', path.select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
237 |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
238 def test_predicate_name(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
239 xml = XML('<root><foo/><bar/></root>') |
234 | 240 path = Path('*[name()="foo"]') |
241 self.assertEqual('<foo/>', path.select(xml).render()) | |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
242 |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
243 def test_predicate_localname(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
244 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
234 | 245 path = Path('*[local-name()="foo"]') |
246 self.assertEqual('<foo xmlns="NS"/>', path.select(xml).render()) | |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
247 |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
248 def test_predicate_namespace(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
249 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
234 | 250 path = Path('*[namespace-uri()="NS"]') |
251 self.assertEqual('<foo xmlns="NS"/>', path.select(xml).render()) | |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
252 |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
253 def test_predicate_not_name(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
254 xml = XML('<root><foo/><bar/></root>') |
234 | 255 path = Path('*[not(name()="foo")]') |
256 self.assertEqual('<bar/>', path.select(xml).render()) | |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
257 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
258 def test_predicate_attr(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
259 xml = XML('<root><item/><item important="very"/></root>') |
234 | 260 path = Path('item[@important]') |
261 self.assertEqual('<item important="very"/>', path.select(xml).render()) | |
262 path = Path('item[@important="very"]') | |
263 self.assertEqual('<item important="very"/>', path.select(xml).render()) | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
264 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
265 def test_predicate_attr_equality(self): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
266 xml = XML('<root><item/><item important="notso"/></root>') |
234 | 267 path = Path('item[@important="very"]') |
268 self.assertEqual('', path.select(xml).render()) | |
269 path = Path('item[@important!="very"]') | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
270 self.assertEqual('<item/><item important="notso"/>', |
234 | 271 path.select(xml).render()) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
272 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
273 def test_predicate_attr_greater_than(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
274 xml = XML('<root><item priority="3"/></root>') |
234 | 275 path = Path('item[@priority>3]') |
276 self.assertEqual('', path.select(xml).render()) | |
277 path = Path('item[@priority>2]') | |
278 self.assertEqual('<item priority="3"/>', path.select(xml).render()) | |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
279 |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
280 def test_predicate_attr_less_than(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
281 xml = XML('<root><item priority="3"/></root>') |
234 | 282 path = Path('item[@priority<3]') |
283 self.assertEqual('', path.select(xml).render()) | |
284 path = Path('item[@priority<4]') | |
285 self.assertEqual('<item priority="3"/>', path.select(xml).render()) | |
1 | 286 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
287 def test_predicate_attr_and(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
288 xml = XML('<root><item/><item important="very"/></root>') |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
289 path = Path('item[@important and @important="very"]') |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
290 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
291 path = Path('item[@important and @important="notso"]') |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
292 self.assertEqual('', path.select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
293 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
294 def test_predicate_attr_or(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
295 xml = XML('<root><item/><item important="very"/></root>') |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
296 path = Path('item[@urgent or @important]') |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
297 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
298 path = Path('item[@urgent or @notso]') |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
299 self.assertEqual('', path.select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
300 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
301 def test_predicate_boolean_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
302 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
303 path = Path('*[boolean("")]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
304 self.assertEqual('', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
305 path = Path('*[boolean("yo")]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
306 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
307 path = Path('*[boolean(0)]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
308 self.assertEqual('', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
309 path = Path('*[boolean(42)]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
310 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
311 path = Path('*[boolean(false())]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
312 self.assertEqual('', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
313 path = Path('*[boolean(true())]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
314 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
315 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
316 def test_predicate_ceil_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
317 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
318 path = Path('*[ceiling("4.5")=5]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
319 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
320 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
321 def test_predicate_concat_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
322 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
323 path = Path('*[name()=concat("f", "oo")]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
324 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
325 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
326 def test_predicate_contains_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
327 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
328 path = Path('*[contains(name(), "oo")]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
329 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
330 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
331 def test_predicate_false_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
332 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
333 path = Path('*[false()]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
334 self.assertEqual('', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
335 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
336 def test_predicate_floor_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
337 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
338 path = Path('*[floor("4.5")=4]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
339 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
340 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
341 def test_predicate_normalize_space_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
342 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
343 path = Path('*[normalize-space(" foo bar ")="foo bar"]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
344 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
345 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
346 def test_predicate_number_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
347 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
348 path = Path('*[number("3.0")=3]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
349 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
163
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
350 path = Path('*[number("3.0")=3.0]') |
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
351 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
352 path = Path('*[number("0.1")=.1]') |
9df6f057efd3
Support for XPath number literals including decimal places.
cmlenz
parents:
162
diff
changeset
|
353 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
354 |
162
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
355 def test_predicate_round_function(self): |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
356 xml = XML('<root><foo>bar</foo></root>') |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
357 path = Path('*[round("4.4")=4]') |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
358 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
359 path = Path('*[round("4.6")=5]') |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
360 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
f767cf98e3e3
Implement the XPath relational operators and the `round()` function.
cmlenz
parents:
155
diff
changeset
|
361 |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
362 def test_predicate_starts_with_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
363 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
364 path = Path('*[starts-with(name(), "f")]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
365 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
282
db5bdeca9192
Fix `starts-with()` XPath function so that it actually compares the two strings. Closes #61.
cmlenz
parents:
259
diff
changeset
|
366 path = Path('*[starts-with(name(), "b")]') |
db5bdeca9192
Fix `starts-with()` XPath function so that it actually compares the two strings. Closes #61.
cmlenz
parents:
259
diff
changeset
|
367 self.assertEqual('', path.select(xml).render()) |
155
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
368 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
369 def test_predicate_string_length_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
370 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
371 path = Path('*[string-length(name())=3]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
372 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
373 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
374 def test_predicate_substring_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
375 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
376 path = Path('*[substring(name(), 1)="oo"]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
377 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
378 path = Path('*[substring(name(), 1, 1)="o"]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
379 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
380 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
381 def test_predicate_substring_after_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
382 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
383 path = Path('*[substring-after(name(), "f")="oo"]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
384 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
385 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
386 def test_predicate_substring_before_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
387 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
388 path = Path('*[substring-before(name(), "oo")="f"]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
389 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
390 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
391 def test_predicate_translate_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
392 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
393 path = Path('*[translate(name(), "fo", "ba")="baa"]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
394 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
395 |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
396 def test_predicate_true_function(self): |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
397 xml = XML('<root><foo>bar</foo></root>') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
398 path = Path('*[true()]') |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
399 self.assertEqual('<foo>bar</foo>', path.select(xml).render()) |
50d4b08017df
* String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents:
145
diff
changeset
|
400 |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
401 def test_predicate_variable(self): |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
402 xml = XML('<root><foo>bar</foo></root>') |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
403 path = Path('*[name()=$bar]') |
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
404 variables = {'bar': 'foo'} |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
405 self.assertEqual('<foo>bar</foo>', |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
406 path.select(xml, variables=variables).render()) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
407 |
228
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
408 def test_predicate_position(self): |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
409 xml = XML('<root><foo id="a1"/><foo id="a2"/><foo id="a3"/></root>') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
410 path = Path('*[2]') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
411 self.assertEqual('<foo id="a2"/>', path.select(xml).render()) |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
412 |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
413 def test_predicate_attr_and_position(self): |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
414 xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
415 path = Path('*[@id][2]') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
416 self.assertEqual('<foo id="a2"/>', path.select(xml).render()) |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
417 |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
418 def test_predicate_position_and_attr(self): |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
419 xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
420 path = Path('*[1][@id]') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
421 self.assertEqual('', path.select(xml).render()) |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
422 path = Path('*[2][@id]') |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
423 self.assertEqual('<foo id="a1"/>', path.select(xml).render()) |
f79b20a50919
Add support for position predicates in XPath expressions.
cmlenz
parents:
224
diff
changeset
|
424 |
224
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
425 def test_name_with_namespace(self): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
426 xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>') |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
427 path = Path('f:foo') |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
428 self.assertEqual('<Path "child::f:foo">', repr(path)) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
429 namespaces = {'f': 'FOO'} |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
430 self.assertEqual('<foo xmlns="FOO">bar</foo>', |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
431 path.select(xml, namespaces=namespaces).render()) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
432 |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
433 def test_wildcard_with_namespace(self): |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
434 xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>') |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
435 path = Path('f:*') |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
436 self.assertEqual('<Path "child::f:*">', repr(path)) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
437 namespaces = {'f': 'FOO'} |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
438 self.assertEqual('<foo xmlns="FOO">bar</foo>', |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
439 path.select(xml, namespaces=namespaces).render()) |
e4dad1145f84
Implement support for namespace prefixes in XPath expressions.
cmlenz
parents:
216
diff
changeset
|
440 |
384 | 441 def test_predicate_termination(self): |
442 """ | |
443 Verify that a patch matching the self axis with a predicate doesn't | |
444 cause an infinite loop. See <http://genshi.edgewall.org/ticket/82>. | |
445 """ | |
446 xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>') | |
447 path = Path('.[@flag="1"]/*') | |
448 self.assertEqual('<li>a</li><li>b</li>', path.select(xml).render()) | |
449 | |
450 xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>') | |
451 path = Path('.[@flag="0"]/*') | |
452 self.assertEqual('', path.select(xml).render()) | |
453 | |
410
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
454 def test_attrname_with_namespace(self): |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
455 xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
456 path = Path('foo[@f:bar]') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
457 self.assertEqual('<foo ns1:bar="baz"/>', |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
458 path.select(xml, namespaces={'f': 'FOO'}).render()) |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
459 |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
460 def test_attrwildcard_with_namespace(self): |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
461 xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
462 path = Path('foo[@f:*]') |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
463 self.assertEqual('<foo ns1:bar="baz"/>', |
3460b04daeac
Improve the handling of namespaces in serialization.
cmlenz
parents:
384
diff
changeset
|
464 path.select(xml, namespaces={'f': 'FOO'}).render()) |
179
a2e0a7986d19
Implemented support for XPath variables in predicates (#31).
cmlenz
parents:
164
diff
changeset
|
465 |
1 | 466 |
467 def suite(): | |
468 suite = unittest.TestSuite() | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
469 suite.addTest(doctest.DocTestSuite(Path.__module__)) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
470 suite.addTest(unittest.makeSuite(PathTestCase, 'test')) |
1 | 471 return suite |
472 | |
473 if __name__ == '__main__': | |
474 unittest.main(defaultTest='suite') |