annotate genshi/tests/path.py @ 818:eab11d35c769

Merged soc2008-xpath branch back into trunk.
author cmlenz
date Wed, 11 Mar 2009 17:03:03 +0000
parents 8bcd86cd6c10
children 7ad34db77566
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
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
18 from genshi.path import Path, PathParser, PathSyntaxError, GenericStrategy, \
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
19 SingleStepStrategy, SimplePathStrategy
26
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
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
22 class FakePath(Path):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
23 def __init__(self, strategy):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
24 self.strategy = strategy
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
25 def test(self, ignore_context = False):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
26 return self.strategy.test(ignore_context)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
27
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
28 class PathTestCase(unittest.TestCase):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
29
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
30 strategies = [GenericStrategy, SingleStepStrategy, SimplePathStrategy]
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
31 def _create_path(self, expression, expected):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
32 return path
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
33
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
34 def _test_strategies(self, stream, path, render,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
35 namespaces=None, variables=None):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
36 for strategy in self.strategies:
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
37 if not strategy.supports(path):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
38 continue
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
39 s = strategy(path)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
40 rendered = FakePath(s).select(stream,namespaces=namespaces,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
41 variables=variables).render()
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
42 msg = "Bad render using %s strategy"%str(strategy)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
43 msg += "\nExpected:\t'%s'"%render
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
44 msg += "\nRendered:\t'%s'"%rendered
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
45 self.assertEqual(render, rendered, msg)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
46
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
47 def _test_expression(self, text, expected, stream=None, render="",
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
48 namespaces=None, variables=None):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
49 path = Path(text)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
50 if expected is not None:
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
51 self.assertEqual(expected, repr(path))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
52
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
53 if stream is None:
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
54 return
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
55
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
56 rendered = path.select(stream, namespaces=namespaces,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
57 variables=variables).render()
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
58 msg = "Bad render using whole path"
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
59 msg += "\nExpected:\t'%s'"%render
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
60 msg += "\nRendered:\t'%s'"%rendered
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
61 self.assertEqual(render, rendered, msg)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
62
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
63 if len(path.paths) == 1:
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
64 self._test_strategies(stream, path.paths[0], render,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
65 namespaces=namespaces, variables=variables)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
66
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
67
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
68 def test_error_no_absolute_path(self):
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
69 self.assertRaises(PathSyntaxError, Path, '/root')
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
70
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
71 def test_error_unsupported_axis(self):
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
72 self.assertRaises(PathSyntaxError, Path, '..')
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
73 self.assertRaises(PathSyntaxError, Path, 'parent::ma')
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
74
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
75 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
76 xml = XML('<root><elem/></root>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
77
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
78 self._test_expression( 'elem',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
79 '<Path "child::elem">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
80 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
81 '<elem/>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
82
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
83 self._test_expression( 'elem',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
84 '<Path "child::elem">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
85 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
86 '<elem/>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
87
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
88 self._test_expression( 'child::elem',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
89 '<Path "child::elem">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
90 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
91 '<elem/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
92
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
93 self._test_expression( '//elem',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
94 '<Path "descendant-or-self::elem">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
95 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
96 '<elem/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
97
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
98 self._test_expression( 'descendant::elem',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
99 '<Path "descendant::elem">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
100 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
101 '<elem/>')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
102
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
103 def test_1step_self(self):
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
104 xml = XML('<root><elem/></root>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
105
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
106 self._test_expression( '.',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
107 '<Path "self::node()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
108 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
109 '<root><elem/></root>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
110
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
111 self._test_expression( 'self::node()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
112 '<Path "self::node()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
113 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
114 '<root><elem/></root>')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
115
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
116 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
117 xml = XML('<root><elem/></root>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
118
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
119 self._test_expression( '*',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
120 '<Path "child::*">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
121 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
122 '<elem/>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
123
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
124 self._test_expression( 'child::*',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
125 '<Path "child::*">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
126 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
127 '<elem/>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
128
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
129 self._test_expression( 'child::node()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
130 '<Path "child::node()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
131 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
132 '<elem/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
133
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
134 self._test_expression( '//*',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
135 '<Path "descendant-or-self::*">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
136 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
137 '<root><elem/></root>')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
138
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
139 def test_1step_attribute(self):
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
140 self._test_expression( '@foo',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
141 '<Path "attribute::foo">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
142 XML('<root/>'),
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
143 '')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
144
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
145 xml = XML('<root foo="bar"/>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
146
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
147 self._test_expression( '@foo',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
148 '<Path "attribute::foo">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
149 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
150 'bar')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
151
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
152 self._test_expression( './@foo',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
153 '<Path "self::node()/attribute::foo">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
154 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
155 'bar')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
156
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
157 def test_1step_text(self):
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
158 xml = XML('<root>Hey</root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
159
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
160 self._test_expression( 'text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
161 '<Path "child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
162 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
163 'Hey')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
164
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
165 self._test_expression( './text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
166 '<Path "self::node()/child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
167 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
168 'Hey')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
169
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
170 self._test_expression( '//text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
171 '<Path "descendant-or-self::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
172 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
173 'Hey')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
174
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
175 self._test_expression( './/text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
176 '<Path "self::node()/descendant-or-self::node()/child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
177 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
178 'Hey')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
179
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
180 def test_2step(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
181 xml = XML('<root><foo/><bar/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
182 self._test_expression('*', None, xml, '<foo/><bar/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
183 self._test_expression('bar', None, xml, '<bar/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
184 self._test_expression('baz', None, xml, '')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
185
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
186 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
187 xml = XML('<elem class="x"><span id="joe">Hey Joe</span></elem>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
188 self._test_expression('@*', None, xml, 'x')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
189 self._test_expression('./@*', None, xml, 'x')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
190 self._test_expression('.//@*', None, xml, 'xjoe')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
191 self._test_expression('*/@*', None, xml, 'joe')
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
192
215
e92135672812 A couple of minor XPath fixes.
cmlenz
parents: 179
diff changeset
193 xml = XML('<elem><foo id="1"/><foo id="2"/></elem>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
194 self._test_expression('@*', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
195 self._test_expression('foo/@*', None, xml, '12')
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
196
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
197 def test_2step_complex(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
198 xml = XML('<root><foo><bar/></foo></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
199
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
200 self._test_expression( 'foo/bar',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
201 '<Path "child::foo/child::bar">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
202 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
203 '<bar/>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
204
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
205 self._test_expression( './bar',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
206 '<Path "self::node()/child::bar">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
207 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
208 '')
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
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
210 self._test_expression( 'foo/*',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
211 '<Path "child::foo/child::*">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
212 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
213 '<bar/>')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
214
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
215 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
216 self._test_expression( './bar',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
217 '<Path "self::node()/child::bar">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
218 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
219 '<bar id="2"/>')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
220
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
221 def test_2step_text(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
222 xml = XML('<root><item>Foo</item></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
223
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
224 self._test_expression( 'item/text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
225 '<Path "child::item/child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
226 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
227 'Foo')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
228
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
229 self._test_expression( '*/text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
230 '<Path "child::*/child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
231 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
232 'Foo')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
233
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
234 self._test_expression( '//text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
235 '<Path "descendant-or-self::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
236 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
237 'Foo')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
238
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
239 self._test_expression( './text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
240 '<Path "self::node()/child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
241 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
242 '')
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
243
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
244 xml = XML('<root><item>Foo</item><item>Bar</item></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
245 self._test_expression( 'item/text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
246 '<Path "child::item/child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
247 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
248 'FooBar')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
249
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
250 def test_3step(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
251 xml = XML('<root><foo><bar/></foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
252 self._test_expression( 'foo/*',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
253 '<Path "child::foo/child::*">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
254 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
255 '<bar/>')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
256
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
257 def test_3step_complex(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
258 xml = XML('<root><foo><bar/></foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
259 self._test_expression( '*/bar',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
260 '<Path "child::*/child::bar">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
261 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
262 '<bar/>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
263
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
264 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
265 self._test_expression( '//bar',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
266 '<Path "descendant-or-self::bar">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
267 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
268 '<bar id="1"/><bar id="2"/>')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
269
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
270 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
271 xml = XML('<root><!-- commented --></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
272 self._test_expression( 'comment()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
273 '<Path "child::comment()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
274 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
275 '<!-- commented -->')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
276
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
277 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
278 xml = XML('<root>Some text <br/>in here.</root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
279 self._test_expression( 'text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
280 '<Path "child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
281 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
282 'Some text in here.')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
283
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
284 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
285 xml = XML('<root>Some text <br/>in here.</root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
286 self._test_expression( 'node()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
287 '<Path "child::node()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
288 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
289 'Some text <br/>in here.',)
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
290
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
291 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
292 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
293
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
294 self._test_expression( '//processing-instruction()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
295 '<Path "descendant-or-self::processing-instruction()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
296 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
297 '<?python x = 2 * 3 ?><?php echo("x") ?>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
298
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
299 self._test_expression( 'processing-instruction()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
300 '<Path "child::processing-instruction()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
301 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
302 '<?php echo("x") ?>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
303
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
304 self._test_expression( 'processing-instruction("php")',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
305 '<Path "child::processing-instruction(\"php\")">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
306 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
307 '<?php echo("x") ?>')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
308
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
309 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
310 xml = XML("""<body>1<br />2<br />3<br /></body>""")
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
311 self._test_expression( '*|text()',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
312 '<Path "child::*|child::text()">',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
313 xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
314 '1<br/>2<br/>3<br/>')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
315
121
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
316 def test_predicate_name(self):
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
317 xml = XML('<root><foo/><bar/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
318 self._test_expression('*[name()="foo"]', None, xml, '<foo/>')
121
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
319
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
320 def test_predicate_localname(self):
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
321 xml = XML('<root><foo xmlns="NS"/><bar/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
322 self._test_expression('*[local-name()="foo"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
323 '<foo xmlns="NS"/>')
121
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
324
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
325 def test_predicate_namespace(self):
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
326 xml = XML('<root><foo xmlns="NS"/><bar/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
327 self._test_expression('*[namespace-uri()="NS"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
328 '<foo xmlns="NS"/>')
121
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
329
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
330 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
331 xml = XML('<root><foo/><bar/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
332 self._test_expression('*[not(name()="foo")]', None, xml, '<bar/>')
121
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
333
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
334 def test_predicate_attr(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
335 xml = XML('<root><item/><item important="very"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
336 self._test_expression('item[@important]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
337 '<item important="very"/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
338 self._test_expression('item[@important="very"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
339 '<item important="very"/>')
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
340
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
341 def test_predicate_attr_equality(self):
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
342 xml = XML('<root><item/><item important="notso"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
343 self._test_expression('item[@important="very"]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
344 self._test_expression('item[@important!="very"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
345 '<item/><item important="notso"/>')
162
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
346
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
347 def test_predicate_attr_greater_than(self):
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
348 xml = XML('<root><item priority="3"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
349 self._test_expression('item[@priority>3]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
350 self._test_expression('item[@priority>2]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
351 '<item priority="3"/>')
162
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
352
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
353 def test_predicate_attr_less_than(self):
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
354 xml = XML('<root><item priority="3"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
355 self._test_expression('item[@priority<3]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
356 self._test_expression('item[@priority<4]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
357 '<item priority="3"/>')
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
358
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
359 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
360 xml = XML('<root><item/><item important="very"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
361 self._test_expression('item[@important and @important="very"]',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
362 None, xml, '<item important="very"/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
363 self._test_expression('item[@important and @important="notso"]',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
364 None, xml, '')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
365
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
366 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
367 xml = XML('<root><item/><item important="very"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
368 self._test_expression('item[@urgent or @important]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
369 '<item important="very"/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
370 self._test_expression('item[@urgent or @notso]', None, xml, '')
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
371
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
372 def test_predicate_boolean_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
373 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
374 self._test_expression('*[boolean("")]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
375 self._test_expression('*[boolean("yo")]', None, xml, '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
376 self._test_expression('*[boolean(0)]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
377 self._test_expression('*[boolean(42)]', None, xml, '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
378 self._test_expression('*[boolean(false())]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
379 self._test_expression('*[boolean(true())]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
380 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
381
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
382 def test_predicate_ceil_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
383 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
384 self._test_expression('*[ceiling("4.5")=5]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
385 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
386
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
387 def test_predicate_concat_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
388 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
389 self._test_expression('*[name()=concat("f", "oo")]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
390 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
391
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
392 def test_predicate_contains_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
393 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
394 self._test_expression('*[contains(name(), "oo")]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
395 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
396
534
cccb6c748609 Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents: 413
diff changeset
397 def test_predicate_matches_function(self):
cccb6c748609 Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents: 413
diff changeset
398 xml = XML('<root><foo>bar</foo><bar>foo</bar></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
399 self._test_expression('*[matches(name(), "foo|bar")]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
400 '<foo>bar</foo><bar>foo</bar>')
534
cccb6c748609 Add XPath `matches()` function which, of course, supports the Python regular
athomas
parents: 413
diff changeset
401
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
402 def test_predicate_false_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
403 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
404 self._test_expression('*[false()]', None, xml, '')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
405
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
406 def test_predicate_floor_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
407 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
408 self._test_expression('*[floor("4.5")=4]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
409 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
410
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
411 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
412 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
413 self._test_expression('*[normalize-space(" foo bar ")="foo bar"]',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
414 None, xml, '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
415
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
416 def test_predicate_number_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
417 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
418 self._test_expression('*[number("3.0")=3]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
419 '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
420 self._test_expression('*[number("3.0")=3.0]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
421 '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
422 self._test_expression('*[number("0.1")=.1]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
423 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
424
162
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
425 def test_predicate_round_function(self):
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
426 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
427 self._test_expression('*[round("4.4")=4]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
428 '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
429 self._test_expression('*[round("4.6")=5]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
430 '<foo>bar</foo>')
162
f767cf98e3e3 Implement the XPath relational operators and the `round()` function.
cmlenz
parents: 155
diff changeset
431
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
432 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
433 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
434 self._test_expression('*[starts-with(name(), "f")]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
435 '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
436 self._test_expression('*[starts-with(name(), "b")]', None, xml, '')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
437
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
438 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
439 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
440 self._test_expression('*[string-length(name())=3]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
441 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
442
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
443 def test_predicate_substring_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
444 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
445 self._test_expression('*[substring(name(), 1)="oo"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
446 '<foo>bar</foo>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
447 self._test_expression('*[substring(name(), 1, 1)="o"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
448 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
449
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
450 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
451 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
452 self._test_expression('*[substring-after(name(), "f")="oo"]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
453 '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
454
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
455 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
456 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
457 self._test_expression('*[substring-before(name(), "oo")="f"]',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
458 None, xml, '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
459
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
460 def test_predicate_translate_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
461 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
462 self._test_expression('*[translate(name(), "fo", "ba")="baa"]',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
463 None, xml, '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
464
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
465 def test_predicate_true_function(self):
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
466 xml = XML('<root><foo>bar</foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
467 self._test_expression('*[true()]', None, xml, '<foo>bar</foo>')
155
50d4b08017df * String literals in XPath expressions that contains spaces are now tokenizes correctly.
cmlenz
parents: 145
diff changeset
468
179
a2e0a7986d19 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 164
diff changeset
469 def test_predicate_variable(self):
a2e0a7986d19 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 164
diff changeset
470 xml = XML('<root><foo>bar</foo></root>')
a2e0a7986d19 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 164
diff changeset
471 variables = {'bar': 'foo'}
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
472 self._test_expression('*[name()=$bar]', None, xml, '<foo>bar</foo>',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
473 variables = variables)
224
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
474
228
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
475 def test_predicate_position(self):
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
476 xml = XML('<root><foo id="a1"/><foo id="a2"/><foo id="a3"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
477 self._test_expression('*[2]', None, xml, '<foo id="a2"/>')
228
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
478
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
479 def test_predicate_attr_and_position(self):
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
480 xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
481 self._test_expression('*[@id][2]', None, xml, '<foo id="a2"/>')
228
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
482
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
483 def test_predicate_position_and_attr(self):
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
484 xml = XML('<root><foo/><foo id="a1"/><foo id="a2"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
485 self._test_expression('*[1][@id]', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
486 self._test_expression('*[2][@id]', None, xml, '<foo id="a1"/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
487
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
488 def test_predicate_advanced_position(self):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
489 xml = XML('<root><a><b><c><d><e/></d></c></b></a></root>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
490 self._test_expression( 'descendant-or-self::*/'
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
491 'descendant-or-self::*/'
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
492 'descendant-or-self::*[2]/'
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
493 'self::*/descendant::*[3]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
494 '<d><e/></d>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
495
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
496 def test_predicate_child_position(self):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
497 xml = XML('\
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
498 <root><a><b>1</b><b>2</b><b>3</b></a><a><b>4</b><b>5</b></a></root>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
499 self._test_expression('//a/b[2]', None, xml, '<b>2</b><b>5</b>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
500 self._test_expression('//a/b[3]', None, xml, '<b>3</b>')
228
f79b20a50919 Add support for position predicates in XPath expressions.
cmlenz
parents: 224
diff changeset
501
224
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
502 def test_name_with_namespace(self):
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
503 xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
504 self._test_expression('f:foo', '<Path "child::f:foo">', xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
505 '<foo xmlns="FOO">bar</foo>',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
506 namespaces = {'f': 'FOO'})
224
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
507
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
508 def test_wildcard_with_namespace(self):
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
509 xml = XML('<root xmlns:f="FOO"><f:foo>bar</f:foo></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
510 self._test_expression('f:*', '<Path "child::f:*">', xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
511 '<foo xmlns="FOO">bar</foo>',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
512 namespaces = {'f': 'FOO'})
224
e4dad1145f84 Implement support for namespace prefixes in XPath expressions.
cmlenz
parents: 216
diff changeset
513
384
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
514 def test_predicate_termination(self):
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
515 """
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
516 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
517 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
518 """
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
519 xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
520 self._test_expression('.[@flag="1"]/*', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
521 '<li>a</li><li>b</li>')
384
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
522
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
523 xml = XML('<ul flag="1"><li>a</li><li>b</li></ul>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
524 self._test_expression('.[@flag="0"]/*', None, xml, '')
384
1596045ff0f3 Fix for infinite loop in XPath test. Closes #82.
cmlenz
parents: 282
diff changeset
525
410
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 384
diff changeset
526 def test_attrname_with_namespace(self):
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 384
diff changeset
527 xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
528 self._test_expression('foo[@f:bar]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
529 '<foo xmlns:ns1="FOO" ns1:bar="baz"/>',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
530 namespaces={'f': 'FOO'})
410
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 384
diff changeset
531
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 384
diff changeset
532 def test_attrwildcard_with_namespace(self):
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 384
diff changeset
533 xml = XML('<root xmlns:f="FOO"><foo f:bar="baz"/></root>')
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
534 self._test_expression('foo[@f:*]', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
535 '<foo xmlns:ns1="FOO" ns1:bar="baz"/>',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
536 namespaces={'f': 'FOO'})
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
537 def test_self_and_descendant(self):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
538 xml = XML('<root><foo/></root>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
539 self._test_expression('self::root', None, xml, '<root><foo/></root>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
540 self._test_expression('self::foo', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
541 self._test_expression('descendant::root', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
542 self._test_expression('descendant::foo', None, xml, '<foo/>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
543 self._test_expression('descendant-or-self::root', None, xml,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
544 '<root><foo/></root>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
545 self._test_expression('descendant-or-self::foo', None, xml, '<foo/>')
179
a2e0a7986d19 Implemented support for XPath variables in predicates (#31).
cmlenz
parents: 164
diff changeset
546
818
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
547 def test_long_simple_paths(self):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
548 xml = XML('<root><a><b><a><d><a><b><a><b><a><b><a><c>!'
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
549 '</c></a></b></a></b></a></b></a></d></a></b></a></root>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
550 self._test_expression('//a/b/a/b/a/c', None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
551 self._test_expression('//a/b/a/c', None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
552 self._test_expression('//a/c', None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
553 self._test_expression('//c', None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
554 # Please note that a//b is NOT the same as a/descendant::b
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
555 # it is a/descendant-or-self::node()/b, which SimplePathStrategy
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
556 # does NOT support
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
557 self._test_expression('a/b/descendant::a/c', None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
558 self._test_expression('a/b/descendant::a/d/descendant::a/c',
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
559 None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
560 self._test_expression('a/b/descendant::a/d/a/c', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
561 self._test_expression('//d/descendant::b/descendant::b/descendant::b'
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
562 '/descendant::c', None, xml, '<c>!</c>')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
563 self._test_expression('//d/descendant::b/descendant::b/descendant::b'
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
564 '/descendant::b/descendant::c', None, xml, '')
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
565 def _test_support(self, strategy_class, text):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
566 path = PathParser(text, None, -1).parse()[0]
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
567 return strategy_class.supports(path)
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
568 def test_simple_strategy_support(self):
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
569 self.assert_(self._test_support(SimplePathStrategy, 'a/b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
570 self.assert_(self._test_support(SimplePathStrategy, 'self::a/b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
571 self.assert_(self._test_support(SimplePathStrategy, 'descendant::a/b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
572 self.assert_(self._test_support(SimplePathStrategy,
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
573 'descendant-or-self::a/b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
574 self.assert_(self._test_support(SimplePathStrategy, '//a/b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
575 self.assert_(self._test_support(SimplePathStrategy, 'a/@b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
576 self.assert_(self._test_support(SimplePathStrategy, 'a/text()'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
577
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
578 # a//b is a/descendant-or-self::node()/b
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
579 self.assert_(not self._test_support(SimplePathStrategy, 'a//b'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
580 self.assert_(not self._test_support(SimplePathStrategy, 'node()/@a'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
581 self.assert_(not self._test_support(SimplePathStrategy, '@a'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
582 self.assert_(not self._test_support(SimplePathStrategy, 'foo:bar'))
eab11d35c769 Merged soc2008-xpath branch back into trunk.
cmlenz
parents: 638
diff changeset
583 self.assert_(not self._test_support(SimplePathStrategy, 'a/@foo:bar'))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
584
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
585 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
586 suite = unittest.TestSuite()
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
587 suite.addTest(doctest.DocTestSuite(Path.__module__))
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
588 suite.addTest(unittest.makeSuite(PathTestCase, 'test'))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
589 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
590
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
591 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
592 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software