1
2
3 """
4 Tests specific to the extended etree API
5
6 Tests that apply to the general ElementTree API should go into
7 test_elementtree
8 """
9
10
11 import unittest, doctest, operator
12
13 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
14 from common_imports import SillyFileLike, canonicalize
15
16 xml_str = '''\
17 <root xmlns="myNS" xmlns:other="otherNS">
18 <c1 a1="A1" a2="A2" other:a3="A3">
19 <c2 a1="C2">0</c2>
20 <c2>1</c2>
21 <other:c2>2</other:c2>
22 </c1>
23 </root>'''
24
26 """Test cases for lxml.elementlib.classlookup
27 """
28 etree = etree
29
35
37 class TestElement(etree.ElementBase):
38 FIND_ME = "namespace class"
39
40 ns = etree.Namespace("myNS")
41 ns[None] = TestElement
42
43 lookup = etree.ElementNamespaceClassLookup()
44 etree.setElementClassLookup(lookup)
45
46 root = etree.XML(xml_str)
47 self.assertEquals(root.FIND_ME,
48 TestElement.FIND_ME)
49 self.assertEquals(root[0].FIND_ME,
50 TestElement.FIND_ME)
51 self.assertFalse(hasattr(root[0][-1], 'FIND_ME'))
52
54 class TestElement(etree.ElementBase):
55 FIND_ME = "default element"
56 class TestComment(etree.CommentBase):
57 FIND_ME = "default comment"
58 class TestPI(etree.PIBase):
59 FIND_ME = "default pi"
60
61 parser = etree.XMLParser()
62
63 lookup = etree.ElementDefaultClassLookup(
64 element=TestElement, comment=TestComment, pi=TestPI)
65 parser.setElementClassLookup(lookup)
66
67 root = etree.XML("""<?xml version='1.0'?>
68 <root>
69 <?myPI?>
70 <!-- hi -->
71 </root>
72 """, parser)
73
74 self.assertEquals("default element", root.FIND_ME)
75 self.assertEquals("default pi", root[0].FIND_ME)
76 self.assertEquals("default comment", root[1].FIND_ME)
77
79 class TestElement(etree.ElementBase):
80 FIND_ME = "namespace class"
81
82 ns = etree.Namespace("myNS")
83 ns[None] = TestElement
84
85 lookup = etree.ElementDefaultClassLookup()
86 etree.setElementClassLookup(lookup)
87
88 root = etree.XML(xml_str)
89 self.assertFalse(hasattr(root, 'FIND_ME'))
90 self.assertFalse(hasattr(root[0][-1], 'FIND_ME'))
91
93 class TestElement(etree.ElementBase):
94 FIND_ME = "attribute_based"
95
96 class_dict = {"A1" : TestElement}
97
98 lookup = etree.AttributeBasedElementClassLookup(
99 "a1", class_dict)
100 etree.setElementClassLookup(lookup)
101
102 root = etree.XML(xml_str)
103 self.assertFalse(hasattr(root, 'FIND_ME'))
104 self.assertEquals(root[0].FIND_ME,
105 TestElement.FIND_ME)
106 self.assertFalse(hasattr(root[0][0], 'FIND_ME'))
107
109 class TestElement(etree.ElementBase):
110 FIND_ME = "custom"
111
112 class MyLookup(etree.CustomElementClassLookup):
113 def lookup(self, t, d, ns, name):
114 if name == 'c1':
115 return TestElement
116
117 etree.setElementClassLookup( MyLookup() )
118
119 root = etree.XML(xml_str)
120 self.assertFalse(hasattr(root, 'FIND_ME'))
121 self.assertEquals(root[0].FIND_ME,
122 TestElement.FIND_ME)
123 self.assertFalse(hasattr(root[0][1], 'FIND_ME'))
124
126 class TestElement1(etree.ElementBase):
127 FIND_ME = "custom"
128
129 class TestElement2(etree.ElementBase):
130 FIND_ME = "nsclasses"
131
132 class MyLookup(etree.CustomElementClassLookup):
133 def lookup(self, t, d, ns, name):
134 if name == 'c1':
135 return TestElement1
136
137 ns = etree.Namespace("otherNS")
138 ns[None] = TestElement2
139
140 lookup = etree.ElementNamespaceClassLookup( MyLookup() )
141 etree.setElementClassLookup(lookup)
142
143 root = etree.XML(xml_str)
144 self.assertFalse(hasattr(root, 'FIND_ME'))
145 self.assertEquals(root[0].FIND_ME,
146 TestElement1.FIND_ME)
147 self.assertFalse(hasattr(root[0][1], 'FIND_ME'))
148 self.assertEquals(root[0][-1].FIND_ME,
149 TestElement2.FIND_ME)
150
152 class TestElement(etree.ElementBase):
153 FIND_ME = "parser_based"
154
155 lookup = etree.ParserBasedElementClassLookup()
156 etree.setElementClassLookup(lookup)
157
158 class MyLookup(etree.CustomElementClassLookup):
159 def lookup(self, t, d, ns, name):
160 return TestElement
161
162 parser = etree.XMLParser()
163 parser.setElementClassLookup( MyLookup() )
164
165 root = etree.parse(StringIO(xml_str), parser).getroot()
166 self.assertEquals(root.FIND_ME,
167 TestElement.FIND_ME)
168 self.assertEquals(root[0].FIND_ME,
169 TestElement.FIND_ME)
170
171 root = etree.parse(StringIO(xml_str)).getroot()
172 self.assertFalse(hasattr(root, 'FIND_ME'))
173 self.assertFalse(hasattr(root[0], 'FIND_ME'))
174
175
177 suite = unittest.TestSuite()
178 suite.addTests([unittest.makeSuite(ClassLookupTestCase)])
179 return suite
180
181 if __name__ == '__main__':
182 unittest.main()
183