annotate markup/tests/path.py @ 138:d91e1e822969

Add some more assertions to the XPath tests.
author cmlenz
date Wed, 09 Aug 2006 12:07:26 +0000
parents 38ddb21b6fa4
children 56d534eb53f9
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
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 38
diff changeset
8 # are also available at http://markup.edgewall.org/wiki/License.
1
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
66
822089ae65ce Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents: 38
diff changeset
12 # history and logs, available at http://markup.edgewall.org/log/.
1
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
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
17 from markup.input import XML
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
18 from markup.path import Path, PathSyntaxError
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
19
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
20
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
21 class PathTestCase(unittest.TestCase):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
22
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
23 def test_error_no_absolute_path(self):
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
24 self.assertRaises(PathSyntaxError, Path, '/root')
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
25
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
26 def test_error_unsupported_axis(self):
137
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')
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
34 self.assertEqual('<Path "descendant::elem">', repr(path))
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('*')
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
65 self.assertEqual('<Path "descendant::*">', repr(path))
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))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
84 self.assertEqual('', path.select(XML('<root/>')).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
85
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
86 xml = XML('<root foo="bar"/>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
87 self.assertEqual('bar', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
88
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
89 path = Path('./@foo')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
90 self.assertEqual('<Path "self::node()/attribute::foo">', repr(path))
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
91 self.assertEqual('bar', Path('./@foo').select(xml).render())
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
92
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
93 def test_1step_text(self):
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
94 xml = XML('<root>Hey</root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
95
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
96 path = Path('text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
97 self.assertEqual('<Path "descendant::text()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
98 self.assertEqual('Hey', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
99
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
100 path = Path('./text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
101 self.assertEqual('<Path "self::node()/child::text()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
102 self.assertEqual('Hey', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
103
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
104 path = Path('//text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
105 self.assertEqual('<Path "descendant-or-self::node()/child::text()">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
106 repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
107 self.assertEqual('Hey', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
108
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
109 path = Path('.//text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
110 self.assertEqual('<Path "self::node()/child::text()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
111 self.assertEqual('Hey', path.select(xml).render())
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
112
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
113 def test_2step(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
114 xml = XML('<root><foo/><bar/></root>')
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
115 self.assertEqual('<foo/><bar/>', Path('*').select(xml).render())
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
116 self.assertEqual('<bar/>', Path('bar').select(xml).render())
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
117 self.assertEqual('', Path('baz').select(xml).render())
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
118
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
119 def test_2step_complex(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
120 xml = XML('<root><foo><bar/></foo></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
121
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
122 path = Path('foo/bar')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
123 self.assertEqual('<Path "descendant::foo/child::bar">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
124 self.assertEqual('<bar/>', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
125
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
126 path = Path('foo/*')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
127 self.assertEqual('<Path "descendant::foo/child::*">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
128 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
129
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
130 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>')
137
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
131 self.assertEqual('<bar id="1"/><bar id="2"/>',
38ddb21b6fa4 Further cleanup of XPath engine.
cmlenz
parents: 128
diff changeset
132 Path('bar').select(xml).render())
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
133
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
134 def test_2step_text(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
135 xml = XML('<root><item>Foo</item></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
136
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
137 path = Path('item/text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
138 self.assertEqual('<Path "descendant::item/child::text()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
139 self.assertEqual('Foo', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
140
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
141 path = Path('*/text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
142 self.assertEqual('<Path "descendant::*/child::text()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
143 self.assertEqual('Foo', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
144
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
145 path = Path('//text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
146 self.assertEqual('<Path "descendant-or-self::node()/child::text()">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
147 repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
148 self.assertEqual('Foo', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
149
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
150 xml = XML('<root><item>Foo</item><item>Bar</item></root>')
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
151 self.assertEqual('FooBar', Path('item/text()').select(xml).render())
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_3step(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
154 xml = XML('<root><foo><bar/></foo></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
155 path = Path('root/foo/*')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
156 self.assertEqual('<Path "descendant::root/child::foo/child::*">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
157 repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
158 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
159
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
160 def test_3step_complex(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
161 xml = XML('<root><foo><bar/></foo></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
162 path = Path('*/bar')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
163 self.assertEqual('<Path "descendant::*/child::bar">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
164 self.assertEqual('<bar/>', path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
165
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
166 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
167 path = Path('//bar')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
168 self.assertEqual('<Path "descendant-or-self::node()/child::bar">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
169 repr(path))
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
170 self.assertEqual('<bar id="1"/><bar id="2"/>',
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
171 path.select(xml).render())
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
172
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
173 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
174 xml = XML('<root><!-- commented --></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
175 path = Path('comment()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
176 self.assertEqual('<Path "descendant::comment()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
177 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
178
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
179 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
180 xml = XML('<root>Some text <br/>in here.</root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
181 path = Path('text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
182 self.assertEqual('<Path "descendant::text()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
183 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
184
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
185 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
186 xml = XML('<root>Some text <br/>in here.</root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
187 path = Path('node()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
188 self.assertEqual('<Path "descendant::node()">', repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
189 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
190
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
191 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
192 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
193
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
194 path = Path('processing-instruction()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
195 self.assertEqual('<Path "descendant::processing-instruction()">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
196 repr(path))
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
197 self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>',
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
198 path.select(xml).render())
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
199
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
200 path = Path('processing-instruction("php")')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
201 self.assertEqual('<Path "descendant::processing-instruction(\"php\")">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
202 repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
203 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
204
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
205 def test_simple_union(self):
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
206 xml = XML('<root>Oh <foo>my</foo></root>')
138
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
207 path = Path('*|text()')
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
208 self.assertEqual('<Path "descendant::*|descendant::text()">',
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
209 repr(path))
d91e1e822969 Add some more assertions to the XPath tests.
cmlenz
parents: 137
diff changeset
210 self.assertEqual('Oh <foo>my</foo>', path.select(xml).render())
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
211
121
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
212 def test_predicate_name(self):
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
213 xml = XML('<root><foo/><bar/></root>')
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
214 self.assertEqual('<foo/>',
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
215 Path('*[name()="foo"]').select(xml).render())
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
216
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
217 def test_predicate_localname(self):
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
218 xml = XML('<root><foo xmlns="NS"/><bar/></root>')
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
219 self.assertEqual('<foo xmlns="NS"/>',
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
220 Path('*[local-name()="foo"]').select(xml).render())
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
221
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
222 def test_predicate_namespace(self):
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
223 xml = XML('<root><foo xmlns="NS"/><bar/></root>')
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
224 self.assertEqual('<foo xmlns="NS"/>',
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
225 Path('*[namespace-uri()="NS"]').select(xml).render())
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
226
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
227 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
228 xml = XML('<root><foo/><bar/></root>')
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
229 self.assertEqual('<bar/>',
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
230 Path('*[not(name()="foo")]').select(xml).render())
22a7080ed242 Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents: 111
diff changeset
231
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
232 def test_predicate_attr(self):
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
233 xml = XML('<root><item/><item important="very"/></root>')
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
234 self.assertEqual('<item important="very"/>',
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
235 Path('root/item[@important]').select(xml).render())
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
236 self.assertEqual('<item important="very"/>',
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
237 Path('root/item[@important="very"]').select(xml).render())
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
238
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
239 def test_predicate_attr_equality(self):
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
240 xml = XML('<root><item/><item important="notso"/></root>')
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
241 self.assertEqual('',
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
242 Path('root/item[@important="very"]').select(xml).render())
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
243 self.assertEqual('<item/><item important="notso"/>',
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
244 Path('root/item[@important!="very"]').select(xml).render())
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
245
106
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
246 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
247 xml = XML('<root><item/><item important="very"/></root>')
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
248 path = Path('root/item[@important and @important="very"]')
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
249 self.assertEqual('<item important="very"/>', path.select(xml).render())
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
250 path = Path('root/item[@important and @important="notso"]')
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
251 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
252
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
253 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
254 xml = XML('<root><item/><item important="very"/></root>')
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
255 path = Path('root/item[@urgent or @important]')
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
256 self.assertEqual('<item important="very"/>', path.select(xml).render())
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
257 path = Path('root/item[@urgent or @notso]')
61fa4cadb766 Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents: 66
diff changeset
258 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
259
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
260
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
261 def suite():
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
262 suite = unittest.TestSuite()
111
8a4d9064f363 Some fixes and more unit tests for the XPath engine.
cmlenz
parents: 106
diff changeset
263 suite.addTest(doctest.DocTestSuite(Path.__module__))
26
039fc5b87405 * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
264 suite.addTest(unittest.makeSuite(PathTestCase, 'test'))
1
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
265 return suite
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
266
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
267 if __name__ == '__main__':
821114ec4f69 Initial import.
cmlenz
parents:
diff changeset
268 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software