Package lxml :: Package tests :: Module test_etree
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_etree

   1  # -*- coding: utf-8 -*- 
   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, warnings 
  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:" 
  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      # Python 2.3 
30 - def sorted(seq):
31 seq = list(seq) 32 seq.sort() 33 return seq
34 35 warnings.simplefilter("error", etree.TagNameWarning) 36
37 -class ETreeOnlyTestCase(HelperTestCase):
38 """Tests only for etree, not ElementTree""" 39 etree = etree 40
41 - def test_version(self):
42 self.assert_(isinstance(etree.__version__, str)) 43 self.assert_(isinstance(etree.LXML_VERSION, tuple)) 44 self.assertEqual(len(etree.LXML_VERSION), 4) 45 self.assert_(isinstance(etree.LXML_VERSION[0], int)) 46 self.assert_(isinstance(etree.LXML_VERSION[1], int)) 47 self.assert_(isinstance(etree.LXML_VERSION[2], int)) 48 self.assert_(isinstance(etree.LXML_VERSION[3], int)) 49 self.assert_(etree.__version__.startswith( 50 str(etree.LXML_VERSION[0])))
51
52 - def test_c_api(self):
53 self.assert_(hasattr(self.etree, '_import_c_api'))
54
55 - def test_element_names(self):
56 Element = self.etree.Element 57 el = Element('name') 58 self.assertEquals(el.tag, 'name') 59 el = Element('{}name') 60 self.assertEquals(el.tag, 'name')
61
62 - def test_element_name_empty(self):
63 Element = self.etree.Element 64 el = Element('name') 65 self.assertRaises(ValueError, Element, '{}') 66 self.assertRaises(ValueError, setattr, el, 'tag', '{}') 67 68 self.assertRaises(ValueError, Element, '{test}') 69 self.assertRaises(ValueError, setattr, el, 'tag', '{test}')
70
71 - def test_element_name_colon(self):
72 Element = self.etree.Element 73 self.assertRaises(self.etree.TagNameWarning, 74 Element, 'p:name') 75 self.assertRaises(self.etree.TagNameWarning, 76 Element, '{test}p:name') 77 78 el = Element('name') 79 self.assertRaises(self.etree.TagNameWarning, 80 setattr, el, 'tag', 'p:name')
81
82 - def test_attribute_set(self):
83 # ElementTree accepts arbitrary attribute values 84 # lxml.etree allows only strings 85 Element = self.etree.Element 86 87 root = Element("root") 88 root.set("attr", "TEST") 89 self.assertEquals("TEST", root.get("attr")) 90 self.assertRaises(TypeError, root.set, "newattr", 5)
91
92 - def test_attrib_pop(self):
93 ElementTree = self.etree.ElementTree 94 95 f = StringIO('<doc one="One" two="Two"/>') 96 doc = ElementTree(file=f) 97 root = doc.getroot() 98 self.assertEquals('One', root.attrib['one']) 99 self.assertEquals('Two', root.attrib['two']) 100 101 self.assertEquals('One', root.attrib.pop('one')) 102 103 self.assertEquals(None, root.attrib.get('one')) 104 self.assertEquals('Two', root.attrib['two'])
105
106 - def test_attrib_pop_unknown(self):
107 root = self.etree.XML('<doc one="One" two="Two"/>') 108 self.assertRaises(KeyError, root.attrib.pop, 'NONE') 109 110 self.assertEquals('One', root.attrib['one']) 111 self.assertEquals('Two', root.attrib['two'])
112
113 - def test_attrib_pop_default(self):
114 root = self.etree.XML('<doc one="One" two="Two"/>') 115 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
116
118 root = self.etree.XML('<doc/>') 119 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
120
122 root = self.etree.XML('<doc one="One" two="Two"/>') 123 self.assertRaises(TypeError, root.attrib.pop, 'One', None, None)
124
125 - def test_pi(self):
126 # lxml.etree separates target and text 127 Element = self.etree.Element 128 SubElement = self.etree.SubElement 129 ProcessingInstruction = self.etree.ProcessingInstruction 130 131 a = Element('a') 132 a.append(ProcessingInstruction('foo', 'some more text')) 133 self.assertEquals(a[0].target, 'foo') 134 self.assertEquals(a[0].text, 'some more text')
135
136 - def test_pi_parse(self):
137 XML = self.etree.XML 138 root = XML("<test><?mypi my test ?></test>") 139 self.assertEquals(root[0].target, "mypi") 140 self.assertEquals(root[0].text, "my test ")
141
142 - def test_deepcopy_pi(self):
143 # previously caused a crash 144 ProcessingInstruction = self.etree.ProcessingInstruction 145 146 a = ProcessingInstruction("PI", "ONE") 147 b = copy.deepcopy(a) 148 b.text = "ANOTHER" 149 150 self.assertEquals('ONE', a.text) 151 self.assertEquals('ANOTHER', b.text)
152
153 - def test_deepcopy_comment(self):
154 # previously caused a crash 155 # not supported by ET! 156 Comment = self.etree.Comment 157 158 a = Comment("ONE") 159 b = copy.deepcopy(a) 160 b.text = "ANOTHER" 161 162 self.assertEquals('ONE', a.text) 163 self.assertEquals('ANOTHER', b.text)
164
165 - def test_attribute_set(self):
166 # ElementTree accepts arbitrary attribute values 167 # lxml.etree allows only strings 168 Element = self.etree.Element 169 170 root = Element("root") 171 root.set("attr", "TEST") 172 self.assertEquals("TEST", root.get("attr")) 173 self.assertRaises(TypeError, root.set, "newattr", 5)
174
175 - def test_parse_error(self):
176 # ET raises ExpatError 177 parse = self.etree.parse 178 # from StringIO 179 f = StringIO('<a><b></c></b></a>') 180 self.assertRaises(SyntaxError, parse, f) 181 f.close()
182
183 - def test_parse_remove_comments(self):
184 parse = self.etree.parse 185 tostring = self.etree.tostring 186 XMLParser = self.etree.XMLParser 187 188 f = StringIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>') 189 parser = XMLParser(remove_comments=True) 190 tree = parse(f, parser) 191 self.assertEquals( 192 '<a><b><c/></b></a>', 193 tostring(tree))
194
195 - def test_parse_remove_pis(self):
196 parse = self.etree.parse 197 tostring = self.etree.tostring 198 XMLParser = self.etree.XMLParser 199 200 xml = '<?test?><a><?A?><b><?B?><c/></b><?C?></a><?tail?>' 201 202 f = StringIO(xml) 203 tree = parse(f) 204 self.assertEquals( 205 xml, 206 tostring(tree)) 207 208 f = StringIO(xml) 209 parser = XMLParser(remove_pis=True) 210 tree = parse(f, parser) 211 self.assertEquals( 212 '<a><b><c/></b></a>', 213 tostring(tree))
214
216 # ET raises IOError only 217 parse = self.etree.parse 218 self.assertRaises(TypeError, parse, 'notthere.xml', object())
219
220 - def test_parse_error_logging(self):
221 parse = self.etree.parse 222 # from StringIO 223 f = StringIO('<a><b></c></b></a>') 224 self.etree.clearErrorLog() 225 try: 226 parse(f) 227 logs = None 228 except SyntaxError, e: 229 logs = e.error_log 230 f.close() 231 self.assert_([ log for log in logs 232 if 'mismatch' in log.message ]) 233 self.assert_([ log for log in logs 234 if 'PARSER' in log.domain_name]) 235 self.assert_([ log for log in logs 236 if 'TAG_NAME_MISMATCH' in log.type_name ])
237
238 - def test_parse_error_from_file(self):
239 parse = self.etree.parse 240 # from file 241 f = open(fileInTestDir('test_broken.xml'), 'r') 242 self.assertRaises(SyntaxError, parse, f) 243 f.close()
244
245 - def test_iterparse_comments(self):
246 # ET removes comments 247 iterparse = self.etree.iterparse 248 tostring = self.etree.tostring 249 250 f = StringIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>') 251 events = list(iterparse(f)) 252 root = events[-1][1] 253 self.assertEquals(3, len(events)) 254 self.assertEquals( 255 '<a><!--A--><b><!-- B --><c/></b><!--C--></a>', 256 tostring(root))
257
259 iterparse = self.etree.iterparse 260 tostring = self.etree.tostring 261 262 f = StringIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>') 263 events = list(iterparse(f, remove_comments=True)) 264 root = events[-1][1] 265 self.assertEquals( 266 '<a><b><c/></b></a>', 267 tostring(root))
268
269 - def test_iterparse_broken(self):
270 iterparse = self.etree.iterparse 271 f = StringIO('<a><b><c/></a>') 272 # ET raises ExpatError, lxml raises XMLSyntaxError 273 self.assertRaises(self.etree.XMLSyntaxError, list, iterparse(f))
274
275 - def test_iterparse_strip(self):
276 iterparse = self.etree.iterparse 277 f = StringIO(""" 278 <a> \n \n <b> b test </b> \n 279 280 \n\t <c> \n </c> </a> \n """) 281 iterator = iterparse(f, remove_blank_text=True) 282 text = [ (element.text, element.tail) 283 for event, element in iterator ] 284 self.assertEquals( 285 [(" b test ", None), (" \n ", None), (None, None)], 286 text)
287
288 - def test_iterparse_tag(self):
289 iterparse = self.etree.iterparse 290 f = StringIO('<a><b><d/></b><c/></a>') 291 292 iterator = iterparse(f, tag="b", events=('start', 'end')) 293 events = list(iterator) 294 root = iterator.root 295 self.assertEquals( 296 [('start', root[0]), ('end', root[0])], 297 events)
298
299 - def test_iterparse_tag_all(self):
300 iterparse = self.etree.iterparse 301 f = StringIO('<a><b><d/></b><c/></a>') 302 303 iterator = iterparse(f, tag="*", events=('start', 'end')) 304 events = list(iterator) 305 self.assertEquals( 306 8, 307 len(events))
308
309 - def test_iterwalk_tag(self):
310 iterwalk = self.etree.iterwalk 311 root = self.etree.XML('<a><b><d/></b><c/></a>') 312 313 iterator = iterwalk(root, tag="b", events=('start', 'end')) 314 events = list(iterator) 315 self.assertEquals( 316 [('start', root[0]), ('end', root[0])], 317 events)
318
319 - def test_iterwalk_tag_all(self):
320 iterwalk = self.etree.iterwalk 321 root = self.etree.XML('<a><b><d/></b><c/></a>') 322 323 iterator = iterwalk(root, tag="*", events=('start', 'end')) 324 events = list(iterator) 325 self.assertEquals( 326 8, 327 len(events))
328
329 - def test_iterwalk(self):
330 iterwalk = self.etree.iterwalk 331 root = self.etree.XML('<a><b></b><c/></a>') 332 333 events = list(iterwalk(root)) 334 self.assertEquals( 335 [('end', root[0]), ('end', root[1]), ('end', root)], 336 events)
337
338 - def test_iterwalk_start(self):
339 iterwalk = self.etree.iterwalk 340 root = self.etree.XML('<a><b></b><c/></a>') 341 342 iterator = iterwalk(root, events=('start',)) 343 events = list(iterator) 344 self.assertEquals( 345 [('start', root), ('start', root[0]), ('start', root[1])], 346 events)
347
348 - def test_iterwalk_start_end(self):
349 iterwalk = self.etree.iterwalk 350 root = self.etree.XML('<a><b></b><c/></a>') 351 352 iterator = iterwalk(root, events=('start','end')) 353 events = list(iterator) 354 self.assertEquals( 355 [('start', root), ('start', root[0]), ('end', root[0]), 356 ('start', root[1]), ('end', root[1]), ('end', root)], 357 events)
358
359 - def test_iterwalk_clear(self):
360 iterwalk = self.etree.iterwalk 361 root = self.etree.XML('<a><b></b><c/></a>') 362 363 iterator = iterwalk(root) 364 for event, elem in iterator: 365 elem.clear() 366 367 self.assertEquals(0, 368 len(root))
369
370 - def test_iterwalk_attrib_ns(self):
371 iterwalk = self.etree.iterwalk 372 root = self.etree.XML('<a xmlns="ns1"><b><c xmlns="ns2"/></b></a>') 373 374 attr_name = '{testns}bla' 375 events = [] 376 iterator = iterwalk(root, events=('start','end','start-ns','end-ns')) 377 for event, elem in iterator: 378 events.append(event) 379 if event == 'start': 380 if elem.tag != '{ns1}a': 381 elem.set(attr_name, 'value') 382 383 self.assertEquals( 384 ['start-ns', 'start', 'start', 'start-ns', 'start', 385 'end', 'end-ns', 'end', 'end', 'end-ns'], 386 events) 387 388 self.assertEquals( 389 None, 390 root.get(attr_name)) 391 self.assertEquals( 392 'value', 393 root[0].get(attr_name))
394
395 - def test_iterwalk_getiterator(self):
396 iterwalk = self.etree.iterwalk 397 root = self.etree.XML('<a><b><d/></b><c/></a>') 398 399 counts = [] 400 for event, elem in iterwalk(root): 401 counts.append(len(list(elem.getiterator()))) 402 self.assertEquals( 403 [1,2,1,4], 404 counts)
405
406 - def test_resolve_string_dtd(self):
407 parse = self.etree.parse 408 parser = self.etree.XMLParser(dtd_validation=True) 409 assertEqual = self.assertEqual 410 test_url = u"__nosuch.dtd" 411 412 class MyResolver(self.etree.Resolver): 413 def resolve(self, url, id, context): 414 assertEqual(url, test_url) 415 return self.resolve_string( 416 u'<!ENTITY myentity "%s">' % url, context)
417 418 parser.resolvers.add(MyResolver()) 419 420 xml = u'<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>' % test_url 421 tree = parse(StringIO(xml), parser) 422 root = tree.getroot() 423 self.assertEquals(root.text, test_url) 424
425 - def test_resolve_empty(self):
426 parse = self.etree.parse 427 parser = self.etree.XMLParser(dtd_validation=True) 428 assertEqual = self.assertEqual 429 test_url = u"__nosuch.dtd" 430 431 class check(object): 432 resolved = False
433 434 class MyResolver(self.etree.Resolver): 435 def resolve(self, url, id, context): 436 assertEqual(url, test_url) 437 check.resolved = True 438 return self.resolve_empty(context) 439 440 parser.resolvers.add(MyResolver()) 441 442 xml = u'<!DOCTYPE doc SYSTEM "%s"><doc>&myentity;</doc>' % test_url 443 tree = parse(StringIO(xml), parser) 444 self.assert_(check.resolved) 445 446 root = tree.getroot() 447 self.assertEquals(root.text, None) 448
449 - def test_resolve_error(self):
450 parse = self.etree.parse 451 parser = self.etree.XMLParser(dtd_validation=True) 452 test_url = u"__nosuch.dtd" 453 454 class _LocalException(Exception): 455 pass
456 457 class MyResolver(self.etree.Resolver): 458 def resolve(self, url, id, context): 459 raise _LocalException 460 461 parser.resolvers.add(MyResolver()) 462 463 xml = u'<!DOCTYPE doc SYSTEM "test"><doc>&myentity;</doc>' 464 self.assertRaises(_LocalException, parse, StringIO(xml), parser) 465 466 # TypeError in etree, AssertionError in ElementTree;
467 - def test_setitem_assert(self):
468 Element = self.etree.Element 469 SubElement = self.etree.SubElement 470 471 a = Element('a') 472 b = SubElement(a, 'b') 473 474 self.assertRaises(TypeError, 475 a.__setitem__, 0, 'foo')
476
477 - def test_append_None(self):
478 # raises AssertionError in ElementTree 479 Element = self.etree.Element 480 self.assertRaises(TypeError, Element('a').append, None)
481
482 - def test_addnext(self):
483 Element = self.etree.Element 484 SubElement = self.etree.SubElement 485 root = Element('root') 486 SubElement(root, 'a') 487 SubElement(root, 'b') 488 489 self.assertEquals(['a', 'b'], 490 [c.tag for c in root]) 491 root[1].addnext(root[0]) 492 self.assertEquals(['b', 'a'], 493 [c.tag for c in root])
494
495 - def test_addprevious(self):
496 Element = self.etree.Element 497 SubElement = self.etree.SubElement 498 root = Element('root') 499 SubElement(root, 'a') 500 SubElement(root, 'b') 501 502 self.assertEquals(['a', 'b'], 503 [c.tag for c in root]) 504 root[0].addprevious(root[1]) 505 self.assertEquals(['b', 'a'], 506 [c.tag for c in root])
507
508 - def test_addnext_root(self):
509 Element = self.etree.Element 510 a = Element('a') 511 b = Element('b') 512 self.assertRaises(TypeError, a.addnext, b)
513
514 - def test_addnext_root(self):
515 Element = self.etree.Element 516 a = Element('a') 517 b = Element('b') 518 self.assertRaises(TypeError, a.addnext, b)
519
520 - def test_addprevious_pi(self):
521 Element = self.etree.Element 522 SubElement = self.etree.SubElement 523 PI = self.etree.PI 524 root = Element('root') 525 SubElement(root, 'a') 526 pi = PI('TARGET', 'TEXT') 527 pi.tail = "TAIL" 528 529 self.assertEquals('<root><a></a></root>', 530 self._writeElement(root)) 531 root[0].addprevious(pi) 532 self.assertEquals('<root><?TARGET TEXT?>TAIL<a></a></root>', 533 self._writeElement(root))
534
535 - def test_addprevious_root_pi(self):
536 Element = self.etree.Element 537 PI = self.etree.PI 538 root = Element('root') 539 pi = PI('TARGET', 'TEXT') 540 pi.tail = "TAIL" 541 542 self.assertEquals('<root></root>', 543 self._writeElement(root)) 544 root.addprevious(pi) 545 self.assertEquals('<?TARGET TEXT?>\n<root></root>', 546 self._writeElement(root))
547
548 - def test_addnext_pi(self):
549 Element = self.etree.Element 550 SubElement = self.etree.SubElement 551 PI = self.etree.PI 552 root = Element('root') 553 SubElement(root, 'a') 554 pi = PI('TARGET', 'TEXT') 555 pi.tail = "TAIL" 556 557 self.assertEquals('<root><a></a></root>', 558 self._writeElement(root)) 559 root[0].addnext(pi) 560 self.assertEquals('<root><a></a><?TARGET TEXT?>TAIL</root>', 561 self._writeElement(root))
562
563 - def test_addnext_root_pi(self):
564 Element = self.etree.Element 565 PI = self.etree.PI 566 root = Element('root') 567 pi = PI('TARGET', 'TEXT') 568 pi.tail = "TAIL" 569 570 self.assertEquals('<root></root>', 571 self._writeElement(root)) 572 root.addnext(pi) 573 self.assertEquals('<root></root>\n<?TARGET TEXT?>', 574 self._writeElement(root))
575
576 - def test_addnext_comment(self):
577 Element = self.etree.Element 578 SubElement = self.etree.SubElement 579 Comment = self.etree.Comment 580 root = Element('root') 581 SubElement(root, 'a') 582 comment = Comment('TEXT ') 583 comment.tail = "TAIL" 584 585 self.assertEquals('<root><a></a></root>', 586 self._writeElement(root)) 587 root[0].addnext(comment) 588 self.assertEquals('<root><a></a><!--TEXT -->TAIL</root>', 589 self._writeElement(root))
590
591 - def test_addnext_root_comment(self):
592 Element = self.etree.Element 593 Comment = self.etree.Comment 594 root = Element('root') 595 comment = Comment('TEXT ') 596 comment.tail = "TAIL" 597 598 self.assertEquals('<root></root>', 599 self._writeElement(root)) 600 root.addnext(comment) 601 self.assertEquals('<root></root>\n<!--TEXT -->', 602 self._writeElement(root))
603
604 - def test_addprevious_comment(self):
605 Element = self.etree.Element 606 SubElement = self.etree.SubElement 607 Comment = self.etree.Comment 608 root = Element('root') 609 SubElement(root, 'a') 610 comment = Comment('TEXT ') 611 comment.tail = "TAIL" 612 613 self.assertEquals('<root><a></a></root>', 614 self._writeElement(root)) 615 root[0].addprevious(comment) 616 self.assertEquals('<root><!--TEXT -->TAIL<a></a></root>', 617 self._writeElement(root))
618
619 - def test_addprevious_root_comment(self):
620 Element = self.etree.Element 621 Comment = self.etree.Comment 622 root = Element('root') 623 comment = Comment('TEXT ') 624 comment.tail = "TAIL" 625 626 self.assertEquals('<root></root>', 627 self._writeElement(root)) 628 root.addprevious(comment) 629 self.assertEquals('<!--TEXT -->\n<root></root>', 630 self._writeElement(root))
631 632 # ET's Elements have items() and key(), but not values()
633 - def test_attribute_values(self):
634 XML = self.etree.XML 635 636 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>') 637 values = root.values() 638 values.sort() 639 self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)
640 641 # gives error in ElementTree
642 - def test_comment_empty(self):
643 Element = self.etree.Element 644 Comment = self.etree.Comment 645 646 a = Element('a') 647 a.append(Comment()) 648 self.assertEquals( 649 '<a><!----></a>', 650 self._writeElement(a))
651 652 # ElementTree ignores comments
653 - def test_comment_parse_empty(self):
654 ElementTree = self.etree.ElementTree 655 tostring = self.etree.tostring 656 657 xml = '<a><b/><!----><c/></a>' 658 f = StringIO(xml) 659 doc = ElementTree(file=f) 660 a = doc.getroot() 661 self.assertEquals( 662 '', 663 a[1].text) 664 self.assertEquals( 665 xml, 666 tostring(a))
667 668 # ElementTree ignores comments
669 - def test_comment_no_proxy_yet(self):
670 ElementTree = self.etree.ElementTree 671 672 f = StringIO('<a><b></b><!-- hoi --><c></c></a>') 673 doc = ElementTree(file=f) 674 a = doc.getroot() 675 self.assertEquals( 676 ' hoi ', 677 a[1].text)
678 679 # ElementTree adds whitespace around comments
680 - def test_comment_text(self):
681 Element = self.etree.Element 682 Comment = self.etree.Comment 683 tostring = self.etree.tostring 684 685 a = Element('a') 686 a.append(Comment('foo')) 687 self.assertEquals( 688 '<a><!--foo--></a>', 689 tostring(a)) 690 691 a[0].text = "TEST" 692 self.assertEquals( 693 '<a><!--TEST--></a>', 694 tostring(a))
695 696 # ElementTree adds whitespace around comments
697 - def test_comment_whitespace(self):
698 Element = self.etree.Element 699 Comment = self.etree.Comment 700 tostring = self.etree.tostring 701 702 a = Element('a') 703 a.append(Comment(' foo ')) 704 self.assertEquals( 705 '<a><!-- foo --></a>', 706 tostring(a))
707 708 # does not raise an exception in ElementTree
709 - def test_comment_immutable(self):
710 Element = self.etree.Element 711 Comment = self.etree.Comment 712 713 c = Comment() 714 el = Element('myel') 715 716 self.assertRaises(TypeError, c.append, el) 717 self.assertRaises(TypeError, c.insert, 0, el) 718 self.assertRaises(TypeError, c.set, "myattr", "test")
719 720 # test weird dictionary interaction leading to segfault previously
721 - def test_weird_dict_interaction(self):
722 root = self.etree.Element('root') 723 add = self.etree.ElementTree(file=StringIO('<foo>Foo</foo>')) 724 root.append(self.etree.Element('baz'))
725 726 # test passing 'None' to dump
727 - def test_dump_none(self):
728 self.assertRaises(TypeError, etree.dump, None)
729
730 - def test_prefix(self):
731 ElementTree = self.etree.ElementTree 732 733 f = StringIO('<a xmlns:foo="http://www.infrae.com/ns/1"><foo:b/></a>') 734 doc = ElementTree(file=f) 735 a = doc.getroot() 736 self.assertEquals( 737 None, 738 a.prefix) 739 self.assertEquals( 740 'foo', 741 a[0].prefix)
742
743 - def test_prefix_default_ns(self):
744 ElementTree = self.etree.ElementTree 745 746 f = StringIO('<a xmlns="http://www.infrae.com/ns/1"><b/></a>') 747 doc = ElementTree(file=f) 748 a = doc.getroot() 749 self.assertEquals( 750 None, 751 a.prefix) 752 self.assertEquals( 753 None, 754 a[0].prefix)
755
756 - def test_getparent(self):
757 Element = self.etree.Element 758 SubElement = self.etree.SubElement 759 760 a = Element('a') 761 b = SubElement(a, 'b') 762 c = SubElement(a, 'c') 763 d = SubElement(b, 'd') 764 self.assertEquals( 765 None, 766 a.getparent()) 767 self.assertEquals( 768 a, 769 b.getparent()) 770 self.assertEquals( 771 b.getparent(), 772 c.getparent()) 773 self.assertEquals( 774 b, 775 d.getparent())
776
777 - def test_iterchildren(self):
778 XML = self.etree.XML 779 780 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>') 781 result = [] 782 for el in root.iterchildren(): 783 result.append(el.tag) 784 self.assertEquals(['one', 'two', 'three'], result)
785
786 - def test_iterchildren_reversed(self):
787 XML = self.etree.XML 788 789 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>') 790 result = [] 791 for el in root.iterchildren(reversed=True): 792 result.append(el.tag) 793 self.assertEquals(['three', 'two', 'one'], result)
794
795 - def test_iterchildren_tag(self):
796 XML = self.etree.XML 797 798 root = XML('<doc><one/><two>Two</two>Hm<two>Bla</two></doc>') 799 result = [] 800 for el in root.iterchildren(tag='two'): 801 result.append(el.text) 802 self.assertEquals(['Two', 'Bla'], result)
803
804 - def test_iterchildren_tag_reversed(self):
805 XML = self.etree.XML 806 807 root = XML('<doc><one/><two>Two</two>Hm<two>Bla</two></doc>') 808 result = [] 809 for el in root.iterchildren(reversed=True, tag='two'): 810 result.append(el.text) 811 self.assertEquals(['Bla', 'Two'], result)
812
813 - def test_iterancestors(self):
814 Element = self.etree.Element 815 SubElement = self.etree.SubElement 816 817 a = Element('a') 818 b = SubElement(a, 'b') 819 c = SubElement(a, 'c') 820 d = SubElement(b, 'd') 821 self.assertEquals( 822 [], 823 list(a.iterancestors())) 824 self.assertEquals( 825 [a], 826 list(b.iterancestors())) 827 self.assertEquals( 828 a, 829 c.iterancestors().next()) 830 self.assertEquals( 831 [b, a], 832 list(d.iterancestors()))
833
834 - def test_iterancestors_tag(self):
835 Element = self.etree.Element 836 SubElement = self.etree.SubElement 837 838 a = Element('a') 839 b = SubElement(a, 'b') 840 c = SubElement(a, 'c') 841 d = SubElement(b, 'd') 842 self.assertEquals( 843 [a], 844 list(d.iterancestors(tag='a')))
845
846 - def test_iterdescendants(self):
847 Element = self.etree.Element 848 SubElement = self.etree.SubElement 849 850 a = Element('a') 851 b = SubElement(a, 'b') 852 c = SubElement(a, 'c') 853 d = SubElement(b, 'd') 854 e = SubElement(c, 'e') 855 856 self.assertEquals( 857 [b, d, c, e], 858 list(a.iterdescendants())) 859 self.assertEquals( 860 [], 861 list(d.iterdescendants()))
862
863 - def test_iterdescendants_tag(self):
864 Element = self.etree.Element 865 SubElement = self.etree.SubElement 866 867 a = Element('a') 868 b = SubElement(a, 'b') 869 c = SubElement(a, 'c') 870 d = SubElement(b, 'd') 871 e = SubElement(c, 'e') 872 873 self.assertEquals( 874 [], 875 list(a.iterdescendants('a'))) 876 a2 = SubElement(e, 'a') 877 self.assertEquals( 878 [a2], 879 list(a.iterdescendants('a'))) 880 self.assertEquals( 881 [a2], 882 list(c.iterdescendants('a')))
883
884 - def test_getroottree(self):
885 Element = self.etree.Element 886 SubElement = self.etree.SubElement 887 888 a = Element('a') 889 b = SubElement(a, 'b') 890 c = SubElement(a, 'c') 891 d = SubElement(b, 'd') 892 self.assertEquals( 893 a, 894 a.getroottree().getroot()) 895 self.assertEquals( 896 a, 897 b.getroottree().getroot()) 898 self.assertEquals( 899 a, 900 d.getroottree().getroot())
901
902 - def test_getnext(self):
903 Element = self.etree.Element 904 SubElement = self.etree.SubElement 905 906 a = Element('a') 907 b = SubElement(a, 'b') 908 c = SubElement(a, 'c') 909 self.assertEquals( 910 None, 911 a.getnext()) 912 self.assertEquals( 913 c, 914 b.getnext()) 915 self.assertEquals( 916 None, 917 c.getnext())
918
919 - def test_getprevious(self):
920 Element = self.etree.Element 921 SubElement = self.etree.SubElement 922 923 a = Element('a') 924 b = SubElement(a, 'b') 925 c = SubElement(a, 'c') 926 d = SubElement(b, 'd') 927 self.assertEquals( 928 None, 929 a.getprevious()) 930 self.assertEquals( 931 b, 932 c.getprevious()) 933 self.assertEquals( 934 None, 935 b.getprevious())
936
937 - def test_itersiblings(self):
938 Element = self.etree.Element 939 SubElement = self.etree.SubElement 940 941 a = Element('a') 942 b = SubElement(a, 'b') 943 c = SubElement(a, 'c') 944 d = SubElement(b, 'd') 945 self.assertEquals( 946 [], 947 list(a.itersiblings())) 948 self.assertEquals( 949 [c], 950 list(b.itersiblings())) 951 self.assertEquals( 952 c, 953 b.itersiblings().next()) 954 self.assertEquals( 955 [], 956 list(c.itersiblings())) 957 self.assertEquals( 958 [b], 959 list(c.itersiblings(preceding=True))) 960 self.assertEquals( 961 [], 962 list(b.itersiblings(preceding=True)))
963
964 - def test_itersiblings_tag(self):
965 Element = self.etree.Element 966 SubElement = self.etree.SubElement 967 968 a = Element('a') 969 b = SubElement(a, 'b') 970 c = SubElement(a, 'c') 971 d = SubElement(b, 'd') 972 self.assertEquals( 973 [], 974 list(a.itersiblings(tag='XXX'))) 975 self.assertEquals( 976 [c], 977 list(b.itersiblings(tag='c'))) 978 self.assertEquals( 979 [b], 980 list(c.itersiblings(preceding=True, tag='b'))) 981 self.assertEquals( 982 [], 983 list(c.itersiblings(preceding=True, tag='c')))
984
985 - def test_parseid(self):
986 parseid = self.etree.parseid 987 XML = self.etree.XML 988 xml_text = ''' 989 <!DOCTYPE document [ 990 <!ELEMENT document (h1,p)*> 991 <!ELEMENT h1 (#PCDATA)> 992 <!ATTLIST h1 myid ID #REQUIRED> 993 <!ELEMENT p (#PCDATA)> 994 <!ATTLIST p someid ID #REQUIRED> 995 ]> 996 <document> 997 <h1 myid="chapter1">...</h1> 998 <p id="note1" class="note">...</p> 999 <p>Regular paragraph.</p> 1000 <p xml:id="xmlid">XML:ID paragraph.</p> 1001 <p someid="warn1" class="warning">...</p> 1002 </document> 1003 ''' 1004 1005 tree, dic = parseid(StringIO(xml_text)) 1006 root = tree.getroot() 1007 root2 = XML(xml_text) 1008 self.assertEquals(self._writeElement(root), 1009 self._writeElement(root2)) 1010 expected = { 1011 "chapter1" : root[0], 1012 "xmlid" : root[3], 1013 "warn1" : root[4] 1014 } 1015 self.assert_("chapter1" in dic) 1016 self.assert_("warn1" in dic) 1017 self.assert_("xmlid" in dic) 1018 self._checkIDDict(dic, expected)
1019
1020 - def test_XMLDTDID(self):
1021 XMLDTDID = self.etree.XMLDTDID 1022 XML = self.etree.XML 1023 xml_text = ''' 1024 <!DOCTYPE document [ 1025 <!ELEMENT document (h1,p)*> 1026 <!ELEMENT h1 (#PCDATA)> 1027 <!ATTLIST h1 myid ID #REQUIRED> 1028 <!ELEMENT p (#PCDATA)> 1029 <!ATTLIST p someid ID #REQUIRED> 1030 ]> 1031 <document> 1032 <h1 myid="chapter1">...</h1> 1033 <p id="note1" class="note">...</p> 1034 <p>Regular paragraph.</p> 1035 <p xml:id="xmlid">XML:ID paragraph.</p> 1036 <p someid="warn1" class="warning">...</p> 1037 </document> 1038 ''' 1039 1040 root, dic = XMLDTDID(xml_text) 1041 root2 = XML(xml_text) 1042 self.assertEquals(self._writeElement(root), 1043 self._writeElement(root2)) 1044 expected = { 1045 "chapter1" : root[0], 1046 "xmlid" : root[3], 1047 "warn1" : root[4] 1048 } 1049 self.assert_("chapter1" in dic) 1050 self.assert_("warn1" in dic) 1051 self.assert_("xmlid" in dic) 1052 self._checkIDDict(dic, expected)
1053
1054 - def test_XMLDTDID_empty(self):
1055 XMLDTDID = self.etree.XMLDTDID 1056 XML = self.etree.XML 1057 xml_text = ''' 1058 <document> 1059 <h1 myid="chapter1">...</h1> 1060 <p id="note1" class="note">...</p> 1061 <p>Regular paragraph.</p> 1062 <p someid="warn1" class="warning">...</p> 1063 </document> 1064 ''' 1065 1066 root, dic = XMLDTDID(xml_text) 1067 root2 = XML(xml_text) 1068 self.assertEquals(self._writeElement(root), 1069 self._writeElement(root2)) 1070 expected = {} 1071 self._checkIDDict(dic, expected)
1072
1073 - def _checkIDDict(self, dic, expected):
1074 self.assertEquals(dic, expected) 1075 self.assertEquals(len(dic), 1076 len(expected)) 1077 self.assertEquals(sorted(dic.items()), 1078 sorted(expected.items())) 1079 self.assertEquals(sorted(dic.iteritems()), 1080 sorted(expected.iteritems())) 1081 self.assertEquals(sorted(dic.keys()), 1082 sorted(expected.keys())) 1083 self.assertEquals(sorted(dic.iterkeys()), 1084 sorted(expected.iterkeys())) 1085 self.assertEquals(sorted(dic.values()), 1086 sorted(expected.values())) 1087 self.assertEquals(sorted(dic.itervalues()), 1088 sorted(expected.itervalues()))
1089
1090 - def test_namespaces(self):
1091 etree = self.etree 1092 1093 r = {'foo': 'http://ns.infrae.com/foo'} 1094 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1095 self.assertEquals( 1096 'foo', 1097 e.prefix) 1098 self.assertEquals( 1099 '<foo:bar xmlns:foo="http://ns.infrae.com/foo"></foo:bar>', 1100 self._writeElement(e))
1101
1102 - def test_namespaces_default(self):
1103 etree = self.etree 1104 1105 r = {None: 'http://ns.infrae.com/foo'} 1106 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1107 self.assertEquals( 1108 None, 1109 e.prefix) 1110 self.assertEquals( 1111 '{http://ns.infrae.com/foo}bar', 1112 e.tag) 1113 self.assertEquals( 1114 '<bar xmlns="http://ns.infrae.com/foo"></bar>', 1115 self._writeElement(e))
1116
1117 - def test_namespaces_default_and_attr(self):
1118 etree = self.etree 1119 1120 r = {None: 'http://ns.infrae.com/foo', 1121 'hoi': 'http://ns.infrae.com/hoi'} 1122 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1123 e.set('{http://ns.infrae.com/hoi}test', 'value') 1124 self.assertEquals( 1125 '<bar xmlns="http://ns.infrae.com/foo" xmlns:hoi="http://ns.infrae.com/hoi" hoi:test="value"></bar>', 1126 self._writeElement(e))
1127
1128 - def test_namespaces_elementtree(self):
1129 etree = self.etree 1130 r = {None: 'http://ns.infrae.com/foo', 1131 'hoi': 'http://ns.infrae.com/hoi'} 1132 e = etree.Element('{http://ns.infrae.com/foo}z', nsmap=r) 1133 tree = etree.ElementTree(element=e) 1134 etree.SubElement(e, '{http://ns.infrae.com/hoi}x') 1135 self.assertEquals( 1136 '<z xmlns="http://ns.infrae.com/foo" xmlns:hoi="http://ns.infrae.com/hoi"><hoi:x></hoi:x></z>', 1137 self._writeElement(e))
1138
1139 - def test_namespaces_default_copy_element(self):
1140 etree = self.etree 1141 1142 r = {None: 'http://ns.infrae.com/foo'} 1143 e1 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1144 e2 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1145 1146 e1.append(e2) 1147 1148 self.assertEquals( 1149 None, 1150 e1.prefix) 1151 self.assertEquals( 1152 None, 1153 e1[0].prefix) 1154 self.assertEquals( 1155 '{http://ns.infrae.com/foo}bar', 1156 e1.tag) 1157 self.assertEquals( 1158 '{http://ns.infrae.com/foo}bar', 1159 e1[0].tag)
1160
1161 - def test_namespaces_copy_element(self):
1162 etree = self.etree 1163 1164 r = {None: 'http://ns.infrae.com/BAR'} 1165 e1 = etree.Element('{http://ns.infrae.com/BAR}bar', nsmap=r) 1166 e2 = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1167 1168 e1.append(e2) 1169 1170 self.assertEquals( 1171 None, 1172 e1.prefix) 1173 self.assertNotEquals( 1174 None, 1175 e2.prefix) 1176 self.assertEquals( 1177 '{http://ns.infrae.com/BAR}bar', 1178 e1.tag) 1179 self.assertEquals( 1180 '{http://ns.infrae.com/foo}bar', 1181 e2.tag)
1182
1183 - def test_namespaces_reuse_after_move(self):
1184 ns_href = "http://a.b.c" 1185 one = self.etree.parse( 1186 StringIO('<foo><bar xmlns:ns="%s"><ns:baz/></bar></foo>' % ns_href)) 1187 baz = one.getroot()[0][0] 1188 1189 two = self.etree.parse( 1190 StringIO('<root xmlns:ns="%s"/>' % ns_href)) 1191 two.getroot().append(baz) 1192 del one # make sure the source document is deallocated 1193 1194 self.assertEquals('{%s}baz' % ns_href, baz.tag) 1195 self.assertEquals( 1196 '<root xmlns:ns="%s"><ns:baz/></root>' % ns_href, 1197 self.etree.tostring(two))
1198
1199 - def _test_namespaces_after_serialize(self):
1200 # FIXME: this currently fails - fix serializer.pxi! 1201 parse = self.etree.parse 1202 tostring = self.etree.tostring 1203 1204 ns_href = "http://a.b.c" 1205 one = parse( 1206 StringIO('<foo><bar xmlns:ns="%s"><ns:baz/></bar></foo>' % ns_href)) 1207 baz = one.getroot()[0][0] 1208 1209 print tostring(baz) 1210 parsed = parse(StringIO( tostring(baz) )).getroot() 1211 1212 self.assertEquals('{%s}baz' % ns_href, parsed.tag)
1213
1214 - def test_element_nsmap(self):
1215 etree = self.etree 1216 1217 r = {None: 'http://ns.infrae.com/foo', 1218 'hoi': 'http://ns.infrae.com/hoi'} 1219 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=r) 1220 self.assertEquals( 1221 r, 1222 e.nsmap)
1223
1224 - def test_subelement_nsmap(self):
1225 etree = self.etree 1226 1227 re = {None: 'http://ns.infrae.com/foo', 1228 'hoi': 'http://ns.infrae.com/hoi'} 1229 e = etree.Element('{http://ns.infrae.com/foo}bar', nsmap=re) 1230 1231 rs = {None: 'http://ns.infrae.com/honk', 1232 'top': 'http://ns.infrae.com/top'} 1233 s = etree.SubElement(e, '{http://ns.infrae.com/honk}bar', nsmap=rs) 1234 1235 r = re.copy() 1236 r.update(rs) 1237 self.assertEquals( 1238 re, 1239 e.nsmap) 1240 self.assertEquals( 1241 r, 1242 s.nsmap)
1243
1244 - def test_getiterator_filter_namespace(self):
1245 Element = self.etree.Element 1246 SubElement = self.etree.SubElement 1247 1248 a = Element('{a}a') 1249 b = SubElement(a, '{a}b') 1250 c = SubElement(a, '{a}c') 1251 d = SubElement(b, '{b}d') 1252 e = SubElement(c, '{a}e') 1253 f = SubElement(c, '{b}f') 1254 1255 self.assertEquals( 1256 [a], 1257 list(a.getiterator('{a}a'))) 1258 self.assertEquals( 1259 [], 1260 list(a.getiterator('{b}a'))) 1261 self.assertEquals( 1262 [], 1263 list(a.getiterator('a'))) 1264 self.assertEquals( 1265 [f], 1266 list(c.getiterator('{b}*'))) 1267 self.assertEquals( 1268 [d, f], 1269 list(a.getiterator('{b}*')))
1270
1271 - def test_getiterator_filter_element(self):
1272 Element = self.etree.Element 1273 Comment = self.etree.Comment 1274 PI = self.etree.PI 1275 SubElement = self.etree.SubElement 1276 1277 a = Element('a') 1278 b = SubElement(a, 'b') 1279 a.append(Comment("test")) 1280 a.append(PI("pi", "content")) 1281 c = SubElement(a, 'c') 1282 1283 self.assertEquals( 1284 [a, b, c], 1285 list(a.getiterator(Element)))
1286
1287 - def test_getiterator_filter_all_comment_pi(self):
1288 # ElementTree iterates over everything here 1289 Element = self.etree.Element 1290 Comment = self.etree.Comment 1291 PI = self.etree.PI 1292 SubElement = self.etree.SubElement 1293 1294 a = Element('a') 1295 b = SubElement(a, 'b') 1296 a.append(Comment("test")) 1297 a.append(PI("pi", "content")) 1298 c = SubElement(a, 'c') 1299 1300 self.assertEquals( 1301 [a, b, c], 1302 list(a.getiterator('*')))
1303
1304 - def test_findall_ns(self):
1305 XML = self.etree.XML 1306 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>') 1307 self.assertEquals(len(root.findall(".//{X}b")), 2) 1308 self.assertEquals(len(root.findall(".//{X}*")), 2) 1309 self.assertEquals(len(root.findall(".//b")), 3)
1310
1311 - def test_index(self):
1312 etree = self.etree 1313 e = etree.Element('foo') 1314 for i in range(10): 1315 etree.SubElement(e, 'a%s' % i) 1316 for i in range(10): 1317 self.assertEquals( 1318 i, 1319 e.index(e[i])) 1320 self.assertEquals( 1321 3, e.index(e[3], 3)) 1322 self.assertRaises( 1323 ValueError, e.index, e[3], 4) 1324 self.assertRaises( 1325 ValueError, e.index, e[3], 0, 2) 1326 self.assertRaises( 1327 ValueError, e.index, e[8], 0, -3) 1328 self.assertRaises( 1329 ValueError, e.index, e[8], -5, -3) 1330 self.assertEquals( 1331 8, e.index(e[8], 0, -1)) 1332 self.assertEquals( 1333 8, e.index(e[8], -12, -1)) 1334 self.assertEquals( 1335 0, e.index(e[0], -12, -1))
1336
1337 - def test_replace(self):
1338 etree = self.etree 1339 e = etree.Element('foo') 1340 for i in range(10): 1341 el = etree.SubElement(e, 'a%s' % i) 1342 el.text = "text%d" % i 1343 el.tail = "tail%d" % i 1344 1345 child0 = e[0] 1346 child1 = e[1] 1347 child2 = e[2] 1348 1349 e.replace(e[0], e[1]) 1350 self.assertEquals( 1351 9, len(e)) 1352 self.assertEquals( 1353 child1, e[0]) 1354 self.assertEquals( 1355 child1.text, "text1") 1356 self.assertEquals( 1357 child1.tail, "tail1") 1358 self.assertEquals( 1359 child0.tail, "tail0") 1360 self.assertEquals( 1361 child2, e[1]) 1362 1363 e.replace(e[-1], e[0]) 1364 self.assertEquals( 1365 child1, e[-1]) 1366 self.assertEquals( 1367 child1.text, "text1") 1368 self.assertEquals( 1369 child1.tail, "tail1") 1370 self.assertEquals( 1371 child2, e[0])
1372
1373 - def test_replace_new(self):
1374 etree = self.etree 1375 e = etree.Element('foo') 1376 for i in range(10): 1377 etree.SubElement(e, 'a%s' % i) 1378 1379 new_element = etree.Element("test") 1380 new_element.text = "TESTTEXT" 1381 new_element.tail = "TESTTAIL" 1382 child1 = e[1] 1383 e.replace(e[0], new_element) 1384 self.assertEquals( 1385 new_element, e[0]) 1386 self.assertEquals( 1387 "TESTTEXT", 1388 e[0].text) 1389 self.assertEquals( 1390 "TESTTAIL", 1391 e[0].tail) 1392 self.assertEquals( 1393 child1, e[1])
1394
1395 - def test_extend(self):
1396 etree = self.etree 1397 root = etree.Element('foo') 1398 for i in range(3): 1399 element = etree.SubElement(root, 'a%s' % i) 1400 element.text = "text%d" % i 1401 element.tail = "tail%d" % i 1402 1403 elements = [] 1404 for i in range(3): 1405 new_element = etree.Element("test%s" % i) 1406 new_element.text = "TEXT%s" % i 1407 new_element.tail = "TAIL%s" % i 1408 elements.append(new_element) 1409 1410 root.extend(elements) 1411 1412 self.assertEquals( 1413 ["a0", "a1", "a2", "test0", "test1", "test2"], 1414 [ el.tag for el in root ]) 1415 self.assertEquals( 1416 ["text0", "text1", "text2", "TEXT0", "TEXT1", "TEXT2"], 1417 [ el.text for el in root ]) 1418 self.assertEquals( 1419 ["tail0", "tail1", "tail2", "TAIL0", "TAIL1", "TAIL2"], 1420 [ el.tail for el in root ])
1421
1422 - def test_sourceline_XML(self):
1423 XML = self.etree.XML 1424 root = XML('''<?xml version="1.0"?> 1425 <root><test> 1426 1427 <bla/></test> 1428 </root> 1429 ''') 1430 1431 self.assertEquals( 1432 [2, 2, 4], 1433 [ el.sourceline for el in root.getiterator() ])
1434
1435 - def test_sourceline_parse(self):
1436 parse = self.etree.parse 1437 tree = parse(fileInTestDir('include/test_xinclude.xml')) 1438 1439 self.assertEquals( 1440 [1, 2, 3], 1441 [ el.sourceline for el in tree.getiterator() ])
1442
1443 - def test_sourceline_element(self):
1444 Element = self.etree.Element 1445 SubElement = self.etree.SubElement 1446 el = Element("test") 1447 self.assertEquals(None, el.sourceline) 1448 1449 child = SubElement(el, "test") 1450 self.assertEquals(None, el.sourceline) 1451 self.assertEquals(None, child.sourceline)
1452
1453 - def test_XML_base_url_docinfo(self):
1454 etree = self.etree 1455 root = etree.XML("<root/>", base_url="http://no/such/url") 1456 docinfo = root.getroottree().docinfo 1457 self.assertEquals(docinfo.URL, "http://no/such/url")
1458
1459 - def test_HTML_base_url_docinfo(self):
1460 etree = self.etree 1461 root = etree.HTML("<html/>", base_url="http://no/such/url") 1462 docinfo = root.getroottree().docinfo 1463 self.assertEquals(docinfo.URL, "http://no/such/url")
1464
1465 - def test_docinfo_public(self):
1466 etree = self.etree 1467 xml_header = '<?xml version="1.0" encoding="ascii"?>' 1468 pub_id = "-//W3C//DTD XHTML 1.0 Transitional//EN" 1469 sys_id = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 1470 doctype_string = '<!DOCTYPE html PUBLIC "%s" "%s">' % (pub_id, sys_id) 1471 1472 xml = xml_header + doctype_string + '<html><body></body></html>' 1473 1474 tree = etree.parse(StringIO(xml)) 1475 docinfo = tree.docinfo 1476 self.assertEquals(docinfo.encoding, "ascii") 1477 self.assertEquals(docinfo.xml_version, "1.0") 1478 self.assertEquals(docinfo.public_id, pub_id) 1479 self.assertEquals(docinfo.system_url, sys_id) 1480 self.assertEquals(docinfo.root_name, 'html') 1481 self.assertEquals(docinfo.doctype, doctype_string)
1482
1483 - def test_docinfo_system(self):
1484 etree = self.etree 1485 xml_header = '<?xml version="1.0" encoding="UTF-8"?>' 1486 sys_id = "some.dtd" 1487 doctype_string = '<!DOCTYPE html SYSTEM "%s">' % sys_id 1488 xml = xml_header + doctype_string + '<html><body></body></html>' 1489 1490 tree = etree.parse(StringIO(xml)) 1491 docinfo = tree.docinfo 1492 self.assertEquals(docinfo.encoding, "UTF-8") 1493 self.assertEquals(docinfo.xml_version, "1.0") 1494 self.assertEquals(docinfo.public_id, None) 1495 self.assertEquals(docinfo.system_url, sys_id) 1496 self.assertEquals(docinfo.root_name, 'html') 1497 self.assertEquals(docinfo.doctype, doctype_string)
1498
1499 - def test_docinfo_empty(self):
1500 etree = self.etree 1501 xml = '<html><body></body></html>' 1502 tree = etree.parse(StringIO(xml)) 1503 docinfo = tree.docinfo 1504 self.assertEquals(docinfo.encoding, None) 1505 self.assertEquals(docinfo.xml_version, "1.0") 1506 self.assertEquals(docinfo.public_id, None) 1507 self.assertEquals(docinfo.system_url, None) 1508 self.assertEquals(docinfo.root_name, 'html') 1509 self.assertEquals(docinfo.doctype, '')
1510
1511 - def test_dtd_io(self):
1512 # check that DTDs that go in also go back out 1513 xml = '''\ 1514 <!DOCTYPE test SYSTEM "test.dtd" [ 1515 <!ENTITY entity "tasty"> 1516 <!ELEMENT test (a)> 1517 <!ELEMENT a (#PCDATA)> 1518 ]> 1519 <test><a>test-test</a></test>\ 1520 ''' 1521 root = self.etree.parse(StringIO(xml)) 1522 self.assertEqual(self.etree.tostring(root).replace(" ", ""), 1523 xml.replace(" ", ""))
1524
1525 - def test_byte_zero(self):
1526 Element = self.etree.Element 1527 1528 a = Element('a') 1529 self.assertRaises(AssertionError, setattr, a, "text", 'ha\0ho') 1530 self.assertRaises(AssertionError, setattr, a, "tail", 'ha\0ho') 1531 1532 self.assertRaises(AssertionError, Element, 'ha\0ho')
1533
1534 - def test_unicode_byte_zero(self):
1535 Element = self.etree.Element 1536 1537 a = Element('a') 1538 self.assertRaises(AssertionError, setattr, a, "text", u'ha\0ho') 1539 self.assertRaises(AssertionError, setattr, a, "tail", u'ha\0ho') 1540 1541 self.assertRaises(AssertionError, Element, u'ha\0ho')
1542
1543 - def test_byte_invalid(self):
1544 Element = self.etree.Element 1545 1546 a = Element('a') 1547 self.assertRaises(AssertionError, setattr, a, "text", 'ha\x07ho') 1548 self.assertRaises(AssertionError, setattr, a, "text", 'ha\x02ho') 1549 1550 self.assertRaises(AssertionError, setattr, a, "tail", 'ha\x07ho') 1551 self.assertRaises(AssertionError, setattr, a, "tail", 'ha\x02ho') 1552 1553 self.assertRaises(AssertionError, Element, 'ha\x07ho') 1554 self.assertRaises(AssertionError, Element, 'ha\x02ho')
1555
1556 - def test_unicode_byte_invalid(self):
1557 Element = self.etree.Element 1558 1559 a = Element('a') 1560 self.assertRaises(AssertionError, setattr, a, "text", u'ha\x07ho') 1561 self.assertRaises(AssertionError, setattr, a, "text", u'ha\x02ho') 1562 1563 self.assertRaises(AssertionError, setattr, a, "tail", u'ha\x07ho') 1564 self.assertRaises(AssertionError, setattr, a, "tail", u'ha\x02ho') 1565 1566 self.assertRaises(AssertionError, Element, u'ha\x07ho') 1567 self.assertRaises(AssertionError, Element, u'ha\x02ho')
1568
1569 - def test_encoding_tostring_utf16(self):
1570 # ElementTree fails to serialize this 1571 tostring = self.etree.tostring 1572 Element = self.etree.Element 1573 SubElement = self.etree.SubElement 1574 1575 a = Element('a') 1576 b = SubElement(a, 'b') 1577 c = SubElement(a, 'c') 1578 1579 result = unicode(tostring(a, 'UTF-16'), 'UTF-16') 1580 self.assertEquals('<a><b></b><c></c></a>', 1581 canonicalize(result))
1582
1583 - def test_tostring_none(self):
1584 # ElementTree raises an AssertionError here 1585 tostring = self.etree.tostring 1586 self.assertRaises(TypeError, self.etree.tostring, None)
1587
1588 - def test_tostring_pretty(self):
1589 tostring = self.etree.tostring 1590 Element = self.etree.Element 1591 SubElement = self.etree.SubElement 1592 1593 a = Element('a') 1594 b = SubElement(a, 'b') 1595 c = SubElement(a, 'c') 1596 1597 result = tostring(a) 1598 self.assertEquals(result, "<a><b/><c/></a>") 1599 1600 result = tostring(a, pretty_print=False) 1601 self.assertEquals(result, "<a><b/><c/></a>") 1602 1603 result = tostring(a, pretty_print=True) 1604 self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>")
1605
1606 - def test_tounicode(self):
1607 tounicode = self.etree.tounicode 1608 Element = self.etree.Element 1609 SubElement = self.etree.SubElement 1610 1611 a = Element('a') 1612 b = SubElement(a, 'b') 1613 c = SubElement(a, 'c') 1614 1615 self.assert_(isinstance(tounicode(a), unicode)) 1616 self.assertEquals('<a><b></b><c></c></a>', 1617 canonicalize(tounicode(a)))
1618
1619 - def test_tounicode_element(self):
1620 tounicode = self.etree.tounicode 1621 Element = self.etree.Element 1622 SubElement = self.etree.SubElement 1623 1624 a = Element('a') 1625 b = SubElement(a, 'b') 1626 c = SubElement(a, 'c') 1627 d = SubElement(c, 'd') 1628 self.assert_(isinstance(tounicode(b), unicode)) 1629 self.assert_(isinstance(tounicode(c), unicode)) 1630 self.assertEquals('<b></b>', 1631 canonicalize(tounicode(b))) 1632 self.assertEquals('<c><d></d></c>', 1633 canonicalize(tounicode(c)))
1634
1635 - def test_tounicode_none(self):
1636 tounicode = self.etree.tounicode 1637 self.assertRaises(TypeError, self.etree.tounicode, None)
1638
1639 - def test_tounicode_element_tail(self):
1640 tounicode = self.etree.tounicode 1641 Element = self.etree.Element 1642 SubElement = self.etree.SubElement 1643 1644 a = Element('a') 1645 b = SubElement(a, 'b') 1646 c = SubElement(a, 'c') 1647 d = SubElement(c, 'd') 1648 b.tail = 'Foo' 1649 1650 self.assert_(isinstance(tounicode(b), unicode)) 1651 self.assert_(tounicode(b) == '<b/>Foo' or 1652 tounicode(b) == '<b />Foo')
1653
1654 - def test_tounicode_pretty(self):
1655 tounicode = self.etree.tounicode 1656 Element = self.etree.Element 1657 SubElement = self.etree.SubElement 1658 1659 a = Element('a') 1660 b = SubElement(a, 'b') 1661 c = SubElement(a, 'c') 1662 1663 result = tounicode(a) 1664 self.assertEquals(result, "<a><b/><c/></a>") 1665 1666 result = tounicode(a, pretty_print=False) 1667 self.assertEquals(result, "<a><b/><c/></a>") 1668 1669 result = tounicode(a, pretty_print=True) 1670 self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>")
1671
1672 - def _writeElement(self, element, encoding='us-ascii'):
1673 """Write out element for comparison. 1674 """ 1675 ElementTree = self.etree.ElementTree 1676 f = StringIO() 1677 tree = ElementTree(element=element) 1678 tree.write(f, encoding) 1679 data = f.getvalue() 1680 return canonicalize(data)
1681 1682
1683 -class XIncludeTestCase(HelperTestCase):
1684 - def test_xinclude_text(self):
1685 filename = fileInTestDir('test_broken.xml') 1686 root = etree.XML('''\ 1687 <doc xmlns:xi="http://www.w3.org/2001/XInclude"> 1688 <xi:include href="%s" parse="text"/> 1689 </doc> 1690 ''' % filename) 1691 old_text = root.text 1692 content = open(filename).read() 1693 old_tail = root[0].tail 1694 1695 self.include( etree.ElementTree(root) ) 1696 self.assertEquals(old_text + content + old_tail, 1697 root.text)
1698
1699 - def test_xinclude(self):
1700 tree = etree.parse(fileInTestDir('include/test_xinclude.xml')) 1701 # process xincludes 1702 self.include( tree ) 1703 # check whether we find it replaced with included data 1704 self.assertEquals( 1705 'a', 1706 tree.getroot()[1].tag)
1707
1708 -class ETreeXIncludeTestCase(XIncludeTestCase):
1709 - def include(self, tree):
1710 tree.xinclude()
1711 1712
1713 -class ElementIncludeTestCase(XIncludeTestCase):
1714 from lxml import ElementInclude
1715 - def include(self, tree):
1716 self.ElementInclude.include(tree.getroot())
1717 1718
1719 -class ETreeC14NTestCase(HelperTestCase):
1720 - def test_c14n(self):
1721 tree = self.parse('<a><b/></a>') 1722 f = StringIO() 1723 tree.write_c14n(f) 1724 s = f.getvalue() 1725 self.assertEquals('<a><b></b></a>', 1726 s)
1727
1728 -def test_suite():
1729 suite = unittest.TestSuite() 1730 suite.addTests([unittest.makeSuite(ETreeOnlyTestCase)]) 1731 suite.addTests([unittest.makeSuite(ETreeXIncludeTestCase)]) 1732 suite.addTests([unittest.makeSuite(ElementIncludeTestCase)]) 1733 suite.addTests([unittest.makeSuite(ETreeC14NTestCase)]) 1734 suite.addTests( 1735 [doctest.DocFileSuite('../../../doc/tutorial.txt')]) 1736 suite.addTests( 1737 [doctest.DocFileSuite('../../../doc/api.txt')]) 1738 suite.addTests( 1739 [doctest.DocFileSuite('../../../doc/parsing.txt')]) 1740 suite.addTests( 1741 [doctest.DocFileSuite('../../../doc/resolvers.txt')]) 1742 return suite
1743 1744 if __name__ == '__main__': 1745 unittest.main() 1746