annotate markup/tests/path.py @ 228:a5b38b459cbb trunk

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