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

Source Code for Module lxml.tests.test_nsclasses

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Test cases related to namespace implementation classes and the 
  5  namespace registry mechanism 
  6  """ 
  7   
  8  import unittest, sys, os.path 
  9   
 10  this_dir = os.path.dirname(__file__) 
 11  if this_dir not in sys.path: 
 12      sys.path.insert(0, this_dir) # needed for Py3 
 13   
 14  from common_imports import etree, HelperTestCase, _bytes 
 15  from common_imports import doctest, make_doctest 
 16   
17 -class ETreeNamespaceClassesTestCase(HelperTestCase):
18
19 - class default_class(etree.ElementBase):
20 pass
21 - class maeh_class(etree.ElementBase):
22 - def maeh(self):
23 return 'maeh'
24 - class bluff_class(etree.ElementBase):
25 - def bluff(self):
26 return 'bluff'
27
28 - def setUp(self):
35
36 - def tearDown(self):
37 etree.set_default_parser() 38 del self.Namespace 39 super(ETreeNamespaceClassesTestCase, self).tearDown()
40
41 - def test_registry(self):
42 ns = self.Namespace('ns01') 43 ns['maeh'] = self.maeh_class 44 45 self.Namespace('ns01').clear() 46 47 self.Namespace('ns02').update({'maeh' : self.maeh_class}) 48 self.Namespace('ns03').update({'bluff' : self.bluff_class}.items()) 49 self.Namespace('ns02').clear() 50 self.Namespace('ns03').clear()
51
52 - def test_ns_classes(self):
53 bluff_dict = {'bluff' : self.bluff_class} 54 maeh_dict = {'maeh' : self.maeh_class} 55 56 self.Namespace('ns10').update(bluff_dict) 57 58 tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')) 59 60 el = tree.getroot() 61 self.assert_(isinstance(el, etree.ElementBase)) 62 self.assert_(hasattr(el, 'bluff')) 63 self.assertFalse(hasattr(el[0], 'maeh')) 64 self.assertFalse(hasattr(el[0], 'bluff')) 65 self.assertEquals(el.bluff(), 'bluff') 66 del el 67 68 self.Namespace('ns11').update(maeh_dict) 69 el = tree.getroot() 70 self.assert_(hasattr(el, 'bluff')) 71 self.assert_(hasattr(el[0], 'maeh')) 72 self.assertEquals(el.bluff(), 'bluff') 73 self.assertEquals(el[0].maeh(), 'maeh') 74 del el 75 76 self.Namespace('ns10').clear() 77 78 tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')) 79 el = tree.getroot() 80 self.assertFalse(hasattr(el, 'bluff')) 81 self.assertFalse(hasattr(el, 'maeh')) 82 self.assertFalse(hasattr(el[0], 'bluff')) 83 self.assert_(hasattr(el[0], 'maeh')) 84 85 self.Namespace('ns11').clear()
86
87 - def test_default_tagname(self):
88 bluff_dict = { 89 None : self.bluff_class, 90 'maeh' : self.maeh_class 91 } 92 93 ns = self.Namespace("uri:nsDefClass") 94 ns.update(bluff_dict) 95 96 tree = self.parse(_bytes(''' 97 <test xmlns="bla" xmlns:ns1="uri:nsDefClass" xmlns:ns2="uri:nsDefClass"> 98 <ns2:el1/><ns1:el2/><ns1:maeh/><ns2:maeh/><maeh/> 99 </test> 100 ''')) 101 102 el = tree.getroot() 103 self.assertFalse(isinstance(el, etree.ElementBase)) 104 for child in el[:-1]: 105 self.assert_(isinstance(child, etree.ElementBase), child.tag) 106 self.assertFalse(isinstance(el[-1], etree.ElementBase)) 107 108 self.assert_(hasattr(el[0], 'bluff')) 109 self.assert_(hasattr(el[1], 'bluff')) 110 self.assert_(hasattr(el[2], 'maeh')) 111 self.assert_(hasattr(el[3], 'maeh')) 112 self.assertFalse(hasattr(el[4], 'maeh')) 113 del el 114 115 ns.clear()
116
117 - def test_create_element(self):
118 bluff_dict = {'bluff' : self.bluff_class} 119 self.Namespace('ns20').update(bluff_dict) 120 121 maeh_dict = {'maeh' : self.maeh_class} 122 self.Namespace('ns21').update(maeh_dict) 123 124 el = etree.Element("{ns20}bluff") 125 self.assert_(hasattr(el, 'bluff')) 126 127 child = etree.SubElement(el, "{ns21}maeh") 128 self.assert_(hasattr(child, 'maeh')) 129 child = etree.SubElement(el, "{ns20}bluff") 130 self.assert_(hasattr(child, 'bluff')) 131 child = etree.SubElement(el, "{ns21}bluff") 132 self.assertFalse(hasattr(child, 'bluff')) 133 self.assertFalse(hasattr(child, 'maeh')) 134 135 self.assert_(hasattr(el[0], 'maeh')) 136 self.assert_(hasattr(el[1], 'bluff')) 137 self.assertFalse(hasattr(el[2], 'bluff')) 138 self.assertFalse(hasattr(el[2], 'maeh')) 139 140 self.assertEquals(el.bluff(), 'bluff') 141 self.assertEquals(el[0].maeh(), 'maeh') 142 self.assertEquals(el[1].bluff(), 'bluff') 143 144 self.Namespace('ns20').clear() 145 self.Namespace('ns21').clear()
146
148 bluff_dict = {None : self.bluff_class} 149 self.Namespace('ns30').update(bluff_dict) 150 151 maeh_dict = {'maeh' : self.maeh_class} 152 self.Namespace(None).update(maeh_dict) 153 154 el = etree.Element("{ns30}bluff") 155 etree.SubElement(el, "maeh") 156 self.assert_(hasattr(el, 'bluff')) 157 self.assert_(hasattr(el[0], 'maeh')) 158 self.assertEquals(el.bluff(), 'bluff') 159 self.assertEquals(el[0].maeh(), 'maeh') 160 161 self.Namespace(None).clear() 162 self.Namespace('ns30').clear()
163
164 -def test_suite():
165 suite = unittest.TestSuite() 166 suite.addTests([unittest.makeSuite(ETreeNamespaceClassesTestCase)]) 167 suite.addTests( 168 [make_doctest('../../../doc/element_classes.txt')]) 169 return suite
170 171 if __name__ == '__main__': 172 print('to test use test.py %s' % __file__) 173