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  from __future__ import absolute_import 
  9   
 10  import unittest 
 11   
 12  from .common_imports import etree, HelperTestCase, _bytes, make_doctest 
 13   
14 -class ETreeNamespaceClassesTestCase(HelperTestCase):
15
16 - class default_class(etree.ElementBase):
17 pass
18 - class maeh_class(etree.ElementBase):
19 - def maeh(self):
20 return 'maeh'
21 - class bluff_class(etree.ElementBase):
22 - def bluff(self):
23 return 'bluff'
24
25 - def setUp(self):
32
33 - def tearDown(self):
34 etree.set_default_parser() 35 del self.Namespace 36 super(ETreeNamespaceClassesTestCase, self).tearDown()
37
38 - def test_registry(self):
39 ns = self.Namespace('ns01') 40 ns['maeh'] = self.maeh_class 41 42 self.Namespace('ns01').clear() 43 44 self.Namespace('ns02').update({'maeh' : self.maeh_class}) 45 self.Namespace('ns03').update({'bluff' : self.bluff_class}.items()) 46 self.Namespace('ns02').clear() 47 self.Namespace('ns03').clear()
48
49 - def test_ns_classes(self):
50 bluff_dict = {'bluff' : self.bluff_class} 51 maeh_dict = {'maeh' : self.maeh_class} 52 53 self.Namespace('ns10').update(bluff_dict) 54 55 tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')) 56 57 el = tree.getroot() 58 self.assertTrue(isinstance(el, etree.ElementBase)) 59 self.assertTrue(hasattr(el, 'bluff')) 60 self.assertFalse(hasattr(el[0], 'maeh')) 61 self.assertFalse(hasattr(el[0], 'bluff')) 62 self.assertEqual(el.bluff(), 'bluff') 63 del el 64 65 self.Namespace('ns11').update(maeh_dict) 66 el = tree.getroot() 67 self.assertTrue(hasattr(el, 'bluff')) 68 self.assertTrue(hasattr(el[0], 'maeh')) 69 self.assertEqual(el.bluff(), 'bluff') 70 self.assertEqual(el[0].maeh(), 'maeh') 71 del el 72 73 self.Namespace('ns10').clear() 74 75 tree = self.parse(_bytes('<bluff xmlns="ns10"><ns11:maeh xmlns:ns11="ns11"/></bluff>')) 76 el = tree.getroot() 77 self.assertFalse(hasattr(el, 'bluff')) 78 self.assertFalse(hasattr(el, 'maeh')) 79 self.assertFalse(hasattr(el[0], 'bluff')) 80 self.assertTrue(hasattr(el[0], 'maeh')) 81 82 self.Namespace('ns11').clear()
83
84 - def test_default_tagname(self):
85 bluff_dict = { 86 None : self.bluff_class, 87 'maeh' : self.maeh_class 88 } 89 90 ns = self.Namespace("uri:nsDefClass") 91 ns.update(bluff_dict) 92 93 tree = self.parse(_bytes(''' 94 <test xmlns="bla" xmlns:ns1="uri:nsDefClass" xmlns:ns2="uri:nsDefClass"> 95 <ns2:el1/><ns1:el2/><ns1:maeh/><ns2:maeh/><maeh/> 96 </test> 97 ''')) 98 99 el = tree.getroot() 100 self.assertFalse(isinstance(el, etree.ElementBase)) 101 for child in el[:-1]: 102 self.assertTrue(isinstance(child, etree.ElementBase), child.tag) 103 self.assertFalse(isinstance(el[-1], etree.ElementBase)) 104 105 self.assertTrue(hasattr(el[0], 'bluff')) 106 self.assertTrue(hasattr(el[1], 'bluff')) 107 self.assertTrue(hasattr(el[2], 'maeh')) 108 self.assertTrue(hasattr(el[3], 'maeh')) 109 self.assertFalse(hasattr(el[4], 'maeh')) 110 del el 111 112 ns.clear()
113
114 - def test_create_element(self):
115 bluff_dict = {'bluff' : self.bluff_class} 116 self.Namespace('ns20').update(bluff_dict) 117 118 maeh_dict = {'maeh' : self.maeh_class} 119 self.Namespace('ns21').update(maeh_dict) 120 121 el = etree.Element("{ns20}bluff") 122 self.assertTrue(hasattr(el, 'bluff')) 123 124 child = etree.SubElement(el, "{ns21}maeh") 125 self.assertTrue(hasattr(child, 'maeh')) 126 child = etree.SubElement(el, "{ns20}bluff") 127 self.assertTrue(hasattr(child, 'bluff')) 128 child = etree.SubElement(el, "{ns21}bluff") 129 self.assertFalse(hasattr(child, 'bluff')) 130 self.assertFalse(hasattr(child, 'maeh')) 131 132 self.assertTrue(hasattr(el[0], 'maeh')) 133 self.assertTrue(hasattr(el[1], 'bluff')) 134 self.assertFalse(hasattr(el[2], 'bluff')) 135 self.assertFalse(hasattr(el[2], 'maeh')) 136 137 self.assertEqual(el.bluff(), 'bluff') 138 self.assertEqual(el[0].maeh(), 'maeh') 139 self.assertEqual(el[1].bluff(), 'bluff') 140 141 self.Namespace('ns20').clear() 142 self.Namespace('ns21').clear()
143
145 bluff_dict = {None : self.bluff_class} 146 self.Namespace('ns30').update(bluff_dict) 147 148 maeh_dict = {'maeh' : self.maeh_class} 149 self.Namespace(None).update(maeh_dict) 150 151 el = etree.Element("{ns30}bluff") 152 etree.SubElement(el, "maeh") 153 self.assertTrue(hasattr(el, 'bluff')) 154 self.assertTrue(hasattr(el[0], 'maeh')) 155 self.assertEqual(el.bluff(), 'bluff') 156 self.assertEqual(el[0].maeh(), 'maeh') 157 158 self.Namespace(None).clear() 159 self.Namespace('ns30').clear()
160
161 - def test_element_creation(self):
162 default, bluff, maeh = ( 163 self.default_class, self.bluff_class, self.maeh_class) 164 165 class honk(etree.ElementBase): 166 TAG = 'HONK' 167 NAMESPACE = 'http://a.b/c'
168 169 el = default( 170 "test", 171 "text", 172 bluff(honk, "TaIL", maeh), 173 maeh("TeXT", bluff, honk(), "TAiL"), 174 "Tail") 175 176 self.assertEqual('default_class', el.tag) 177 self.assertEqual('testtext', el.text) 178 self.assertEqual(None, el.tail) 179 self.assertEqual(2, len(el)) 180 self.assertEqual(7, len(list(el.iter()))) 181 182 self.assertEqual('bluff_class', el[0].tag) 183 self.assertEqual('TaIL', el[0][0].tail) 184 self.assertEqual('TaIL', ''.join(el[0].itertext())) 185 self.assertEqual('{http://a.b/c}HONK', 186 el[0][0].tag) 187 self.assertEqual('maeh_class', 188 el[0][1].tag) 189 190 self.assertEqual('maeh_class', el[1].tag) 191 self.assertEqual('TeXT', el[1].text) 192 self.assertEqual('bluff_class', el[1][0].tag) 193 self.assertEqual('{http://a.b/c}HONK', el[1][1].tag) 194 self.assertEqual('TAiL', el[1][1].tail) 195 196 self.assertEqual('TeXTTAiL', 197 ''.join(el[1].itertext())) 198 self.assertEqual('Tail', el[1].tail) 199 self.assertEqual('TAiL', el[1][1].tail) 200 self.assertEqual('bluff_class', el[1][0].tag) 201 self.assertEqual('{http://a.b/c}HONK', el[1][1].tag)
202 203
204 -def test_suite():
205 suite = unittest.TestSuite() 206 suite.addTests([unittest.makeSuite(ETreeNamespaceClassesTestCase)]) 207 suite.addTests( 208 [make_doctest('../../../doc/element_classes.txt')]) 209 return suite
210 211 if __name__ == '__main__': 212 print('to test use test.py %s' % __file__) 213