annotate markup/tests/path.py @ 27:b4f78c05e5c9 trunk

* Fix the boilerplate in the Python source files. * Some more docstrings and cosmetic fixes.
author cmlenz
date Wed, 28 Jun 2006 09:28:09 +0000
parents 3c1a022be04c
children ee669cb9cccc
rev   line source
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
2 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006 Christopher Lenz
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
5 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 26
diff changeset
8 # are also available at http://markup.cmlenz.net/wiki/License.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
9 #
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
27
b4f78c05e5c9 * Fix the boilerplate in the Python source files.
cmlenz
parents: 26
diff changeset
12 # history and logs, available at http://markup.cmlenz.net/log/.
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
13
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
14 import doctest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
15 import unittest
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
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
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
18 from markup.path import Path
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
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
23 def test_1step(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
24 xml = XML('<root/>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
25 self.assertEqual('<root/>', Path('root').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
26 self.assertEqual('<root/>', Path('//root').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
27
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
28 def test_1step_wildcard(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
29 xml = XML('<root/>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
30 self.assertEqual('<root/>', Path('*').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
31 self.assertEqual('<root/>', Path('//*').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
32
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
33 def test_1step_attribute(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
34 path = Path('@foo')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
35 self.assertEqual('', path.select(XML('<root/>')).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
36 self.assertEqual('bar', path.select(XML('<root foo="bar"/>')).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
37
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
38 def test_1step_attribute(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
39 path = Path('@foo')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
40 self.assertEqual('', path.select(XML('<root/>')).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
41 self.assertEqual('bar', path.select(XML('<root foo="bar"/>')).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
42
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
43 def test_2step(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
44 xml = XML('<root><foo/><bar/></root>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
45 self.assertEqual('<foo/><bar/>', Path('root/*').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
46 self.assertEqual('<bar/>', Path('root/bar').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
47 self.assertEqual('', Path('root/baz').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
48
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
49 def test_2step_complex(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
50 xml = XML('<root><foo><bar/></foo></root>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
51 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
52 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
53 self.assertEqual('', Path('root/bar').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
54
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
55 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
56 self.assertEqual('<bar id="2"/>', Path('root/bar').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
57
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
58 def test_2step_text(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
59 xml = XML('<root><item>Foo</item></root>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
60 self.assertEqual('Foo', Path('item/text()').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
61 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
62 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
63
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
64 def test_3step(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
65 xml = XML('<root><foo><bar/></foo></root>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
66 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
67
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
68 def test_3step_complex(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
69 xml = XML('<root><foo><bar/></foo></root>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
70 self.assertEqual('<bar/>', Path('root/*/bar').select(xml).render())
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>')
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
72 self.assertEqual('<bar id="1"/><bar id="2"/>',
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
73 Path('root//bar').select(xml).render())
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_predicate_attr(self):
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
76 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
77 self.assertEqual('<item important="very"/>',
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
78 Path('root/item[@important]').select(xml).render())
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
79 self.assertEqual('<item important="very"/>',
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
80 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
81
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
82 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
83 self.assertEqual('',
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
84 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
85 self.assertEqual('<item/><item important="notso"/>',
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
86 Path('root/item[@important!="very"]').select(xml).render())
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
87
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
88
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
89 def suite():
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
90 suite = unittest.TestSuite()
26
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
91 suite.addTest(doctest.DocTestSuite(Path.__module__))
3c1a022be04c * Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents: 1
diff changeset
92 suite.addTest(unittest.makeSuite(PathTestCase, 'test'))
1
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
93 return suite
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
94
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
95 if __name__ == '__main__':
5479aae32f5a Initial import.
cmlenz
parents:
diff changeset
96 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software