annotate genshi/tests/path.py @ 410:3460b04daeac

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