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

Source Code for Module lxml.tests.test_elementtree

   1  # -*- coding: utf-8 -*- 
   2   
   3  """ 
   4  Tests for the ElementTree API 
   5   
   6  Only test cases that apply equally well to etree and ElementTree 
   7  belong here. Note that there is a second test module called test_io.py 
   8  for IO related test cases. 
   9  """ 
  10   
  11  import unittest, doctest 
  12  import os, re, shutil, tempfile, copy 
  13   
  14  from common_imports import StringIO, etree, ElementTree 
  15  from common_imports import HelperTestCase, fileInTestDir, canonicalize 
  16   
17 -class ETreeTestCaseBase(HelperTestCase):
18 etree = None 19
20 - def setUp(self):
21 super(ETreeTestCaseBase, self).setUp() 22 self._temp_dir = tempfile.mkdtemp()
23
24 - def tearDown(self):
25 shutil.rmtree(self._temp_dir) 26 super(ETreeTestCaseBase, self).tearDown()
27
28 - def getTestFilePath(self, name):
29 return os.path.join(self._temp_dir, name)
30
31 - def test_element(self):
32 for i in range(10): 33 e = self.etree.Element('foo')
34
35 - def test_tree(self):
36 Element = self.etree.Element 37 ElementTree = self.etree.ElementTree 38 39 element = Element('top') 40 tree = ElementTree(element) 41 self.buildNodes(element, 10, 3) 42 f = open(self.getTestFilePath('testdump.xml'), 'w') 43 tree.write(f, 'UTF-8') 44 f.close() 45 f = open(self.getTestFilePath('testdump.xml'), 'r') 46 tree = ElementTree(file=f) 47 f.close() 48 f = open(self.getTestFilePath('testdump2.xml'), 'w') 49 tree.write(f, 'UTF-8') 50 f.close() 51 f = open(self.getTestFilePath('testdump.xml'), 'r') 52 data1 = f.read() 53 f.close() 54 f = open(self.getTestFilePath('testdump2.xml'), 'r') 55 data2 = f.read() 56 f.close() 57 self.assertEquals(data1, data2)
58
59 - def buildNodes(self, element, children, depth):
60 Element = self.etree.Element 61 62 if depth == 0: 63 return 64 for i in range(children): 65 new_element = Element('element_%s_%s' % (depth, i)) 66 self.buildNodes(new_element, children, depth - 1) 67 element.append(new_element)
68
69 - def test_simple(self):
70 Element = self.etree.Element 71 72 root = Element('root') 73 root.append(Element('one')) 74 root.append(Element('two')) 75 root.append(Element('three')) 76 self.assertEquals(3, len(root)) 77 self.assertEquals('one', root[0].tag) 78 self.assertEquals('two', root[1].tag) 79 self.assertEquals('three', root[2].tag) 80 self.assertRaises(IndexError, root.__getitem__, 3)
81
82 - def test_subelement(self):
83 Element = self.etree.Element 84 SubElement = self.etree.SubElement 85 86 root = Element('root') 87 SubElement(root, 'one') 88 SubElement(root, 'two') 89 SubElement(root, 'three') 90 self.assertEquals(3, len(root)) 91 self.assertEquals('one', root[0].tag) 92 self.assertEquals('two', root[1].tag) 93 self.assertEquals('three', root[2].tag)
94
95 - def test_element_contains(self):
96 Element = self.etree.Element 97 SubElement = self.etree.SubElement 98 99 root1 = Element('root') 100 SubElement(root1, 'one') 101 self.assert_(root1[0] in root1) 102 103 root2 = Element('root') 104 SubElement(root2, 'two') 105 SubElement(root2, 'three') 106 self.assert_(root2[0] in root2) 107 self.assert_(root2[1] in root2) 108 109 self.assertFalse(root1[0] in root2) 110 self.assertFalse(root2[0] in root1) 111 self.assertFalse(None in root2)
112
114 ElementTree = self.etree.ElementTree 115 116 f = StringIO('<doc>Test<one>One</one></doc>') 117 doc = ElementTree(file=f) 118 root = doc.getroot() 119 self.assertEquals(1, len(root)) 120 self.assertEquals('one', root[0].tag) 121 self.assertRaises(IndexError, root.__getitem__, 1)
122
124 ElementTree = self.etree.ElementTree 125 126 f = StringIO('<doc><one>One</one><two>Two</two>hm<three>Three</three></doc>') 127 doc = ElementTree(file=f) 128 root = doc.getroot() 129 self.assertEquals(3, len(root)) 130 self.assertEquals('one', root[0].tag) 131 self.assertEquals('two', root[1].tag) 132 self.assertEquals('three', root[2].tag)
133
135 ElementTree = self.etree.ElementTree 136 137 f = StringIO('<doc>Test</doc>') 138 doc = ElementTree(file=f) 139 root = doc.getroot() 140 self.assertEquals(0, len(root))
141
143 Element = self.etree.Element 144 SubElement = self.etree.SubElement 145 a = Element('a') 146 b = SubElement(a, 'b') 147 c = SubElement(a, 'c') 148 d = SubElement(a, 'd') 149 self.assertEquals(d, a[-1]) 150 self.assertEquals(c, a[-2]) 151 self.assertEquals(b, a[-3]) 152 self.assertRaises(IndexError, a.__getitem__, -4) 153 a[-1] = e = Element('e') 154 self.assertEquals(e, a[-1]) 155 del a[-1] 156 self.assertEquals(2, len(a))
157
158 - def test_elementtree(self):
159 ElementTree = self.etree.ElementTree 160 161 f = StringIO('<doc><one>One</one><two>Two</two></doc>') 162 doc = ElementTree(file=f) 163 root = doc.getroot() 164 self.assertEquals(2, len(root)) 165 self.assertEquals('one', root[0].tag) 166 self.assertEquals('two', root[1].tag)
167
168 - def test_text(self):
169 ElementTree = self.etree.ElementTree 170 171 f = StringIO('<doc>This is a text</doc>') 172 doc = ElementTree(file=f) 173 root = doc.getroot() 174 self.assertEquals('This is a text', root.text)
175
176 - def test_text_empty(self):
177 ElementTree = self.etree.ElementTree 178 179 f = StringIO('<doc></doc>') 180 doc = ElementTree(file=f) 181 root = doc.getroot() 182 self.assertEquals(None, root.text)
183
184 - def test_text_other(self):
185 ElementTree = self.etree.ElementTree 186 187 f = StringIO('<doc><one>One</one></doc>') 188 doc = ElementTree(file=f) 189 root = doc.getroot() 190 self.assertEquals(None, root.text) 191 self.assertEquals('One', root[0].text)
192
193 - def test_text_escape_in(self):
194 ElementTree = self.etree.ElementTree 195 196 f = StringIO('<doc>This is &gt; than a text</doc>') 197 doc = ElementTree(file=f) 198 root = doc.getroot() 199 self.assertEquals('This is > than a text', root.text)
200
201 - def test_text_escape_out(self):
202 Element = self.etree.Element 203 204 a = Element("a") 205 a.text = "<>&" 206 self.assertXML('<a>&lt;&gt;&amp;</a>', 207 a)
208
209 - def test_text_escape_tostring(self):
210 tostring = self.etree.tostring 211 Element = self.etree.Element 212 213 a = Element("a") 214 a.text = "<>&" 215 self.assertEquals('<a>&lt;&gt;&amp;</a>', 216 tostring(a))
217
218 - def test_text_str_subclass(self):
219 Element = self.etree.Element 220 221 class strTest(str): 222 pass
223 224 a = Element("a") 225 a.text = strTest("text") 226 self.assertXML('<a>text</a>', 227 a)
228
229 - def test_tail(self):
230 ElementTree = self.etree.ElementTree 231 232 f = StringIO('<doc>This is <i>mixed</i> content.</doc>') 233 doc = ElementTree(file=f) 234 root = doc.getroot() 235 self.assertEquals(1, len(root)) 236 self.assertEquals('This is ', root.text) 237 self.assertEquals(None, root.tail) 238 self.assertEquals('mixed', root[0].text) 239 self.assertEquals(' content.', root[0].tail)
240
241 - def test_tail_str_subclass(self):
242 Element = self.etree.Element 243 SubElement = self.etree.SubElement 244 245 class strTest(str): 246 pass
247 248 a = Element("a") 249 SubElement(a, "t").tail = strTest("tail") 250 self.assertXML('<a><t></t>tail</a>', 251 a) 252
253 - def test_ElementTree(self):
254 Element = self.etree.Element 255 ElementTree = self.etree.ElementTree 256 257 el = Element('hoi') 258 doc = ElementTree(el) 259 root = doc.getroot() 260 self.assertEquals(None, root.text) 261 self.assertEquals('hoi', root.tag)
262
263 - def test_attributes(self):
264 ElementTree = self.etree.ElementTree 265 266 f = StringIO('<doc one="One" two="Two"/>') 267 doc = ElementTree(file=f) 268 root = doc.getroot() 269 self.assertEquals('One', root.attrib['one']) 270 self.assertEquals('Two', root.attrib['two']) 271 self.assertRaises(KeyError, root.attrib.__getitem__, 'three')
272
273 - def test_attributes2(self):
274 ElementTree = self.etree.ElementTree 275 276 f = StringIO('<doc one="One" two="Two"/>') 277 doc = ElementTree(file=f) 278 root = doc.getroot() 279 self.assertEquals('One', root.attrib.get('one')) 280 self.assertEquals('Two', root.attrib.get('two')) 281 self.assertEquals(None, root.attrib.get('three')) 282 self.assertEquals('foo', root.attrib.get('three', 'foo'))
283
284 - def test_attributes3(self):
285 ElementTree = self.etree.ElementTree 286 287 f = StringIO('<doc one="One" two="Two"/>') 288 doc = ElementTree(file=f) 289 root = doc.getroot() 290 self.assertEquals('One', root.get('one')) 291 self.assertEquals('Two', root.get('two')) 292 self.assertEquals(None, root.get('three')) 293 self.assertEquals('foo', root.get('three', 'foo'))
294
295 - def test_attrib_clear(self):
296 XML = self.etree.XML 297 298 root = XML('<doc one="One" two="Two"/>') 299 self.assertEquals('One', root.get('one')) 300 self.assertEquals('Two', root.get('two')) 301 root.attrib.clear() 302 self.assertEquals(None, root.get('one')) 303 self.assertEquals(None, root.get('two'))
304
305 - def test_attrib_set_clear(self):
306 Element = self.etree.Element 307 308 root = Element("root", one="One") 309 root.set("two", "Two") 310 self.assertEquals('One', root.get('one')) 311 self.assertEquals('Two', root.get('two')) 312 root.attrib.clear() 313 self.assertEquals(None, root.get('one')) 314 self.assertEquals(None, root.get('two'))
315
316 - def test_attribute_update_dict(self):
317 XML = self.etree.XML 318 319 root = XML('<doc alpha="Alpha" beta="Beta"/>') 320 items = root.attrib.items() 321 items.sort() 322 self.assertEquals( 323 [('alpha', 'Alpha'), ('beta', 'Beta')], 324 items) 325 326 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}) 327 328 items = root.attrib.items() 329 items.sort() 330 self.assertEquals( 331 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 332 items)
333
334 - def test_attribute_update_sequence(self):
335 XML = self.etree.XML 336 337 root = XML('<doc alpha="Alpha" beta="Beta"/>') 338 items = root.attrib.items() 339 items.sort() 340 self.assertEquals( 341 [('alpha', 'Alpha'), ('beta', 'Beta')], 342 items) 343 344 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}.items()) 345 346 items = root.attrib.items() 347 items.sort() 348 self.assertEquals( 349 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 350 items)
351
352 - def test_attribute_update_iter(self):
353 XML = self.etree.XML 354 355 root = XML('<doc alpha="Alpha" beta="Beta"/>') 356 items = root.attrib.items() 357 items.sort() 358 self.assertEquals( 359 [('alpha', 'Alpha'), ('beta', 'Beta')], 360 items) 361 362 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}.iteritems()) 363 364 items = root.attrib.items() 365 items.sort() 366 self.assertEquals( 367 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 368 items)
369
370 - def test_attribute_keys(self):
371 XML = self.etree.XML 372 373 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>') 374 keys = root.attrib.keys() 375 keys.sort() 376 self.assertEquals(['alpha', 'beta', 'gamma'], keys)
377
378 - def test_attribute_keys2(self):
379 XML = self.etree.XML 380 381 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>') 382 keys = root.keys() 383 keys.sort() 384 self.assertEquals(['alpha', 'beta', 'gamma'], keys)
385
386 - def test_attribute_items2(self):
387 XML = self.etree.XML 388 389 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>') 390 items = root.items() 391 items.sort() 392 self.assertEquals( 393 [('alpha','Alpha'), ('beta','Beta'), ('gamma','Gamma')], 394 items)
395
396 - def test_attribute_keys_ns(self):
397 XML = self.etree.XML 398 399 root = XML('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />') 400 keys = root.keys() 401 keys.sort() 402 self.assertEquals(['bar', '{http://ns.codespeak.net/test}baz'], 403 keys)
404
405 - def test_attribute_values(self):
406 XML = self.etree.XML 407 408 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>') 409 values = root.attrib.values() 410 values.sort() 411 self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)
412
413 - def test_attribute_values_ns(self):
414 XML = self.etree.XML 415 416 root = XML('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />') 417 values = root.attrib.values() 418 values.sort() 419 self.assertEquals( 420 ['Bar', 'Baz'], values)
421
422 - def test_attribute_items(self):
423 XML = self.etree.XML 424 425 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>') 426 items = root.attrib.items() 427 items.sort() 428 self.assertEquals([ 429 ('alpha', 'Alpha'), 430 ('beta', 'Beta'), 431 ('gamma', 'Gamma'), 432 ], 433 items)
434
435 - def test_attribute_items_ns(self):
436 XML = self.etree.XML 437 438 root = XML('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />') 439 items = root.attrib.items() 440 items.sort() 441 self.assertEquals( 442 [('bar', 'Bar'), ('{http://ns.codespeak.net/test}baz', 'Baz')], 443 items)
444
445 - def test_attribute_str(self):
446 XML = self.etree.XML 447 448 root = XML('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />') 449 # XXX hope this is not dependent on unpredictable attribute order 450 self.assertEquals( 451 "{'{http://ns.codespeak.net/test}baz': 'Baz', 'bar': 'Bar'}", 452 str(root.attrib))
453
454 - def test_attribute_has_key(self):
455 XML = self.etree.XML 456 457 root = XML('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />') 458 self.assertEquals( 459 True, root.attrib.has_key('bar')) 460 self.assertEquals( 461 False, root.attrib.has_key('baz')) 462 self.assertEquals( 463 False, root.attrib.has_key('hah')) 464 self.assertEquals( 465 True, 466 root.attrib.has_key('{http://ns.codespeak.net/test}baz'))
467
468 - def test_attribute_contains(self):
469 XML = self.etree.XML 470 471 root = XML('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />') 472 self.assertEquals( 473 True, 'bar' in root.attrib) 474 self.assertEquals( 475 False, 'baz' in root.attrib) 476 self.assertEquals( 477 False, 'hah' in root.attrib) 478 self.assertEquals( 479 True, 480 '{http://ns.codespeak.net/test}baz' in root.attrib)
481
482 - def test_attribute_set(self):
483 Element = self.etree.Element 484 485 root = Element("root") 486 root.set("attr", "TEST") 487 self.assertEquals("TEST", root.get("attr"))
488
489 - def test_XML(self):
490 XML = self.etree.XML 491 492 root = XML('<doc>This is a text.</doc>') 493 self.assertEquals(0, len(root)) 494 self.assertEquals('This is a text.', root.text)
495
496 - def test_XMLID(self):
497 XMLID = self.etree.XMLID 498 XML = self.etree.XML 499 xml_text = ''' 500 <document> 501 <h1 id="chapter1">...</h1> 502 <p id="note1" class="note">...</p> 503 <p>Regular paragraph.</p> 504 <p xml:id="xmlid">XML:ID paragraph.</p> 505 <p id="warn1" class="warning">...</p> 506 </document> 507 ''' 508 509 root, dic = XMLID(xml_text) 510 root2 = XML(xml_text) 511 self.assertEquals(self._writeElement(root), 512 self._writeElement(root2)) 513 expected = { 514 "chapter1" : root[0], 515 "note1" : root[1], 516 "warn1" : root[4] 517 } 518 self.assertEquals(dic, expected)
519
520 - def test_fromstring(self):
521 fromstring = self.etree.fromstring 522 523 root = fromstring('<doc>This is a text.</doc>') 524 self.assertEquals(0, len(root)) 525 self.assertEquals('This is a text.', root.text)
526
527 - def test_iselement(self):
528 iselement = self.etree.iselement 529 Element = self.etree.Element 530 ElementTree = self.etree.ElementTree 531 XML = self.etree.XML 532 Comment = self.etree.Comment 533 ProcessingInstruction = self.etree.ProcessingInstruction 534 535 el = Element('hoi') 536 self.assert_(iselement(el)) 537 538 el2 = XML('<foo/>') 539 self.assert_(iselement(el2)) 540 541 tree = ElementTree(element=Element('dag')) 542 self.assert_(not iselement(tree)) 543 self.assert_(iselement(tree.getroot())) 544 545 c = Comment('test') 546 self.assert_(iselement(c)) 547 548 p = ProcessingInstruction("test", "some text") 549 self.assert_(iselement(p))
550
551 - def test_iteration(self):
552 XML = self.etree.XML 553 554 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>') 555 result = [] 556 for el in root: 557 result.append(el.tag) 558 self.assertEquals(['one', 'two', 'three'], result)
559
560 - def test_iteration_empty(self):
561 XML = self.etree.XML 562 563 root = XML('<doc></doc>') 564 result = [] 565 for el in root: 566 result.append(el.tag) 567 self.assertEquals([], result)
568
569 - def test_iteration_text_only(self):
570 XML = self.etree.XML 571 572 root = XML('<doc>Text</doc>') 573 result = [] 574 for el in root: 575 result.append(el.tag) 576 self.assertEquals([], result)
577
578 - def test_iteration_crash(self):
579 # this would cause a crash in the past 580 fromstring = self.etree.fromstring 581 root = etree.fromstring('<html><p></p>x</html>') 582 for elem in root: 583 elem.tail = ''
584
585 - def test_iteration_reversed(self):
586 XML = self.etree.XML 587 588 try: 589 reversed(()) 590 except NameError: 591 # before Python 2.4 592 return 593 594 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>') 595 result = [] 596 for el in reversed(root): 597 result.append(el.tag) 598 self.assertEquals(['three', 'two', 'one'], result)
599
600 - def test_iteration_subelement(self):
601 XML = self.etree.XML 602 603 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>') 604 result = [] 605 add = True 606 for el in root: 607 result.append(el.tag) 608 if add: 609 self.etree.SubElement(root, 'four') 610 add = False 611 self.assertEquals(['one', 'two', 'three', 'four'], result)
612
613 - def test_iteration_del_child(self):
614 XML = self.etree.XML 615 616 root = XML('<doc><one/><two>Two</two>Hm<three/></doc>') 617 result = [] 618 for el in root: 619 result.append(el.tag) 620 del root[-1] 621 self.assertEquals(['one', 'two'], result)
622
623 - def test_iteration_double(self):
624 XML = self.etree.XML 625 626 root = XML('<doc><one/><two/></doc>') 627 result = [] 628 for el0 in root: 629 result.append(el0.tag) 630 for el1 in root: 631 result.append(el1.tag) 632 self.assertEquals(['one','one', 'two', 'two', 'one', 'two'], result)
633
634 - def test_attribute_iterator(self):
635 XML = self.etree.XML 636 637 root = XML('<doc alpha="Alpha" beta="Beta" gamma="Gamma" />') 638 result = [] 639 for key in root.attrib: 640 result.append(key) 641 result.sort() 642 self.assertEquals(['alpha', 'beta', 'gamma'], result)
643
644 - def test_findall(self):
645 XML = self.etree.XML 646 root = XML('<a><b><c/></b><b/><c><b/></c></a>') 647 self.assertEquals(len(root.findall("c")), 1) 648 self.assertEquals(len(root.findall(".//c")), 2) 649 self.assertEquals(len(root.findall(".//b")), 3) 650 self.assertEquals(len(root.findall(".//b")[0]), 1) 651 self.assertEquals(len(root.findall(".//b")[1]), 0) 652 self.assertEquals(len(root.findall(".//b")[2]), 0)
653
654 - def test_findall_ns(self):
655 XML = self.etree.XML 656 root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>') 657 self.assertEquals(len(root.findall(".//{X}b")), 2) 658 self.assertEquals(len(root.findall(".//b")), 3) 659 self.assertEquals(len(root.findall("b")), 2)
660
661 - def test_element_with_attributes_keywords(self):
662 Element = self.etree.Element 663 664 el = Element('tag', foo='Foo', bar='Bar') 665 self.assertEquals('Foo', el.attrib['foo']) 666 self.assertEquals('Bar', el.attrib['bar'])
667
668 - def test_element_with_attributes(self):
669 Element = self.etree.Element 670 671 el = Element('tag', {'foo':'Foo', 'bar':'Bar'}) 672 self.assertEquals('Foo', el.attrib['foo']) 673 self.assertEquals('Bar', el.attrib['bar'])
674
675 - def test_element_with_attributes_ns(self):
676 Element = self.etree.Element 677 678 el = Element('tag', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 679 self.assertEquals('Foo', el.attrib['{ns1}foo']) 680 self.assertEquals('Bar', el.attrib['{ns2}bar'])
681
682 - def test_subelement_with_attributes(self):
683 Element = self.etree.Element 684 SubElement = self.etree.SubElement 685 686 el = Element('tag') 687 SubElement(el, 'foo', attrib={'foo':'Foo'}, baz="Baz") 688 self.assertEquals("Baz", el[0].attrib['baz']) 689 self.assertEquals('Foo', el[0].attrib['foo'])
690
691 - def test_subelement_with_attributes_ns(self):
692 Element = self.etree.Element 693 SubElement = self.etree.SubElement 694 695 el = Element('tag') 696 SubElement(el, 'foo', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 697 self.assertEquals('Foo', el[0].attrib['{ns1}foo']) 698 self.assertEquals('Bar', el[0].attrib['{ns2}bar'])
699
700 - def test_write(self):
701 ElementTree = self.etree.ElementTree 702 XML = self.etree.XML 703 704 for i in range(10): 705 f = StringIO() 706 root = XML('<doc%s>This is a test.</doc%s>' % (i, i)) 707 tree = ElementTree(element=root) 708 tree.write(f) 709 data = f.getvalue() 710 self.assertEquals( 711 '<doc%s>This is a test.</doc%s>' % (i, i), 712 canonicalize(data))
713
714 - def test_write_fail(self):
715 ElementTree = self.etree.ElementTree 716 XML = self.etree.XML 717 718 tree = ElementTree( XML('<doc>This is a test.</doc>') ) 719 self.assertRaises(IOError, tree.write, 720 "definitely////\\-\\nonexisting\\-\\////FILE")
721 722 # this could trigger a crash, apparently because the document 723 # reference was prematurely garbage collected
724 - def test_crash(self):
725 Element = self.etree.Element 726 727 element = Element('tag') 728 for i in range(10): 729 element.attrib['key'] = 'value' 730 value = element.attrib['key'] 731 self.assertEquals(value, 'value')
732 733 # from doctest; for some reason this caused crashes too
734 - def test_write_ElementTreeDoctest(self):
735 Element = self.etree.Element 736 ElementTree = self.etree.ElementTree 737 738 f = StringIO() 739 for i in range(10): 740 element = Element('tag%s' % i) 741 self._check_element(element) 742 tree = ElementTree(element) 743 tree.write(f) 744 self._check_element_tree(tree)
745
746 - def test_subelement_reference(self):
747 Element = self.etree.Element 748 SubElement = self.etree.SubElement 749 750 el = Element('foo') 751 el2 = SubElement(el, 'bar') 752 el3 = SubElement(el2, 'baz') 753 754 al = Element('foo2') 755 al2 = SubElement(al, 'bar2') 756 al3 = SubElement(al2, 'baz2') 757 758 # now move al2 into el 759 el.append(al2) 760 761 # now change al3 directly 762 al3.text = 'baz2-modified' 763 764 # it should have changed through this route too 765 self.assertEquals( 766 'baz2-modified', 767 el[1][0].text)
768
769 - def test_set_text(self):
770 Element = self.etree.Element 771 SubElement = self.etree.SubElement 772 773 a = Element('a') 774 b = SubElement(a, 'b') 775 a.text = 'hoi' 776 self.assertEquals( 777 'hoi', 778 a.text) 779 self.assertEquals( 780 'b', 781 a[0].tag)
782
783 - def test_set_text2(self):
784 Element = self.etree.Element 785 SubElement = self.etree.SubElement 786 787 a = Element('a') 788 a.text = 'hoi' 789 b = SubElement(a ,'b') 790 self.assertEquals( 791 'hoi', 792 a.text) 793 self.assertEquals( 794 'b', 795 a[0].tag)
796
797 - def test_set_text_none(self):
798 Element = self.etree.Element 799 800 a = Element('a') 801 802 a.text = 'foo' 803 a.text = None 804 805 self.assertEquals( 806 None, 807 a.text) 808 self.assertXML('<a></a>', a)
809
810 - def test_set_text_empty(self):
811 Element = self.etree.Element 812 813 a = Element('a') 814 self.assertEquals(None, a.text) 815 816 a.text = '' 817 self.assertEquals('', a.text) 818 self.assertXML('<a></a>', a)
819
820 - def test_tail1(self):
821 Element = self.etree.Element 822 SubElement = self.etree.SubElement 823 824 a = Element('a') 825 a.tail = 'dag' 826 self.assertEquals('dag', 827 a.tail) 828 b = SubElement(a, 'b') 829 b.tail = 'hoi' 830 self.assertEquals('hoi', 831 b.tail) 832 self.assertEquals('dag', 833 a.tail)
834
835 - def test_tail_append(self):
836 Element = self.etree.Element 837 838 a = Element('a') 839 b = Element('b') 840 b.tail = 'b_tail' 841 a.append(b) 842 self.assertEquals('b_tail', 843 b.tail)
844
845 - def test_tail_set_twice(self):
846 Element = self.etree.Element 847 SubElement = self.etree.SubElement 848 849 a = Element('a') 850 b = SubElement(a, 'b') 851 b.tail = 'foo' 852 b.tail = 'bar' 853 self.assertEquals('bar', 854 b.tail) 855 self.assertXML('<a><b></b>bar</a>', a)
856
857 - def test_tail_set_none(self):
858 Element = self.etree.Element 859 a = Element('a') 860 a.tail = 'foo' 861 a.tail = None 862 self.assertEquals( 863 None, 864 a.tail) 865 self.assertXML('<a></a>', a)
866
867 - def test_comment(self):
868 Element = self.etree.Element 869 SubElement = self.etree.SubElement 870 Comment = self.etree.Comment 871 872 a = Element('a') 873 a.append(Comment('foo')) 874 self.assertEquals(a[0].tag, Comment) 875 self.assertEquals(a[0].text, 'foo')
876
877 - def test_comment_text(self):
878 Element = self.etree.Element 879 SubElement = self.etree.SubElement 880 Comment = self.etree.Comment 881 882 a = Element('a') 883 a.append(Comment('foo')) 884 self.assertEquals(a[0].text, 'foo') 885 886 a[0].text = "TEST" 887 self.assertEquals(a[0].text, 'TEST')
888
889 - def test_comment_whitespace(self):
890 Element = self.etree.Element 891 SubElement = self.etree.SubElement 892 Comment = self.etree.Comment 893 894 a = Element('a') 895 a.append(Comment(' foo ')) 896 self.assertEquals(a[0].text, ' foo ')
897
898 - def test_comment_nonsense(self):
899 Comment = self.etree.Comment 900 c = Comment('foo') 901 self.assertEquals({}, c.attrib) 902 self.assertEquals([], c.keys()) 903 self.assertEquals([], c.items()) 904 self.assertEquals(None, c.get('hoi')) 905 self.assertEquals(0, len(c)) 906 # should not iterate 907 for i in c: 908 pass
909
910 - def test_pi(self):
911 # lxml.etree separates target and text 912 Element = self.etree.Element 913 SubElement = self.etree.SubElement 914 ProcessingInstruction = self.etree.ProcessingInstruction 915 916 a = Element('a') 917 a.append(ProcessingInstruction('foo', 'some more text')) 918 self.assertEquals(a[0].tag, ProcessingInstruction) 919 self.assertXML("<a><?foo some more text?></a>", 920 a)
921
922 - def test_pi_nonsense(self):
923 ProcessingInstruction = self.etree.ProcessingInstruction 924 pi = ProcessingInstruction('foo') 925 self.assertEquals({}, pi.attrib) 926 self.assertEquals([], pi.keys()) 927 self.assertEquals([], pi.items()) 928 self.assertEquals(None, pi.get('hoi')) 929 self.assertEquals(0, len(pi)) 930 # should not iterate 931 for i in pi: 932 pass
933
934 - def test_setitem(self):
935 Element = self.etree.Element 936 SubElement = self.etree.SubElement 937 938 a = Element('a') 939 b = SubElement(a, 'b') 940 c = Element('c') 941 a[0] = c 942 self.assertEquals( 943 c, 944 a[0]) 945 self.assertXML('<a><c></c></a>', 946 a) 947 self.assertXML('<b></b>', 948 b)
949
950 - def test_setitem2(self):
951 Element = self.etree.Element 952 SubElement = self.etree.SubElement 953 954 a = Element('a') 955 for i in range(5): 956 b = SubElement(a, 'b%s' % i) 957 c = SubElement(b, 'c') 958 for i in range(5): 959 d = Element('d') 960 e = SubElement(d, 'e') 961 a[i] = d 962 self.assertXML( 963 '<a><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d></a>', 964 a) 965 self.assertXML('<c></c>', 966 c)
967
968 - def test_setitem_replace(self):
969 Element = self.etree.Element 970 SubElement = self.etree.SubElement 971 972 a = Element('a') 973 SubElement(a, 'b') 974 d = Element('d') 975 a[0] = d 976 self.assertXML('<a><d></d></a>', a)
977
978 - def test_setitem_indexerror(self):
979 Element = self.etree.Element 980 SubElement = self.etree.SubElement 981 982 a = Element('a') 983 b = SubElement(a, 'b') 984 985 self.assertRaises(IndexError, a.__setitem__, 1, Element('c'))
986
987 - def test_setitem_tail(self):
988 Element = self.etree.Element 989 SubElement = self.etree.SubElement 990 991 a = Element('a') 992 b = SubElement(a, 'b') 993 b.tail = 'B2' 994 c = Element('c') 995 c.tail = 'C2' 996 997 a[0] = c 998 self.assertXML( 999 '<a><c></c>C2</a>', 1000 a)
1001
1002 - def test_tag_write(self):
1003 Element = self.etree.Element 1004 SubElement = self.etree.SubElement 1005 1006 a = Element('a') 1007 b = SubElement(a, 'b') 1008 1009 a.tag = 'c' 1010 1011 self.assertEquals( 1012 'c', 1013 a.tag) 1014 1015 self.assertXML( 1016 '<c><b></b></c>', 1017 a)
1018
1019 - def test_tag_reset_ns(self):
1020 Element = self.etree.Element 1021 SubElement = self.etree.SubElement 1022 tostring = self.etree.tostring 1023 1024 a = Element('{a}a') 1025 b1 = SubElement(a, '{a}b') 1026 b2 = SubElement(a, '{b}b') 1027 1028 self.assertEquals('{a}b', b1.tag) 1029 1030 b1.tag = 'c' 1031 1032 # can't use C14N here! 1033 self.assertEquals('c', b1.tag) 1034 self.assertEquals('<c', tostring(b1)[:2]) 1035 self.assert_('<c' in tostring(a))
1036
1037 - def test_tag_reset_root_ns(self):
1038 Element = self.etree.Element 1039 SubElement = self.etree.SubElement 1040 tostring = self.etree.tostring 1041 1042 a = Element('{a}a') 1043 b1 = SubElement(a, '{a}b') 1044 b2 = SubElement(a, '{b}b') 1045 1046 a.tag = 'c' 1047 1048 self.assertEquals( 1049 'c', 1050 a.tag) 1051 1052 # can't use C14N here! 1053 self.assertEquals('c', a.tag) 1054 self.assertEquals('<c', tostring(a)[:2])
1055
1056 - def test_tag_str_subclass(self):
1057 Element = self.etree.Element 1058 1059 class strTest(str): 1060 pass
1061 1062 a = Element("a") 1063 a.tag = strTest("TAG") 1064 self.assertXML('<TAG></TAG>', 1065 a) 1066
1067 - def test_delitem(self):
1068 Element = self.etree.Element 1069 SubElement = self.etree.SubElement 1070 1071 a = Element('a') 1072 b = SubElement(a, 'b') 1073 c = SubElement(a, 'c') 1074 d = SubElement(a, 'd') 1075 1076 del a[1] 1077 self.assertXML( 1078 '<a><b></b><d></d></a>', 1079 a) 1080 1081 del a[0] 1082 self.assertXML( 1083 '<a><d></d></a>', 1084 a) 1085 1086 del a[0] 1087 self.assertXML( 1088 '<a></a>', 1089 a) 1090 # move deleted element into other tree afterwards 1091 other = Element('other') 1092 other.append(c) 1093 self.assertXML( 1094 '<other><c></c></other>', 1095 other)
1096
1097 - def test_del_insert(self):
1098 Element = self.etree.Element 1099 SubElement = self.etree.SubElement 1100 1101 a = Element('a') 1102 b = SubElement(a, 'b') 1103 bs = SubElement(b, 'bs') 1104 c = SubElement(a, 'c') 1105 cs = SubElement(c, 'cs') 1106 1107 el = a[0] 1108 self.assertXML( 1109 '<a><b><bs></bs></b><c><cs></cs></c></a>', 1110 a) 1111 self.assertXML('<b><bs></bs></b>', b) 1112 self.assertXML('<c><cs></cs></c>', c) 1113 1114 del a[0] 1115 self.assertXML( 1116 '<a><c><cs></cs></c></a>', 1117 a) 1118 self.assertXML('<b><bs></bs></b>', b) 1119 self.assertXML('<c><cs></cs></c>', c) 1120 1121 a.insert(0, el) 1122 self.assertXML( 1123 '<a><b><bs></bs></b><c><cs></cs></c></a>', 1124 a) 1125 self.assertXML('<b><bs></bs></b>', b) 1126 self.assertXML('<c><cs></cs></c>', c)
1127
1128 - def test_del_setitem(self):
1129 Element = self.etree.Element 1130 SubElement = self.etree.SubElement 1131 1132 a = Element('a') 1133 b = SubElement(a, 'b') 1134 bs = SubElement(b, 'bs') 1135 c = SubElement(a, 'c') 1136 cs = SubElement(c, 'cs') 1137 1138 el = a[0] 1139 del a[0] 1140 a[0] = el 1141 self.assertXML( 1142 '<a><b><bs></bs></b></a>', 1143 a) 1144 self.assertXML('<b><bs></bs></b>', b) 1145 self.assertXML('<c><cs></cs></c>', c)
1146
1147 - def test_del_setslice(self):
1148 Element = self.etree.Element 1149 SubElement = self.etree.SubElement 1150 1151 a = Element('a') 1152 b = SubElement(a, 'b') 1153 bs = SubElement(b, 'bs') 1154 c = SubElement(a, 'c') 1155 cs = SubElement(c, 'cs') 1156 1157 el = a[0] 1158 del a[0] 1159 a[0:0] = [el] 1160 self.assertXML( 1161 '<a><b><bs></bs></b><c><cs></cs></c></a>', 1162 a) 1163 self.assertXML('<b><bs></bs></b>', b) 1164 self.assertXML('<c><cs></cs></c>', c)
1165
1166 - def test_delslice_tail(self):
1167 XML = self.etree.XML 1168 a = XML('<a><b></b>B2<c></c>C2</a>') 1169 b, c = a 1170 1171 del a[:] 1172 1173 self.assertEquals("B2", b.tail) 1174 self.assertEquals("C2", c.tail)
1175
1176 - def test_replace_slice_tail(self):
1177 XML = self.etree.XML 1178 a = XML('<a><b></b>B2<c></c>C2</a>') 1179 b, c = a 1180 1181 a[:] = [] 1182 1183 self.assertEquals("B2", b.tail) 1184 self.assertEquals("C2", c.tail)
1185
1186 - def test_delitem_tail(self):
1187 ElementTree = self.etree.ElementTree 1188 f = StringIO('<a><b></b>B2<c></c>C2</a>') 1189 doc = ElementTree(file=f) 1190 a = doc.getroot() 1191 del a[0] 1192 self.assertXML( 1193 '<a><c></c>C2</a>', 1194 a)
1195
1196 - def test_clear(self):
1197 Element = self.etree.Element 1198 1199 a = Element('a') 1200 a.text = 'foo' 1201 a.tail = 'bar' 1202 a.set('hoi', 'dag') 1203 a.clear() 1204 self.assertEquals(None, a.text) 1205 self.assertEquals(None, a.tail) 1206 self.assertEquals(None, a.get('hoi')) 1207 self.assertEquals('a', a.tag)
1208
1209 - def test_clear_sub(self):
1210 Element = self.etree.Element 1211 SubElement = self.etree.SubElement 1212 1213 a = Element('a') 1214 a.text = 'foo' 1215 a.tail = 'bar' 1216 a.set('hoi', 'dag') 1217 b = SubElement(a, 'b') 1218 c = SubElement(b, 'c') 1219 a.clear() 1220 self.assertEquals(None, a.text) 1221 self.assertEquals(None, a.tail) 1222 self.assertEquals(None, a.get('hoi')) 1223 self.assertEquals('a', a.tag) 1224 self.assertEquals(0, len(a)) 1225 self.assertXML('<a></a>', 1226 a) 1227 self.assertXML('<b><c></c></b>', 1228 b)
1229
1230 - def test_clear_tail(self):
1231 ElementTree = self.etree.ElementTree 1232 f = StringIO('<a><b></b>B2<c></c>C2</a>') 1233 doc = ElementTree(file=f) 1234 a = doc.getroot() 1235 a.clear() 1236 self.assertXML( 1237 '<a></a>', 1238 a)
1239
1240 - def test_insert(self):
1241 Element = self.etree.Element 1242 SubElement = self.etree.SubElement 1243 1244 a = Element('a') 1245 b = SubElement(a, 'b') 1246 c = SubElement(a, 'c') 1247 d = Element('d') 1248 a.insert(0, d) 1249 1250 self.assertEquals( 1251 d, 1252 a[0]) 1253 1254 self.assertXML( 1255 '<a><d></d><b></b><c></c></a>', 1256 a) 1257 1258 e = Element('e') 1259 a.insert(2, e) 1260 self.assertEquals( 1261 e, 1262 a[2]) 1263 self.assertXML( 1264 '<a><d></d><b></b><e></e><c></c></a>', 1265 a)
1266
1267 - def test_insert_beyond_index(self):
1268 Element = self.etree.Element 1269 SubElement = self.etree.SubElement 1270 1271 a = Element('a') 1272 b = SubElement(a, 'b') 1273 c = Element('c') 1274 1275 a.insert(2, c) 1276 self.assertEquals( 1277 c, 1278 a[1]) 1279 self.assertXML( 1280 '<a><b></b><c></c></a>', 1281 a)
1282
1283 - def test_insert_negative(self):
1284 Element = self.etree.Element 1285 SubElement = self.etree.SubElement 1286 1287 a = Element('a') 1288 b = SubElement(a, 'b') 1289 c = SubElement(a, 'c') 1290 1291 d = Element('d') 1292 a.insert(-1, d) 1293 self.assertEquals( 1294 d, 1295 a[-2]) 1296 self.assertXML( 1297 '<a><b></b><d></d><c></c></a>', 1298 a)
1299
1300 - def test_insert_tail(self):
1301 Element = self.etree.Element 1302 SubElement = self.etree.SubElement 1303 1304 a = Element('a') 1305 b = SubElement(a, 'b') 1306 1307 c = Element('c') 1308 c.tail = 'C2' 1309 1310 a.insert(0, c) 1311 self.assertXML( 1312 '<a><c></c>C2<b></b></a>', 1313 a)
1314
1315 - def test_remove(self):
1316 Element = self.etree.Element 1317 SubElement = self.etree.SubElement 1318 1319 a = Element('a') 1320 b = SubElement(a, 'b') 1321 c = SubElement(a, 'c') 1322 1323 a.remove(b) 1324 self.assertEquals( 1325 c, 1326 a[0]) 1327 self.assertXML( 1328 '<a><c></c></a>', 1329 a)
1330
1331 - def test_remove_ns(self):
1332 Element = self.etree.Element 1333 SubElement = self.etree.SubElement 1334 1335 a = Element('{http://test}a') 1336 b = SubElement(a, '{http://test}b') 1337 c = SubElement(a, '{http://test}c') 1338 1339 a.remove(b) 1340 self.assertXML( 1341 '<ns0:a xmlns:ns0="http://test"><ns0:c></ns0:c></ns0:a>', 1342 a) 1343 self.assertXML( 1344 '<ns0:b xmlns:ns0="http://test"></ns0:b>', 1345 b)
1346
1347 - def test_remove_nonexisting(self):
1348 Element = self.etree.Element 1349 SubElement = self.etree.SubElement 1350 1351 a = Element('a') 1352 b = SubElement(a, 'b') 1353 c = SubElement(a, 'c') 1354 d = Element('d') 1355 self.assertRaises( 1356 ValueError, a.remove, d)
1357
1358 - def test_remove_tail(self):
1359 Element = self.etree.Element 1360 SubElement = self.etree.SubElement 1361 1362 a = Element('a') 1363 b = SubElement(a, 'b') 1364 b.tail = 'b2' 1365 a.remove(b) 1366 self.assertXML( 1367 '<a></a>', 1368 a) 1369 self.assertEquals('b2', b.tail)
1370
1371 - def test_getchildren(self):
1372 Element = self.etree.Element 1373 SubElement = self.etree.SubElement 1374 1375 a = Element('a') 1376 b = SubElement(a, 'b') 1377 c = SubElement(a, 'c') 1378 d = SubElement(b, 'd') 1379 e = SubElement(c, 'e') 1380 self.assertXML( 1381 '<a><b><d></d></b><c><e></e></c></a>', 1382 a) 1383 self.assertEquals( 1384 [b, c], 1385 a.getchildren()) 1386 self.assertEquals( 1387 [d], 1388 b.getchildren()) 1389 self.assertEquals( 1390 [], 1391 d.getchildren())
1392
1393 - def test_makeelement(self):
1394 Element = self.etree.Element 1395 1396 a = Element('a') 1397 b = a.makeelement('c', {'hoi':'dag'}) 1398 self.assertXML( 1399 '<c hoi="dag"></c>', 1400 b)
1401
1402 - def test_getiterator(self):
1403 Element = self.etree.Element 1404 SubElement = self.etree.SubElement 1405 1406 a = Element('a') 1407 b = SubElement(a, 'b') 1408 c = SubElement(a, 'c') 1409 d = SubElement(b, 'd') 1410 e = SubElement(c, 'e') 1411 1412 self.assertEquals( 1413 [a, b, d, c, e], 1414 list(a.getiterator())) 1415 self.assertEquals( 1416 [d], 1417 list(d.getiterator()))
1418
1419 - def test_getiterator_empty(self):
1420 Element = self.etree.Element 1421 SubElement = self.etree.SubElement 1422 1423 a = Element('a') 1424 b = SubElement(a, 'b') 1425 c = SubElement(a, 'c') 1426 d = SubElement(b, 'd') 1427 e = SubElement(c, 'e') 1428 1429 self.assertEquals( 1430 [], 1431 list(a.getiterator('none'))) 1432 self.assertEquals( 1433 [], 1434 list(e.getiterator('none'))) 1435 self.assertEquals( 1436 [e], 1437 list(e.getiterator()))
1438
1439 - def test_getiterator_filter(self):
1440 Element = self.etree.Element 1441 SubElement = self.etree.SubElement 1442 1443 a = Element('a') 1444 b = SubElement(a, 'b') 1445 c = SubElement(a, 'c') 1446 d = SubElement(b, 'd') 1447 e = SubElement(c, 'e') 1448 1449 self.assertEquals( 1450 [a], 1451 list(a.getiterator('a'))) 1452 a2 = SubElement(e, 'a') 1453 self.assertEquals( 1454 [a, a2], 1455 list(a.getiterator('a'))) 1456 self.assertEquals( 1457 [a2], 1458 list(c.getiterator('a')))
1459
1460 - def test_getiterator_filter_all(self):
1461 Element = self.etree.Element 1462 SubElement = self.etree.SubElement 1463 1464 a = Element('a') 1465 b = SubElement(a, 'b') 1466 c = SubElement(a, 'c') 1467 d = SubElement(b, 'd') 1468 e = SubElement(c, 'e') 1469 1470 self.assertEquals( 1471 [a, b, d, c, e], 1472 list(a.getiterator('*')))
1473
1474 - def test_getiterator_filter_comment(self):
1475 Element = self.etree.Element 1476 Comment = self.etree.Comment 1477 SubElement = self.etree.SubElement 1478 1479 a = Element('a') 1480 b = SubElement(a, 'b') 1481 comment_b = Comment("TEST-b") 1482 b.append(comment_b) 1483 1484 self.assertEquals( 1485 [comment_b], 1486 list(a.getiterator(Comment))) 1487 1488 comment_a = Comment("TEST-a") 1489 a.append(comment_a) 1490 1491 self.assertEquals( 1492 [comment_b, comment_a], 1493 list(a.getiterator(Comment))) 1494 1495 self.assertEquals( 1496 [comment_b], 1497 list(b.getiterator(Comment)))
1498
1499 - def test_getiterator_filter_pi(self):
1500 Element = self.etree.Element 1501 PI = self.etree.ProcessingInstruction 1502 SubElement = self.etree.SubElement 1503 1504 a = Element('a') 1505 b = SubElement(a, 'b') 1506 pi_b = PI("TEST-b") 1507 b.append(pi_b) 1508 1509 self.assertEquals( 1510 [pi_b], 1511 list(a.getiterator(PI))) 1512 1513 pi_a = PI("TEST-a") 1514 a.append(pi_a) 1515 1516 self.assertEquals( 1517 [pi_b, pi_a], 1518 list(a.getiterator(PI))) 1519 1520 self.assertEquals( 1521 [pi_b], 1522 list(b.getiterator(PI)))
1523
1524 - def test_getiterator_with_text(self):
1525 Element = self.etree.Element 1526 SubElement = self.etree.SubElement 1527 1528 a = Element('a') 1529 a.text = 'a' 1530 b = SubElement(a, 'b') 1531 b.text = 'b' 1532 b.tail = 'b1' 1533 c = SubElement(a, 'c') 1534 c.text = 'c' 1535 c.tail = 'c1' 1536 d = SubElement(b, 'd') 1537 c.text = 'd' 1538 c.tail = 'd1' 1539 e = SubElement(c, 'e') 1540 e.text = 'e' 1541 e.tail = 'e1' 1542 1543 self.assertEquals( 1544 [a, b, d, c, e], 1545 list(a.getiterator()))
1546 #self.assertEquals( 1547 # [d], 1548 # list(d.getiterator())) 1549
1550 - def test_getiterator_filter_with_text(self):
1551 Element = self.etree.Element 1552 SubElement = self.etree.SubElement 1553 1554 a = Element('a') 1555 a.text = 'a' 1556 b = SubElement(a, 'b') 1557 b.text = 'b' 1558 b.tail = 'b1' 1559 c = SubElement(a, 'c') 1560 c.text = 'c' 1561 c.tail = 'c1' 1562 d = SubElement(b, 'd') 1563 c.text = 'd' 1564 c.tail = 'd1' 1565 e = SubElement(c, 'e') 1566 e.text = 'e' 1567 e.tail = 'e1' 1568 1569 self.assertEquals( 1570 [a], 1571 list(a.getiterator('a'))) 1572 a2 = SubElement(e, 'a') 1573 self.assertEquals( 1574 [a, a2], 1575 list(a.getiterator('a'))) 1576 self.assertEquals( 1577 [a2], 1578 list(e.getiterator('a')))
1579
1580 - def test_attribute_manipulation(self):
1581 Element = self.etree.Element 1582 1583 a = Element('a') 1584 a.attrib['foo'] = 'Foo' 1585 a.attrib['bar'] = 'Bar' 1586 self.assertEquals('Foo', a.attrib['foo']) 1587 del a.attrib['foo'] 1588 self.assertRaises(KeyError, a.attrib.__getitem__, 'foo')
1589
1590 - def test_getslice(self):
1591 Element = self.etree.Element 1592 SubElement = self.etree.SubElement 1593 1594 a = Element('a') 1595 b = SubElement(a, 'b') 1596 c = SubElement(a, 'c') 1597 d = SubElement(a, 'd') 1598 1599 self.assertEquals( 1600 [b, c], 1601 a[0:2]) 1602 self.assertEquals( 1603 [b, c, d], 1604 a[:]) 1605 self.assertEquals( 1606 [b, c, d], 1607 a[:10]) 1608 self.assertEquals( 1609 [b], 1610 a[0:1]) 1611 self.assertEquals( 1612 [], 1613 a[10:12])
1614
1615 - def test_getslice_negative(self):
1616 Element = self.etree.Element 1617 SubElement = self.etree.SubElement 1618 1619 a = Element('a') 1620 b = SubElement(a, 'b') 1621 c = SubElement(a, 'c') 1622 d = SubElement(a, 'd') 1623 1624 self.assertEquals( 1625 [d], 1626 a[-1:]) 1627 self.assertEquals( 1628 [c, d], 1629 a[-2:]) 1630 self.assertEquals( 1631 [c], 1632 a[-2:-1]) 1633 self.assertEquals( 1634 [b, c], 1635 a[-3:-1]) 1636 self.assertEquals( 1637 [b, c], 1638 a[-3:2])
1639
1640 - def test_getslice_text(self):
1641 ElementTree = self.etree.ElementTree 1642 1643 f = StringIO('<a><b>B</b>B1<c>C</c>C1</a>') 1644 doc = ElementTree(file=f) 1645 a = doc.getroot() 1646 b = a[0] 1647 c = a[1] 1648 self.assertEquals( 1649 [b, c], 1650 a[:]) 1651 self.assertEquals( 1652 [b], 1653 a[0:1]) 1654 self.assertEquals( 1655 [c], 1656 a[1:])
1657
1658 - def test_comment_getitem_getslice(self):
1659 Element = self.etree.Element 1660 Comment = self.etree.Comment 1661 SubElement = self.etree.SubElement 1662 1663 a = Element('a') 1664 b = SubElement(a, 'b') 1665 foo = Comment('foo') 1666 a.append(foo) 1667 c = SubElement(a, 'c') 1668 self.assertEquals( 1669 [b, foo, c], 1670 a[:]) 1671 self.assertEquals( 1672 foo, 1673 a[1]) 1674 a[1] = new = Element('new') 1675 self.assertEquals( 1676 new, 1677 a[1]) 1678 self.assertXML( 1679 '<a><b></b><new></new><c></c></a>', 1680 a)
1681
1682 - def test_delslice(self):
1683 Element = self.etree.Element 1684 SubElement = self.etree.SubElement 1685 1686 a = Element('a') 1687 b = SubElement(a, 'b') 1688 c = SubElement(a, 'c') 1689 d = SubElement(a, 'd') 1690 e = SubElement(a, 'e') 1691 1692 del a[1:3] 1693 self.assertEquals( 1694 [b, e], 1695 list(a))
1696
1697 - def test_delslice_negative1(self):
1698 Element = self.etree.Element 1699 SubElement = self.etree.SubElement 1700 1701 a = Element('a') 1702 b = SubElement(a, 'b') 1703 c = SubElement(a, 'c') 1704 d = SubElement(a, 'd') 1705 e = SubElement(a, 'e') 1706 1707 del a[1:-1] 1708 self.assertEquals( 1709 [b, e], 1710 list(a))
1711
1712 - def test_delslice_negative2(self):
1713 Element = self.etree.Element 1714 SubElement = self.etree.SubElement 1715 1716 a = Element('a') 1717 b = SubElement(a, 'b') 1718 c = SubElement(a, 'c') 1719 d = SubElement(a, 'd') 1720 e = SubElement(a, 'e') 1721 1722 del a[-3:-1] 1723 self.assertEquals( 1724 [b, e], 1725 list(a))
1726
1727 - def test_delslice_tail(self):
1728 ElementTree = self.etree.ElementTree 1729 f = StringIO('<a><b></b>B2<c></c>C2<d></d>D2<e></e>E2</a>') 1730 doc = ElementTree(file=f) 1731 a = doc.getroot() 1732 del a[1:3] 1733 self.assertXML( 1734 '<a><b></b>B2<e></e>E2</a>', 1735 a)
1736
1737 - def test_delslice_memory(self):
1738 # this could trigger a crash 1739 Element = self.etree.Element 1740 SubElement = self.etree.SubElement 1741 a = Element('a') 1742 b = SubElement(a, 'b') 1743 c = SubElement(b, 'c') 1744 del b # no more reference to b 1745 del a[:] 1746 self.assertEquals('c', c.tag)
1747
1748 - def test_setslice(self):
1749 Element = self.etree.Element 1750 SubElement = self.etree.SubElement 1751 1752 a = Element('a') 1753 b = SubElement(a, 'b') 1754 c = SubElement(a, 'c') 1755 d = SubElement(a, 'd') 1756 1757 e = Element('e') 1758 f = Element('f') 1759 g = Element('g') 1760 1761 s = [e, f, g] 1762 a[1:2] = s 1763 self.assertEquals( 1764 [b, e, f, g, d], 1765 list(a))
1766
1767 - def test_setslice_tail(self):
1768 ElementTree = self.etree.ElementTree 1769 Element = self.etree.Element 1770 f = StringIO('<a><b></b>B2<c></c>C2<d></d>D2<e></e>E2</a>') 1771 doc = ElementTree(file=f) 1772 a = doc.getroot() 1773 x = Element('x') 1774 y = Element('y') 1775 z = Element('z') 1776 x.tail = 'X2' 1777 y.tail = 'Y2' 1778 z.tail = 'Z2' 1779 a[1:3] = [x, y, z] 1780 self.assertXML( 1781 '<a><b></b>B2<x></x>X2<y></y>Y2<z></z>Z2<e></e>E2</a>', 1782 a)
1783
1784 - def test_setslice_negative(self):
1785 Element = self.etree.Element 1786 SubElement = self.etree.SubElement 1787 1788 a = Element('a') 1789 b = SubElement(a, 'b') 1790 c = SubElement(a, 'c') 1791 d = SubElement(a, 'd') 1792 1793 x = Element('x') 1794 y = Element('y') 1795 1796 a[1:-1] = [x, y] 1797 self.assertEquals( 1798 [b, x, y, d], 1799 list(a))
1800
1801 - def test_setslice_end(self):
1802 Element = self.etree.Element 1803 SubElement = self.etree.SubElement 1804 1805 a = Element('a') 1806 b = SubElement(a, 'b') 1807 c = SubElement(a, 'c') 1808 d = SubElement(a, 'd') 1809 1810 e = Element('e') 1811 f = Element('f') 1812 g = Element('g') 1813 1814 s = [e, f, g] 1815 a[3:] = s 1816 self.assertEquals( 1817 [b, c, d, e, f, g], 1818 list(a))
1819
1820 - def test_setslice_empty(self):
1821 Element = self.etree.Element 1822 1823 a = Element('a') 1824 1825 b = Element('b') 1826 c = Element('c') 1827 1828 a[:] = [b, c] 1829 self.assertEquals( 1830 [b, c], 1831 list(a))
1832
1833 - def test_tail_elementtree_root(self):
1834 Element = self.etree.Element 1835 ElementTree = self.etree.ElementTree 1836 1837 a = Element('a') 1838 a.tail = 'A2' 1839 t = ElementTree(element=a) 1840 self.assertEquals('A2', 1841 a.tail)
1842
1843 - def test_elementtree_getiterator(self):
1844 Element = self.etree.Element 1845 SubElement = self.etree.SubElement 1846 ElementTree = self.etree.ElementTree 1847 1848 a = Element('a') 1849 b = SubElement(a, 'b') 1850 c = SubElement(a, 'c') 1851 d = SubElement(b, 'd') 1852 e = SubElement(c, 'e') 1853 t = ElementTree(element=a) 1854 1855 self.assertEquals( 1856 [a, b, d, c, e], 1857 list(t.getiterator()))
1858
1859 - def test_elementtree_getiterator_filter(self):
1860 Element = self.etree.Element 1861 SubElement = self.etree.SubElement 1862 ElementTree = self.etree.ElementTree 1863 a = Element('a') 1864 b = SubElement(a, 'b') 1865 c = SubElement(a, 'c') 1866 d = SubElement(b, 'd') 1867 e = SubElement(c, 'e') 1868 t = ElementTree(element=a) 1869 1870 self.assertEquals( 1871 [a], 1872 list(t.getiterator('a'))) 1873 a2 = SubElement(e, 'a') 1874 self.assertEquals( 1875 [a, a2], 1876 list(t.getiterator('a')))
1877
1878 - def test_ns_access(self):
1879 ElementTree = self.etree.ElementTree 1880 ns = 'http://xml.infrae.com/1' 1881 f = StringIO('<x:a xmlns:x="%s"><x:b></x:b></x:a>' % ns) 1882 t = ElementTree(file=f) 1883 a = t.getroot() 1884 self.assertEquals('{%s}a' % ns, 1885 a.tag) 1886 self.assertEquals('{%s}b' % ns, 1887 a[0].tag)
1888
1889 - def test_ns_access2(self):
1890 ElementTree = self.etree.ElementTree 1891 ns = 'http://xml.infrae.com/1' 1892 ns2 = 'http://xml.infrae.com/2' 1893 f = StringIO('<x:a xmlns:x="%s" xmlns:y="%s"><x:b></x:b><y:b></y:b></x:a>' % (ns, ns2)) 1894 t = ElementTree(file=f) 1895 a = t.getroot() 1896 self.assertEquals('{%s}a' % ns, 1897 a.tag) 1898 self.assertEquals('{%s}b' % ns, 1899 a[0].tag) 1900 self.assertEquals('{%s}b' % ns2, 1901 a[1].tag)
1902
1903 - def test_ns_setting(self):
1904 Element = self.etree.Element 1905 SubElement = self.etree.SubElement 1906 ns = 'http://xml.infrae.com/1' 1907 ns2 = 'http://xml.infrae.com/2' 1908 a = Element('{%s}a' % ns) 1909 b = SubElement(a, '{%s}b' % ns2) 1910 c = SubElement(a, '{%s}c' % ns) 1911 self.assertEquals('{%s}a' % ns, 1912 a.tag) 1913 self.assertEquals('{%s}b' % ns2, 1914 b.tag) 1915 self.assertEquals('{%s}c' % ns, 1916 c.tag) 1917 self.assertEquals('{%s}a' % ns, 1918 a.tag) 1919 self.assertEquals('{%s}b' % ns2, 1920 b.tag) 1921 self.assertEquals('{%s}c' % ns, 1922 c.tag)
1923
1924 - def test_ns_tag_parse(self):
1925 Element = self.etree.Element 1926 SubElement = self.etree.SubElement 1927 ElementTree = self.etree.ElementTree 1928 1929 ns = 'http://xml.infrae.com/1' 1930 ns2 = 'http://xml.infrae.com/2' 1931 f = StringIO('<a xmlns="%s" xmlns:x="%s"><x:b></x:b><b></b></a>' % (ns, ns2)) 1932 t = ElementTree(file=f) 1933 1934 a = t.getroot() 1935 self.assertEquals('{%s}a' % ns, 1936 a.tag) 1937 self.assertEquals('{%s}b' % ns2, 1938 a[0].tag) 1939 self.assertEquals('{%s}b' % ns, 1940 a[1].tag)
1941
1942 - def test_ns_attr(self):
1943 Element = self.etree.Element 1944 ns = 'http://xml.infrae.com/1' 1945 ns2 = 'http://xml.infrae.com/2' 1946 a = Element('a') 1947 a.set('{%s}foo' % ns, 'Foo') 1948 a.set('{%s}bar' % ns2, 'Bar') 1949 self.assertEquals( 1950 'Foo', 1951 a.get('{%s}foo' % ns)) 1952 self.assertEquals( 1953 'Bar', 1954 a.get('{%s}bar' % ns2)) 1955 self.assertXML( 1956 '<a xmlns:ns0="%s" xmlns:ns1="%s" ns0:foo="Foo" ns1:bar="Bar"></a>' % (ns, ns2), 1957 a)
1958
1959 - def test_ns_move(self):
1960 Element = self.etree.Element 1961 ElementTree = self.etree.ElementTree 1962 1963 one = self.etree.parse( 1964 StringIO('<foo><bar xmlns:ns="http://a.b.c"><ns:baz/></bar></foo>')) 1965 baz = one.getroot()[0][0] 1966 1967 two = ElementTree(Element('root')) 1968 two.getroot().append(baz) 1969 # removing the originating document could cause a crash/error before 1970 # as namespace is not moved along with it 1971 del one 1972 self.assertEquals('{http://a.b.c}baz', baz.tag)
1973
1974 - def test_ns_decl(self):
1975 tostring = self.etree.tostring 1976 root = self.etree.XML( 1977 '<foo><bar xmlns:ns="http://a.b.c"><ns:baz/></bar></foo>') 1978 baz = root[0][0] 1979 1980 nsdecl = re.findall("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']", 1981 tostring(baz)) 1982 self.assertEquals(["http://a.b.c"], nsdecl)
1983
1984 - def test_ns_decl_default(self):
1985 tostring = self.etree.tostring 1986 root = self.etree.XML( 1987 '<foo><bar xmlns="http://a.b.c"><baz/></bar></foo>') 1988 baz = root[0][0] 1989 1990 nsdecl = re.findall("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']", 1991 tostring(baz)) 1992 self.assertEquals(["http://a.b.c"], nsdecl)
1993
1994 - def test_ns_decl_root(self):
1995 tostring = self.etree.tostring 1996 root = self.etree.XML( 1997 '<foo xmlns:ns="http://a.b.c"><bar><ns:baz/></bar></foo>') 1998 baz = root[0][0] 1999 2000 nsdecl = re.findall("xmlns(?::[a-z0-9]+)?=[\"']([^\"']+)[\"']", 2001 tostring(baz)) 2002 2003 self.assertEquals(["http://a.b.c"], nsdecl)
2004
2005 - def test_attribute_xmlns_move(self):
2006 Element = self.etree.Element 2007 2008 root = Element('element') 2009 2010 subelement = Element('subelement', 2011 {"{http://www.w3.org/XML/1998/namespace}id": "foo"}) 2012 self.assertEquals(1, len(subelement.attrib)) 2013 self.assertEquals( 2014 "foo", 2015 subelement.get("{http://www.w3.org/XML/1998/namespace}id")) 2016 2017 root.append(subelement) 2018 self.assertEquals(1, len(subelement.attrib)) 2019 self.assertEquals( 2020 {"{http://www.w3.org/XML/1998/namespace}id" : "foo"}.items(), 2021 subelement.attrib.items()) 2022 self.assertEquals( 2023 "foo", 2024 subelement.get("{http://www.w3.org/XML/1998/namespace}id"))
2025
2026 - def test_tostring(self):
2027 tostring = self.etree.tostring 2028 Element = self.etree.Element 2029 SubElement = self.etree.SubElement 2030 2031 a = Element('a') 2032 b = SubElement(a, 'b') 2033 c = SubElement(a, 'c') 2034 2035 self.assertEquals('<a><b></b><c></c></a>', 2036 canonicalize(tostring(a)))
2037
2038 - def test_tostring_element(self):
2039 tostring = self.etree.tostring 2040 Element = self.etree.Element 2041 SubElement = self.etree.SubElement 2042 2043 a = Element('a') 2044 b = SubElement(a, 'b') 2045 c = SubElement(a, 'c') 2046 d = SubElement(c, 'd') 2047 self.assertEquals('<b></b>', 2048 canonicalize(tostring(b))) 2049 self.assertEquals('<c><d></d></c>', 2050 canonicalize(tostring(c)))
2051
2052 - def test_tostring_element_tail(self):
2053 tostring = self.etree.tostring 2054 Element = self.etree.Element 2055 SubElement = self.etree.SubElement 2056 2057 a = Element('a') 2058 b = SubElement(a, 'b') 2059 c = SubElement(a, 'c') 2060 d = SubElement(c, 'd') 2061 b.tail = 'Foo' 2062 2063 self.assert_(tostring(b) == '<b/>Foo' or 2064 tostring(b) == '<b />Foo')
2065
2066 - def test_iterparse(self):
2067 iterparse = self.etree.iterparse 2068 f = StringIO('<a><b></b><c/></a>') 2069 2070 iterator = iterparse(f) 2071 self.assertEquals(None, 2072 iterator.root) 2073 events = list(iterator) 2074 root = iterator.root 2075 self.assertEquals( 2076 [('end', root[0]), ('end', root[1]), ('end', root)], 2077 events)
2078
2079 - def test_iterparse_file(self):
2080 iterparse = self.etree.iterparse 2081 iterator = iterparse(fileInTestDir("test.xml")) 2082 self.assertEquals(None, 2083 iterator.root) 2084 events = list(iterator) 2085 root = iterator.root 2086 self.assertEquals( 2087 [('end', root[0]), ('end', root)], 2088 events)
2089
2090 - def test_iterparse_start(self):
2091 iterparse = self.etree.iterparse 2092 f = StringIO('<a><b></b><c/></a>') 2093 2094 iterator = iterparse(f, events=('start',)) 2095 events = list(iterator) 2096 root = iterator.root 2097 self.assertEquals( 2098 [('start', root), ('start', root[0]), ('start', root[1])], 2099 events)
2100
2101 - def test_iterparse_start_end(self):
2102 iterparse = self.etree.iterparse 2103 f = StringIO('<a><b></b><c/></a>') 2104 2105 iterator = iterparse(f, events=('start','end')) 2106 events = list(iterator) 2107 root = iterator.root 2108 self.assertEquals( 2109 [('start', root), ('start', root[0]), ('end', root[0]), 2110 ('start', root[1]), ('end', root[1]), ('end', root)], 2111 events)
2112
2113 - def test_iterparse_clear(self):
2114 iterparse = self.etree.iterparse 2115 f = StringIO('<a><b></b><c/></a>') 2116 2117 iterator = iterparse(f) 2118 for event, elem in iterator: 2119 elem.clear() 2120 2121 root = iterator.root 2122 self.assertEquals(0, 2123 len(root))
2124
2125 - def test_iterparse_large(self):
2126 iterparse = self.etree.iterparse 2127 CHILD_COUNT = 12345 2128 f = StringIO('<a>' + ('<b>test</b>'*CHILD_COUNT) + '</a>') 2129 2130 i = 0 2131 for key in iterparse(f): 2132 event, element = key 2133 i += 1 2134 self.assertEquals(i, CHILD_COUNT + 1)
2135
2136 - def test_iterparse_attrib_ns(self):
2137 iterparse = self.etree.iterparse 2138 f = StringIO('<a xmlns="ns1"><b><c xmlns="ns2"/></b></a>') 2139 2140 attr_name = '{testns}bla' 2141 events = [] 2142 iterator = iterparse(f, events=('start','end','start-ns','end-ns')) 2143 for event, elem in iterator: 2144 events.append(event) 2145 if event == 'start': 2146 if elem.tag != '{ns1}a': 2147 elem.set(attr_name, 'value') 2148 2149 self.assertEquals( 2150 ['start-ns', 'start', 'start', 'start-ns', 'start', 2151 'end', 'end-ns', 'end', 'end', 'end-ns'], 2152 events) 2153 2154 root = iterator.root 2155 self.assertEquals( 2156 None, 2157 root.get(attr_name)) 2158 self.assertEquals( 2159 'value', 2160 root[0].get(attr_name))
2161
2162 - def test_iterparse_getiterator(self):
2163 iterparse = self.etree.iterparse 2164 f = StringIO('<a><b><d/></b><c/></a>') 2165 2166 counts = [] 2167 for event, elem in iterparse(f): 2168 counts.append(len(list(elem.getiterator()))) 2169 self.assertEquals( 2170 [1,2,1,4], 2171 counts)
2172
2173 - def test_parse_file(self):
2174 parse = self.etree.parse 2175 # from file 2176 tree = parse(fileInTestDir('test.xml')) 2177 self.assertXML( 2178 '<a><b></b></a>', 2179 tree.getroot())
2180
2181 - def test_parse_file_nonexistent(self):
2182 parse = self.etree.parse 2183 self.assertRaises(IOError, parse, fileInTestDir('notthere.xml'))
2184
2185 - def test_parse_file_object(self):
2186 parse = self.etree.parse 2187 # from file object 2188 f = open(fileInTestDir('test.xml'), 'r') 2189 tree = parse(f) 2190 f.close() 2191 self.assertXML( 2192 '<a><b></b></a>', 2193 tree.getroot())
2194
2195 - def test_parse_stringio(self):
2196 parse = self.etree.parse 2197 # from StringIO 2198 f = StringIO('<a><b></b></a>') 2199 tree = parse(f) 2200 f.close() 2201 self.assertXML( 2202 '<a><b></b></a>', 2203 tree.getroot() 2204 )
2205
2206 - def test_parse_with_encoding(self):
2207 # this can fail in libxml2 <= 2.6.22 2208 parse = self.etree.parse 2209 tree = parse(StringIO('<?xml version="1.0" encoding="ascii"?><html/>')) 2210 self.assertXML('<html></html>', 2211 tree.getroot())
2212
2213 - def test_encoding(self):
2214 Element = self.etree.Element 2215 2216 a = Element('a') 2217 a.text = u'Søk på nettet' 2218 self.assertXML( 2219 u'<a>Søk på nettet</a>'.encode('UTF-8'), 2220 a, 'utf-8')
2221
2222 - def test_encoding_exact(self):
2223 ElementTree = self.etree.ElementTree 2224 Element = self.etree.Element 2225 2226 a = Element('a') 2227 a.text = u'Søk på nettet' 2228 2229 f = StringIO() 2230 tree = ElementTree(element=a) 2231 tree.write(f, 'utf-8') 2232 self.assertEquals(u'<a>Søk på nettet</a>'.encode('UTF-8'), 2233 f.getvalue())
2234
2235 - def test_parse_file_encoding(self):
2236 parse = self.etree.parse 2237 # from file 2238 tree = parse(fileInTestDir('test-string.xml')) 2239 self.assertXML( 2240 u'<a>Søk på nettet</a>'.encode('UTF-8'), 2241 tree.getroot(), 'UTF-8')
2242
2243 - def test_parse_file_object_encoding(self):
2244 parse = self.etree.parse 2245 # from file object 2246 f = open(fileInTestDir('test-string.xml'), 'r') 2247 tree = parse(f) 2248 f.close() 2249 self.assertXML( 2250 u'<a>Søk på nettet</a>'.encode('UTF-8'), 2251 tree.getroot(), 'UTF-8')
2252
2253 - def test_encoding_8bit_latin1(self):
2254 ElementTree = self.etree.ElementTree 2255 Element = self.etree.Element 2256 2257 a = Element('a') 2258 a.text = u'Søk på nettet' 2259 2260 f = StringIO() 2261 tree = ElementTree(element=a) 2262 tree.write(f, 'iso-8859-1') 2263 result = f.getvalue() 2264 declaration = "<?xml version=\'1.0\' encoding=\'iso-8859-1\'?>" 2265 self.assertEncodingDeclaration(result,'iso-8859-1') 2266 result = result.split('?>', 1)[-1] 2267 if result[0] == '\n': 2268 result = result[1:] 2269 self.assertEquals(u'<a>Søk på nettet</a>'.encode('iso-8859-1'), 2270 result)
2271 2272 # raise error on wrong (left-over?) encoding declaration in unicode strings
2273 - def _test_wrong_unicode_encoding(self):
2274 XML = self.etree.XML 2275 test_utf = u'<?xml version=\'1.0\' encoding=\'iso-8859-1\'?><a>Søk på nettet</a>' 2276 self.assertRaises(SyntaxError, XML, test_utf)
2277
2278 - def test_encoding_write_default_encoding(self):
2279 ElementTree = self.etree.ElementTree 2280 Element = self.etree.Element 2281 2282 a = Element('a') 2283 a.text = u'Søk på nettet' 2284 2285 f = StringIO() 2286 tree = ElementTree(element=a) 2287 tree.write(f) 2288 data = f.getvalue() 2289 self.assertEquals( 2290 u'<a>Søk på nettet</a>'.encode('ASCII', 'xmlcharrefreplace'), 2291 data)
2292
2293 - def test_encoding_tostring(self):
2294 Element = self.etree.Element 2295 tostring = self.etree.tostring 2296 2297 a = Element('a') 2298 a.text = u'Søk på nettet' 2299 self.assertEquals(u'<a>Søk på nettet</a>'.encode('UTF-8'), 2300 tostring(a, 'utf-8'))
2301
2302 - def test_encoding_tostring_unknown(self):
2303 Element = self.etree.Element 2304 tostring = self.etree.tostring 2305 2306 a = Element('a') 2307 a.text = u'Søk på nettet' 2308 self.assertRaises(LookupError, tostring, a, 'Invalid Encoding')
2309
2310 - def test_encoding_tostring_sub(self):
2311 Element = self.etree.Element 2312 SubElement = self.etree.SubElement 2313 tostring = self.etree.tostring 2314 2315 a = Element('a') 2316 b = SubElement(a, 'b') 2317 b.text = u'Søk på nettet' 2318 self.assertEquals(u'<b>Søk på nettet</b>'.encode('UTF-8'), 2319 tostring(b, 'utf-8'))
2320
2321 - def test_encoding_tostring_sub_tail(self):
2322 Element = self.etree.Element 2323 SubElement = self.etree.SubElement 2324 tostring = self.etree.tostring 2325 2326 a = Element('a') 2327 b = SubElement(a, 'b') 2328 b.text = u'Søk på nettet' 2329 b.tail = u'Søk' 2330 self.assertEquals(u'<b>Søk på nettet</b>Søk'.encode('UTF-8'), 2331 tostring(b, 'utf-8'))
2332
2333 - def test_encoding_tostring_default_encoding(self):
2334 Element = self.etree.Element 2335 SubElement = self.etree.SubElement 2336 tostring = self.etree.tostring 2337 2338 a = Element('a') 2339 a.text = u'Søk på nettet' 2340 2341 expected = '<a>S&#248;k p&#229; nettet</a>' 2342 self.assertEquals( 2343 expected, 2344 tostring(a))
2345
2346 - def test_encoding_sub_tostring_default_encoding(self):
2347 Element = self.etree.Element 2348 SubElement = self.etree.SubElement 2349 tostring = self.etree.tostring 2350 2351 a = Element('a') 2352 b = SubElement(a, 'b') 2353 b.text = u'Søk på nettet' 2354 2355 expected = '<b>S&#248;k p&#229; nettet</b>' 2356 self.assertEquals( 2357 expected, 2358 tostring(b))
2359
2360 - def test_encoding_8bit_xml(self):
2361 utext = u'Søk på nettet' 2362 uxml = u'<p>%s</p>' % utext 2363 prologue = '<?xml version="1.0" encoding="iso-8859-1" ?>' 2364 isoxml = prologue + uxml.encode('iso-8859-1') 2365 tree = self.etree.XML(isoxml) 2366 self.assertEquals(utext, tree.text)
2367
2368 - def test_encoding_8bit_parse_stringio(self):
2369 utext = u'Søk på nettet' 2370 uxml = u'<p>%s</p>' % utext 2371 prologue = '<?xml version="1.0" encoding="iso-8859-1" ?>' 2372 isoxml = prologue + uxml.encode('iso-8859-1') 2373 el = self.etree.parse(StringIO(isoxml)).getroot() 2374 self.assertEquals(utext, el.text)
2375
2376 - def test_deepcopy_elementtree(self):
2377 Element = self.etree.Element 2378 ElementTree = self.etree.ElementTree 2379 2380 a = Element('a') 2381 a.text = "Foo" 2382 atree = ElementTree(a) 2383 2384 btree = copy.deepcopy(atree) 2385 self.assertEqual("Foo", atree.getroot().text) 2386 self.assertEqual("Foo", btree.getroot().text) 2387 self.assertFalse(btree is atree) 2388 self.assertFalse(btree.getroot() is atree.getroot())
2389
2390 - def test_deepcopy(self):
2391 Element = self.etree.Element 2392 2393 a = Element('a') 2394 a.text = 'Foo' 2395 2396 b = copy.deepcopy(a) 2397 self.assertEquals('Foo', b.text) 2398 2399 b.text = 'Bar' 2400 self.assertEquals('Bar', b.text) 2401 self.assertEquals('Foo', a.text) 2402 2403 del a 2404 self.assertEquals('Bar', b.text)
2405
2406 - def test_deepcopy_tail(self):
2407 Element = self.etree.Element 2408 2409 a = Element('a') 2410 a.tail = 'Foo' 2411 2412 b = copy.deepcopy(a) 2413 self.assertEquals('Foo', b.tail) 2414 2415 b.tail = 'Bar' 2416 self.assertEquals('Bar', b.tail) 2417 self.assertEquals('Foo', a.tail) 2418 2419 del a 2420 self.assertEquals('Bar', b.tail)
2421
2422 - def test_deepcopy_subelement(self):
2423 Element = self.etree.Element 2424 SubElement = self.etree.SubElement 2425 2426 root = Element('root') 2427 a = SubElement(root, 'a') 2428 a.text = 'FooText' 2429 a.tail = 'FooTail' 2430 2431 b = copy.deepcopy(a) 2432 self.assertEquals('FooText', b.text) 2433 self.assertEquals('FooTail', b.tail) 2434 2435 b.text = 'BarText' 2436 b.tail = 'BarTail' 2437 self.assertEquals('BarTail', b.tail) 2438 self.assertEquals('FooTail', a.tail) 2439 self.assertEquals('BarText', b.text) 2440 self.assertEquals('FooText', a.text) 2441 2442 del a 2443 self.assertEquals('BarTail', b.tail) 2444 self.assertEquals('BarText', b.text)
2445
2446 - def test_deepcopy_namespaces(self):
2447 root = self.etree.XML('''<doc xmlns="dns" xmlns:t="tns"> 2448 <parent><node t:foo="bar" /></parent> 2449 </doc>''') 2450 self.assertEquals( 2451 root[0][0].get('{tns}foo'), 2452 copy.deepcopy(root[0])[0].get('{tns}foo') ) 2453 self.assertEquals( 2454 root[0][0].get('{tns}foo'), 2455 copy.deepcopy(root[0][0]).get('{tns}foo') )
2456
2457 - def test_deepcopy_append(self):
2458 # previously caused a crash 2459 Element = self.etree.Element 2460 tostring = self.etree.tostring 2461 2462 a = Element('a') 2463 b = copy.deepcopy(a) 2464 a.append( Element('C') ) 2465 b.append( Element('X') ) 2466 2467 self.assertEquals('<a><C/></a>', 2468 tostring(a).replace(' ', '')) 2469 self.assertEquals('<a><X/></a>', 2470 tostring(b).replace(' ', ''))
2471
2472 - def test_shallowcopy(self):
2473 Element = self.etree.Element 2474 2475 a = Element('a') 2476 a.text = 'Foo' 2477 2478 b = copy.copy(a) 2479 self.assertEquals('Foo', b.text) 2480 2481 b.text = 'Bar' 2482 self.assertEquals('Bar', b.text) 2483 self.assertEquals('Foo', a.text)
2484 # XXX ElementTree will share nodes, but lxml.etree won't.. 2485
2486 - def test_shallowcopy_elementtree(self):
2487 Element = self.etree.Element 2488 ElementTree = self.etree.ElementTree 2489 2490 a = Element('a') 2491 a.text = 'Foo' 2492 atree = ElementTree(a) 2493 2494 btree = copy.copy(atree) 2495 self.assertFalse(btree is atree) 2496 self.assert_(btree.getroot() is atree.getroot()) 2497 self.assertEquals('Foo', atree.getroot().text)
2498
2499 - def test_element_boolean(self):
2500 etree = self.etree 2501 e = etree.Element('foo') 2502 self.assertEquals(False, bool(e)) 2503 etree.SubElement(e, 'bar') 2504 self.assertEquals(True, bool(e)) 2505 e = etree.Element('foo') 2506 e.text = 'hey' 2507 self.assertEquals(False, bool(e)) 2508 e = etree.Element('foo') 2509 e.tail = 'bar' 2510 self.assertEquals(False, bool(e)) 2511 e = etree.