1
2
3 """
4 Tests for the ElementPath implementation.
5 """
6
7 from __future__ import absolute_import
8
9 import unittest
10 from .common_imports import etree, HelperTestCase
11
12
14 etree = etree
15 from lxml import _elementpath
16
18 self._elementpath._cache.clear()
19 el = self.etree.XML(b'<a><b><c/><c/></b></a>')
20 self.assertFalse(self._elementpath._cache)
21
22 self.assertTrue(el.findall('b/c'))
23 self.assertEqual(1, len(self._elementpath._cache))
24 self.assertTrue(el.findall('b/c'))
25 self.assertEqual(1, len(self._elementpath._cache))
26 self.assertFalse(el.findall('xxx'))
27 self.assertEqual(2, len(self._elementpath._cache))
28 self.assertFalse(el.findall('xxx'))
29 self.assertEqual(2, len(self._elementpath._cache))
30 self.assertTrue(el.findall('b/c'))
31 self.assertEqual(2, len(self._elementpath._cache))
32
34 def assert_tokens(tokens, path, namespaces=None):
35 self.assertEqual(tokens, list(self._elementpath.xpath_tokenizer(path, namespaces)))
36
37 assert_tokens(
38 [('/', '')],
39 '/',
40 )
41 assert_tokens(
42 [('.', ''), ('/', ''), ('', 'a'), ('/', ''), ('', 'b'), ('/', ''), ('', 'c')],
43 './a/b/c',
44 )
45 assert_tokens(
46 [('/', ''), ('', 'a'), ('/', ''), ('', 'b'), ('/', ''), ('', 'c')],
47 '/a/b/c',
48 )
49 assert_tokens(
50 [('/', ''), ('', '{nsx}a'), ('/', ''), ('', '{nsy}b'), ('/', ''), ('', 'c')],
51 '/x:a/y:b/c',
52 {'x': 'nsx', 'y': 'nsy'},
53 )
54 assert_tokens(
55 [('/', ''), ('', '{nsx}a'), ('/', ''), ('', '{nsy}b'), ('/', ''), ('', '{nsnone}c')],
56 '/x:a/y:b/c',
57 {'x': 'nsx', 'y': 'nsy', None: 'nsnone'},
58 )
59
60
61
62
63
64
65
71
72
73 if __name__ == '__main__':
74 print('to test use test.py %s' % __file__)
75