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