Mercurial > genshi > mirror
annotate markup/tests/path.py @ 131:2ad83f1d337c trunk
* Support for line numbers in exceptions in expression evaluation (#22).
* Fix bug in expression evaluation when item access was used inside a lambda or list comprehension. Thanks to Kevin Dangoor for reporting the problem.
author | cmlenz |
---|---|
date | Fri, 04 Aug 2006 13:07:52 +0000 |
parents | 33c646c7a8d6 |
children | ac0bc4a6aeba |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
38
diff
changeset
|
3 # Copyright (C) 2006 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
38
diff
changeset
|
8 # are also available at http://markup.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
66
59eb24184e9c
Switch copyright to Edgewall and URLs to markup.edgewall.org.
cmlenz
parents:
38
diff
changeset
|
12 # history and logs, available at http://markup.edgewall.org/log/. |
1 | 13 |
14 import doctest | |
15 import unittest | |
16 | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
17 from markup.input import XML |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
18 from markup.path import Path, PathSyntaxError |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
19 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
20 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
21 class PathTestCase(unittest.TestCase): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
22 |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
23 def test_error_no_absolute_path(self): |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
24 self.assertRaises(PathSyntaxError, Path, '/root') |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
25 |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
26 def test_error_unsupported_axis(self): |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
27 self.assertRaises(PathSyntaxError, Path, 'parent::ma') |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
28 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
29 def test_1step(self): |
38
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
30 xml = XML('<root><elem/></root>') |
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
31 self.assertEqual('<elem/>', Path('elem').select(xml).render()) |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
32 self.assertEqual('<elem/>', Path('child::elem').select(xml).render()) |
38
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
33 self.assertEqual('<elem/>', Path('//elem').select(xml).render()) |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
34 self.assertEqual('<elem/>', Path('descendant::elem').select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
35 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
36 def test_1step_self(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
37 xml = XML('<root><elem/></root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
38 self.assertEqual('<root><elem/></root>', Path('.').select(xml).render()) |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
39 #self.assertEqual('<root><elem/></root>', Path('self::node()').select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
40 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
41 def test_1step_wildcard(self): |
38
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
42 xml = XML('<root><elem/></root>') |
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
43 self.assertEqual('<elem/>', Path('*').select(xml).render()) |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
44 self.assertEqual('<elem/>', Path('child::node()').select(xml).render()) |
38
ee669cb9cccc
Fix for #2 (incorrect context node in path expressions). Still some paths that produce incorrect results, but the common case seems to work now.
cmlenz
parents:
27
diff
changeset
|
45 self.assertEqual('<elem/>', Path('//*').select(xml).render()) |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
46 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
47 def test_1step_attribute(self): |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
48 self.assertEqual('', Path('@foo').select(XML('<root/>')).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
49 xml = XML('<root foo="bar"/>') |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
50 self.assertEqual('bar', Path('@foo').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
51 self.assertEqual('bar', Path('./@foo').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
52 |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
53 def test_1step_text(self): |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
54 xml = XML('<root>Hey</root>') |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
55 self.assertEqual('Hey', Path('text()').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
56 self.assertEqual('Hey', Path('./text()').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
57 self.assertEqual('Hey', Path('//text()').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
58 self.assertEqual('Hey', Path('.//text()').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
59 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
60 def test_2step(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
61 xml = XML('<root><foo/><bar/></root>') |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
62 self.assertEqual('<foo/><bar/>', Path('*').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
63 self.assertEqual('<bar/>', Path('bar').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
64 self.assertEqual('', Path('baz').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
65 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
66 def test_2step_complex(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
67 xml = XML('<root><foo><bar/></foo></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
68 self.assertEqual('<bar/>', Path('foo/bar').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
69 self.assertEqual('<bar/>', Path('foo/*').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
70 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
71 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
128
33c646c7a8d6
Revert accidential checkin of unit test changes in [160].
cmlenz
parents:
126
diff
changeset
|
72 self.assertEqual('<bar id="1"/><bar id="2"/>', |
33c646c7a8d6
Revert accidential checkin of unit test changes in [160].
cmlenz
parents:
126
diff
changeset
|
73 Path('bar').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
74 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
75 def test_2step_text(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
76 xml = XML('<root><item>Foo</item></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
77 self.assertEqual('Foo', Path('item/text()').select(xml).render()) |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
78 self.assertEqual('Foo', Path('*/text()').select(xml).render()) |
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
79 self.assertEqual('Foo', Path('//text()').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
80 xml = XML('<root><item>Foo</item><item>Bar</item></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
81 self.assertEqual('FooBar', Path('item/text()').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
82 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
83 def test_3step(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
84 xml = XML('<root><foo><bar/></foo></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
85 self.assertEqual('<bar/>', Path('root/foo/*').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
86 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
87 def test_3step_complex(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
88 xml = XML('<root><foo><bar/></foo></root>') |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
89 self.assertEqual('<bar/>', Path('*/bar').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
90 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
91 self.assertEqual('<bar id="1"/><bar id="2"/>', |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
92 Path('//bar').select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
93 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
94 def test_node_type_comment(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
95 xml = XML('<root><!-- commented --></root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
96 self.assertEqual('<!-- commented -->', |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
97 Path('comment()').select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
98 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
99 def test_node_type_text(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
100 xml = XML('<root>Some text <br/>in here.</root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
101 self.assertEqual('Some text in here.', |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
102 Path('text()').select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
103 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
104 def test_node_type_node(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
105 xml = XML('<root>Some text <br/>in here.</root>') |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
106 self.assertEqual('Some text <br/>in here.', |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
107 Path('node()').select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
108 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
109 def test_node_type_processing_instruction(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
110 xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
111 self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>', |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
112 Path('processing-instruction()').select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
113 self.assertEqual('<?php echo("x") ?>', |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
114 Path('processing-instruction("php")').select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
115 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
116 def test_simple_union(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
117 xml = XML('<root>Oh <foo>my</foo></root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
118 self.assertEqual('Oh <foo>my</foo>', |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
119 Path('*|text()').select(xml).render()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
120 |
121
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
121 def test_predicate_name(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
122 xml = XML('<root><foo/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
123 self.assertEqual('<foo/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
124 Path('*[name()="foo"]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
125 |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
126 def test_predicate_localname(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
127 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
128 self.assertEqual('<foo xmlns="NS"/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
129 Path('*[local-name()="foo"]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
130 |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
131 def test_predicate_namespace(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
132 xml = XML('<root><foo xmlns="NS"/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
133 self.assertEqual('<foo xmlns="NS"/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
134 Path('*[namespace-uri()="NS"]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
135 |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
136 def test_predicate_not_name(self): |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
137 xml = XML('<root><foo/><bar/></root>') |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
138 self.assertEqual('<bar/>', |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
139 Path('*[not(name()="foo")]').select(xml).render()) |
062e51ad7b19
Added support for the XPath functions `name()`, `namespace-uri()`, `local-name()`, and `not()`.
cmlenz
parents:
111
diff
changeset
|
140 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
141 def test_predicate_attr(self): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
142 xml = XML('<root><item/><item important="very"/></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
143 self.assertEqual('<item important="very"/>', |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
144 Path('root/item[@important]').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
145 self.assertEqual('<item important="very"/>', |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
146 Path('root/item[@important="very"]').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
147 |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
148 def test_predicate_attr_equality(self): |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
149 xml = XML('<root><item/><item important="notso"/></root>') |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
150 self.assertEqual('', |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
151 Path('root/item[@important="very"]').select(xml).render()) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
152 self.assertEqual('<item/><item important="notso"/>', |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
153 Path('root/item[@important!="very"]').select(xml).render()) |
1 | 154 |
106
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
155 def test_predicate_attr_and(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
156 xml = XML('<root><item/><item important="very"/></root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
157 path = Path('root/item[@important and @important="very"]') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
158 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
159 path = Path('root/item[@important and @important="notso"]') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
160 self.assertEqual('', path.select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
161 |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
162 def test_predicate_attr_or(self): |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
163 xml = XML('<root><item/><item important="very"/></root>') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
164 path = Path('root/item[@urgent or @important]') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
165 self.assertEqual('<item important="very"/>', path.select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
166 path = Path('root/item[@urgent or @notso]') |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
167 self.assertEqual('', path.select(xml).render()) |
f9473bdc93b2
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
168 |
1 | 169 |
170 def suite(): | |
171 suite = unittest.TestSuite() | |
111
2368c3becc52
Some fixes and more unit tests for the XPath engine.
cmlenz
parents:
106
diff
changeset
|
172 suite.addTest(doctest.DocTestSuite(Path.__module__)) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
173 suite.addTest(unittest.makeSuite(PathTestCase, 'test')) |
1 | 174 return suite |
175 | |
176 if __name__ == '__main__': | |
177 unittest.main(defaultTest='suite') |