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, copy, sys, operator
12
13 from common_imports import etree, StringIO, HelperTestCase, fileInTestDir
14 from common_imports import SillyFileLike, canonicalize, doctest
15
16 print
17 print "TESTED VERSION:", etree.__version__
18 print " Python: ", sys.version_info
19 print " lxml.etree: ", etree.LXML_VERSION
20 print " libxml used: ", etree.LIBXML_VERSION
21 print " libxml compiled: ", etree.LIBXML_COMPILED_VERSION
22 print " libxslt used: ", etree.LIBXSLT_VERSION
23 print " libxslt compiled: ", etree.LIBXSLT_COMPILED_VERSION
24 print
25
26 try:
27 sorted
28 except NameError:
29
31 seq = list(seq)
32 seq.sort()
33 return seq
34
36 """Tests only for etree, not ElementTree"""
37 etree = etree
38
49
58
65
67 Element = self.etree.Element
68 el = Element('name')
69 self.assertRaises(ValueError, Element, '{}')
70 self.assertRaises(ValueError, setattr, el, 'tag', '{}')
71
72 self.assertRaises(ValueError, Element, '{test}')
73 self.assertRaises(ValueError, setattr, el, 'tag', '{test}')
74
82
84 Element = self.etree.Element
85 self.assertRaises(ValueError, Element, "p'name")
86 self.assertRaises(ValueError, Element, 'p"name')
87
88 self.assertRaises(ValueError, Element, "{test}p'name")
89 self.assertRaises(ValueError, Element, '{test}p"name')
90
91 el = Element('name')
92 self.assertRaises(ValueError, setattr, el, 'tag', "p'name")
93 self.assertRaises(ValueError, setattr, el, 'tag', 'p"name')
94
96 Element = self.etree.Element
97 self.assertRaises(ValueError, Element, ' name ')
98 self.assertRaises(ValueError, Element, 'na me')
99 self.assertRaises(ValueError, Element, '{test} name')
100
101 el = Element('name')
102 self.assertRaises(ValueError, setattr, el, 'tag', ' name ')
103
111
119
121 Element = self.etree.Element
122 SubElement = self.etree.SubElement
123
124 el = Element('name')
125 self.assertRaises(ValueError, SubElement, el, "p'name")
126 self.assertRaises(ValueError, SubElement, el, "{test}p'name")
127
128 self.assertRaises(ValueError, SubElement, el, 'p"name')
129 self.assertRaises(ValueError, SubElement, el, '{test}p"name')
130
139
141 QName = self.etree.QName
142 self.assertRaises(ValueError, QName, '')
143 self.assertRaises(ValueError, QName, 'test', '')
144
146 QName = self.etree.QName
147 self.assertRaises(ValueError, QName, 'p:name')
148 self.assertRaises(ValueError, QName, 'test', 'p:name')
149
151 QName = self.etree.QName
152 self.assertRaises(ValueError, QName, ' name ')
153 self.assertRaises(ValueError, QName, 'na me')
154 self.assertRaises(ValueError, QName, 'test', ' name')
155
157
158 etree = self.etree
159 qname = etree.QName('http://myns', 'a')
160 a = etree.Element(qname, nsmap={'p' : 'http://myns'})
161 a.text = qname
162
163 self.assertEquals("p:a", a.text)
164
170
178
192
194 root = self.etree.XML('<doc one="One" two="Two"/>')
195 self.assertRaises(KeyError, root.attrib.pop, 'NONE')
196
197 self.assertEquals('One', root.attrib['one'])
198 self.assertEquals('Two', root.attrib['two'])
199
201 root = self.etree.XML('<doc one="One" two="Two"/>')
202 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
203
207
209 root = self.etree.XML('<doc one="One" two="Two"/>')
210 self.assertRaises(TypeError, root.attrib.pop, 'One', None, None)
211
213
214 Element = self.etree.Element
215 SubElement = self.etree.SubElement
216 ProcessingInstruction = self.etree.ProcessingInstruction
217
218 a = Element('a')
219 a.append(ProcessingInstruction('foo', 'some more text'))
220 self.assertEquals(a[0].target, 'foo')
221 self.assertEquals(a[0].text, 'some more text')
222
224 XML = self.etree.XML
225 root = XML("<test><?mypi my test ?></test>")
226 self.assertEquals(root[0].target, "mypi")
227 self.assertEquals(root[0].text, "my test ")
228
230
231 ProcessingInstruction = self.etree.ProcessingInstruction
232
233 a = ProcessingInstruction("PI", "ONE")
234 b = copy.deepcopy(a)
235 b.text = "ANOTHER"
236
237 self.assertEquals('ONE', a.text)
238 self.assertEquals('ANOTHER', b.text)
239
251
261
263
264 parse = self.etree.parse
265
266 f = StringIO('<a><b></c></b></a>')
267 self.assertRaises(SyntaxError, parse, f)
268 f.close()
269
281
300
305
307 parse = self.etree.parse
308
309 f = StringIO('<a><b></c></b></a>')
310 self.etree.clearErrorLog()
311 try:
312 parse(f)
313 logs = None
314 except SyntaxError, e:
315 logs = e.error_log
316 f.close()
317 self.assert_([ log for log in logs
318 if 'mismatch' in log.message ])
319 self.assert_([ log for log in logs
320 if 'PARSER' in log.domain_name])
321 self.assert_([ log for log in logs
322 if 'TAG_NAME_MISMATCH' in log.type_name ])
323 self.assert_([ log for log in logs
324 if 1 == log.line ])
325 self.assert_([ log for log in logs
326 if 15 == log.column ])
327
334
347
358
364
366 iterparse = self.etree.iterparse
367 f = StringIO("""
368 <a> \n \n <b> b test </b> \n
369
370 \n\t <c> \n </c> </a> \n """)
371 iterator = iterparse(f, remove_blank_text=True)
372 text = [ (element.text, element.tail)
373 for event, element in iterator ]
374 self.assertEquals(
375 [(" b test ", None), (" \n ", None), (None, None)],
376 text)
377
379 iterparse = self.etree.iterparse
380 f = StringIO('<a><b><d/></b><c/></a>')
381
382 iterator = iterparse(f, tag="b", events=('start', 'end'))
383 events = list(iterator)
384 root = iterator.root
385 self.assertEquals(
386 [('start', root[0]), ('end', root[0])],
387 events)
388
390 iterparse = self.etree.iterparse
391 f = StringIO('<a><b><d/></b><c/></a>')
392
393 iterator = iterparse(f, tag="*", events=('start', 'end'))
394 events = list(iterator)
395 self.assertEquals(
396 8,
397 len(events))
398
400 text = u'Søk på nettet'
401 wrong_declaration = "<?xml version='1.0' encoding='UTF-8'?>"
402 xml_latin1 = (u'%s<a>%s</a>' % (wrong_declaration, text)
403 ).encode('iso-8859-1')
404
405 self.assertRaises(self.etree.ParseError,
406 list, self.etree.iterparse(StringIO(xml_latin1)))
407
408 iterator = self.etree.iterparse(StringIO(xml_latin1),
409 encoding="iso-8859-1")
410 self.assertEquals(1, len(list(iterator)))
411
412 a = iterator.root
413 self.assertEquals(a.text, text)
414
418
420 assertEquals = self.assertEquals
421 assertFalse = self.assertFalse
422
423 events = []
424 class Target(object):
425 def start(self, tag, attrib):
426 events.append("start")
427 assertFalse(attrib)
428 assertEquals("TAG", tag)
429 def end(self, tag):
430 events.append("end")
431 assertEquals("TAG", tag)
432 def close(self):
433 return "DONE"
434
435 parser = self.etree.XMLParser(target=Target())
436 tree = self.etree.ElementTree()
437
438 self.assertRaises(TypeError,
439 tree.parse, StringIO("<TAG/>"), parser=parser)
440 self.assertEquals(["start", "end"], events)
441
443 events = []
444 class Target(object):
445 def start(self, tag, attrib):
446 events.append("start-" + tag)
447 def end(self, tag):
448 events.append("end-" + tag)
449 def data(self, data):
450 events.append("data-" + data)
451 def close(self):
452 events.append("close")
453 return "DONE"
454
455 parser = self.etree.XMLParser(target=Target(),
456 recover=True)
457
458 parser.feed('<root>A<a>ca</a>B</not-root>')
459 done = parser.close()
460
461 self.assertEquals("DONE", done)
462 self.assertEquals(["start-root", "data-A", "start-a",
463 "data-ca", "end-a", "data-B",
464 "end-root", "close"],
465 events)
466
476
486
495
505
507 iterwalk = self.etree.iterwalk
508 root = self.etree.XML('<a><b></b><c/></a>')
509
510 iterator = iterwalk(root, events=('start','end'))
511 events = list(iterator)
512 self.assertEquals(
513 [('start', root), ('start', root[0]), ('end', root[0]),
514 ('start', root[1]), ('end', root[1]), ('end', root)],
515 events)
516
527
529 iterwalk = self.etree.iterwalk
530 root = self.etree.XML('<a xmlns="ns1"><b><c xmlns="ns2"/></b></a>')
531
532 attr_name = '{testns}bla'
533 events = []
534 iterator = iterwalk(root, events=('start','end','start-ns','end-ns'))
535 for event, elem in iterator:
536 events.append(event)
537 if event == 'start':
538 if elem.tag != '{ns1}a':
539 elem.set(attr_name, 'value')
540
541 self.assertEquals(
542 ['start-ns', 'start', 'start', 'start-ns', 'start',
543 'end', 'end-ns', 'end', 'end', 'end-ns'],
544 events)
545
546 self.assertEquals(
547 None,
548 root.get(attr_name))
549 self.assertEquals(
550 'value',
551 root[0].get(attr_name))
552
554 iterwalk = self.etree.iterwalk
555 root = self.etree.XML('<a><b><d/></b><c/></a>')
556
557 counts = []
558 for event, elem in iterwalk(root):
559 counts.append(len(list(elem.getiterator())))
560 self.assertEquals(
561 [1,2,1,4],
562 counts)
563
565 parse = self.etree.parse
566 parser = self.etree.XMLParser(dtd_validation=True)
567 assertEqual = self.assertEqual
568 test_url = u"__nosuch.dtd"
569
570 class MyResolver(self.etree.Resolver):
571 def resolve(self, url, id, context):
572 assertEqual(url, test_url)
573 return self.resolve_string(
574 u'''<!ENTITY myentity "%s">
575 <!ELEMENT doc ANY>''' % url, context)
576
577 parser.resolvers.add(MyResolver())
578
579 xml = u'<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>' % test_url
580 tree = parse(StringIO(xml), parser)
581 root = tree.getroot()
582 self.assertEquals(root.text, test_url)
583
585 parse = self.etree.parse
586 parser = self.etree.XMLParser(attribute_defaults=True)
587 assertEqual = self.assertEqual
588 test_url = u"__nosuch.dtd"
589
590 class MyResolver(self.etree.Resolver):
591 def resolve(self, url, id, context):
592 assertEqual(url, test_url)
593 return self.resolve_filename(
594 fileInTestDir('test.dtd'), context)
595
596 parser.resolvers.add(MyResolver())
597
598 xml = u'<!DOCTYPE a SYSTEM "%s"><a><b/></a>' % test_url
599 tree = parse(StringIO(xml), parser)
600 root = tree.getroot()
601 self.assertEquals(
602 root.attrib, {'default': 'valueA'})
603 self.assertEquals(
604 root[0].attrib, {'default': 'valueB'})
605
607 parse = self.etree.parse
608 parser = self.etree.XMLParser(load_dtd=True)
609 assertEqual = self.assertEqual
610 test_url = u"__nosuch.dtd"
611
612 class check(object):
613 resolved = False
614
615 class MyResolver(self.etree.Resolver):
616 def resolve(self, url, id, context):
617 assertEqual(url, test_url)
618 check.resolved = True
619 return self.resolve_empty(context)
620
621 parser.resolvers.add(MyResolver())
622
623 xml = u'<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>' % test_url
624 self.assertRaises(etree.XMLSyntaxError, parse, StringIO(xml), parser)
625 self.assert_(check.resolved)
626
634
635 class MyResolver(self.etree.Resolver):
636 def resolve(self, url, id, context):
637 raise _LocalException
638
639 parser.resolvers.add(MyResolver())
640
641 xml = u'<!DOCTYPE doc SYSTEM "test"><doc>&myentity;</doc>'
642 self.assertRaises(_LocalException, parse, StringIO(xml), parser)
643
644 if etree.LIBXML_VERSION > (2,6,20):
661
677
684
686 Entity = self.etree.Entity
687 self.assertRaises(ValueError, Entity, 'a b c')
688 self.assertRaises(ValueError, Entity, 'a,b')
689 self.assertRaises(AssertionError, Entity, 'a\0b')
690 self.assertRaises(ValueError, Entity, '#abc')
691 self.assertRaises(ValueError, Entity, '#xxyz')
692
693
703
708
721
734
740
746
761
763 Element = self.etree.Element
764 PI = self.etree.PI
765 root = Element('root')
766 pi = PI('TARGET', 'TEXT')
767 pi.tail = "TAIL"
768
769 self.assertEquals('<root></root>',
770 self._writeElement(root))
771 root.addprevious(pi)
772 self.assertEquals('<?TARGET TEXT?>\n<root></root>',
773 self._writeElement(root))
774
789
802
817
830
845
858
859
861 XML = self.etree.XML
862
863 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')
864 values = root.values()
865 values.sort()
866 self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)
867
868
878
879
894
895
905
906
922
923
934
935
946
947
952
953
955 self.assertRaises(TypeError, etree.dump, None)
956
958 ElementTree = self.etree.ElementTree
959
960 f = StringIO('<a xmlns:foo="http://www.infrae.com/ns/1"><foo:b/></a>')
961 doc = ElementTree(file=f)
962 a = doc.getroot()
963 self.assertEquals(
964 None,
965 a.prefix)
966 self.assertEquals(
967 'foo',
968 a[0].prefix)
969
971 ElementTree = self.etree.ElementTree
972
973 f = StringIO('<a xmlns="http://www.infrae.com/ns/1"><b/></a>')
974 doc = ElementTree(file=f)
975 a = doc.getroot()
976 self.assertEquals(
977 None,
978 a.prefix)
979 self.assertEquals(
980 None,
981 a[0].prefix)
982
1003
1005 XML = self.etree.XML
1006
1007 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>')
1008 result = []
1009 for el in root.iterchildren():
1010 result.append(el.tag)
1011 self.assertEquals(['one', 'two', 'three'], result)
1012
1014 XML = self.etree.XML
1015
1016 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>')
1017 result = []
1018 for el in root.iterchildren(reversed=True):
1019 result.append(el.tag)
1020 self.assertEquals(['three', 'two', 'one'], result)
1021
1023 XML = self.etree.XML
1024
1025 root = XML('<doc><one/><two>Two</two>Hm<two>Bla</two></doc>')
1026 result = []
1027 for el in root.iterchildren(tag='two'):
1028 result.append(el.text)
1029 self.assertEquals(['Two', 'Bla'], result)
1030
1032 XML = self.etree.XML
1033
1034 root = XML('<doc><one/><two>Two</two>Hm<two>Bla</two></doc>')
1035 result = []
1036 for el in root.iterchildren(reversed=True, tag='two'):
1037 result.append(el.text)
1038 self.assertEquals(['Bla', 'Two'], result)
1039
1041 Element = self.etree.Element
1042 SubElement = self.etree.SubElement
1043
1044 a = Element('a')
1045 b = SubElement(a, 'b')
1046 c = SubElement(a, 'c')
1047 d = SubElement(b, 'd')
1048 self.assertEquals(
1049 [],
1050 list(a.iterancestors()))
1051 self.assertEquals(
1052 [a],
1053 list(b.iterancestors()))
1054 self.assertEquals(
1055 a,
1056 c.iterancestors().next())
1057 self.assertEquals(
1058 [b, a],
1059 list(d.iterancestors()))
1060
1072
1089
1110
1128
1145
1163
1165 Element = self.etree.Element
1166 SubElement = self.etree.SubElement
1167
1168 a = Element('a')
1169 b = SubElement(a, 'b')
1170 c = SubElement(a, 'c')
1171 d = SubElement(b, 'd')
1172 self.assertEquals(
1173 [],
1174 list(a.itersiblings()))
1175 self.assertEquals(
1176 [c],
1177 list(b.itersiblings()))
1178 self.assertEquals(
1179 c,
1180 b.itersiblings().next())
1181 self.assertEquals(
1182 [],
1183 list(c.itersiblings()))
1184 self.assertEquals(
1185 [b],
1186 list(c.itersiblings(preceding=True)))
1187 self.assertEquals(
1188 [],
1189 list(b.itersiblings(preceding=True)))
1190
1192 Element = self.etree.Element
1193 SubElement = self.etree.SubElement
1194
1195 a = Element('a')
1196 b = SubElement(a, 'b')
1197 c = SubElement(a, 'c')
1198 d = SubElement(b, 'd')
1199 self.assertEquals(
1200 [],
1201 list(a.itersiblings(tag='XXX')))
1202 self.assertEquals(
1203 [c],
1204 list(b.itersiblings(tag='c')))
1205 self.assertEquals(
1206 [b],
1207 list(c.itersiblings(preceding=True, tag='b')))
1208 self.assertEquals(
1209 [],
1210 list(c.itersiblings(preceding=True, tag='c')))
1211
1213 parseid = self.etree.parseid
1214 XML = self.etree.XML
1215 xml_text = '''
1216 <!DOCTYPE document [
1217 <!ELEMENT document (h1,p)*>
1218 <!ELEMENT h1 (#PCDATA)>
1219 <!ATTLIST h1 myid ID #REQUIRED>
1220 <!ELEMENT p (#PCDATA)>
1221 <!ATTLIST p someid ID #REQUIRED>
1222 ]>
1223 <document>
1224 <h1 myid="chapter1">...</h1>
1225 <p id="note1" class="note">...</p>
1226 <p>Regular paragraph.</p>
1227 <p xml:id="xmlid">XML:ID paragraph.</p>
1228 <p someid="warn1" class="warning">...</p>
1229 </document>
1230 '''
1231
1232 tree, dic = parseid(StringIO(xml_text))
1233 root = tree.getroot()
1234 root2 = XML(xml_text)
1235 self.assertEquals(self._writeElement(root),
1236 self._writeElement(root2))
1237 expected = {
1238 "chapter1" : root[0],
1239 "xmlid" : root[3],
1240 "warn1" : root[4]
1241 }
1242 self.assert_("chapter1" in dic)
1243 self.assert_("warn1" in dic)
1244 self.assert_("xmlid" in dic)
1245 self._checkIDDict(dic, expected)
1246
1248 XMLDTDID = self.etree.XMLDTDID
1249 XML = self.etree.XML
1250 xml_text = '''
1251 <!DOCTYPE document [
1252 <!ELEMENT document (h1,p)*>
1253 <!ELEMENT h1 (#PCDATA)>
1254 <!ATTLIST h1 myid ID #REQUIRED>
1255 <!ELEMENT p (#PCDATA)>
1256 <!ATTLIST p someid ID #REQUIRED>
1257 ]>
1258 <document>
1259 <h1 myid="chapter1">...</h1>
1260 <p id="note1" class="note">...</p>
1261 <p>Regular paragraph.</p>
1262 <p xml:id="xmlid">XML:ID paragraph.</p>
1263 <p someid="warn1" class="warning">...</p>
1264 </document>
1265 '''
1266
1267 root, dic = XMLDTDID(xml_text)
1268 root2 = XML(xml_text)
1269 self.assertEquals(self._writeElement(root),
1270 self._writeElement(root2))
1271 expected = {
1272 "chapter1" : root[0],
1273 "xmlid" : root[3],
1274 "warn1" : root[4]
1275 }
1276 self.assert_("chapter1" in dic)
1277 self.assert_("warn1" in dic)
1278 self.assert_("xmlid" in dic)
1279 self._checkIDDict(dic, expected)
1280
1282 XMLDTDID = self.etree.XMLDTDID
1283 XML = self.etree.XML
1284 xml_text = '''
1285 <document>
1286 <h1 myid="chapter1">...</h1>
1287 <p id="note1" class="note">...</p>
1288 <p>Regular paragraph.</p>
1289 <p someid="warn1" class="warning">...</p>
1290 </document>
1291 '''
1292
1293 root, dic = XMLDTDID(xml_text)
1294 root2 = XML(xml_text)
1295 self.assertEquals(self._writeElement(root),
1296 self._writeElement(root2))
1297 expected = {}
1298 self._checkIDDict(dic, expected)
1299
1301 self.assertEquals(dic, expected)
1302 self.assertEquals(len(dic),
1303 len(expected))
1304 self.assertEquals(sorted(dic.items()),
1305 sorted(expected.items()))
1306 self.assertEquals(sorted(dic.iteritems()),
1307 sorted(expected.iteritems()))
1308 self.assertEquals(sorted(dic.keys()),
1309 sorted(expected.keys()))
1310 self.assertEquals(sorted(dic.iterkeys()),
1311 sorted(expected.iterkeys()))
1312 self.assertEquals(sorted(dic.values()),
1313 sorted(expected.values()))
1314 self.assertEquals(sorted(dic.itervalues()),
1315 sorted(expected.itervalues()))
1316
1318 etree = self.etree
1319
1320 r = {'foo': 'http://ns.infrae.com/foo'}
1321 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1322 self.assertEquals(
1323 'foo',
1324 e.prefix)
1325 self.assertEquals(
1326 '<foo:bar xmlns:foo="http://ns.infrae.com/foo"></foo:bar>',
1327 self._writeElement(e))
1328
1330 etree = self.etree
1331
1332 r = {None: 'http://ns.infrae.com/foo'}
1333 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1334 self.assertEquals(
1335 None,
1336 e.prefix)
1337 self.assertEquals(
1338 '{http://ns.infrae.com/foo}bar',
1339 e.tag)
1340 self.assertEquals(
1341 '<bar xmlns="http://ns.infrae.com/foo"></bar>',
1342 self._writeElement(e))
1343
1345 etree = self.etree
1346
1347 r = {None: 'http://ns.infrae.com/foo',
1348 'hoi': 'http://ns.infrae.com/hoi'}
1349 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1350 e.set('{http://ns.infrae.com/hoi}test', 'value')
1351 self.assertEquals(
1352 '<bar xmlns="http://ns.infrae.com/foo" xmlns:hoi="http://ns.infrae.com/hoi" hoi:test="value"></bar>',
1353 self._writeElement(e))
1354
1356 etree = self.etree
1357 r = {None: 'http://ns.infrae.com/foo',
1358 'hoi': 'http://ns.infrae.com/hoi'}
1359 e = etree.Element('{http://ns.infrae.com/foo}z', nsmap=r)
1360 tree = etree.ElementTree(element=e)
1361 etree.SubElement(e, '{http://ns.infrae.com/hoi}x')
1362 self.assertEquals(
1363 '<z xmlns="http://ns.infrae.com/foo" xmlns:hoi="http://ns.infrae.com/hoi"><hoi:x></hoi:x></z>',
1364 self._writeElement(e))
1365
1367 etree = self.etree
1368
1369 r = {None: 'http://ns.infrae.com/foo'}
1370 e1 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1371 e2 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1372
1373 e1.append(e2)
1374
1375 self.assertEquals(
1376 None,
1377 e1.prefix)
1378 self.assertEquals(
1379 None,
1380 e1[0].prefix)
1381 self.assertEquals(
1382 '{http://ns.infrae.com/foo}bar',
1383 e1.tag)
1384 self.assertEquals(
1385 '{http://ns.infrae.com/foo}bar',
1386 e1[0].tag)
1387
1389 etree = self.etree
1390
1391 r = {None: 'http://ns.infrae.com/BAR'}
1392 e1 = etree.Element('{http://ns.infrae.com/BAR}bar', nsmap=r)
1393 e2 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1394
1395 e1.append(e2)
1396
1397 self.assertEquals(
1398 None,
1399 e1.prefix)
1400 self.assertNotEquals(
1401 None,
1402 e2.prefix)
1403 self.assertEquals(
1404 '{http://ns.infrae.com/BAR}bar',
1405 e1.tag)
1406 self.assertEquals(
1407 '{http://ns.infrae.com/foo}bar',
1408 e2.tag)
1409
1411 ns_href = "http://a.b.c"
1412 one = self.etree.fromstring(
1413 '<foo><bar xmlns:ns="%s"><ns:baz/></bar></foo>' % ns_href)
1414 baz = one[0][0]
1415
1416 two = self.etree.fromstring(
1417 '<root xmlns:ns="%s"/>' % ns_href)
1418 two.append(baz)
1419 del one
1420
1421 self.assertEquals('{%s}baz' % ns_href, baz.tag)
1422 self.assertEquals(
1423 '<root xmlns:ns="%s"><ns:baz/></root>' % ns_href,
1424 self.etree.tostring(two))
1425
1427 parse = self.etree.parse
1428 tostring = self.etree.tostring
1429
1430 ns_href = "http://a.b.c"
1431 one = parse(
1432 StringIO('<foo><bar xmlns:ns="%s"><ns:baz/></bar></foo>' % ns_href))
1433 baz = one.getroot()[0][0]
1434
1435 parsed = parse(StringIO( tostring(baz) )).getroot()
1436 self.assertEquals('{%s}baz' % ns_href, parsed.tag)
1437
1439 etree = self.etree
1440
1441 r = {None: 'http://ns.infrae.com/foo',
1442 'hoi': 'http://ns.infrae.com/hoi'}
1443 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r)
1444 self.assertEquals(
1445 r,
1446 e.nsmap)
1447
1449 etree = self.etree
1450
1451 re = {None: 'http://ns.infrae.com/foo',
1452 'hoi': 'http://ns.infrae.com/hoi'}
1453 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=re)
1454
1455 rs = {None: 'http://ns.infrae.com/honk',
1456 'top': 'http://ns.infrae.com/top'}
1457 s = etree.SubElement(e, '{http://ns.infrae.com/honk}bar', nsmap=rs)
1458
1459 r = re.copy()
1460 r.update(rs)
1461 self.assertEquals(
1462 re,
1463 e.nsmap)
1464 self.assertEquals(
1465 r,
1466 s.nsmap)
1467
1469 Element = self.etree.Element
1470 SubElement = self.etree.SubElement
1471
1472 a = Element('{a}a')
1473 b = SubElement(a, '{a}b')
1474 c = SubElement(a, '{a}c')
1475 d = SubElement(b, '{b}d')
1476 e = SubElement(c, '{a}e')
1477 f = SubElement(c, '{b}f')
1478
1479 self.assertEquals(
1480 [a],
1481 list(a.getiterator('{a}a')))
1482 self.assertEquals(
1483 [],
1484 list(a.getiterator('{b}a')))
1485 self.assertEquals(
1486 [],
1487 list(a.getiterator('a')))
1488 self.assertEquals(
1489 [f],
1490 list(c.getiterator('{b}*')))
1491 self.assertEquals(
1492 [d, f],
1493 list(a.getiterator('{b}*')))
1494
1519
1535
1552
1553 - def test_itertext(self):
1554
1555 XML = self.etree.XML
1556 root = XML("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")
1557
1558 text = list(root.itertext())
1559 self.assertEquals(["RTEXT", "ATAIL", "CTEXT", "CTAIL"],
1560 text)
1561
1563
1564 XML = self.etree.XML
1565 root = XML("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")
1566
1567 text = list(root[2].itertext())
1568 self.assertEquals(["CTEXT"],
1569 text)
1570
1577
1584
1593
1595 XML = self.etree.XML
1596 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')
1597 self.assertEquals(len(root.findall(".//{X}b")), 2)
1598 self.assertEquals(len(root.findall(".//{X}*")), 2)
1599 self.assertEquals(len(root.findall(".//b")), 3)
1600
1602 etree = self.etree
1603 e = etree.Element('foo')
1604 for i in range(10):
1605 etree.SubElement(e, 'a%s' % i)
1606 for i in range(10):
1607 self.assertEquals(
1608 i,
1609 e.index(e[i]))
1610 self.assertEquals(
1611 3, e.index(e[3], 3))
1612 self.assertRaises(
1613 ValueError, e.index, e[3], 4)
1614 self.assertRaises(
1615 ValueError, e.index, e[3], 0, 2)
1616 self.assertRaises(
1617 ValueError, e.index, e[8], 0, -3)
1618 self.assertRaises(
1619 ValueError, e.index, e[8], -5, -3)
1620 self.assertEquals(
1621 8, e.index(e[8], 0, -1))
1622 self.assertEquals(
1623 8, e.index(e[8], -12, -1))
1624 self.assertEquals(
1625 0, e.index(e[0], -12, -1))
1626
1628 etree = self.etree
1629 e = etree.Element('foo')
1630 for i in range(10):
1631 el = etree.SubElement(e, 'a%s' % i)
1632 el.text = "text%d" % i
1633 el.tail = "tail%d" % i
1634
1635 child0 = e[0]
1636 child1 = e[1]
1637 child2 = e[2]
1638
1639 e.replace(e[0], e[1])
1640 self.assertEquals(
1641 9, len(e))
1642 self.assertEquals(
1643 child1, e[0])
1644 self.assertEquals(
1645 child1.text, "text1")
1646 self.assertEquals(
1647 child1.tail, "tail1")
1648 self.assertEquals(
1649 child0.tail, "tail0")
1650 self.assertEquals(
1651 child2, e[1])
1652
1653 e.replace(e[-1], e[0])
1654 self.assertEquals(
1655 child1, e[-1])
1656 self.assertEquals(
1657 child1.text, "text1")
1658 self.assertEquals(
1659 child1.tail, "tail1")
1660 self.assertEquals(
1661 child2, e[0])
1662
1664 etree = self.etree
1665 e = etree.Element('foo')
1666 for i in range(10):
1667 etree.SubElement(e, 'a%s' % i)
1668
1669 new_element = etree.Element("test")
1670 new_element.text = "TESTTEXT"
1671 new_element.tail = "TESTTAIL"
1672 child1 = e[1]
1673 e.replace(e[0], new_element)
1674 self.assertEquals(
1675 new_element, e[0])
1676 self.assertEquals(
1677 "TESTTEXT",
1678 e[0].text)
1679 self.assertEquals(
1680 "TESTTAIL",
1681 e[0].tail)
1682 self.assertEquals(
1683 child1, e[1])
1684
1700
1718
1736
1754
1756 Element = self.etree.Element
1757 SubElement = self.etree.SubElement
1758 try:
1759 slice
1760 except NameError:
1761 print "slice() not found"
1762 return
1763
1764 a = Element('a')
1765 b = SubElement(a, 'b')
1766 c = SubElement(a, 'c')
1767 d = SubElement(a, 'd')
1768 e = SubElement(a, 'e')
1769
1770 x = Element('x')
1771 y = Element('y')
1772 z = Element('z')
1773
1774 self.assertRaises(
1775 ValueError,
1776 operator.setitem, a, slice(1,None,2), [x, y, z])
1777
1778 self.assertEquals(
1779 [b, c, d, e],
1780 list(a))
1781
1783 etree = self.etree
1784 root = etree.Element('foo')
1785 for i in range(3):
1786 element = etree.SubElement(root, 'a%s' % i)
1787 element.text = "text%d" % i
1788 element.tail = "tail%d" % i
1789
1790 elements = []
1791 for i in range(3):
1792 new_element = etree.Element("test%s" % i)
1793 new_element.text = "TEXT%s" % i
1794 new_element.tail = "TAIL%s" % i
1795 elements.append(new_element)
1796
1797 root.extend(elements)
1798
1799 self.assertEquals(
1800 ["a0", "a1", "a2", "test0", "test1", "test2"],
1801 [ el.tag for el in root ])
1802 self.assertEquals(
1803 ["text0", "text1", "text2", "TEXT0", "TEXT1", "TEXT2"],
1804 [ el.text for el in root ])
1805 self.assertEquals(
1806 ["tail0", "tail1", "tail2", "TAIL0", "TAIL1", "TAIL2"],
1807 [ el.tail for el in root ])
1808
1810 XML = self.etree.XML
1811 root = XML('''<?xml version="1.0"?>
1812 <root><test>
1813
1814 <bla/></test>
1815 </root>
1816 ''')
1817
1818 self.assertEquals(
1819 [2, 2, 4],
1820 [ el.sourceline for el in root.getiterator() ])
1821
1823 parse = self.etree.parse
1824 tree = parse(fileInTestDir('include/test_xinclude.xml'))
1825
1826 self.assertEquals(
1827 [1, 2, 3],
1828 [ el.sourceline for el in tree.getiterator() ])
1829
1838
1840 iterparse = self.etree.iterparse
1841 lines = [ el.sourceline for (event, el) in
1842 iterparse(fileInTestDir('include/test_xinclude.xml'),
1843 events=("start",)) ]
1844
1845 self.assertEquals(
1846 [1, 2, 3],
1847 lines)
1848
1858
1860 etree = self.etree
1861 root = etree.XML("<root/>", base_url="http://no/such/url")
1862 docinfo = root.getroottree().docinfo
1863 self.assertEquals(docinfo.URL, "http://no/such/url")
1864
1866 etree = self.etree
1867 tree = etree.parse(StringIO("<root/>"), base_url="http://no/such/url")
1868 docinfo = tree.docinfo
1869 self.assertEquals(docinfo.URL, "http://no/such/url")
1870
1872 etree = self.etree
1873 tree = etree.parse(fileInTestDir('include/test_xinclude.xml'),
1874 base_url="http://no/such/url")
1875 docinfo = tree.docinfo
1876 self.assertEquals(docinfo.URL, "http://no/such/url")
1877
1879 etree = self.etree
1880 root = etree.HTML("<html/>", base_url="http://no/such/url")
1881 docinfo = root.getroottree().docinfo
1882 self.assertEquals(docinfo.URL, "http://no/such/url")
1883
1885 etree = self.etree
1886 xml_header = '<?xml version="1.0" encoding="ascii"?>'
1887 pub_id = "-//W3C//DTD XHTML 1.0 Transitional//EN"
1888 sys_id = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
1889 doctype_string = '<!DOCTYPE html PUBLIC "%s" "%s">' % (pub_id, sys_id)
1890
1891 xml = xml_header + doctype_string + '<html><body></body></html>'
1892
1893 tree = etree.parse(StringIO(xml))
1894 docinfo = tree.docinfo
1895 self.assertEquals(docinfo.encoding, "ascii")
1896 self.assertEquals(docinfo.xml_version, "1.0")
1897 self.assertEquals(docinfo.public_id, pub_id)
1898 self.assertEquals(docinfo.system_url, sys_id)
1899 self.assertEquals(docinfo.root_name, 'html')
1900 self.assertEquals(docinfo.doctype, doctype_string)
1901
1903 etree = self.etree
1904 xml_header = '<?xml version="1.0" encoding="UTF-8"?>'
1905 sys_id = "some.dtd"
1906 doctype_string = '<!DOCTYPE html SYSTEM "%s">' % sys_id
1907 xml = xml_header + doctype_string + '<html><body></body></html>'
1908
1909 tree = etree.parse(StringIO(xml))
1910 docinfo = tree.docinfo
1911 self.assertEquals(docinfo.encoding, "UTF-8")
1912 self.assertEquals(docinfo.xml_version, "1.0")
1913 self.assertEquals(docinfo.public_id, None)
1914 self.assertEquals(docinfo.system_url, sys_id)
1915 self.assertEquals(docinfo.root_name, 'html')
1916 self.assertEquals(docinfo.doctype, doctype_string)
1917
1919 etree = self.etree
1920 xml = '<html><body></body></html>'
1921 tree = etree.parse(StringIO(xml))
1922 docinfo = tree.docinfo
1923 self.assertEquals(docinfo.encoding, "UTF-8")
1924 self.assertEquals(docinfo.xml_version, "1.0")
1925 self.assertEquals(docinfo.public_id, None)
1926 self.assertEquals(docinfo.system_url, None)
1927 self.assertEquals(docinfo.root_name, 'html')
1928 self.assertEquals(docinfo.doctype, '')
1929
1931
1932 xml = '''\
1933 <!DOCTYPE test SYSTEM "test.dtd" [
1934 <!ENTITY entity "tasty">
1935 <!ELEMENT test (a)>
1936 <!ELEMENT a (#PCDATA)>
1937 ]>
1938 <test><a>test-test</a></test>\
1939 '''
1940 tree = self.etree.parse(StringIO(xml))
1941 self.assertEqual(self.etree.tostring(tree).replace(" ", ""),
1942 xml.replace(" ", ""))
1943
1945 Element = self.etree.Element
1946
1947 a = Element('a')
1948 self.assertRaises(AssertionError, setattr, a, "text", 'ha\0ho')
1949 self.assertRaises(AssertionError, setattr, a, "tail", 'ha\0ho')
1950
1951 self.assertRaises(AssertionError, Element, 'ha\0ho')
1952
1954 Element = self.etree.Element
1955
1956 a = Element('a')
1957 self.assertRaises(AssertionError, setattr, a, "text", u'ha\0ho')
1958 self.assertRaises(AssertionError, setattr, a, "tail", u'ha\0ho')
1959
1960 self.assertRaises(AssertionError, Element, u'ha\0ho')
1961
1963 Element = self.etree.Element
1964
1965 a = Element('a')
1966 self.assertRaises(AssertionError, setattr, a, "text", 'ha\x07ho')
1967 self.assertRaises(AssertionError, setattr, a, "text", 'ha\x02ho')
1968
1969 self.assertRaises(AssertionError, setattr, a, "tail", 'ha\x07ho')
1970 self.assertRaises(AssertionError, setattr, a, "tail", 'ha\x02ho')
1971
1972 self.assertRaises(AssertionError, Element, 'ha\x07ho')
1973 self.assertRaises(AssertionError, Element, 'ha\x02ho')
1974
1976 Element = self.etree.Element
1977
1978 a = Element('a')
1979 self.assertRaises(AssertionError, setattr, a, "text", u'ha\x07ho')
1980 self.assertRaises(AssertionError, setattr, a, "text", u'ha\x02ho')
1981
1982 self.assertRaises(AssertionError, setattr, a, "tail", u'ha\x07ho')
1983 self.assertRaises(AssertionError, setattr, a, "tail", u'ha\x02ho')
1984
1985 self.assertRaises(AssertionError, Element, u'ha\x07ho')
1986 self.assertRaises(AssertionError, Element, u'ha\x02ho')
1987
2001
2006
2024
2044
2046 tostring = self.etree.tostring
2047 Element = self.etree.Element
2048 SubElement = self.etree.SubElement
2049
2050 a = Element('a')
2051 a.text = "A"
2052 a.tail = "tail"
2053 b = SubElement(a, 'b')
2054 b.text = "B"
2055 b.tail = u"Søk på nettet"
2056 c = SubElement(a, 'c')
2057 c.text = "C"
2058
2059 result = tostring(a, method="text", encoding="UTF-16")
2060
2061 self.assertEquals(u'ABSøk på nettetCtail'.encode("UTF-16"),
2062 result)
2063
2076
2092
2096
2111
2129
2142
2144 tostring = self.etree.tostring
2145 Element = self.etree.Element
2146 SubElement = self.etree.SubElement
2147
2148 a = Element('a')
2149 b = SubElement(a, 'b')
2150 c = SubElement(a, 'c')
2151 d = SubElement(c, 'd')
2152 self.assert_(isinstance(tostring(b, encoding=unicode), unicode))
2153 self.assert_(isinstance(tostring(c, encoding=unicode), unicode))
2154 self.assertEquals('<b></b>',
2155 canonicalize(tostring(b, encoding=unicode)))
2156 self.assertEquals('<c><d></d></c>',
2157 canonicalize(tostring(c, encoding=unicode)))
2158
2163
2178
2180 tostring = self.etree.tostring
2181 Element = self.etree.Element
2182 SubElement = self.etree.SubElement
2183
2184 a = Element('a')
2185 b = SubElement(a, 'b')
2186 c = SubElement(a, 'c')
2187
2188 result = tostring(a, encoding=unicode)
2189 self.assertEquals(result, "<a><b/><c/></a>")
2190
2191 result = tostring(a, encoding=unicode, pretty_print=False)
2192 self.assertEquals(result, "<a><b/><c/></a>")
2193
2194 result = tostring(a, encoding=unicode, pretty_print=True)
2195 self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>\n")
2196
2197
2198
2208
2209
2212 filename = fileInTestDir('test_broken.xml')
2213 root = etree.XML('''\
2214 <doc xmlns:xi="http://www.w3.org/2001/XInclude">
2215 <xi:include href="%s" parse="text"/>
2216 </doc>
2217 ''' % filename)
2218 old_text = root.text
2219 content = open(filename).read()
2220 old_tail = root[0].tail
2221
2222 self.include( etree.ElementTree(root) )
2223 self.assertEquals(old_text + content + old_tail,
2224 root.text)
2225
2227 tree = etree.parse(fileInTestDir('include/test_xinclude.xml'))
2228 self.assertNotEquals(
2229 'a',
2230 tree.getroot()[1].tag)
2231
2232 self.include( tree )
2233
2234 self.assertEquals(
2235 'a',
2236 tree.getroot()[1].tag)
2237
2241
2242
2247
2248
2251 tree = self.parse('<a><b/></a>')
2252 f = StringIO()
2253 tree.write_c14n(f)
2254 s = f.getvalue()
2255 self.assertEquals('<a><b></b></a>',
2256 s)
2257
2259 suite = unittest.TestSuite()
2260 suite.addTests([unittest.makeSuite(ETreeOnlyTestCase)])
2261 suite.addTests([unittest.makeSuite(ETreeXIncludeTestCase)])
2262 suite.addTests([unittest.makeSuite(ElementIncludeTestCase)])
2263 suite.addTests([unittest.makeSuite(ETreeC14NTestCase)])
2264 suite.addTests(
2265 [doctest.DocFileSuite('../../../doc/tutorial.txt')])
2266 suite.addTests(
2267 [doctest.DocFileSuite('../../../doc/api.txt')])
2268 suite.addTests(
2269 [doctest.DocFileSuite('../../../doc/parsing.txt')])
2270 suite.addTests(
2271 [doctest.DocFileSuite('../../../doc/resolvers.txt')])
2272 return suite
2273
2274 if __name__ == '__main__':
2275 print 'to test use test.py %s' % __file__
2276