Package lxml :: Package tests :: Module test_classlookup
[frames] | no frames]

Source Code for Module lxml.tests.test_classlookup

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  Tests for different Element class lookup mechanisms. 
  5  """ 
  6   
  7   
  8  import unittest, doctest, operator 
  9   
 10  from common_imports import etree, StringIO, HelperTestCase, fileInTestDir 
 11  from common_imports import SillyFileLike, canonicalize 
 12   
 13  xml_str = '''\ 
 14  <root xmlns="myNS" xmlns:other="otherNS"> 
 15    <c1 a1="A1" a2="A2" other:a3="A3"> 
 16      <c2 a1="C2">0</c2> 
 17      <c2>1</c2> 
 18      <other:c2>2</other:c2> 
 19    </c1> 
 20  </root>''' 
 21   
22 -class ClassLookupTestCase(HelperTestCase):
23 """Test cases for different Element class lookup mechanisms. 24 """ 25 etree = etree 26
27 - def tearDown(self):
30
31 - def test_namespace_lookup(self):
32 class TestElement(etree.ElementBase): 33 FIND_ME = "namespace class"
34 35 lookup = etree.ElementNamespaceClassLookup() 36 etree.setElementClassLookup(lookup) 37 38 ns = lookup.get_namespace("myNS") 39 ns[None] = TestElement 40 41 root = etree.XML(xml_str) 42 self.assertEquals(root.FIND_ME, 43 TestElement.FIND_ME) 44 self.assertEquals(root[0].FIND_ME, 45 TestElement.FIND_ME) 46 self.assertFalse(hasattr(root[0][-1], 'FIND_ME'))
47
48 - def test_default_class_lookup(self):
49 class TestElement(etree.ElementBase): 50 FIND_ME = "default element"
51 class TestComment(etree.CommentBase): 52 FIND_ME = "default comment" 53 class TestPI(etree.PIBase): 54 FIND_ME = "default pi" 55 56 parser = etree.XMLParser() 57 58 lookup = etree.ElementDefaultClassLookup( 59 element=TestElement, comment=TestComment, pi=TestPI) 60 parser.setElementClassLookup(lookup) 61 62 root = etree.XML("""<?xml version='1.0'?> 63 <root> 64 <?myPI?> 65 <!-- hi --> 66 </root> 67 """, parser) 68 69 self.assertEquals("default element", root.FIND_ME) 70 self.assertEquals("default pi", root[0].FIND_ME) 71 self.assertEquals("default comment", root[1].FIND_ME) 72
73 - def test_attribute_based_lookup(self):
74 class TestElement(etree.ElementBase): 75 FIND_ME = "attribute_based"
76 77 class_dict = {"A1" : TestElement} 78 79 lookup = etree.AttributeBasedElementClassLookup( 80 "a1", class_dict) 81 etree.setElementClassLookup(lookup) 82 83 root = etree.XML(xml_str) 84 self.assertFalse(hasattr(root, 'FIND_ME')) 85 self.assertEquals(root[0].FIND_ME, 86 TestElement.FIND_ME) 87 self.assertFalse(hasattr(root[0][0], 'FIND_ME')) 88
89 - def test_custom_lookup(self):
90 class TestElement(etree.ElementBase): 91 FIND_ME = "custom"
92 93 class MyLookup(etree.CustomElementClassLookup): 94 def lookup(self, t, d, ns, name): 95 if name == 'c1': 96 return TestElement 97 98 etree.setElementClassLookup( MyLookup() ) 99 100 root = etree.XML(xml_str) 101 self.assertFalse(hasattr(root, 'FIND_ME')) 102 self.assertEquals(root[0].FIND_ME, 103 TestElement.FIND_ME) 104 self.assertFalse(hasattr(root[0][1], 'FIND_ME')) 105
106 - def test_custom_lookup_ns_fallback(self):
107 class TestElement1(etree.ElementBase): 108 FIND_ME = "custom"
109 110 class TestElement2(etree.ElementBase): 111 FIND_ME = "nsclasses" 112 113 class MyLookup(etree.CustomElementClassLookup): 114 def lookup(self, t, d, ns, name): 115 if name == 'c1': 116 return TestElement1 117 118 lookup = etree.ElementNamespaceClassLookup( MyLookup() ) 119 etree.setElementClassLookup(lookup) 120 121 ns = lookup.get_namespace("otherNS") 122 ns[None] = TestElement2 123 124 root = etree.XML(xml_str) 125 self.assertFalse(hasattr(root, 'FIND_ME')) 126 self.assertEquals(root[0].FIND_ME, 127 TestElement1.FIND_ME) 128 self.assertFalse(hasattr(root[0][1], 'FIND_ME')) 129 self.assertEquals(root[0][-1].FIND_ME, 130 TestElement2.FIND_ME) 131
132 - def test_parser_based_lookup(self):
133 class TestElement(etree.ElementBase): 134 FIND_ME = "parser_based"
135 136 lookup = etree.ParserBasedElementClassLookup() 137 etree.setElementClassLookup(lookup) 138 139 class MyLookup(etree.CustomElementClassLookup): 140 def lookup(self, t, d, ns, name): 141 return TestElement 142 143 parser = etree.XMLParser() 144 parser.setElementClassLookup( MyLookup() ) 145 146 root = etree.parse(StringIO(xml_str), parser).getroot() 147 self.assertEquals(root.FIND_ME, 148 TestElement.FIND_ME) 149 self.assertEquals(root[0].FIND_ME, 150 TestElement.FIND_ME) 151 152 root = etree.parse(StringIO(xml_str)).getroot() 153 self.assertFalse(hasattr(root, 'FIND_ME')) 154 self.assertFalse(hasattr(root[0], 'FIND_ME')) 155
156 - def test_lookup_without_fallback(self):
157 class Lookup(etree.CustomElementClassLookup): 158 def __init__(self): 159 # no super call here, so no fallback is set 160 pass
161 162 def lookup(self, node_type, document, namespace, name): 163 return Foo 164 165 class Foo(etree.ElementBase): 166 def custom(self): 167 return "test" 168 169 parser = self.etree.XMLParser() 170 parser.set_element_class_lookup( Lookup() ) 171 172 root = etree.XML('<foo/>', parser) 173 174 self.assertEquals("test", root.custom()) 175 176
177 -def test_suite():
178 suite = unittest.TestSuite() 179 suite.addTests([unittest.makeSuite(ClassLookupTestCase)]) 180 return suite
181 182 if __name__ == '__main__': 183 print 'to test use test.py %s' % __file__ 184