Mercurial > genshi > genshi-test
annotate markup/tests/path.py @ 106:61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
author | cmlenz |
---|---|
date | Fri, 28 Jul 2006 16:51:35 +0000 |
parents | 822089ae65ce |
children | 8a4d9064f363 |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
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 | 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
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 | 9 # |
10 # This software consists of voluntary contributions made by many | |
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 | 13 |
14 import doctest | |
15 import unittest | |
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 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
18 from markup.path import Path |
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 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
23 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
|
24 xml = XML('<root><elem/></root>') |
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
|
25 self.assertEqual('<elem/>', Path('elem').select(xml).render()) |
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
|
26 self.assertEqual('<elem/>', Path('//elem').select(xml).render()) |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
27 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
28 def test_1step_self(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
29 xml = XML('<root><elem/></root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
30 self.assertEqual('<root><elem/></root>', Path('.').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
31 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
32 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
|
33 xml = XML('<root><elem/></root>') |
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
|
34 self.assertEqual('<elem/>', Path('*').select(xml).render()) |
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
|
35 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
|
36 |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
37 def test_1step_attribute(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
38 path = Path('@foo') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
39 self.assertEqual('', path.select(XML('<root/>')).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
40 self.assertEqual('bar', path.select(XML('<root foo="bar"/>')).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
41 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
42 def test_1step_attribute(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
43 path = Path('@foo') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
44 self.assertEqual('', path.select(XML('<root/>')).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
45 self.assertEqual('bar', path.select(XML('<root foo="bar"/>')).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
46 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
47 def test_2step(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
48 xml = XML('<root><foo/><bar/></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
49 self.assertEqual('<foo/><bar/>', Path('root/*').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
50 self.assertEqual('<bar/>', Path('root/bar').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
51 self.assertEqual('', Path('root/baz').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
52 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
53 def test_2step_complex(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
54 xml = XML('<root><foo><bar/></foo></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
55 self.assertEqual('<bar/>', Path('foo/bar').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
56 self.assertEqual('<bar/>', Path('foo/*').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
57 self.assertEqual('', Path('root/bar').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
58 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
59 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
60 self.assertEqual('<bar id="2"/>', Path('root/bar').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
61 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
62 def test_2step_text(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
63 xml = XML('<root><item>Foo</item></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
64 self.assertEqual('Foo', Path('item/text()').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
65 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
|
66 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
|
67 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
68 def test_3step(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
69 xml = XML('<root><foo><bar/></foo></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
70 self.assertEqual('<bar/>', Path('root/foo/*').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
71 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
72 def test_3step_complex(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
73 xml = XML('<root><foo><bar/></foo></root>') |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
74 self.assertEqual('<bar/>', Path('*/bar').select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
75 xml = XML('<root><foo><bar id="1"/></foo><bar id="2"/></root>') |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
76 self.assertEqual('<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
|
77 Path('//bar').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
78 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
79 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
|
80 xml = XML('<root><!-- commented --></root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
81 self.assertEqual('<!-- commented -->', |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
82 Path('comment()').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
83 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
84 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
|
85 xml = XML('<root>Some text <br/>in here.</root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
86 self.assertEqual('Some text in here.', |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
87 Path('text()').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
88 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
89 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
|
90 xml = XML('<root>Some text <br/>in here.</root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
91 self.assertEqual('<root>Some text <br/>in here.</root>', |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
92 Path('node()').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
93 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
94 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
|
95 xml = XML('<?python x = 2 * 3 ?><root><?php echo("x") ?></root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
96 self.assertEqual('<?python x = 2 * 3 ?><?php echo("x") ?>', |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
97 Path('processing-instruction()').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
98 self.assertEqual('<?php echo("x") ?>', |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
99 Path('processing-instruction("php")').select(xml).render()) |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
100 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
101 def test_simple_union(self): |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
102 xml = XML('<root>Oh <foo>my</foo></root>') |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
103 self.assertEqual('Oh <foo>my</foo>', |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
104 Path('*|text()').select(xml).render()) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
105 |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
106 def test_predicate_attr(self): |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
107 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
|
108 self.assertEqual('<item important="very"/>', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
109 Path('root/item[@important]').select(xml).render()) |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
110 self.assertEqual('<item important="very"/>', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
111 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
|
112 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
113 def test_predicate_attr_equality(self): |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
114 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
|
115 self.assertEqual('', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
116 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
|
117 self.assertEqual('<item/><item important="notso"/>', |
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
118 Path('root/item[@important!="very"]').select(xml).render()) |
1 | 119 |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 |
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 |
1 | 134 |
135 def suite(): | |
136 suite = unittest.TestSuite() | |
106
61fa4cadb766
Complete rewrite of the XPath parsing, which was a mess before. Closes #19.
cmlenz
parents:
66
diff
changeset
|
137 #suite.addTest(doctest.DocTestSuite(Path.__module__)) |
26
039fc5b87405
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
1
diff
changeset
|
138 suite.addTest(unittest.makeSuite(PathTestCase, 'test')) |
1 | 139 return suite |
140 | |
141 if __name__ == '__main__': | |
142 unittest.main(defaultTest='suite') |