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

Source Code for Module lxml.tests.test_pyclasslookup

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Tests specific to the Python based class lookup. 
  5  """ 
  6   
  7   
  8  import unittest, operator 
  9   
 10  from common_imports import etree, StringIO, HelperTestCase, fileInTestDir 
 11  from common_imports import SillyFileLike, canonicalize, doctest 
 12  from common_imports import itemgetter 
 13   
 14  from lxml.pyclasslookup import PythonElementClassLookup 
 15   
 16  xml_str = '''\ 
 17  <obj:root xmlns:obj="objectified" xmlns:other="otherNS"> 
 18    <obj:c1 a1="A1" a2="A2" other:a3="A3"> 
 19      <obj:c2>0</obj:c2> 
 20      <obj:c2>1</obj:c2> 
 21      <obj:c2>2</obj:c2> 
 22      <other:c2>3</other:c2> 
 23      <c2>3</c2> 
 24    </obj:c1> 
 25  </obj:root>''' 
 26   
 27   
28 -class PyClassLookupTestCase(HelperTestCase):
29 """Test cases for the lxml.pyclasslookup class lookup mechanism. 30 """ 31 etree = etree 32 parser = etree.XMLParser() 33 Element = parser.makeelement 34
35 - def tearDown(self):
38
39 - def _setClassLookup(self, lookup_function):
40 class Lookup(PythonElementClassLookup): 41 def lookup(self, *args): 42 return lookup_function(*args)
43 self.parser.setElementClassLookup( Lookup() ) 44
45 - def _buildElementClass(self):
46 class LocalElement(etree.ElementBase): 47 pass
48 return LocalElement 49
50 - def XML(self, xml):
51 return self.etree.XML(xml, self.parser)
52 53 # --- Test cases 54
55 - def test_lookup(self):
56 el_class = self._buildElementClass() 57 el_class.i = 1 58 def lookup(*args): 59 if el_class.i == 1: 60 el_class.i = 2 61 return el_class
62 self._setClassLookup(lookup) 63 root = self.XML(xml_str) 64 self.assertEquals(2, el_class.i) 65
66 - def test_lookup_keep_ref_assertion(self):
67 el_class = self._buildElementClass() 68 el_class.EL = None 69 def lookup(doc, el): 70 if el_class.EL is None: 71 el_class.EL = el 72 return el_class
73 self._setClassLookup(lookup) 74 root = self.XML(xml_str) 75 self.assertNotEquals(None, el_class.EL) 76 self.assertRaises(AssertionError, el_class.EL.getchildren) 77
78 - def test_lookup_tag(self):
79 el_class = self._buildElementClass() 80 el_class.TAG = None 81 def lookup(doc, el): 82 if el_class.TAG is None: 83 el_class.TAG = el.tag 84 return el_class
85 self._setClassLookup(lookup) 86 root = self.XML(xml_str) 87 self.assertNotEquals(None, root.TAG) 88 self.assertEquals(root.tag, root.TAG) 89
90 - def test_lookup_text(self):
91 el_class = self._buildElementClass() 92 el_class.TEXT = None 93 def lookup(doc, el): 94 if el_class.TEXT is None: 95 el_class.TEXT = el.text 96 return el_class
97 self._setClassLookup(lookup) 98 root = self.XML(xml_str) 99 self.assertNotEquals(None, root.TEXT) 100 self.assertEquals(root.text, root.TEXT) 101
102 - def test_lookup_tail(self):
103 el_class = self._buildElementClass() 104 el_class.TAIL = None 105 def lookup(doc, el): 106 if el_class.TAIL is None: 107 el_class.TAIL = el.tail 108 return el_class
109 self._setClassLookup(lookup) 110 root = self.XML(xml_str) 111 self.assertEquals(root.tail, root.TAIL) 112
113 - def test_lookup_attrib(self):
114 el_class = self._buildElementClass() 115 el_class.ATTRIB = None 116 def lookup(doc, el): 117 if el_class.ATTRIB is None: 118 el_class.ATTRIB = el[0].attrib 119 return el_class
120 self._setClassLookup(lookup) 121 root = self.XML(xml_str) 122 items1 = root[0].attrib.items() 123 items1.sort() 124 items2 = root.ATTRIB.items() 125 items2.sort() 126 self.assertEquals(items1, items2) 127
128 - def test_lookup_prefix(self):
129 el_class = self._buildElementClass() 130 el_class.PREFIX = None 131 def lookup(doc, el): 132 if el_class.PREFIX is None: 133 el_class.PREFIX = el.prefix 134 return el_class
135 self._setClassLookup(lookup) 136 root = self.XML(xml_str) 137 self.assertEquals(root.prefix, root.PREFIX) 138
139 - def test_lookup_sourceline(self):
140 el_class = self._buildElementClass() 141 el_class.LINE = None 142 def lookup(doc, el): 143 if el_class.LINE is None: 144 el_class.LINE = el.sourceline 145 return el_class
146 self._setClassLookup(lookup) 147 root = self.XML(xml_str) 148 self.assertEquals(root.sourceline, root.LINE) 149
150 - def test_lookup_getitem(self):
151 el_class = self._buildElementClass() 152 el_class.CHILD_TAG = None 153 def lookup(doc, el): 154 el_class.CHILD_TAG = el[0].tag 155 return el_class
156 self._setClassLookup(lookup) 157 root = self.XML(xml_str) 158 child_tag = root.CHILD_TAG 159 self.assertNotEquals(None, child_tag) 160 self.assertEquals(root[0].tag, child_tag) 161
162 - def test_lookup_getitem_neg(self):
163 el_class = self._buildElementClass() 164 el_class.CHILD_TAG = None 165 def lookup(doc, el): 166 if el_class.CHILD_TAG is None: 167 el_class.CHILD_TAG = el[-1].tag 168 return el_class
169 self._setClassLookup(lookup) 170 root = self.XML(xml_str) 171 child_tag = root.CHILD_TAG 172 self.assertNotEquals(None, child_tag) 173 self.assertEquals(root[-1].tag, child_tag) 174
175 - def test_lookup_getslice(self):
176 el_class = self._buildElementClass() 177 el_class.CHILD_TAGS = None 178 def lookup(doc, el): 179 if el_class.CHILD_TAGS is None: 180 el_class.CHILD_TAGS = [ c.tag for c in el[1:-1] ] 181 return el_class
182 self._setClassLookup(lookup) 183 root = self.XML(xml_str) 184 child_tags = root.CHILD_TAGS 185 self.assertNotEquals(None, child_tags) 186 self.assertEquals([ c.tag for c in root[1:-1] ], 187 child_tags) 188
189 - def test_lookup_len(self):
190 el_class = self._buildElementClass() 191 el_class.LEN = None 192 def lookup(doc, el): 193 if el_class.LEN is None: 194 el_class.LEN = len(el) 195 return el_class
196 self._setClassLookup(lookup) 197 root = self.XML(xml_str) 198 self.assertEquals(1, el_class.LEN) 199
200 - def test_lookup_bool(self):
201 el_class = self._buildElementClass() 202 el_class.TRUE = None 203 def lookup(doc, el): 204 if el_class.TRUE is None: 205 el_class.TRUE = bool(el) 206 return el_class
207 self._setClassLookup(lookup) 208 root = self.XML(xml_str) 209 self.assert_(el_class.TRUE) 210
211 - def test_lookup_get(self):
212 el_class = self._buildElementClass() 213 el_class.VAL = None 214 def lookup(doc, el): 215 if el_class.VAL is None: 216 el_class.VAL = el[0].get('a1') 217 return el_class
218 self._setClassLookup(lookup) 219 root = self.XML(xml_str) 220 self.assertNotEquals(None, el_class.VAL) 221 self.assertEquals(root[0].get('a1'), el_class.VAL) 222
223 - def test_lookup_get_default(self):
224 el_class = self._buildElementClass() 225 default = str(id(el_class)) 226 el_class.VAL = None 227 def lookup(doc, el): 228 if el_class.VAL is None: 229 el_class.VAL = el[0].get('unknownattribute', default) 230 return el_class
231 self._setClassLookup(lookup) 232 root = self.XML(xml_str) 233 self.assertEquals(default, el_class.VAL) 234
235 - def test_lookup_getchildren(self):
236 el_class = self._buildElementClass() 237 el_class.CHILD_TAGS = None 238 def lookup(doc, el): 239 if el_class.CHILD_TAGS is None: 240 el_class.CHILD_TAGS = [ c.tag for c in el.getchildren() ] 241 return el_class
242 self._setClassLookup(lookup) 243 root = self.XML(xml_str) 244 child_tags = root.CHILD_TAGS 245 self.assertNotEquals(None, child_tags) 246 self.assertEquals([ c.tag for c in root.getchildren() ], 247 child_tags) 248
249 - def test_lookup_getparent(self):
250 el_class = self._buildElementClass() 251 el_class.PARENT = None 252 def lookup(doc, el): 253 if el_class.PARENT is None: 254 el_class.PARENT = el[0].getparent().tag 255 return el_class
256 self._setClassLookup(lookup) 257 root = self.XML(xml_str) 258 self.assertEquals(root.tag, root.PARENT) 259
260 - def test_lookup_getnext(self):
261 el_class = self._buildElementClass() 262 el_class.NEXT = None 263 def lookup(doc, el): 264 if el_class.NEXT is None: 265 el_class.NEXT = el[0][1].getnext().tag 266 return el_class
267 self._setClassLookup(lookup) 268 root = self.XML(xml_str) 269 self.assertNotEquals(None, el_class.NEXT) 270 self.assertEquals(root[0][1].getnext().tag, el_class.NEXT) 271
272 - def test_lookup_getprevious(self):
273 el_class = self._buildElementClass() 274 el_class.PREV = None 275 def lookup(doc, el): 276 if el_class.PREV is None: 277 el_class.PREV = el[0][1].getprevious().tag 278 return el_class
279 self._setClassLookup(lookup) 280 root = self.XML(xml_str) 281 self.assertNotEquals(None, el_class.PREV) 282 self.assertEquals(root[0][1].getprevious().tag, el_class.PREV) 283 284
285 -def test_suite():
286 suite = unittest.TestSuite() 287 suite.addTests([unittest.makeSuite(PyClassLookupTestCase)]) 288 return suite
289 290 if __name__ == '__main__': 291 unittest.main() 292