Mercurial > genshi > genshi-test
annotate markup/tests/path.py @ 145:56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
* The first location step in path expressions longer assumes the `descendant::` axis, but rather the `child::` axis.
* Import cleanups.
author | cmlenz |
---|---|
date | Tue, 15 Aug 2006 09:52:47 +0000 |
parents | d91e1e822969 |
children | 50d4b08017df |
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 | |
66
822089ae65ce
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
822089ae65ce
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
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
17 from markup.input import XML |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
18 from markup.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)) | |
84 self.assertEqual('', path.select(XML('<root/>')).render()) | |
85 | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
86 xml = XML('<root foo="bar"/>') |
138 | 87 self.assertEqual('bar', path.select(xml).render()) |
88 | |
89 path = Path('./@foo') | |
90 self.assertEqual('<Path "self::node()/attribute::foo">', repr(path)) | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
91 self.assertEqual('bar', Path('./@foo').select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
92 |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
93 def test_1step_text(self): |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
94 xml = XML('<root>Hey</root>') |
138 | 95 |
96 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
|
97 self.assertEqual('<Path "child::text()">', repr(path)) |
138 | 98 self.assertEqual('Hey', path.select(xml).render()) |
99 | |
100 path = Path('./text()') | |
101 self.assertEqual('<Path "self::node()/child::text()">', repr(path)) | |
102 self.assertEqual('Hey', path.select(xml).render()) | |
103 | |
104 path = Path('//text()') | |
105 self.assertEqual('<Path "descendant-or-self::node()/child::text()">', | |
106 repr(path)) | |
107 self.assertEqual('Hey', path.select(xml).render()) | |
108 | |
109 path = Path('.//text()') | |
110 self.assertEqual('<Path "self::node()/child::text()">', repr(path)) | |
111 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
|
112 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
113 def test_2step(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
114 xml = XML('<root><foo/><bar/></root>') |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
115 self.assertEqual('<foo/><bar/>', Path('*').select(xml).render()) |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
116 self.assertEqual('<bar/>', Path('bar').select(xml).render()) |
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
117 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
|
118 |
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
|
119 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
|
120 xml = XML('<elem class="x"><span id="joe">Hey Joe</span></elem>') |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
121 #self.assertEqual('x', 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
|
122 #self.assertEqual('x', 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
|
123 #self.assertEqual('xjoe', 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
|
124 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
|
125 |
56d534eb53f9
* Fix error in expression evaluation when the expression evaluates to an iterable that does not produce event tuples.
cmlenz
parents:
138
diff
changeset
|
126 xml = XML('<elem><foo id="1"/>foo id="2"/></elem>') |
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('', 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 #self.assertEqual('12', Path('foo/@*').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
|
129 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
130 def test_2step_complex(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
131 xml = XML('<root><foo><bar/></foo></root>') |
138 | 132 |
133 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
|
134 self.assertEqual('<Path "child::foo/child::bar">', repr(path)) |
138 | 135 self.assertEqual('<bar/>', path.select(xml).render()) |
136 | |
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 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
|
138 self.assertEqual('<Path "self::node()/child::bar">', 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
|
139 #self.assertEqual('', 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
|
140 |
138 | 141 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
|
142 self.assertEqual('<Path "child::foo/child::*">', repr(path)) |
138 | 143 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
|
144 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
145 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
|
146 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
|
147 self.assertEqual('<Path "self::node()/child::bar">', 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
|
148 #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
|
149 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
150 def test_2step_text(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
151 xml = XML('<root><item>Foo</item></root>') |
138 | 152 |
153 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
|
154 self.assertEqual('<Path "child::item/child::text()">', repr(path)) |
138 | 155 self.assertEqual('Foo', path.select(xml).render()) |
156 | |
157 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
|
158 self.assertEqual('<Path "child::*/child::text()">', repr(path)) |
138 | 159 self.assertEqual('Foo', path.select(xml).render()) |
160 | |
161 path = Path('//text()') | |
162 self.assertEqual('<Path "descendant-or-self::node()/child::text()">', | |
163 repr(path)) | |
164 self.assertEqual('Foo', path.select(xml).render()) | |
165 | |
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
|
166 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
|
167 self.assertEqual('<Path "self::node()/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
|
168 #self.assertEqual('', 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
|
169 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
170 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
|
171 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
|
172 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
|
173 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
|
174 |
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 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
|
176 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
|
177 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
178 def test_3step(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
179 xml = XML('<root><foo><bar/></foo></root>') |
138 | 180 path = Path('root/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
|
181 self.assertEqual('<Path "child::root/child::foo/child::*">', |
138 | 182 repr(path)) |
183 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
|
184 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
185 def test_3step_complex(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
186 xml = XML('<root><foo><bar/></foo></root>') |
138 | 187 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
|
188 self.assertEqual('<Path "child::*/child::bar">', repr(path)) |
138 | 189 self.assertEqual('<bar/>', path.select(xml).render()) |
190 | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
191 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
138 | 192 path = Path('//bar') |
193 self.assertEqual('<Path "descendant-or-self::node()/child::bar">', | |
194 repr(path)) | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
195 self.assertEqual('<bar id="1"/><bar id="2"/>', |
138 | 196 path.select(xml).render()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
197 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
198 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
|
199 xml = XML('<root><!-- commented --></root>') |
138 | 200 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
|
201 self.assertEqual('<Path "child::comment()">', repr(path)) |
138 | 202 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
|
203 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
204 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
|
205 xml = XML('<root>Some text <br/>in here.</root>') |
138 | 206 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
|
207 self.assertEqual('<Path "child::text()">', repr(path)) |
138 | 208 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
|
209 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
210 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
|
211 xml = XML('<root>Some text <br/>in here.</root>') |
138 | 212 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
|
213 self.assertEqual('<Path "child::node()">', repr(path)) |
138 | 214 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
|
215 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
216 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
|
217 xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>') |
138 | 218 |
219 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
|
220 self.assertEqual('<Path "child::processing-instruction()">', |
138 | 221 repr(path)) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
222 self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>', |
138 | 223 path.select(xml).render()) |
224 | |
225 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
|
226 self.assertEqual('<Path "child::processing-instruction(\"php\")">', |
138 | 227 repr(path)) |
228 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
|
229 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
230 def test_simple_union(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
231 xml = XML('<root>Oh <foo>my</foo></root>') |
138 | 232 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
|
233 self.assertEqual('<Path "child::*|child::text()">', |
138 | 234 repr(path)) |
235 self.assertEqual('Oh <foo>my</foo>', path.select(xml).render()) | |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
236 |
121
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
237 def test_predicate_name(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
238 xml = XML('<root><foo/><bar/></root>') |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
239 self.assertEqual('<foo/>', |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
240 Path('*[name()="foo"]').select(xml).render()) |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
241 |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
242 def test_predicate_localname(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
243 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
244 self.assertEqual('<foo xmlns="NS"/>', |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
245 Path('*[local-name()="foo"]').select(xml).render()) |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
246 |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
247 def test_predicate_namespace(self): |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
248 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
249 self.assertEqual('<foo xmlns="NS"/>', |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
250 Path('*[namespace-uri()="NS"]').select(xml).render()) |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
251 |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
252 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
|
253 xml = XML('<root><foo/><bar/></root>') |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
254 self.assertEqual('<bar/>', |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
255 Path('*[not(name()="foo")]').select(xml).render()) |
22a7080ed242
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
256 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
257 def test_predicate_attr(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
258 xml = XML('<root><item/><item important="very"/></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
259 self.assertEqual('<item important="very"/>', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
260 Path('root/item[@important]').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
261 self.assertEqual('<item important="very"/>', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
262 Path('root/item[@important="very"]').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
263 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
264 def test_predicate_attr_equality(self): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
265 xml = XML('<root><item/><item important="notso"/></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
266 self.assertEqual('', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
267 Path('root/item[@important="very"]').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
268 self.assertEqual('<item/><item important="notso"/>', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
269 Path('root/item[@important!="very"]').select(xml).render()) |
1 | 270 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
271 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
|
272 xml = XML('<root><item/><item important="very"/></root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
273 path = Path('root/item[@important and @important="very"]') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
274 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
275 path = Path('root/item[@important and @important="notso"]') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
276 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
|
277 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
278 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
|
279 xml = XML('<root><item/><item important="very"/></root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
280 path = Path('root/item[@urgent or @important]') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
281 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
282 path = Path('root/item[@urgent or @notso]') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
283 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
|
284 |
1 | 285 |
286 def suite(): | |
287 suite = unittest.TestSuite() | |
111
8a4d9064f363
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
288 suite.addTest(doctest.DocTestSuite(Path.__module__)) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
289 suite.addTest(unittest.makeSuite(PathTestCase, 'test')) |
1 | 290 return suite |
291 | |
292 if __name__ == '__main__': | |
293 unittest.main(defaultTest='suite') |