lxml.tests.test_elementpath
Package lxml :: Package tests :: Module test_elementpath
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_elementpath

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Tests for the ElementPath implementation. 
  5  """ 
  6   
  7  from __future__ import absolute_import 
  8   
  9  import unittest 
 10  from copy import deepcopy 
 11  from .common_imports import etree, HelperTestCase 
 12   
 13   
14 -def summarize(elem):
15 return elem.tag
16
17 -def summarize_list(seq):
18 return list(map(summarize, seq))
19
20 -def normalize_crlf(tree):
21 for elem in tree.getiterator(): 22 if elem.text: elem.text = elem.text.replace("\r\n", "\n") 23 if elem.tail: elem.tail = elem.tail.replace("\r\n", "\n")
24 25
26 -class EtreeElementPathTestCase(HelperTestCase):
27 etree = etree 28 from lxml import _elementpath 29
30 - def test_cache(self):
31 self._elementpath._cache.clear() 32 el = self.etree.XML(b'<a><b><c/><c/></b></a>') 33 self.assertFalse(self._elementpath._cache) 34 35 self.assertTrue(el.findall('b/c')) 36 self.assertEqual(1, len(self._elementpath._cache)) 37 self.assertTrue(el.findall('b/c')) 38 self.assertEqual(1, len(self._elementpath._cache)) 39 self.assertFalse(el.findall('xxx')) 40 self.assertEqual(2, len(self._elementpath._cache)) 41 self.assertFalse(el.findall('xxx')) 42 self.assertEqual(2, len(self._elementpath._cache)) 43 self.assertTrue(el.findall('b/c')) 44 self.assertEqual(2, len(self._elementpath._cache))
45
46 - def _assert_tokens(self, tokens, path, namespaces=None):
47 self.assertEqual(tokens, list(self._elementpath.xpath_tokenizer(path, namespaces)))
48
49 - def test_tokenizer(self):
50 assert_tokens = self._assert_tokens 51 assert_tokens( 52 [('/', '')], 53 '/', 54 ) 55 assert_tokens( 56 [('.', ''), ('/', ''), ('', 'a'), ('/', ''), ('', 'b'), ('/', ''), ('', 'c')], 57 './a/b/c', 58 ) 59 assert_tokens( 60 [('/', ''), ('', 'a'), ('/', ''), ('', 'b'), ('/', ''), ('', 'c')], 61 '/a/b/c', 62 ) 63 assert_tokens( 64 [('/', ''), ('', '{nsx}a'), ('/', ''), ('', '{nsy}b'), ('/', ''), ('', 'c')], 65 '/x:a/y:b/c', 66 {'x': 'nsx', 'y': 'nsy'}, 67 ) 68 assert_tokens( 69 [('/', ''), ('', '{nsx}a'), ('/', ''), ('', '{nsy}b'), ('/', ''), ('', '{nsnone}c')], 70 '/x:a/y:b/c', 71 {'x': 'nsx', 'y': 'nsy', None: 'nsnone'}, 72 )
73
75 assert_tokens = self._assert_tokens 76 assert_tokens( 77 [('', 'a'), ('[', ''), ('', 'b'), (']', '')], 78 'a[b]', 79 ) 80 assert_tokens( 81 [('', 'a'), ('[', ''), ('', 'b'), ('=', ''), ('"abc"', ''), (']', '')], 82 'a[b="abc"]', 83 ) 84 assert_tokens( 85 [('', 'a'), ('[', ''), ('.', ''), ('', ''), ('=', ''), ('', ''), ('"abc"', ''), (']', '')], 86 'a[. = "abc"]', 87 )
88
89 - def test_find(self):
90 """ 91 Test find methods (including xpath syntax). 92 Originally copied from 'selftest.py'. 93 """ 94 elem = etree.XML(""" 95 <body> 96 <tag class='a'>text</tag> 97 <tag class='b' /> 98 <section> 99 <tag class='b' id='inner'>subtext</tag> 100 </section> 101 </body> 102 """) 103 104 self.assertEqual(elem.find("tag").tag, 105 'tag') 106 self.assertEqual(etree.ElementTree(elem).find("tag").tag, 107 'tag') 108 self.assertEqual(elem.find("section/tag").tag, 109 'tag') 110 self.assertEqual(etree.ElementTree(elem).find("section/tag").tag, 111 'tag') 112 113 self.assertEqual(elem.findtext("tag"), 114 'text') 115 self.assertEqual(elem.findtext("tog"), 116 None) 117 self.assertEqual(elem.findtext("tog", "default"), 118 'default') 119 self.assertEqual(etree.ElementTree(elem).findtext("tag"), 120 'text') 121 self.assertEqual(elem.findtext("section/tag"), 122 'subtext') 123 self.assertEqual(etree.ElementTree(elem).findtext("section/tag"), 124 'subtext') 125 126 self.assertEqual(summarize_list(elem.findall("tag")), 127 ['tag', 'tag']) 128 self.assertEqual(summarize_list(elem.findall("*")), 129 ['tag', 'tag', 'section']) 130 self.assertEqual(summarize_list(elem.findall(".//tag")), 131 ['tag', 'tag', 'tag']) 132 self.assertEqual(summarize_list(elem.findall("section/tag")), 133 ['tag']) 134 self.assertEqual(summarize_list(elem.findall("section//tag")), 135 ['tag']) 136 137 self.assertEqual(summarize_list(elem.findall("section/*")), 138 ['tag']) 139 self.assertEqual(summarize_list(elem.findall("section//*")), 140 ['tag']) 141 self.assertEqual(summarize_list(elem.findall("section/.//*")), 142 ['tag']) 143 self.assertEqual(summarize_list(elem.findall("*/*")), 144 ['tag']) 145 self.assertEqual(summarize_list(elem.findall("*//*")), 146 ['tag']) 147 self.assertEqual(summarize_list(elem.findall("*/tag")), 148 ['tag']) 149 self.assertEqual(summarize_list(elem.findall("*/./tag")), 150 ['tag']) 151 self.assertEqual(summarize_list(elem.findall("./tag")), 152 ['tag', 'tag']) 153 self.assertEqual(summarize_list(elem.findall(".//tag")), 154 ['tag', 'tag', 'tag']) 155 self.assertEqual(summarize_list(elem.findall("././tag")), 156 ['tag', 'tag']) 157 158 self.assertEqual(summarize_list(elem.findall(".//tag[@class]")), 159 ['tag', 'tag', 'tag']) 160 self.assertEqual(summarize_list(elem.findall(".//tag[ @class]")), 161 ['tag', 'tag', 'tag']) 162 self.assertEqual(summarize_list(elem.findall(".//tag[@class ]")), 163 ['tag', 'tag', 'tag']) 164 self.assertEqual(summarize_list(elem.findall(".//tag[ @class ]")), 165 ['tag', 'tag', 'tag']) 166 self.assertEqual(summarize_list(elem.findall(".//tag[@class='a']")), 167 ['tag']) 168 self.assertEqual(summarize_list(elem.findall('.//tag[@class="a"]')), 169 ['tag']) 170 self.assertEqual(summarize_list(elem.findall(".//tag[@class='b']")), 171 ['tag', 'tag']) 172 self.assertEqual(summarize_list(elem.findall('.//tag[@class="b"]')), 173 ['tag', 'tag']) 174 self.assertEqual(summarize_list(elem.findall('.//tag[@class = "b"]')), 175 ['tag', 'tag']) 176 self.assertEqual(summarize_list(elem.findall(".//tag[@id]")), 177 ['tag']) 178 self.assertEqual(summarize_list(elem.findall(".//tag[@class][@id]")), 179 ['tag']) 180 self.assertEqual(summarize_list(elem.findall(".//section[tag]")), 181 ['section']) 182 self.assertEqual(summarize_list(elem.findall(".//section[element]")), 183 []) 184 185 self.assertEqual(summarize_list(elem.findall(".//section[tag='subtext']")), 186 ['section']) 187 self.assertEqual(summarize_list(elem.findall(".//section[tag ='subtext']")), 188 ['section']) 189 self.assertEqual(summarize_list(elem.findall(".//section[tag= 'subtext']")), 190 ['section']) 191 self.assertEqual(summarize_list(elem.findall(".//section[tag = 'subtext']")), 192 ['section']) 193 self.assertEqual(summarize_list(elem.findall(".//section[ tag = 'subtext' ]")), 194 ['section']) 195 self.assertEqual(summarize_list(elem.findall(".//tag[.='subtext']")), 196 ['tag']) 197 self.assertEqual(summarize_list(elem.findall(".//tag[. ='subtext']")), 198 ['tag']) 199 self.assertEqual(summarize_list(elem.findall('.//tag[.= "subtext"]')), 200 ['tag']) 201 self.assertEqual(summarize_list(elem.findall(".//tag[. = 'subtext']")), 202 ['tag']) 203 self.assertEqual(summarize_list(elem.findall(".//tag[. = 'subtext ']")), 204 []) 205 self.assertEqual(summarize_list(elem.findall(".//tag[.= ' subtext']")), 206 []) 207 208 self.assertEqual(summarize_list(elem.findall("../tag")), 209 []) 210 self.assertEqual(summarize_list(elem.findall("section/../tag")), 211 ['tag', 'tag']) 212 self.assertEqual(summarize_list(etree.ElementTree(elem).findall("./tag")), 213 ['tag', 'tag']) 214 215 # FIXME: ET's Path module handles this case incorrectly; this gives 216 # a warning in 1.3, and the behaviour will be modified in 1.4. 217 self.assertEqual(summarize_list(etree.ElementTree(elem).findall("/tag")), 218 ['tag', 'tag']) 219 220 # duplicate section => 2x tag matches 221 elem[1] = deepcopy(elem[2]) 222 self.assertEqual(summarize_list(elem.findall(".//section[tag = 'subtext']")), 223 ['section', 'section']) 224 self.assertEqual(summarize_list(elem.findall(".//tag[. = 'subtext']")), 225 ['tag', 'tag']) 226 self.assertEqual(summarize_list(elem.findall(".//tag[@class][@id]")), 227 ['tag', 'tag'])
228 229 230 #class ElementTreeElementPathTestCase(EtreeElementPathTestCase): 231 # import xml.etree.ElementTree as etree 232 # import xml.etree.ElementPath as _elementpath 233 234
235 -def test_suite():
236 suite = unittest.TestSuite() 237 suite.addTests([unittest.makeSuite(EtreeElementPathTestCase)]) 238 #suite.addTests([unittest.makeSuite(ElementTreeElementPathTestCase)]) 239 return suite
240 241 242 if __name__ == '__main__': 243 print('to test use test.py %s' % __file__) 244