1
2
3 """
4 Tests specific to the Python based class lookup.
5 """
6
7
8 import unittest, operator
9
10 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
11 from common_imports import SillyFileLike, canonicalize, doctest
12
13 from lxml.pyclasslookup import PythonElementClassLookup
14
15 xml_str = '''\
16 <obj:root xmlns:obj="objectified" xmlns:other="otherNS">
17 <obj:c1 a1="A1" a2="A2" other:a3="A3">
18 <obj:c2>0</obj:c2>
19 <obj:c2>1</obj:c2>
20 <obj:c2>2</obj:c2>
21 <other:c2>3</other:c2>
22 <c2>3</c2>
23 </obj:c1>
24 </obj:root>'''
25
26
28 """Test cases for the lxml.pyclasslookup class lookup mechanism.
29 """
30 etree = etree
31 parser = etree.XMLParser()
32 Element = parser.makeelement
33
37
39 class Lookup(PythonElementClassLookup):
40 def lookup(self, *args):
41 return lookup_function(*args)
42 self.parser.setElementClassLookup( Lookup() )
43
45 class LocalElement(etree.ElementBase):
46 pass
47 return LocalElement
48
51
52
53
55 el_class = self._buildElementClass()
56 el_class.i = 1
57 def lookup(*args):
58 if el_class.i == 1:
59 el_class.i = 2
60 return el_class
61 self._setClassLookup(lookup)
62 root = self.XML(xml_str)
63 self.assertEquals(2, el_class.i)
64
66 el_class = self._buildElementClass()
67 el_class.EL = None
68 def lookup(doc, el):
69 if el_class.EL is None:
70 el_class.EL = el
71 return el_class
72 self._setClassLookup(lookup)
73 root = self.XML(xml_str)
74 self.assertNotEquals(None, el_class.EL)
75 self.assertRaises(AssertionError, el_class.EL.getchildren)
76
78 el_class = self._buildElementClass()
79 el_class.TAG = None
80 def lookup(doc, el):
81 if el_class.TAG is None:
82 el_class.TAG = el.tag
83 return el_class
84 self._setClassLookup(lookup)
85 root = self.XML(xml_str)
86 self.assertNotEquals(None, root.TAG)
87 self.assertEquals(root.tag, root.TAG)
88
90 el_class = self._buildElementClass()
91 el_class.TEXT = None
92 def lookup(doc, el):
93 if el_class.TEXT is None:
94 el_class.TEXT = el.text
95 return el_class
96 self._setClassLookup(lookup)
97 root = self.XML(xml_str)
98 self.assertNotEquals(None, root.TEXT)
99 self.assertEquals(root.text, root.TEXT)
100
102 el_class = self._buildElementClass()
103 el_class.TAIL = None
104 def lookup(doc, el):
105 if el_class.TAIL is None:
106 el_class.TAIL = el.tail
107 return el_class
108 self._setClassLookup(lookup)
109 root = self.XML(xml_str)
110 self.assertEquals(root.tail, root.TAIL)
111
113 el_class = self._buildElementClass()
114 el_class.ATTRIB = None
115 def lookup(doc, el):
116 if el_class.ATTRIB is None:
117 el_class.ATTRIB = el[0].attrib
118 return el_class
119 self._setClassLookup(lookup)
120 root = self.XML(xml_str)
121 items1 = root[0].attrib.items()
122 items1.sort()
123 items2 = root.ATTRIB.items()
124 items2.sort()
125 self.assertEquals(items1, items2)
126
128 el_class = self._buildElementClass()
129 el_class.PREFIX = None
130 def lookup(doc, el):
131 if el_class.PREFIX is None:
132 el_class.PREFIX = el.prefix
133 return el_class
134 self._setClassLookup(lookup)
135 root = self.XML(xml_str)
136 self.assertEquals(root.prefix, root.PREFIX)
137
139 el_class = self._buildElementClass()
140 el_class.LINE = None
141 def lookup(doc, el):
142 if el_class.LINE is None:
143 el_class.LINE = el.sourceline
144 return el_class
145 self._setClassLookup(lookup)
146 root = self.XML(xml_str)
147 self.assertEquals(root.sourceline, root.LINE)
148
150 el_class = self._buildElementClass()
151 el_class.CHILD_TAG = None
152 def lookup(doc, el):
153 el_class.CHILD_TAG = el[0].tag
154 return el_class
155 self._setClassLookup(lookup)
156 root = self.XML(xml_str)
157 child_tag = root.CHILD_TAG
158 self.assertNotEquals(None, child_tag)
159 self.assertEquals(root[0].tag, child_tag)
160
162 el_class = self._buildElementClass()
163 el_class.CHILD_TAG = None
164 def lookup(doc, el):
165 if el_class.CHILD_TAG is None:
166 el_class.CHILD_TAG = el[-1].tag
167 return el_class
168 self._setClassLookup(lookup)
169 root = self.XML(xml_str)
170 child_tag = root.CHILD_TAG
171 self.assertNotEquals(None, child_tag)
172 self.assertEquals(root[-1].tag, child_tag)
173
175 el_class = self._buildElementClass()
176 el_class.CHILD_TAGS = None
177 def lookup(doc, el):
178 if el_class.CHILD_TAGS is None:
179 el_class.CHILD_TAGS = [ c.tag for c in el[1:-1] ]
180 return el_class
181 self._setClassLookup(lookup)
182 root = self.XML(xml_str)
183 child_tags = root.CHILD_TAGS
184 self.assertNotEquals(None, child_tags)
185 self.assertEquals([ c.tag for c in root[1:-1] ],
186 child_tags)
187
189 el_class = self._buildElementClass()
190 el_class.LEN = None
191 def lookup(doc, el):
192 if el_class.LEN is None:
193 el_class.LEN = len(el)
194 return el_class
195 self._setClassLookup(lookup)
196 root = self.XML(xml_str)
197 self.assertEquals(1, el_class.LEN)
198
200 el_class = self._buildElementClass()
201 el_class.TRUE = None
202 def lookup(doc, el):
203 if el_class.TRUE is None:
204 el_class.TRUE = bool(el)
205 return el_class
206 self._setClassLookup(lookup)
207 root = self.XML(xml_str)
208 self.assert_(el_class.TRUE)
209
211 el_class = self._buildElementClass()
212 el_class.VAL = None
213 def lookup(doc, el):
214 if el_class.VAL is None:
215 el_class.VAL = el[0].get('a1')
216 return el_class
217 self._setClassLookup(lookup)
218 root = self.XML(xml_str)
219 self.assertNotEquals(None, el_class.VAL)
220 self.assertEquals(root[0].get('a1'), el_class.VAL)
221
223 el_class = self._buildElementClass()
224 default = str(id(el_class))
225 el_class.VAL = None
226 def lookup(doc, el):
227 if el_class.VAL is None:
228 el_class.VAL = el[0].get('unknownattribute', default)
229 return el_class
230 self._setClassLookup(lookup)
231 root = self.XML(xml_str)
232 self.assertEquals(default, el_class.VAL)
233
235 el_class = self._buildElementClass()
236 el_class.CHILD_TAGS = None
237 def lookup(doc, el):
238 if el_class.CHILD_TAGS is None:
239 el_class.CHILD_TAGS = [ c.tag for c in el.getchildren() ]
240 return el_class
241 self._setClassLookup(lookup)
242 root = self.XML(xml_str)
243 child_tags = root.CHILD_TAGS
244 self.assertNotEquals(None, child_tags)
245 self.assertEquals([ c.tag for c in root.getchildren() ],
246 child_tags)
247
249 el_class = self._buildElementClass()
250 el_class.CHILD_TAGS = None
251 def lookup(doc, el):
252 if el_class.CHILD_TAGS is None:
253 el_class.CHILD_TAGS = [ c.tag for c in el ]
254 return el_class
255 self._setClassLookup(lookup)
256 root = self.XML(xml_str)
257 child_tags = root.CHILD_TAGS
258 self.assertNotEquals(None, child_tags)
259 self.assertEquals([ c.tag for c in root.getchildren() ],
260 child_tags)
261
263 el_class = self._buildElementClass()
264 el_class.CHILD_TAGS = None
265 def lookup(doc, el):
266 if el_class.CHILD_TAGS is None:
267 el_class.CHILD_TAGS = [ c.tag for c in el.iterchildren() ]
268 return el_class
269 self._setClassLookup(lookup)
270 root = self.XML(xml_str)
271 child_tags = root.CHILD_TAGS
272 self.assertNotEquals(None, child_tags)
273 self.assertEquals([ c.tag for c in root.getchildren() ],
274 child_tags)
275
277 el_class = self._buildElementClass()
278 el_class.CHILD_TAGS = None
279 def lookup(doc, el):
280 if not el_class.CHILD_TAGS:
281 el_class.CHILD_TAGS = [
282 c.tag for c in el.iterchildren(tag='{objectified}c2') ]
283 return el_class
284 self._setClassLookup(lookup)
285
286 root = self.XML(xml_str)
287 child_tags = root.CHILD_TAGS
288 self.assertNotEquals(None, child_tags)
289 self.assertEquals([], child_tags)
290
291 c1 = root[0]
292 child_tags = root.CHILD_TAGS
293 self.assertNotEquals(None, child_tags)
294 self.assertNotEquals([], child_tags)
295 self.assertEquals(
296 [ c.tag for c in root[0].iterchildren(tag='{objectified}c2') ],
297 child_tags)
298
300 el_class = self._buildElementClass()
301 el_class.PARENT = None
302 def lookup(doc, el):
303 if el_class.PARENT is None:
304 el_class.PARENT = el[0].getparent().tag
305 return el_class
306 self._setClassLookup(lookup)
307 root = self.XML(xml_str)
308 self.assertEquals(root.tag, root.PARENT)
309
311 el_class = self._buildElementClass()
312 el_class.NEXT = None
313 def lookup(doc, el):
314 if el_class.NEXT is None:
315 el_class.NEXT = el[0][1].getnext().tag
316 return el_class
317 self._setClassLookup(lookup)
318 root = self.XML(xml_str)
319 self.assertNotEquals(None, el_class.NEXT)
320 self.assertEquals(root[0][1].getnext().tag, el_class.NEXT)
321
323 el_class = self._buildElementClass()
324 el_class.PREV = None
325 def lookup(doc, el):
326 if el_class.PREV is None:
327 el_class.PREV = el[0][1].getprevious().tag
328 return el_class
329 self._setClassLookup(lookup)
330 root = self.XML(xml_str)
331 self.assertNotEquals(None, el_class.PREV)
332 self.assertEquals(root[0][1].getprevious().tag, el_class.PREV)
333
334
336 suite = unittest.TestSuite()
337 suite.addTests([unittest.makeSuite(PyClassLookupTestCase)])
338 return suite
339
340 if __name__ == '__main__':
341 print 'to test use test.py %s' % __file__
342