Package lxml :: Package tests :: Module test_etree
[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, 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      # Python 2.3 
30 - def sorted(seq):
31 seq = list(seq) 32 seq.sort() 33 return seq
34
35 -class ETreeOnlyTestCase(HelperTestCase):
36 """Tests only for etree, not ElementTree""" 37 etree = etree 38
39 - def test_version(self):
40 self.assert_(isinstance(etree.__version__, str)) 41 self.assert_(isinstance(etree.LXML_VERSION, tuple)) 42 self.assertEqual(len(etree.LXML_VERSION), 4) 43 self.assert_(isinstance(etree.LXML_VERSION[0], int)) 44 self.assert_(isinstance(etree.LXML_VERSION[1], int)) 45 self.assert_(isinstance(etree.LXML_VERSION[2], int)) 46 self.assert_(isinstance(etree.LXML_VERSION[3], int)) 47 self.assert_(etree.__version__.startswith( 48 str(etree.LXML_VERSION[0])))
49
50 - def test_c_api(self):
51 if hasattr(self.etree, '__pyx_capi__'): 52 # newer Pyrex compatible C-API 53 self.assert_(isinstance(self.etree.__pyx_capi__, dict)) 54 self.assert_(len(self.etree.__pyx_capi__) > 0) 55 else: 56 # older C-API mechanism 57 self.assert_(hasattr(self.etree, '_import_c_api'))
58
59 - def test_element_names(self):
60 Element = self.etree.Element 61 el = Element('name') 62 self.assertEquals(el.tag, 'name') 63 el = Element('{}name') 64 self.assertEquals(el.tag, 'name')
65
66 - def test_element_name_empty(self):
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
75 - def test_element_name_colon(self):
76 Element = self.etree.Element 77 self.assertRaises(ValueError, Element, 'p:name') 78 self.assertRaises(ValueError, Element, '{test}p:name') 79 80 el = Element('name') 81 self.assertRaises(ValueError, setattr, el, 'tag', 'p:name')
82
83 - def test_element_name_quote(self):
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
95 - def test_element_name_space(self):
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
104 - def test_subelement_name_empty(self):
105 Element = self.etree.Element 106 SubElement = self.etree.SubElement 107 108 el = Element('name') 109 self.assertRaises(ValueError, SubElement, el, '{}') 110 self.assertRaises(ValueError, SubElement, el, '{test}')
111
112 - def test_subelement_name_colon(self):
113 Element = self.etree.Element 114 SubElement = self.etree.SubElement 115 116 el = Element('name') 117 self.assertRaises(ValueError, SubElement, el, 'p:name') 118 self.assertRaises(ValueError, SubElement, el, '{test}p:name')
119
120 - def test_subelement_name_quote(self):
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
131 - def test_subelement_name_space(self):
132 Element = self.etree.Element 133 SubElement = self.etree.SubElement 134 135 el = Element('name') 136 self.assertRaises(ValueError, SubElement, el, ' name ') 137 self.assertRaises(ValueError, SubElement, el, 'na me') 138 self.assertRaises(ValueError, SubElement, el, '{test} name')
139
140 - def test_qname_empty(self):
141 QName = self.etree.QName 142 self.assertRaises(ValueError, QName, '') 143 self.assertRaises(ValueError, QName, 'test', '')
144
145 - def test_qname_colon(self):
146 QName = self.etree.QName 147 self.assertRaises(ValueError, QName, 'p:name') 148 self.assertRaises(ValueError, QName, 'test', 'p:name')
149
150 - def test_qname_space(self):
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
156 - def test_qname_text_resolve(self):
157 # ET doesn't resove QNames as text values 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
165 - def test_attribute_set(self):
166 Element = self.etree.Element 167 root = Element("root") 168 root.set("attr", "TEST") 169 self.assertEquals("TEST", root.get("attr"))
170
171 - def test_attribute_set_invalid(self):
172 # ElementTree accepts arbitrary attribute values 173 # lxml.etree allows only strings 174 Element = self.etree.Element 175 root = Element("root") 176 self.assertRaises(TypeError, root.set, "newattr", 5) 177 self.assertRaises(TypeError, root.set, "newattr", None)
178
179 - def test_attrib_pop(self):
180 ElementTree = self.etree.ElementTree 181 182 f = StringIO('<doc one="One" two="Two"/>') 183 doc = ElementTree(file=f) 184 root = doc.getroot() 185 self.assertEquals('One', root.attrib['one']) 186 self.assertEquals('Two', root.attrib['two']) 187 188 self.assertEquals('One', root.attrib.pop('one')) 189 190 self.assertEquals(None, root.attrib.get('one')) 191 self.assertEquals('Two', root.attrib['two'])
192
193 - def test_attrib_pop_unknown(self):
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
200 - def test_attrib_pop_default(self):
201 root = self.etree.XML('<doc one="One" two="Two"/>') 202 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
203
205 root = self.etree.XML('<doc/>') 206 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
207
209 root = self.etree.XML('<doc one="One" two="Two"/>') 210 self.assertRaises(TypeError, root.attrib.pop, 'One', None, None)
211
212 - def test_pi(self):
213 # lxml.etree separates target and text 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
223 - def test_pi_parse(self):
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
229 - def test_deepcopy_pi(self):
230 # previously caused a crash 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
240 - def test_deepcopy_comment(self):
241 # previously caused a crash 242 # not supported by ET! 243 Comment = self.etree.Comment 244 245 a = Comment("ONE") 246 b = copy.deepcopy(a) 247 b.text = "ANOTHER" 248 249 self.assertEquals('ONE', a.text) 250 self.assertEquals('ANOTHER', b.text)
251
252 - def test_attribute_set(self):
253 # ElementTree accepts arbitrary attribute values 254 # lxml.etree allows only strings 255 Element = self.etree.Element 256 257 root = Element("root") 258 root.set("attr", "TEST") 259 self.assertEquals("TEST", root.get("attr")) 260 self.assertRaises(TypeError, root.set, "newattr", 5)
261
262 - def test_parse_error(self):
263 # ET raises ExpatError 264 parse = self.etree.parse 265 # from StringIO 266 f = StringIO('<a><b></c></b></a>') 267 self.assertRaises(SyntaxError, parse, f) 268 f.close()
269
270 - def test_parse_remove_comments(self):
271 fromstring = self.etree.fromstring 272 tostring = self.etree.tostring 273 XMLParser = self.etree.XMLParser 274 275 xml = '<a><!--A--><b><!-- B --><c/></b><!--C--></a>' 276 parser = XMLParser(remove_comments=True) 277 root = fromstring(xml, parser) 278 self.assertEquals( 279 '<a><b><c/></b></a>', 280 tostring(root))
281
282 - def test_parse_remove_pis(self):
283 parse = self.etree.parse 284 tostring = self.etree.tostring 285 XMLParser = self.etree.XMLParser 286 287 xml = '<?test?><a><?A?><b><?B?><c/></b><?C?></a><?tail?>' 288 289 f = StringIO(xml) 290 tree = parse(f) 291 self.assertEquals( 292 xml, 293 tostring(tree)) 294 295 parser = XMLParser(remove_pis=True) 296 tree = parse(f, parser) 297 self.assertEquals( 298 '<a><b><c/></b></a>', 299 tostring(tree))
300
302 # ET raises IOError only 303 parse = self.etree.parse 304 self.assertRaises(TypeError, parse, 'notthere.xml', object())
305
306 - def test_parse_error_logging(self):
307 parse = self.etree.parse 308 # from StringIO 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
328 - def test_parse_error_from_file(self):
329 parse = self.etree.parse 330 # from file 331 f = open(fileInTestDir('test_broken.xml'), 'r') 332 self.assertRaises(SyntaxError, parse, f) 333 f.close()
334
335 - def test_iterparse_comments(self):
336 # ET removes comments 337 iterparse = self.etree.iterparse 338 tostring = self.etree.tostring 339 340 f = StringIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>') 341 events = list(iterparse(f)) 342 root = events[-1][1] 343 self.assertEquals(3, len(events)) 344 self.assertEquals( 345 '<a><!--A--><b><!-- B --><c/></b><!--C--></a>', 346 tostring(root))
347
349 iterparse = self.etree.iterparse 350 tostring = self.etree.tostring 351 352 f = StringIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>') 353 events = list(iterparse(f, remove_comments=True)) 354 root = events[-1][1] 355 self.assertEquals( 356 '<a><b><c/></b></a>', 357 tostring(root))
358
359 - def test_iterparse_broken(self):
360 iterparse = self.etree.iterparse 361 f = StringIO('<a><b><c/></a>') 362 # ET raises ExpatError, lxml raises XMLSyntaxError 363 self.assertRaises(self.etree.XMLSyntaxError, list, iterparse(f))
364
365 - def test_iterparse_strip(self):
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
378 - def test_iterparse_tag(self):
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
389 - def test_iterparse_tag_all(self):
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
416 self.assertRaises( 417 LookupError, self.etree.XMLParser, encoding="hopefully unknown")
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" # no Element! 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
442 - def test_parser_target_recover(self):
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
467 - def test_iterwalk_tag(self):
468 iterwalk = self.etree.iterwalk 469 root = self.etree.XML('<a><b><d/></b><c/></a>') 470 471 iterator = iterwalk(root, tag="b", events=('start', 'end')) 472 events = list(iterator) 473 self.assertEquals( 474 [('start', root[0]), ('end', root[0])], 475 events)
476
477 - def test_iterwalk_tag_all(self):
478 iterwalk = self.etree.iterwalk 479 root = self.etree.XML('<a><b><d/></b><c/></a>') 480 481 iterator = iterwalk(root, tag="*", events=('start', 'end')) 482 events = list(iterator) 483 self.assertEquals( 484 8, 485 len(events))
486
487 - def test_iterwalk(self):
488 iterwalk = self.etree.iterwalk 489 root = self.etree.XML('<a><b></b><c/></a>') 490 491 events = list(iterwalk(root)) 492 self.assertEquals( 493 [('end', root[0]), ('end', root[1]), ('end', root)], 494 events)
495
496 - def test_iterwalk_start(self):
497 iterwalk = self.etree.iterwalk 498 root = self.etree.XML('<a><b></b><c/></a>') 499 500 iterator = iterwalk(root, events=('start',)) 501 events = list(iterator) 502 self.assertEquals( 503 [('start', root), ('start', root[0]), ('start', root[1])], 504 events)
505
506 - def test_iterwalk_start_end(self):
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
517 - def test_iterwalk_clear(self):
518 iterwalk = self.etree.iterwalk 519 root = self.etree.XML('<a><b></b><c/></a>') 520 521 iterator = iterwalk(root) 522 for event, elem in iterator: 523 elem.clear() 524 525 self.assertEquals(0, 526 len(root))
527
528 - def test_iterwalk_attrib_ns(self):
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
553 - def test_iterwalk_getiterator(self):
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
564 - def test_resolve_string_dtd(self):
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
584 - def test_resolve_filename_dtd(self):
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
606 - def test_resolve_empty(self):
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
627 - def test_resolve_error(self):
628 parse = self.etree.parse 629 parser = self.etree.XMLParser(dtd_validation=True) 630 test_url = u"__nosuch.dtd" 631 632 class _LocalException(Exception): 633 pass
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):
645 - def test_entity_parse(self):
646 parse = self.etree.parse 647 tostring = self.etree.tostring 648 parser = self.etree.XMLParser(resolve_entities=False) 649 Entity = self.etree.Entity 650 651 xml = '<!DOCTYPE doc SYSTEM "test"><doc>&myentity;</doc>' 652 tree = parse(StringIO(xml), parser) 653 root = tree.getroot() 654 self.assertEquals(root[0].tag, Entity) 655 self.assertEquals(root[0].text, "&myentity;") 656 self.assertEquals(root[0].tail, None) 657 self.assertEquals(root[0].name, "myentity") 658 659 self.assertEquals('<doc>&myentity;</doc>', 660 tostring(root))
661
662 - def test_entity_append(self):
663 Entity = self.etree.Entity 664 Element = self.etree.Element 665 tostring = self.etree.tostring 666 667 root = Element("root") 668 root.append( Entity("test") ) 669 670 self.assertEquals(root[0].tag, Entity) 671 self.assertEquals(root[0].text, "&test;") 672 self.assertEquals(root[0].tail, None) 673 self.assertEquals(root[0].name, "test") 674 675 self.assertEquals('<root>&test;</root>', 676 tostring(root))
677
678 - def test_entity_values(self):
679 Entity = self.etree.Entity 680 self.assertEquals(Entity("test").text, '&test;') 681 self.assertEquals(Entity("#17683").text, '&#17683;') 682 self.assertEquals(Entity("#x1768").text, '&#x1768;') 683 self.assertEquals(Entity("#x98AF").text, '&#x98AF;')
684
685 - def test_entity_error(self):
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 # TypeError in etree, AssertionError in ElementTree;
694 - def test_setitem_assert(self):
695 Element = self.etree.Element 696 SubElement = self.etree.SubElement 697 698 a = Element('a') 699 b = SubElement(a, 'b') 700 701 self.assertRaises(TypeError, 702 a.__setitem__, 0, 'foo')
703
704 - def test_append_None(self):
705 # raises AssertionError in ElementTree 706 Element = self.etree.Element 707 self.assertRaises(TypeError, Element('a').append, None)
708
709 - def test_addnext(self):
710 Element = self.etree.Element 711 SubElement = self.etree.SubElement 712 root = Element('root') 713 SubElement(root, 'a') 714 SubElement(root, 'b') 715 716 self.assertEquals(['a', 'b'], 717 [c.tag for c in root]) 718 root[1].addnext(root[0]) 719 self.assertEquals(['b', 'a'], 720 [c.tag for c in root])
721
722 - def test_addprevious(self):
723 Element = self.etree.Element 724 SubElement = self.etree.SubElement 725 root = Element('root') 726 SubElement(root, 'a') 727 SubElement(root, 'b') 728 729 self.assertEquals(['a', 'b'], 730 [c.tag for c in root]) 731 root[0].addprevious(root[1]) 732 self.assertEquals(['b', 'a'], 733 [c.tag for c in root])
734
735 - def test_addnext_root(self):
736 Element = self.etree.Element 737 a = Element('a') 738 b = Element('b') 739 self.assertRaises(TypeError, a.addnext, b)
740
741 - def test_addnext_root(self):
742 Element = self.etree.Element 743 a = Element('a') 744 b = Element('b') 745 self.assertRaises(TypeError, a.addnext, b)
746
747 - def test_addprevious_pi(self):
748 Element = self.etree.Element 749 SubElement = self.etree.SubElement 750 PI = self.etree.PI 751 root = Element('root') 752 SubElement(root, 'a') 753 pi = PI('TARGET', 'TEXT') 754 pi.tail = "TAIL" 755 756 self.assertEquals('<root><a></a></root>', 757 self._writeElement(root)) 758 root[0].addprevious(pi) 759 self.assertEquals('<root><?TARGET TEXT?>TAIL<a></a></root>', 760 self._writeElement(root))
761
762 - def test_addprevious_root_pi(self):
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
775 - def test_addnext_pi(self):
776 Element = self.etree.Element 777 SubElement = self.etree.SubElement 778 PI = self.etree.PI 779 root = Element('root') 780 SubElement(root, 'a') 781 pi = PI('TARGET', 'TEXT') 782 pi.tail = "TAIL" 783 784 self.assertEquals('<root><a></a></root>', 785 self._writeElement(root)) 786 root[0].addnext(pi) 787 self.assertEquals('<root><a></a><?TARGET TEXT?>TAIL</root>', 788 self._writeElement(root))
789
790 - def test_addnext_root_pi(self):
791 Element = self.etree.Element 792 PI = self.etree.PI 793 root = Element('root') 794 pi = PI('TARGET', 'TEXT') 795 pi.tail = "TAIL" 796 797 self.assertEquals('<root></root>', 798 self._writeElement(root)) 799 root.addnext(pi) 800 self.assertEquals('<root></root>\n<?TARGET TEXT?>', 801 self._writeElement(root))
802
803 - def test_addnext_comment(self):
804 Element = self.etree.Element 805 SubElement = self.etree.SubElement 806 Comment = self.etree.Comment 807 root = Element('root') 808 SubElement(root, 'a') 809 comment = Comment('TEXT ') 810 comment.tail = "TAIL" 811 812 self.assertEquals('<root><a></a></root>', 813 self._writeElement(root)) 814 root[0].addnext(comment) 815 self.assertEquals('<root><a></a><!--TEXT -->TAIL</root>', 816 self._writeElement(root))
817
818 - def test_addnext_root_comment(self):
819 Element = self.etree.Element 820 Comment = self.etree.Comment 821 root = Element('root') 822 comment = Comment('TEXT ') 823 comment.tail = "TAIL" 824 825 self.assertEquals('<root></root>', 826 self._writeElement(root)) 827 root.addnext(comment) 828 self.assertEquals('<root></root>\n<!--TEXT -->', 829 self._writeElement(root))
830
831 - def test_addprevious_comment(self):
832 Element = self.etree.Element 833 SubElement = self.etree.SubElement 834 Comment = self.etree.Comment 835 root = Element('root') 836 SubElement(root, 'a') 837 comment = Comment('TEXT ') 838 comment.tail = "TAIL" 839 840 self.assertEquals('<root><a></a></root>', 841 self._writeElement(root)) 842 root[0].addprevious(comment) 843 self.assertEquals('<root><!--TEXT -->TAIL<a></a></root>', 844 self._writeElement(root))
845
846 - def test_addprevious_root_comment(self):
847 Element = self.etree.Element 848 Comment = self.etree.Comment 849 root = Element('root') 850 comment = Comment('TEXT ') 851 comment.tail = "TAIL" 852 853 self.assertEquals('<root></root>', 854 self._writeElement(root)) 855 root.addprevious(comment) 856 self.assertEquals('<!--TEXT -->\n<root></root>', 857 self._writeElement(root))
858 859 # ET's Elements have items() and key(), but not values()
860 - def test_attribute_values(self):
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 # gives error in ElementTree
869 - def test_comment_empty(self):
870 Element = self.etree.Element 871 Comment = self.etree.Comment 872 873 a = Element('a') 874 a.append(Comment()) 875 self.assertEquals( 876 '<a><!----></a>', 877 self._writeElement(a))
878 879 # ElementTree ignores comments
880 - def test_comment_parse_empty(self):
881 ElementTree = self.etree.ElementTree 882 tostring = self.etree.tostring 883 884 xml = '<a><b/><!----><c/></a>' 885 f = StringIO(xml) 886 doc = ElementTree(file=f) 887 a = doc.getroot() 888 self.assertEquals( 889 '', 890 a[1].text) 891 self.assertEquals( 892 xml, 893 tostring(a))
894 895 # ElementTree ignores comments
896 - def test_comment_no_proxy_yet(self):
897 ElementTree = self.etree.ElementTree 898 899 f = StringIO('<a><b></b><!-- hoi --><c></c></a>') 900 doc = ElementTree(file=f) 901 a = doc.getroot() 902 self.assertEquals( 903 ' hoi ', 904 a[1].text)
905 906 # ElementTree adds whitespace around comments
907 - def test_comment_text(self):
908 Element = self.etree.Element 909 Comment = self.etree.Comment 910 tostring = self.etree.tostring 911 912 a = Element('a') 913 a.append(Comment('foo')) 914 self.assertEquals( 915 '<a><!--foo--></a>', 916 tostring(a)) 917 918 a[0].text = "TEST" 919 self.assertEquals( 920 '<a><!--TEST--></a>', 921 tostring(a))
922 923 # ElementTree adds whitespace around comments
924 - def test_comment_whitespace(self):
925 Element = self.etree.Element 926 Comment = self.etree.Comment 927 tostring = self.etree.tostring 928 929 a = Element('a') 930 a.append(Comment(' foo ')) 931 self.assertEquals( 932 '<a><!-- foo --></a>', 933 tostring(a))
934 935 # does not raise an exception in ElementTree
936 - def test_comment_immutable(self):
937 Element = self.etree.Element 938 Comment = self.etree.Comment 939 940 c = Comment() 941 el = Element('myel') 942 943 self.assertRaises(TypeError, c.append, el) 944 self.assertRaises(TypeError, c.insert, 0, el) 945 self.assertRaises(TypeError, c.set, "myattr", "test")
946 947 # test weird dictionary interaction leading to segfault previously
948 - def test_weird_dict_interaction(self):
949 root = self.etree.Element('root') 950 add = self.etree.ElementTree(file=StringIO('<foo>Foo</foo>')) 951 root.append(self.etree.Element('baz'))
952 953 # test passing 'None' to dump
954 - def test_dump_none(self):
955 self.assertRaises(TypeError, etree.dump, None)
956
957 - def test_prefix(self):
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
970 - def test_prefix_default_ns(self):
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
983 - def test_getparent(self):
984 Element = self.etree.Element 985 SubElement = self.etree.SubElement 986 987 a = Element('a') 988 b = SubElement(a, 'b') 989 c = SubElement(a, 'c') 990 d = SubElement(b, 'd') 991 self.assertEquals( 992 None, 993 a.getparent()) 994 self.assertEquals( 995 a, 996 b.getparent()) 997 self.assertEquals( 998 b.getparent(), 999 c.getparent()) 1000 self.assertEquals( 1001 b, 1002 d.getparent())
1003
1004 - def test_iterchildren(self):
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
1013 - def test_iterchildren_reversed(self):
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
1022 - def test_iterchildren_tag(self):
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
1031 - def test_iterchildren_tag_reversed(self):
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
1040 - def test_iterancestors(self):
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
1061 - def test_iterancestors_tag(self):
1062 Element = self.etree.Element 1063 SubElement = self.etree.SubElement 1064 1065 a = Element('a') 1066 b = SubElement(a, 'b') 1067 c = SubElement(a, 'c') 1068 d = SubElement(b, 'd') 1069 self.assertEquals( 1070 [a], 1071 list(d.iterancestors(tag='a')))
1072
1073 - def test_iterdescendants(self):
1074 Element = self.etree.Element 1075 SubElement = self.etree.SubElement 1076 1077 a = Element('a') 1078 b = SubElement(a, 'b') 1079 c = SubElement(a, 'c') 1080 d = SubElement(b, 'd') 1081 e = SubElement(c, 'e') 1082 1083 self.assertEquals( 1084 [b, d, c, e], 1085 list(a.iterdescendants())) 1086 self.assertEquals( 1087 [], 1088 list(d.iterdescendants()))
1089
1090 - def test_iterdescendants_tag(self):
1091 Element = self.etree.Element 1092 SubElement = self.etree.SubElement 1093 1094 a = Element('a') 1095 b = SubElement(a, 'b') 1096 c = SubElement(a, 'c') 1097 d = SubElement(b, 'd') 1098 e = SubElement(c, 'e') 1099 1100 self.assertEquals( 1101 [], 1102 list(a.iterdescendants('a'))) 1103 a2 = SubElement(e, 'a') 1104 self.assertEquals( 1105 [a2], 1106 list(a.iterdescendants('a'))) 1107 self.assertEquals( 1108 [a2], 1109 list(c.iterdescendants('a')))
1110
1111 - def test_getroottree(self):
1112 Element = self.etree.Element 1113 SubElement = self.etree.SubElement 1114 1115 a = Element('a') 1116 b = SubElement(a, 'b') 1117 c = SubElement(a, 'c') 1118 d = SubElement(b, 'd') 1119 self.assertEquals( 1120 a, 1121 a.getroottree().getroot()) 1122 self.assertEquals( 1123 a, 1124 b.getroottree().getroot()) 1125 self.assertEquals( 1126 a, 1127 d.getroottree().getroot())
1128
1129 - def test_getnext(self):
1130 Element = self.etree.Element 1131 SubElement = self.etree.SubElement 1132 1133 a = Element('a') 1134 b = SubElement(a, 'b') 1135 c = SubElement(a, 'c') 1136 self.assertEquals( 1137 None, 1138 a.getnext()) 1139 self.assertEquals( 1140 c, 1141 b.getnext()) 1142 self.assertEquals( 1143 None, 1144 c.getnext())
1145
1146 - def test_getprevious(self):
1147 Element = self.etree.Element 1148 SubElement = self.etree.SubElement 1149 1150 a = Element('a') 1151 b = SubElement(a, 'b') 1152 c = SubElement(a, 'c') 1153 d = SubElement(b, 'd') 1154 self.assertEquals( 1155 None, 1156 a.getprevious()) 1157 self.assertEquals( 1158 b, 1159 c.getprevious()) 1160 self.assertEquals( 1161 None, 1162 b.getprevious())
1163
1164 - def test_itersiblings(self):
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
1191 - def test_itersiblings_tag(self):
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
1212 - def test_parseid(self):
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
1247 - def test_XMLDTDID(self):
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
1281 - def test_XMLDTDID_empty(self):
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
1300 - def _checkIDDict(self, dic, expected):
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
1317 - def test_namespaces(self):
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
1329 - def test_namespaces_default(self):
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
1344 - def test_namespaces_default_and_attr(self):
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
1355 - def test_namespaces_elementtree(self):
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
1366 - def test_namespaces_default_copy_element(self):
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
1388 - def test_namespaces_copy_element(self):
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
1410 - def test_namespaces_reuse_after_move(self):
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 # make sure the source document is deallocated 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
1426 - def test_namespaces_after_serialize(self):
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
1438 - def test_element_nsmap(self):
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
1448 - def test_subelement_nsmap(self):
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
1468 - def test_getiterator_filter_namespace(self):
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
1495 - def test_getiterator_filter_entities(self):
1496 Element = self.etree.Element 1497 Entity = self.etree.Entity 1498 SubElement = self.etree.SubElement 1499 1500 a = Element('a') 1501 b = SubElement(a, 'b') 1502 entity_b = Entity("TEST-b") 1503 b.append(entity_b) 1504 1505 self.assertEquals( 1506 [entity_b], 1507 list(a.getiterator(Entity))) 1508 1509 entity_a = Entity("TEST-a") 1510 a.append(entity_a) 1511 1512 self.assertEquals( 1513 [entity_b, entity_a], 1514 list(a.getiterator(Entity))) 1515 1516 self.assertEquals( 1517 [entity_b], 1518 list(b.getiterator(Entity)))
1519
1520 - def test_getiterator_filter_element(self):
1521 Element = self.etree.Element 1522 Comment = self.etree.Comment 1523 PI = self.etree.PI 1524 SubElement = self.etree.SubElement 1525 1526 a = Element('a') 1527 b = SubElement(a, 'b') 1528 a.append(Comment("test")) 1529 a.append(PI("pi", "content")) 1530 c = SubElement(a, 'c') 1531 1532 self.assertEquals( 1533 [a, b, c], 1534 list(a.getiterator(Element)))
1535
1536 - def test_getiterator_filter_all_comment_pi(self):
1537 # ElementTree iterates over everything here 1538 Element = self.etree.Element 1539 Comment = self.etree.Comment 1540 PI = self.etree.PI 1541 SubElement = self.etree.SubElement 1542 1543 a = Element('a') 1544 b = SubElement(a, 'b') 1545 a.append(Comment("test")) 1546 a.append(PI("pi", "content")) 1547 c = SubElement(a, 'c') 1548 1549 self.assertEquals( 1550 [a, b, c], 1551 list(a.getiterator('*')))
1552
1553 - def test_itertext(self):
1554 # ET 1.3+ 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
1562 - def test_itertext_child(self):
1563 # ET 1.3+ 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
1571 - def test_elementtree_find_qname(self):
1572 XML = self.etree.XML 1573 ElementTree = self.etree.ElementTree 1574 QName = self.etree.QName 1575 tree = ElementTree(XML('<a><b><c/></b><b/><c><b/></c></a>')) 1576 self.assertEquals(tree.find(QName("c")), tree.getroot()[2])
1577
1578 - def test_elementtree_findall_qname(self):
1579 XML = self.etree.XML 1580 ElementTree = self.etree.ElementTree 1581 QName = self.etree.QName 1582 tree = ElementTree(XML('<a><b><c/></b><b/><c><b/></c></a>')) 1583 self.assertEquals(len(list(tree.findall(QName("c")))), 1)
1584
1585 - def test_elementtree_findall_ns_qname(self):
1586 XML = self.etree.XML 1587 ElementTree = self.etree.ElementTree 1588 QName = self.etree.QName 1589 tree = ElementTree(XML( 1590 '<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')) 1591 self.assertEquals(len(list(tree.findall(QName("b")))), 2) 1592 self.assertEquals(len(list(tree.findall(QName("X", "b")))), 1)
1593
1594 - def test_findall_ns(self):
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
1601 - def test_index(self):
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
1627 - def test_replace(self):
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
1663 - def test_replace_new(self):
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
1685 - def test_setslice_all_empty_reversed(self):
1686 Element = self.etree.Element 1687 SubElement = self.etree.SubElement 1688 1689 a = Element('a') 1690 1691 e = Element('e') 1692 f = Element('f') 1693 g = Element('g') 1694 1695 s = [e, f, g] 1696 a[::-1] = s 1697 self.assertEquals( 1698 [g, f, e], 1699 list(a))
1700
1701 - def test_setslice_step(self):
1702 Element = self.etree.Element 1703 SubElement = self.etree.SubElement 1704 1705 a = Element('a') 1706 b = SubElement(a, 'b') 1707 c = SubElement(a, 'c') 1708 d = SubElement(a, 'd') 1709 e = SubElement(a, 'e') 1710 1711 x = Element('x') 1712 y = Element('y') 1713 1714 a[1::2] = [x, y] 1715 self.assertEquals( 1716 [b, x, d, y], 1717 list(a))
1718
1719 - def test_setslice_step_negative(self):
1720 Element = self.etree.Element 1721 SubElement = self.etree.SubElement 1722 1723 a = Element('a') 1724 b = SubElement(a, 'b') 1725 c = SubElement(a, 'c') 1726 d = SubElement(a, 'd') 1727 e = SubElement(a, 'e') 1728 1729 x = Element('x') 1730 y = Element('y') 1731 1732 a[1::-1] = [x, y] 1733 self.assertEquals( 1734 [y, x, d, e], 1735 list(a))
1736
1737 - def test_setslice_step_negative2(self):
1738 Element = self.etree.Element 1739 SubElement = self.etree.SubElement 1740 1741 a = Element('a') 1742 b = SubElement(a, 'b') 1743 c = SubElement(a, 'c') 1744 d = SubElement(a, 'd') 1745 e = SubElement(a, 'e') 1746 1747 x = Element('x') 1748 y = Element('y') 1749 1750 a[::-2] = [x, y] 1751 self.assertEquals( 1752 [b, y, d, x], 1753 list(a))
1754
1755 - def test_setslice_step_overrun(self):
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
1782 - def test_extend(self):
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
1809 - def test_sourceline_XML(self):
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
1822 - def test_sourceline_parse(self):
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
1830 - def test_sourceline_iterparse_end(self):
1831 iterparse = self.etree.iterparse 1832 lines = [ el.sourceline for (event, el) in 1833 iterparse(fileInTestDir('include/test_xinclude.xml')) ] 1834 1835 self.assertEquals( 1836 [2, 3, 1], 1837 lines)
1838
1839 - def test_sourceline_iterparse_start(self):
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
1849 - def test_sourceline_element(self):
1850 Element = self.etree.Element 1851 SubElement = self.etree.SubElement 1852 el = Element("test") 1853 self.assertEquals(None, el.sourceline) 1854 1855 child = SubElement(el, "test") 1856 self.assertEquals(None, el.sourceline) 1857 self.assertEquals(None, child.sourceline)
1858
1859 - def test_XML_base_url_docinfo(self):
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
1865 - def test_parse_stringio_base_url(self):
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
1871 - def test_parse_base_url_docinfo(self):
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
1878 - def test_HTML_base_url_docinfo(self):
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
1884 - def test_docinfo_public(self):
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
1902 - def test_docinfo_system(self):
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
1918 - def test_docinfo_empty(self):
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
1930 - def test_dtd_io(self):
1931 # check that DTDs that go in also go back out 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
1944 - def test_byte_zero(self):
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
1953 - def test_unicode_byte_zero(self):
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
1962 - def test_byte_invalid(self):
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
1975 - def test_unicode_byte_invalid(self):
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
1988 - def test_encoding_tostring_utf16(self):
1989 # ElementTree fails to serialize this 1990 tostring = self.etree.tostring 1991 Element = self.etree.Element 1992 SubElement = self.etree.SubElement 1993 1994 a = Element('a') 1995 b = SubElement(a, 'b') 1996 c = SubElement(a, 'c') 1997 1998 result = unicode(tostring(a, encoding='UTF-16'), 'UTF-16') 1999 self.assertEquals('<a><b></b><c></c></a>', 2000 canonicalize(result))
2001
2002 - def test_tostring_none(self):
2003 # ElementTree raises an AssertionError here 2004 tostring = self.etree.tostring 2005 self.assertRaises(TypeError, self.etree.tostring, None)
2006
2007 - def test_tostring_pretty(self):
2008 tostring = self.etree.tostring 2009 Element = self.etree.Element 2010 SubElement = self.etree.SubElement 2011 2012 a = Element('a') 2013 b = SubElement(a, 'b') 2014 c = SubElement(a, 'c') 2015 2016 result = tostring(a) 2017 self.assertEquals(result, "<a><b/><c/></a>") 2018 2019 result = tostring(a, pretty_print=False) 2020 self.assertEquals(result, "<a><b/><c/></a>") 2021 2022 result = tostring(a, pretty_print=True) 2023 self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>\n")
2024
2025 - def test_tostring_with_tail(self):
2026 tostring = self.etree.tostring 2027 Element = self.etree.Element 2028 SubElement = self.etree.SubElement 2029 2030 a = Element('a') 2031 a.tail = "aTAIL" 2032 b = SubElement(a, 'b') 2033 b.tail = "bTAIL" 2034 c = SubElement(a, 'c') 2035 2036 result = tostring(a) 2037 self.assertEquals(result, "<a><b/>bTAIL<c/></a>aTAIL") 2038 2039 result = tostring(a, with_tail=False) 2040 self.assertEquals(result, "<a><b/>bTAIL<c/></a>") 2041 2042 result = tostring(a, with_tail=True) 2043 self.assertEquals(result, "<a><b/>bTAIL<c/></a>aTAIL")
2044
2045 - def test_tostring_method_text_encoding(self):
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
2064 - def test_tounicode(self):
2065 tounicode = self.etree.tounicode 2066 Element = self.etree.Element 2067 SubElement = self.etree.SubElement 2068 2069 a = Element('a') 2070 b = SubElement(a, 'b') 2071 c = SubElement(a, 'c') 2072 2073 self.assert_(isinstance(tounicode(a), unicode)) 2074 self.assertEquals('<a><b></b><c></c></a>', 2075 canonicalize(tounicode(a)))
2076
2077 - def test_tounicode_element(self):
2078 tounicode = self.etree.tounicode 2079 Element = self.etree.Element 2080 SubElement = self.etree.SubElement 2081 2082 a = Element('a') 2083 b = SubElement(a, 'b') 2084 c = SubElement(a, 'c') 2085 d = SubElement(c, 'd') 2086 self.assert_(isinstance(tounicode(b), unicode)) 2087 self.assert_(isinstance(tounicode(c), unicode)) 2088 self.assertEquals('<b></b>', 2089 canonicalize(tounicode(b))) 2090 self.assertEquals('<c><d></d></c>', 2091 canonicalize(tounicode(c)))
2092
2093 - def test_tounicode_none(self):
2094 tounicode = self.etree.tounicode 2095 self.assertRaises(TypeError, self.etree.tounicode, None)
2096
2097 - def test_tounicode_element_tail(self):
2098 tounicode = self.etree.tounicode 2099 Element = self.etree.Element 2100 SubElement = self.etree.SubElement 2101 2102 a = Element('a') 2103 b = SubElement(a, 'b') 2104 c = SubElement(a, 'c') 2105 d = SubElement(c, 'd') 2106 b.tail = 'Foo' 2107 2108 self.assert_(isinstance(tounicode(b), unicode)) 2109 self.assert_(tounicode(b) == '<b/>Foo' or 2110 tounicode(b) == '<b />Foo')
2111
2112 - def test_tounicode_pretty(self):
2113 tounicode = self.etree.tounicode 2114 Element = self.etree.Element 2115 SubElement = self.etree.SubElement 2116 2117 a = Element('a') 2118 b = SubElement(a, 'b') 2119 c = SubElement(a, 'c') 2120 2121 result = tounicode(a) 2122 self.assertEquals(result, "<a><b/><c/></a>") 2123 2124 result = tounicode(a, pretty_print=False) 2125 self.assertEquals(result, "<a><b/><c/></a>") 2126 2127 result = tounicode(a, pretty_print=True) 2128 self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>\n")
2129
2130 - def test_tostring_unicode(self):
2131 tostring = self.etree.tostring 2132 Element = self.etree.Element 2133 SubElement = self.etree.SubElement 2134 2135 a = Element('a') 2136 b = SubElement(a, 'b') 2137 c = SubElement(a, 'c') 2138 2139 self.assert_(isinstance(tostring(a, encoding=unicode), unicode)) 2140 self.assertEquals('<a><b></b><c></c></a>', 2141 canonicalize(tostring(a, encoding=unicode)))
2142
2143 - def test_tostring_unicode_element(self):
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
2159 - def test_tostring_unicode_none(self):
2160 tostring = self.etree.tostring 2161 self.assertRaises(TypeError, self.etree.tostring, 2162 None, encoding=unicode)
2163
2164 - def test_tostring_unicode_element_tail(self):
2165 tostring = self.etree.tostring 2166 Element = self.etree.Element 2167 SubElement = self.etree.SubElement 2168 2169 a = Element('a') 2170 b = SubElement(a, 'b') 2171 c = SubElement(a, 'c') 2172 d = SubElement(c, 'd') 2173 b.tail = 'Foo' 2174 2175 self.assert_(isinstance(tostring(b, encoding=unicode), unicode)) 2176 self.assert_(tostring(b, encoding=unicode) == '<b/>Foo' or 2177 tostring(b, encoding=unicode) == '<b />Foo')
2178
2179 - def test_tostring_unicode_pretty(self):
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 # helper methods 2198
2199 - def _writeElement(self, element, encoding='us-ascii'):
2200 """Write out element for comparison. 2201 """ 2202 ElementTree = self.etree.ElementTree 2203 f = StringIO() 2204 tree = ElementTree(element=element) 2205 tree.write(f, encoding=encoding) 2206 data = f.getvalue() 2207 return canonicalize(data)
2208 2209
2210 -class XIncludeTestCase(HelperTestCase):
2211 - def test_xinclude_text(self):
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
2226 - def test_xinclude(self):
2227 tree = etree.parse(fileInTestDir('include/test_xinclude.xml')) 2228 self.assertNotEquals( 2229 'a', 2230 tree.getroot()[1].tag) 2231 # process xincludes 2232 self.include( tree ) 2233 # check whether we find it replaced with included data 2234 self.assertEquals( 2235 'a', 2236 tree.getroot()[1].tag)
2237
2238 -class ETreeXIncludeTestCase(XIncludeTestCase):
2239 - def include(self, tree):
2240 tree.xinclude()
2241 2242
2243 -class ElementIncludeTestCase(XIncludeTestCase):
2244 from lxml import ElementInclude
2245 - def include(self, tree):
2246 self.ElementInclude.include(tree.getroot())
2247 2248
2249 -class ETreeC14NTestCase(HelperTestCase):
2250 - def test_c14n(self):
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
2258 -def test_suite():
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