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 
  12  import os, re, tempfile, copy, operator, gc, sys 
  13   
  14  this_dir = os.path.dirname(__file__) 
  15  if this_dir not in sys.path: 
  16      sys.path.insert(0, this_dir) # needed for Py3 
  17   
  18  from common_imports import StringIO, BytesIO, etree 
  19  from common_imports import ElementTree, cElementTree, ET_VERSION, CET_VERSION 
  20  from common_imports import filter_by_version, fileInTestDir, canonicalize, HelperTestCase 
  21  from common_imports import _str, _bytes, unicode 
  22   
  23  if cElementTree is not None and (CET_VERSION <= (1,0,7) or sys.version_info >= (3,3)): 
  24      cElementTree = None 
  25   
  26  if ElementTree is not None: 
  27      print("Comparing with ElementTree %s" % getattr(ElementTree, "VERSION", "?")) 
  28   
  29  if cElementTree is not None: 
  30      print("Comparing with cElementTree %s" % getattr(cElementTree, "VERSION", "?")) 
  31   
  32  try: 
  33      reversed 
  34  except NameError: 
  35      # Python 2.3 
36 - def reversed(seq):
37 seq = list(seq)[::-1] 38 return seq
39
40 -class _ETreeTestCaseBase(HelperTestCase):
41 etree = None 42 required_versions_ET = {} 43 required_versions_cET = {} 44
45 - def test_element(self):
46 for i in range(10): 47 e = self.etree.Element('foo') 48 self.assertEquals(e.tag, 'foo') 49 self.assertEquals(e.text, None) 50 self.assertEquals(e.tail, None)
51
52 - def test_simple(self):
53 Element = self.etree.Element 54 55 root = Element('root') 56 root.append(Element('one')) 57 root.append(Element('two')) 58 root.append(Element('three')) 59 self.assertEquals(3, len(root)) 60 self.assertEquals('one', root[0].tag) 61 self.assertEquals('two', root[1].tag) 62 self.assertEquals('three', root[2].tag) 63 self.assertRaises(IndexError, operator.getitem, root, 3)
64 65 # test weird dictionary interaction leading to segfault previously
67 root = self.etree.Element('root') 68 self.assertEquals(root.tag, "root") 69 add = self.etree.ElementTree(file=BytesIO('<foo>Foo</foo>')) 70 self.assertEquals(add.getroot().tag, "foo") 71 self.assertEquals(add.getroot().text, "Foo") 72 root.append(self.etree.Element('baz')) 73 self.assertEquals(root.tag, "root") 74 self.assertEquals(root[0].tag, "baz")
75
76 - def test_subelement(self):
77 Element = self.etree.Element 78 SubElement = self.etree.SubElement 79 80 root = Element('root') 81 SubElement(root, 'one') 82 SubElement(root, 'two') 83 SubElement(root, 'three') 84 self.assertEquals(3, len(root)) 85 self.assertEquals('one', root[0].tag) 86 self.assertEquals('two', root[1].tag) 87 self.assertEquals('three', root[2].tag)
88
89 - def test_element_contains(self):
90 Element = self.etree.Element 91 SubElement = self.etree.SubElement 92 93 root1 = Element('root') 94 SubElement(root1, 'one') 95 self.assert_(root1[0] in root1) 96 97 root2 = Element('root') 98 SubElement(root2, 'two') 99 SubElement(root2, 'three') 100 self.assert_(root2[0] in root2) 101 self.assert_(root2[1] in root2) 102 103 self.assertFalse(root1[0] in root2) 104 self.assertFalse(root2[0] in root1) 105 self.assertFalse(None in root2)
106
108 ElementTree = self.etree.ElementTree 109 110 f = BytesIO('<doc>Test<one>One</one></doc>') 111 doc = ElementTree(file=f) 112 root = doc.getroot() 113 self.assertEquals(1, len(root)) 114 self.assertEquals('one', root[0].tag) 115 self.assertRaises(IndexError, operator.getitem, root, 1)
116
118 ElementTree = self.etree.ElementTree 119 120 f = BytesIO('<doc><one>One</one><two>Two</two>hm<three>Three</three></doc>') 121 doc = ElementTree(file=f) 122 root = doc.getroot() 123 self.assertEquals(3, len(root)) 124 self.assertEquals('one', root[0].tag) 125 self.assertEquals('two', root[1].tag) 126 self.assertEquals('three', root[2].tag)
127
129 ElementTree = self.etree.ElementTree 130 131 f = BytesIO('<doc>Test</doc>') 132 doc = ElementTree(file=f) 133 root = doc.getroot() 134 self.assertEquals(0, len(root))
135
137 Element = self.etree.Element 138 SubElement = self.etree.SubElement 139 a = Element('a') 140 b = SubElement(a, 'b') 141 c = SubElement(a, 'c') 142 d = SubElement(a, 'd') 143 self.assertEquals(d, a[-1]) 144 self.assertEquals(c, a[-2]) 145 self.assertEquals(b, a[-3]) 146 self.assertRaises(IndexError, operator.getitem, a, -4) 147 a[-1] = e = Element('e') 148 self.assertEquals(e, a[-1]) 149 del a[-1] 150 self.assertEquals(2, len(a))
151
152 - def test_elementtree(self):
153 ElementTree = self.etree.ElementTree 154 155 f = BytesIO('<doc><one>One</one><two>Two</two></doc>') 156 doc = ElementTree(file=f) 157 root = doc.getroot() 158 self.assertEquals(2, len(root)) 159 self.assertEquals('one', root[0].tag) 160 self.assertEquals('two', root[1].tag)
161
162 - def test_text(self):
163 ElementTree = self.etree.ElementTree 164 165 f = BytesIO('<doc>This is a text</doc>') 166 doc = ElementTree(file=f) 167 root = doc.getroot() 168 self.assertEquals('This is a text', root.text)
169
170 - def test_text_empty(self):
171 ElementTree = self.etree.ElementTree 172 173 f = BytesIO('<doc></doc>') 174 doc = ElementTree(file=f) 175 root = doc.getroot() 176 self.assertEquals(None, root.text)
177
178 - def test_text_other(self):
179 ElementTree = self.etree.ElementTree 180 181 f = BytesIO('<doc><one>One</one></doc>') 182 doc = ElementTree(file=f) 183 root = doc.getroot() 184 self.assertEquals(None, root.text) 185 self.assertEquals('One', root[0].text)
186
187 - def test_text_escape_in(self):
188 ElementTree = self.etree.ElementTree 189 190 f = BytesIO('<doc>This is &gt; than a text</doc>') 191 doc = ElementTree(file=f) 192 root = doc.getroot() 193 self.assertEquals('This is > than a text', root.text)
194
195 - def test_text_escape_out(self):
196 Element = self.etree.Element 197 198 a = Element("a") 199 a.text = "<>&" 200 self.assertXML(_bytes('<a>&lt;&gt;&amp;</a>'), 201 a)
202
203 - def test_text_escape_tostring(self):
204 tostring = self.etree.tostring 205 Element = self.etree.Element 206 207 a = Element("a") 208 a.text = "<>&" 209 self.assertEquals(_bytes('<a>&lt;&gt;&amp;</a>'), 210 tostring(a))
211
212 - def test_text_str_subclass(self):
213 Element = self.etree.Element 214 215 class strTest(str): 216 pass
217 218 a = Element("a") 219 a.text = strTest("text") 220 self.assertXML(_bytes('<a>text</a>'), 221 a)
222
223 - def test_tail(self):
224 ElementTree = self.etree.ElementTree 225 226 f = BytesIO('<doc>This is <i>mixed</i> content.</doc>') 227 doc = ElementTree(file=f) 228 root = doc.getroot() 229 self.assertEquals(1, len(root)) 230 self.assertEquals('This is ', root.text) 231 self.assertEquals(None, root.tail) 232 self.assertEquals('mixed', root[0].text) 233 self.assertEquals(' content.', root[0].tail)
234
235 - def test_tail_str_subclass(self):
236 Element = self.etree.Element 237 SubElement = self.etree.SubElement 238 239 class strTest(str): 240 pass
241 242 a = Element("a") 243 SubElement(a, "t").tail = strTest("tail") 244 self.assertXML(_bytes('<a><t></t>tail</a>'), 245 a) 246
247 - def _test_del_tail(self):
248 # this is discouraged for ET compat, should not be tested... 249 XML = self.etree.XML 250 251 root = XML(_bytes('<doc>This is <i>mixed</i> content.</doc>')) 252 self.assertEquals(1, len(root)) 253 self.assertEquals('This is ', root.text) 254 self.assertEquals(None, root.tail) 255 self.assertEquals('mixed', root[0].text) 256 self.assertEquals(' content.', root[0].tail) 257 258 del root[0].tail 259 260 self.assertEquals(1, len(root)) 261 self.assertEquals('This is ', root.text) 262 self.assertEquals(None, root.tail) 263 self.assertEquals('mixed', root[0].text) 264 self.assertEquals(None, root[0].tail) 265 266 root[0].tail = "TAIL" 267 268 self.assertEquals(1, len(root)) 269 self.assertEquals('This is ', root.text) 270 self.assertEquals(None, root.tail) 271 self.assertEquals('mixed', root[0].text) 272 self.assertEquals('TAIL', root[0].tail)
273
274 - def test_ElementTree(self):
275 Element = self.etree.Element 276 ElementTree = self.etree.ElementTree 277 278 el = Element('hoi') 279 doc = ElementTree(el) 280 root = doc.getroot() 281 self.assertEquals(None, root.text) 282 self.assertEquals('hoi', root.tag)
283
284 - def test_attributes(self):
285 ElementTree = self.etree.ElementTree 286 287 f = BytesIO('<doc one="One" two="Two"/>') 288 doc = ElementTree(file=f) 289 root = doc.getroot() 290 self.assertEquals('One', root.attrib['one']) 291 self.assertEquals('Two', root.attrib['two']) 292 self.assertRaises(KeyError, operator.getitem, root.attrib, 'three')
293
294 - def test_attributes2(self):
295 ElementTree = self.etree.ElementTree 296 297 f = BytesIO('<doc one="One" two="Two"/>') 298 doc = ElementTree(file=f) 299 root = doc.getroot() 300 self.assertEquals('One', root.attrib.get('one')) 301 self.assertEquals('Two', root.attrib.get('two')) 302 self.assertEquals(None, root.attrib.get('three')) 303 self.assertEquals('foo', root.attrib.get('three', 'foo'))
304
305 - def test_attributes3(self):
306 ElementTree = self.etree.ElementTree 307 308 f = BytesIO('<doc one="One" two="Two"/>') 309 doc = ElementTree(file=f) 310 root = doc.getroot() 311 self.assertEquals('One', root.get('one')) 312 self.assertEquals('Two', root.get('two')) 313 self.assertEquals(None, root.get('three')) 314 self.assertEquals('foo', root.get('three', 'foo'))
315
316 - def test_attrib_clear(self):
317 XML = self.etree.XML 318 319 root = XML(_bytes('<doc one="One" two="Two"/>')) 320 self.assertEquals('One', root.get('one')) 321 self.assertEquals('Two', root.get('two')) 322 root.attrib.clear() 323 self.assertEquals(None, root.get('one')) 324 self.assertEquals(None, root.get('two'))
325
326 - def test_attrib_set_clear(self):
327 Element = self.etree.Element 328 329 root = Element("root", one="One") 330 root.set("two", "Two") 331 self.assertEquals('One', root.get('one')) 332 self.assertEquals('Two', root.get('two')) 333 root.attrib.clear() 334 self.assertEquals(None, root.get('one')) 335 self.assertEquals(None, root.get('two'))
336
337 - def test_attrib_ns_clear(self):
338 Element = self.etree.Element 339 SubElement = self.etree.SubElement 340 341 attribNS = '{http://foo/bar}x' 342 343 parent = Element('parent') 344 parent.set(attribNS, 'a') 345 child = SubElement(parent, 'child') 346 child.set(attribNS, 'b') 347 348 self.assertEquals('a', parent.get(attribNS)) 349 self.assertEquals('b', child.get(attribNS)) 350 351 parent.clear() 352 self.assertEquals(None, parent.get(attribNS)) 353 self.assertEquals('b', child.get(attribNS))
354
355 - def test_attrib_pop(self):
356 ElementTree = self.etree.ElementTree 357 358 f = BytesIO('<doc one="One" two="Two"/>') 359 doc = ElementTree(file=f) 360 root = doc.getroot() 361 self.assertEquals('One', root.attrib['one']) 362 self.assertEquals('Two', root.attrib['two']) 363 364 self.assertEquals('One', root.attrib.pop('one')) 365 366 self.assertEquals(None, root.attrib.get('one')) 367 self.assertEquals('Two', root.attrib['two'])
368
369 - def test_attrib_pop_unknown(self):
370 root = self.etree.XML(_bytes('<doc one="One" two="Two"/>')) 371 self.assertRaises(KeyError, root.attrib.pop, 'NONE') 372 373 self.assertEquals('One', root.attrib['one']) 374 self.assertEquals('Two', root.attrib['two'])
375
376 - def test_attrib_pop_default(self):
377 root = self.etree.XML(_bytes('<doc one="One" two="Two"/>')) 378 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
379
380 - def test_attrib_pop_empty_default(self):
381 root = self.etree.XML(_bytes('<doc/>')) 382 self.assertEquals('Three', root.attrib.pop('three', 'Three'))
383
384 - def test_attrib_pop_invalid_args(self):
385 root = self.etree.XML(_bytes('<doc one="One" two="Two"/>')) 386 self.assertRaises(TypeError, root.attrib.pop, 'One', None, None)
387
388 - def test_attribute_update_dict(self):
389 XML = self.etree.XML 390 391 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 392 items = list(root.attrib.items()) 393 items.sort() 394 self.assertEquals( 395 [('alpha', 'Alpha'), ('beta', 'Beta')], 396 items) 397 398 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}) 399 400 items = list(root.attrib.items()) 401 items.sort() 402 self.assertEquals( 403 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 404 items)
405
406 - def test_attribute_update_sequence(self):
407 XML = self.etree.XML 408 409 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 410 items = list(root.attrib.items()) 411 items.sort() 412 self.assertEquals( 413 [('alpha', 'Alpha'), ('beta', 'Beta')], 414 items) 415 416 root.attrib.update({'alpha' : 'test', 'gamma' : 'Gamma'}.items()) 417 418 items = list(root.attrib.items()) 419 items.sort() 420 self.assertEquals( 421 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 422 items)
423
424 - def test_attribute_update_iter(self):
425 XML = self.etree.XML 426 427 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 428 items = list(root.attrib.items()) 429 items.sort() 430 self.assertEquals( 431 [('alpha', 'Alpha'), ('beta', 'Beta')], 432 items) 433 434 root.attrib.update(iter({'alpha' : 'test', 'gamma' : 'Gamma'}.items())) 435 436 items = list(root.attrib.items()) 437 items.sort() 438 self.assertEquals( 439 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 440 items)
441
442 - def test_attribute_update_attrib(self):
443 XML = self.etree.XML 444 445 root = XML(_bytes('<doc alpha="Alpha" beta="Beta"/>')) 446 items = list(root.attrib.items()) 447 items.sort() 448 self.assertEquals( 449 [('alpha', 'Alpha'), ('beta', 'Beta')], 450 items) 451 452 other = XML(_bytes('<doc alpha="test" gamma="Gamma"/>')) 453 root.attrib.update(other.attrib) 454 455 items = list(root.attrib.items()) 456 items.sort() 457 self.assertEquals( 458 [('alpha', 'test'), ('beta', 'Beta'), ('gamma', 'Gamma')], 459 items)
460
461 - def test_attribute_keys(self):
462 XML = self.etree.XML 463 464 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 465 keys = list(root.attrib.keys()) 466 keys.sort() 467 self.assertEquals(['alpha', 'beta', 'gamma'], keys)
468
469 - def test_attribute_keys2(self):
470 XML = self.etree.XML 471 472 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 473 keys = list(root.keys()) 474 keys.sort() 475 self.assertEquals(['alpha', 'beta', 'gamma'], keys)
476
477 - def test_attribute_items2(self):
478 XML = self.etree.XML 479 480 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 481 items = list(root.items()) 482 items.sort() 483 self.assertEquals( 484 [('alpha','Alpha'), ('beta','Beta'), ('gamma','Gamma')], 485 items)
486
487 - def test_attribute_keys_ns(self):
488 XML = self.etree.XML 489 490 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 491 keys = list(root.keys()) 492 keys.sort() 493 self.assertEquals(['bar', '{http://ns.codespeak.net/test}baz'], 494 keys)
495
496 - def test_attribute_values(self):
497 XML = self.etree.XML 498 499 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 500 values = list(root.attrib.values()) 501 values.sort() 502 self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)
503
504 - def test_attribute_values_ns(self):
505 XML = self.etree.XML 506 507 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 508 values = list(root.attrib.values()) 509 values.sort() 510 self.assertEquals( 511 ['Bar', 'Baz'], values)
512
513 - def test_attribute_items(self):
514 XML = self.etree.XML 515 516 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 517 items = list(root.attrib.items()) 518 items.sort() 519 self.assertEquals([ 520 ('alpha', 'Alpha'), 521 ('beta', 'Beta'), 522 ('gamma', 'Gamma'), 523 ], 524 items)
525
526 - def test_attribute_items_ns(self):
527 XML = self.etree.XML 528 529 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 530 items = list(root.attrib.items()) 531 items.sort() 532 self.assertEquals( 533 [('bar', 'Bar'), ('{http://ns.codespeak.net/test}baz', 'Baz')], 534 items)
535
536 - def test_attribute_str(self):
537 XML = self.etree.XML 538 539 expected = "{'{http://ns.codespeak.net/test}baz': 'Baz', 'bar': 'Bar'}" 540 alternative = "{'bar': 'Bar', '{http://ns.codespeak.net/test}baz': 'Baz'}" 541 542 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 543 try: 544 self.assertEquals(expected, str(root.attrib)) 545 except AssertionError: 546 self.assertEquals(alternative, str(root.attrib))
547
548 - def test_attribute_contains(self):
549 XML = self.etree.XML 550 551 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 552 self.assertEquals( 553 True, 'bar' in root.attrib) 554 self.assertEquals( 555 False, 'baz' in root.attrib) 556 self.assertEquals( 557 False, 'hah' in root.attrib) 558 self.assertEquals( 559 True, 560 '{http://ns.codespeak.net/test}baz' in root.attrib)
561
562 - def test_attribute_set(self):
563 Element = self.etree.Element 564 565 root = Element("root") 566 root.set("attr", "TEST") 567 self.assertEquals("TEST", root.get("attr"))
568
569 - def test_attribute_iterator(self):
570 XML = self.etree.XML 571 572 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma" />')) 573 result = [] 574 for key in root.attrib: 575 result.append(key) 576 result.sort() 577 self.assertEquals(['alpha', 'beta', 'gamma'], result)
578
579 - def test_attribute_manipulation(self):
580 Element = self.etree.Element 581 582 a = Element('a') 583 a.attrib['foo'] = 'Foo' 584 a.attrib['bar'] = 'Bar' 585 self.assertEquals('Foo', a.attrib['foo']) 586 del a.attrib['foo'] 587 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')
588
589 - def test_del_attribute_ns(self):
590 Element = self.etree.Element 591 592 a = Element('a') 593 a.attrib['{http://a/}foo'] = 'Foo' 594 a.attrib['{http://a/}bar'] = 'Bar' 595 self.assertEquals(None, a.get('foo')) 596 self.assertEquals('Foo', a.get('{http://a/}foo')) 597 self.assertEquals('Foo', a.attrib['{http://a/}foo']) 598 599 self.assertRaises(KeyError, operator.delitem, a.attrib, 'foo') 600 self.assertEquals('Foo', a.attrib['{http://a/}foo']) 601 602 del a.attrib['{http://a/}foo'] 603 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')
604
605 - def test_del_attribute_ns_parsed(self):
606 XML = self.etree.XML 607 608 a = XML(_bytes('<a xmlns:nsa="http://a/" nsa:foo="FooNS" foo="Foo" />')) 609 610 self.assertEquals('Foo', a.attrib['foo']) 611 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 612 613 del a.attrib['foo'] 614 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 615 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 616 self.assertRaises(KeyError, operator.delitem, a.attrib, 'foo') 617 618 del a.attrib['{http://a/}foo'] 619 self.assertRaises(KeyError, operator.getitem, a.attrib, '{http://a/}foo') 620 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 621 622 a = XML(_bytes('<a xmlns:nsa="http://a/" foo="Foo" nsa:foo="FooNS" />')) 623 624 self.assertEquals('Foo', a.attrib['foo']) 625 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 626 627 del a.attrib['foo'] 628 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 629 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 630 631 del a.attrib['{http://a/}foo'] 632 self.assertRaises(KeyError, operator.getitem, a.attrib, '{http://a/}foo') 633 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')
634
635 - def test_XML(self):
636 XML = self.etree.XML 637 638 root = XML(_bytes('<doc>This is a text.</doc>')) 639 self.assertEquals(0, len(root)) 640 self.assertEquals('This is a text.', root.text)
641
642 - def test_XMLID(self):
643 XMLID = self.etree.XMLID 644 XML = self.etree.XML 645 xml_text = _bytes(''' 646 <document> 647 <h1 id="chapter1">...</h1> 648 <p id="note1" class="note">...</p> 649 <p>Regular paragraph.</p> 650 <p xml:id="xmlid">XML:ID paragraph.</p> 651 <p id="warn1" class="warning">...</p> 652 </document> 653 ''') 654 655 root, dic = XMLID(xml_text) 656 root2 = XML(xml_text) 657 self.assertEquals(self._writeElement(root), 658 self._writeElement(root2)) 659 expected = { 660 "chapter1" : root[0], 661 "note1" : root[1], 662 "warn1" : root[4] 663 } 664 self.assertEquals(dic, expected)
665
666 - def test_fromstring(self):
667 fromstring = self.etree.fromstring 668 669 root = fromstring('<doc>This is a text.</doc>') 670 self.assertEquals(0, len(root)) 671 self.assertEquals('This is a text.', root.text)
672 673 required_versions_ET['test_fromstringlist'] = (1,3)
674 - def test_fromstringlist(self):
675 fromstringlist = self.etree.fromstringlist 676 677 root = fromstringlist(["<do", "c>T", "hi", "s is", 678 " a text.<", "/doc", ">"]) 679 self.assertEquals(0, len(root)) 680 self.assertEquals('This is a text.', root.text)
681 682 required_versions_ET['test_fromstringlist_characters'] = (1,3)
683 - def test_fromstringlist_characters(self):
684 fromstringlist = self.etree.fromstringlist 685 686 root = fromstringlist(list('<doc>This is a text.</doc>')) 687 self.assertEquals(0, len(root)) 688 self.assertEquals('This is a text.', root.text)
689 690 required_versions_ET['test_fromstringlist_single'] = (1,3)
691 - def test_fromstringlist_single(self):
692 fromstringlist = self.etree.fromstringlist 693 694 root = fromstringlist(['<doc>This is a text.</doc>']) 695 self.assertEquals(0, len(root)) 696 self.assertEquals('This is a text.', root.text)
697
698 - def test_iselement(self):
699 iselement = self.etree.iselement 700 Element = self.etree.Element 701 ElementTree = self.etree.ElementTree 702 XML = self.etree.XML 703 Comment = self.etree.Comment 704 ProcessingInstruction = self.etree.ProcessingInstruction 705 706 el = Element('hoi') 707 self.assert_(iselement(el)) 708 709 el2 = XML(_bytes('<foo/>')) 710 self.assert_(iselement(el2)) 711 712 tree = ElementTree(element=Element('dag')) 713 self.assert_(not iselement(tree)) 714 self.assert_(iselement(tree.getroot())) 715 716 c = Comment('test') 717 self.assert_(iselement(c)) 718 719 p = ProcessingInstruction("test", "some text") 720 self.assert_(iselement(p))
721
722 - def test_iteration(self):
723 XML = self.etree.XML 724 725 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 726 result = [] 727 for el in root: 728 result.append(el.tag) 729 self.assertEquals(['one', 'two', 'three'], result)
730
731 - def test_iteration_empty(self):
732 XML = self.etree.XML 733 734 root = XML(_bytes('<doc></doc>')) 735 result = [] 736 for el in root: 737 result.append(el.tag) 738 self.assertEquals([], result)
739
740 - def test_iteration_text_only(self):
741 XML = self.etree.XML 742 743 root = XML(_bytes('<doc>Text</doc>')) 744 result = [] 745 for el in root: 746 result.append(el.tag) 747 self.assertEquals([], result)
748
749 - def test_iteration_crash(self):
750 # this would cause a crash in the past 751 fromstring = self.etree.fromstring 752 root = etree.fromstring('<html><p></p>x</html>') 753 for elem in root: 754 elem.tail = ''
755
756 - def test_iteration_reversed(self):
757 XML = self.etree.XML 758 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 759 result = [] 760 for el in reversed(root): 761 result.append(el.tag) 762 self.assertEquals(['three', 'two', 'one'], result)
763
764 - def test_iteration_subelement(self):
765 XML = self.etree.XML 766 767 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 768 result = [] 769 add = True 770 for el in root: 771 result.append(el.tag) 772 if add: 773 self.etree.SubElement(root, 'four') 774 add = False 775 self.assertEquals(['one', 'two', 'three', 'four'], result)
776
777 - def test_iteration_del_child(self):
778 XML = self.etree.XML 779 780 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 781 result = [] 782 for el in root: 783 result.append(el.tag) 784 del root[-1] 785 self.assertEquals(['one', 'two'], result)
786
787 - def test_iteration_double(self):
788 XML = self.etree.XML 789 790 root = XML(_bytes('<doc><one/><two/></doc>')) 791 result = [] 792 for el0 in root: 793 result.append(el0.tag) 794 for el1 in root: 795 result.append(el1.tag) 796 self.assertEquals(['one','one', 'two', 'two', 'one', 'two'], result)
797 798 required_versions_ET['test_itertext'] = (1,3)
799 - def test_itertext(self):
800 # ET 1.3+ 801 XML = self.etree.XML 802 root = XML(_bytes("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")) 803 804 text = list(root.itertext()) 805 self.assertEquals(["RTEXT", "ATAIL", "CTEXT", "CTAIL"], 806 text)
807 808 required_versions_ET['test_itertext_child'] = (1,3)
809 - def test_itertext_child(self):
810 # ET 1.3+ 811 XML = self.etree.XML 812 root = XML(_bytes("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")) 813 814 text = list(root[2].itertext()) 815 self.assertEquals(["CTEXT"], 816 text)
817
818 - def test_findall(self):
819 XML = self.etree.XML 820 root = XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')) 821 self.assertEquals(len(list(root.findall("c"))), 1) 822 self.assertEquals(len(list(root.findall(".//c"))), 2) 823 self.assertEquals(len(list(root.findall(".//b"))), 3) 824 self.assertEquals(len(list(root.findall(".//b"))[0]), 1) 825 self.assertEquals(len(list(root.findall(".//b"))[1]), 0) 826 self.assertEquals(len(list(root.findall(".//b"))[2]), 0)
827
828 - def test_findall_ns(self):
829 XML = self.etree.XML 830 root = XML(_bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')) 831 self.assertEquals(len(list(root.findall(".//{X}b"))), 2) 832 self.assertEquals(len(list(root.findall(".//b"))), 3) 833 self.assertEquals(len(list(root.findall("b"))), 2)
834
835 - def test_element_with_attributes_keywords(self):
836 Element = self.etree.Element 837 838 el = Element('tag', foo='Foo', bar='Bar') 839 self.assertEquals('Foo', el.attrib['foo']) 840 self.assertEquals('Bar', el.attrib['bar'])
841
842 - def test_element_with_attributes(self):
843 Element = self.etree.Element 844 845 el = Element('tag', {'foo':'Foo', 'bar':'Bar'}) 846 self.assertEquals('Foo', el.attrib['foo']) 847 self.assertEquals('Bar', el.attrib['bar'])
848
849 - def test_element_with_attributes_ns(self):
850 Element = self.etree.Element 851 852 el = Element('tag', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 853 self.assertEquals('Foo', el.attrib['{ns1}foo']) 854 self.assertEquals('Bar', el.attrib['{ns2}bar'])
855
856 - def test_subelement_with_attributes(self):
857 Element = self.etree.Element 858 SubElement = self.etree.SubElement 859 860 el = Element('tag') 861 SubElement(el, 'foo', {'foo':'Foo'}, baz="Baz") 862 self.assertEquals("Baz", el[0].attrib['baz']) 863 self.assertEquals('Foo', el[0].attrib['foo'])
864
865 - def test_subelement_with_attributes_ns(self):
866 Element = self.etree.Element 867 SubElement = self.etree.SubElement 868 869 el = Element('tag') 870 SubElement(el, 'foo', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 871 self.assertEquals('Foo', el[0].attrib['{ns1}foo']) 872 self.assertEquals('Bar', el[0].attrib['{ns2}bar'])
873
874 - def test_write(self):
875 ElementTree = self.etree.ElementTree 876 XML = self.etree.XML 877 878 for i in range(10): 879 f = BytesIO() 880 root = XML(_bytes('<doc%s>This is a test.</doc%s>' % (i, i))) 881 tree = ElementTree(element=root) 882 tree.write(f) 883 data = f.getvalue() 884 self.assertEquals( 885 _bytes('<doc%s>This is a test.</doc%s>' % (i, i)), 886 canonicalize(data))
887 888 required_versions_ET['test_write_method_html'] = (1,3)
889 - def test_write_method_html(self):
890 ElementTree = self.etree.ElementTree 891 Element = self.etree.Element 892 SubElement = self.etree.SubElement 893 894 html = Element('html') 895 body = SubElement(html, 'body') 896 p = SubElement(body, 'p') 897 p.text = "html" 898 SubElement(p, 'br').tail = "test" 899 900 tree = ElementTree(element=html) 901 f = BytesIO() 902 tree.write(f, method="html") 903 data = f.getvalue().replace(_bytes('\n'),_bytes('')) 904 905 self.assertEquals(_bytes('<html><body><p>html<br>test</p></body></html>'), 906 data)
907 908 required_versions_ET['test_write_method_text'] = (1,3)
909 - def test_write_method_text(self):
910 ElementTree = self.etree.ElementTree 911 Element = self.etree.Element 912 SubElement = self.etree.SubElement 913 914 a = Element('a') 915 a.text = "A" 916 a.tail = "tail" 917 b = SubElement(a, 'b') 918 b.text = "B" 919 b.tail = "TAIL" 920 c = SubElement(a, 'c') 921 c.text = "C" 922 923 tree = ElementTree(element=a) 924 f = BytesIO() 925 tree.write(f, method="text") 926 data = f.getvalue() 927 928 self.assertEquals(_bytes('ABTAILCtail'), 929 data)
930
931 - def test_write_fail(self):
932 ElementTree = self.etree.ElementTree 933 XML = self.etree.XML 934 935 tree = ElementTree( XML(_bytes('<doc>This is a test.</doc>')) ) 936 self.assertRaises(IOError, tree.write, 937 "definitely////\\-\\nonexisting\\-\\////FILE")
938 939 # this could trigger a crash, apparently because the document 940 # reference was prematurely garbage collected
941 - def test_crash(self):
942 Element = self.etree.Element 943 944 element = Element('tag') 945 for i in range(10): 946 element.attrib['key'] = 'value' 947 value = element.attrib['key'] 948 self.assertEquals(value, 'value')
949 950 # from doctest; for some reason this caused crashes too
951 - def test_write_ElementTreeDoctest(self):
952 Element = self.etree.Element 953 ElementTree = self.etree.ElementTree 954 955 f = BytesIO() 956 for i in range(10): 957 element = Element('tag%s' % i) 958 self._check_element(element) 959 tree = ElementTree(element) 960 tree.write(f) 961 self._check_element_tree(tree)
962
963 - def test_subelement_reference(self):
964 Element = self.etree.Element 965 SubElement = self.etree.SubElement 966 967 el = Element('foo') 968 el2 = SubElement(el, 'bar') 969 el3 = SubElement(el2, 'baz') 970 971 al = Element('foo2') 972 al2 = SubElement(al, 'bar2') 973 al3 = SubElement(al2, 'baz2') 974 975 # now move al2 into el 976 el.append(al2) 977 978 # now change al3 directly 979 al3.text = 'baz2-modified' 980 981 # it should have changed through this route too 982 self.assertEquals( 983 'baz2-modified', 984 el[1][0].text)
985
986 - def test_set_text(self):
987 Element = self.etree.Element 988 SubElement = self.etree.SubElement 989 990 a = Element('a') 991 b = SubElement(a, 'b') 992 a.text = 'hoi' 993 self.assertEquals( 994 'hoi', 995 a.text) 996 self.assertEquals( 997 'b', 998 a[0].tag)
999
1000 - def test_set_text2(self):
1001 Element = self.etree.Element 1002 SubElement = self.etree.SubElement 1003 1004 a = Element('a') 1005 a.text = 'hoi' 1006 b = SubElement(a ,'b') 1007 self.assertEquals( 1008 'hoi', 1009 a.text) 1010 self.assertEquals( 1011 'b', 1012 a[0].tag)
1013
1014 - def test_set_text_none(self):
1015 Element = self.etree.Element 1016 1017 a = Element('a') 1018 1019 a.text = 'foo' 1020 a.text = None 1021 1022 self.assertEquals( 1023 None, 1024 a.text) 1025 self.assertXML(_bytes('<a></a>'), a)
1026
1027 - def test_set_text_empty(self):
1028 Element = self.etree.Element 1029 1030 a = Element('a') 1031 self.assertEquals(None, a.text) 1032 1033 a.text = '' 1034 self.assertEquals('', a.text) 1035 self.assertXML(_bytes('<a></a>'), a)
1036
1037 - def test_tail1(self):
1038 Element = self.etree.Element 1039 SubElement = self.etree.SubElement 1040 1041 a = Element('a') 1042 a.tail = 'dag' 1043 self.assertEquals('dag', 1044 a.tail) 1045 b = SubElement(a, 'b') 1046 b.tail = 'hoi' 1047 self.assertEquals('hoi', 1048 b.tail) 1049 self.assertEquals('dag', 1050 a.tail)
1051
1052 - def test_tail_append(self):
1053 Element = self.etree.Element 1054 1055 a = Element('a') 1056 b = Element('b') 1057 b.tail = 'b_tail' 1058 a.append(b) 1059 self.assertEquals('b_tail', 1060 b.tail)
1061
1062 - def test_tail_set_twice(self):
1063 Element = self.etree.Element 1064 SubElement = self.etree.SubElement 1065 1066 a = Element('a') 1067 b = SubElement(a, 'b') 1068 b.tail = 'foo' 1069 b.tail = 'bar' 1070 self.assertEquals('bar', 1071 b.tail) 1072 self.assertXML(_bytes('<a><b></b>bar</a>'), a)
1073
1074 - def test_tail_set_none(self):
1075 Element = self.etree.Element 1076 a = Element('a') 1077 a.tail = 'foo' 1078 a.tail = None 1079 self.assertEquals( 1080 None, 1081 a.tail) 1082 self.assertXML(_bytes('<a></a>'), a)
1083 1084 required_versions_ET['test_extend'] = (1,3)
1085 - def test_extend(self):
1086 root = self.etree.Element('foo') 1087 for i in range(3): 1088 element = self.etree.SubElement(root, 'a%s' % i) 1089 element.text = "text%d" % i 1090 element.tail = "tail%d" % i 1091 1092 elements = [] 1093 for i in range(3): 1094 new_element = self.etree.Element("test%s" % i) 1095 new_element.text = "TEXT%s" % i 1096 new_element.tail = "TAIL%s" % i 1097 elements.append(new_element) 1098 1099 root.extend(elements) 1100 1101 self.assertEquals( 1102 ["a0", "a1", "a2", "test0", "test1", "test2"], 1103 [ el.tag for el in root ]) 1104 self.assertEquals( 1105 ["text0", "text1", "text2", "TEXT0", "TEXT1", "TEXT2"], 1106 [ el.text for el in root ]) 1107 self.assertEquals( 1108 ["tail0", "tail1", "tail2", "TAIL0", "TAIL1", "TAIL2"], 1109 [ el.tail for el in root ])
1110
1111 - def test_comment(self):
1112 Element = self.etree.Element 1113 SubElement = self.etree.SubElement 1114 Comment = self.etree.Comment 1115 1116 a = Element('a') 1117 a.append(Comment('foo')) 1118 self.assertEquals(a[0].tag, Comment) 1119 self.assertEquals(a[0].text, 'foo')
1120 1121 # ElementTree < 1.3 adds whitespace around comments 1122 required_versions_ET['test_comment_text'] = (1,3)
1123 - def test_comment_text(self):
1124 Element = self.etree.Element 1125 SubElement = self.etree.SubElement 1126 Comment = self.etree.Comment 1127 tostring = self.etree.tostring 1128 1129 a = Element('a') 1130 a.append(Comment('foo')) 1131 self.assertEquals(a[0].text, 'foo') 1132 1133 self.assertEquals( 1134 _bytes('<a><!--foo--></a>'), 1135 tostring(a)) 1136 1137 a[0].text = "TEST" 1138 self.assertEquals(a[0].text, 'TEST') 1139 1140 self.assertEquals( 1141 _bytes('<a><!--TEST--></a>'), 1142 tostring(a))
1143 1144 # ElementTree < 1.3 adds whitespace around comments 1145 required_versions_ET['test_comment_whitespace'] = (1,3)
1146 - def test_comment_whitespace(self):
1147 Element = self.etree.Element 1148 SubElement = self.etree.SubElement 1149 Comment = self.etree.Comment 1150 tostring = self.etree.tostring 1151 1152 a = Element('a') 1153 a.append(Comment(' foo ')) 1154 self.assertEquals(a[0].text, ' foo ') 1155 self.assertEquals( 1156 _bytes('<a><!-- foo --></a>'), 1157 tostring(a))
1158
1159 - def test_comment_nonsense(self):
1160 Comment = self.etree.Comment 1161 c = Comment('foo') 1162 self.assertEquals({}, c.attrib) 1163 self.assertEquals([], list(c.keys())) 1164 self.assertEquals([], list(c.items())) 1165 self.assertEquals(None, c.get('hoi')) 1166 self.assertEquals(0, len(c)) 1167 # should not iterate 1168 for i in c: 1169 pass
1170
1171 - def test_pi(self):
1172 # lxml.etree separates target and text 1173 Element = self.etree.Element 1174 SubElement = self.etree.SubElement 1175 ProcessingInstruction = self.etree.ProcessingInstruction 1176 1177 a = Element('a') 1178 a.append(ProcessingInstruction('foo', 'some more text')) 1179 self.assertEquals(a[0].tag, ProcessingInstruction) 1180 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1181 a)
1182
1183 - def test_processinginstruction(self):
1184 # lxml.etree separates target and text 1185 Element = self.etree.Element 1186 SubElement = self.etree.SubElement 1187 ProcessingInstruction = self.etree.PI 1188 1189 a = Element('a') 1190 a.append(ProcessingInstruction('foo', 'some more text')) 1191 self.assertEquals(a[0].tag, ProcessingInstruction) 1192 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1193 a)
1194
1195 - def test_pi_nonsense(self):
1196 ProcessingInstruction = self.etree.ProcessingInstruction 1197 pi = ProcessingInstruction('foo') 1198 self.assertEquals({}, pi.attrib) 1199 self.assertEquals([], list(pi.keys())) 1200 self.assertEquals([], list(pi.items())) 1201 self.assertEquals(None, pi.get('hoi')) 1202 self.assertEquals(0, len(pi)) 1203 # should not iterate 1204 for i in pi: 1205 pass
1206
1207 - def test_setitem(self):
1208 Element = self.etree.Element 1209 SubElement = self.etree.SubElement 1210 1211 a = Element('a') 1212 b = SubElement(a, 'b') 1213 c = Element('c') 1214 a[0] = c 1215 self.assertEquals( 1216 c, 1217 a[0]) 1218 self.assertXML(_bytes('<a><c></c></a>'), 1219 a) 1220 self.assertXML(_bytes('<b></b>'), 1221 b)
1222
1223 - def test_setitem2(self):
1224 Element = self.etree.Element 1225 SubElement = self.etree.SubElement 1226 1227 a = Element('a') 1228 for i in range(5): 1229 b = SubElement(a, 'b%s' % i) 1230 c = SubElement(b, 'c') 1231 for i in range(5): 1232 d = Element('d') 1233 e = SubElement(d, 'e') 1234 a[i] = d 1235 self.assertXML( 1236 _bytes('<a><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d></a>'), 1237 a) 1238 self.assertXML(_bytes('<c></c>'), 1239 c)
1240
1241 - def test_setitem_replace(self):
1242 Element = self.etree.Element 1243 SubElement = self.etree.SubElement 1244 1245 a = Element('a') 1246 SubElement(a, 'b') 1247 d = Element('d') 1248 a[0] = d 1249 self.assertXML(_bytes('<a><d></d></a>'), a)
1250
1251 - def test_setitem_indexerror(self):
1252 Element = self.etree.Element 1253 SubElement = self.etree.SubElement 1254 1255 a = Element('a') 1256 b = SubElement(a, 'b') 1257 1258 self.assertRaises(IndexError, operator.setitem, a, 1, Element('c'))
1259
1260 - def test_setitem_tail(self):
1261 Element = self.etree.Element 1262 SubElement = self.etree.SubElement 1263 1264 a = Element('a') 1265 b = SubElement(a, 'b') 1266 b.tail = 'B2' 1267 c = Element('c') 1268 c.tail = 'C2' 1269 1270 a[0] = c 1271 self.assertXML( 1272 _bytes('<a><c></c>C2</a>'), 1273 a)
1274
1275 - def test_tag_write(self):
1276 Element = self.etree.Element 1277 SubElement = self.etree.SubElement 1278 1279 a = Element('a') 1280 b = SubElement(a, 'b') 1281 1282 a.tag = 'c' 1283 1284 self.assertEquals( 1285 'c', 1286 a.tag) 1287 1288 self.assertXML( 1289 _bytes('<c><b></b></c>'), 1290 a)
1291
1292 - def test_tag_reset_ns(self):
1293 Element = self.etree.Element 1294 SubElement = self.etree.SubElement 1295 tostring = self.etree.tostring 1296 1297 a = Element('{a}a') 1298 b1 = SubElement(a, '{a}b') 1299 b2 = SubElement(a, '{b}b') 1300 1301 self.assertEquals('{a}b', b1.tag) 1302 1303 b1.tag = 'c' 1304 1305 # can't use C14N here! 1306 self.assertEquals('c', b1.tag) 1307 self.assertEquals(_bytes('<c'), tostring(b1)[:2]) 1308 self.assert_(_bytes('<c') in tostring(a))
1309
1310 - def test_tag_reset_root_ns(self):
1311 Element = self.etree.Element 1312 SubElement = self.etree.SubElement 1313 tostring = self.etree.tostring 1314 1315 a = Element('{a}a') 1316 b1 = SubElement(a, '{a}b') 1317 b2 = SubElement(a, '{b}b') 1318 1319 a.tag = 'c' 1320 1321 self.assertEquals( 1322 'c', 1323 a.tag) 1324 1325 # can't use C14N here! 1326 self.assertEquals('c', a.tag) 1327 self.assertEquals(_bytes('<c'), tostring(a)[:2])
1328
1329 - def test_tag_str_subclass(self):
1330 Element = self.etree.Element 1331 1332 class strTest(str): 1333 pass
1334 1335 a = Element("a") 1336 a.tag = strTest("TAG") 1337 self.assertXML(_bytes('<TAG></TAG>'), 1338 a) 1339
1340 - def test_delitem(self):
1341 Element = self.etree.Element 1342 SubElement = self.etree.SubElement 1343 1344 a = Element('a') 1345 b = SubElement(a, 'b') 1346 c = SubElement(a, 'c') 1347 d = SubElement(a, 'd') 1348 1349 del a[1] 1350 self.assertXML( 1351 _bytes('<a><b></b><d></d></a>'), 1352 a) 1353 1354 del a[0] 1355 self.assertXML( 1356 _bytes('<a><d></d></a>'), 1357 a) 1358 1359 del a[0] 1360 self.assertXML( 1361 _bytes('<a></a>'), 1362 a) 1363 # move deleted element into other tree afterwards 1364 other = Element('other') 1365 other.append(c) 1366 self.assertXML( 1367 _bytes('<other><c></c></other>'), 1368 other)
1369
1370 - def test_del_insert(self):
1371 Element = self.etree.Element 1372 SubElement = self.etree.SubElement 1373 1374 a = Element('a') 1375 b = SubElement(a, 'b') 1376 bs = SubElement(b, 'bs') 1377 c = SubElement(a, 'c') 1378 cs = SubElement(c, 'cs') 1379 1380 el = a[0] 1381 self.assertXML( 1382 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1383 a) 1384 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1385 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1386 1387 del a[0] 1388 self.assertXML( 1389 _bytes('<a><c><cs></cs></c></a>'), 1390 a) 1391 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1392 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1393 1394 a.insert(0, el) 1395 self.assertXML( 1396 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1397 a) 1398 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1399 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1400
1401 - def test_del_setitem(self):
1402 Element = self.etree.Element 1403 SubElement = self.etree.SubElement 1404 1405 a = Element('a') 1406 b = SubElement(a, 'b') 1407 bs = SubElement(b, 'bs') 1408 c = SubElement(a, 'c') 1409 cs = SubElement(c, 'cs') 1410 1411 el = a[0] 1412 del a[0] 1413 a[0] = el 1414 self.assertXML( 1415 _bytes('<a><b><bs></bs></b></a>'), 1416 a) 1417 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1418 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1419
1420 - def test_del_setslice(self):
1421 Element = self.etree.Element 1422 SubElement = self.etree.SubElement 1423 1424 a = Element('a') 1425 b = SubElement(a, 'b') 1426 bs = SubElement(b, 'bs') 1427 c = SubElement(a, 'c') 1428 cs = SubElement(c, 'cs') 1429 1430 el = a[0] 1431 del a[0] 1432 a[0:0] = [el] 1433 self.assertXML( 1434 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1435 a) 1436 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1437 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1438
1439 - def test_replace_slice_tail(self):
1440 XML = self.etree.XML 1441 a = XML(_bytes('<a><b></b>B2<c></c>C2</a>')) 1442 b, c = a 1443 1444 a[:] = [] 1445 1446 self.assertEquals("B2", b.tail) 1447 self.assertEquals("C2", c.tail)
1448
1449 - def test_merge_namespaced_subtree_as_slice(self):
1450 XML = self.etree.XML 1451 root = XML(_bytes( 1452 '<foo><bar xmlns:baz="http://huhu"><puh><baz:bump1 /><baz:bump2 /></puh></bar></foo>')) 1453 root[:] = root.findall('.//puh') # delete bar from hierarchy 1454 1455 # previously, this lost a namespace declaration on bump2 1456 result = self.etree.tostring(root) 1457 foo = self.etree.fromstring(result) 1458 1459 self.assertEquals('puh', foo[0].tag) 1460 self.assertEquals('{http://huhu}bump1', foo[0][0].tag) 1461 self.assertEquals('{http://huhu}bump2', foo[0][1].tag)
1462
1463 - def test_delitem_tail(self):
1464 ElementTree = self.etree.ElementTree 1465 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1466 doc = ElementTree(file=f) 1467 a = doc.getroot() 1468 del a[0] 1469 self.assertXML( 1470 _bytes('<a><c></c>C2</a>'), 1471 a)
1472
1473 - def test_clear(self):
1474 Element = self.etree.Element 1475 1476 a = Element('a') 1477 a.text = 'foo' 1478 a.tail = 'bar' 1479 a.set('hoi', 'dag') 1480 a.clear() 1481 self.assertEquals(None, a.text) 1482 self.assertEquals(None, a.tail) 1483 self.assertEquals(None, a.get('hoi')) 1484 self.assertEquals('a', a.tag)
1485
1486 - def test_clear_sub(self):
1487 Element = self.etree.Element 1488 SubElement = self.etree.SubElement 1489 1490 a = Element('a') 1491 a.text = 'foo' 1492 a.tail = 'bar' 1493 a.set('hoi', 'dag') 1494 b = SubElement(a, 'b') 1495 c = SubElement(b, 'c') 1496 a.clear() 1497 self.assertEquals(None, a.text) 1498 self.assertEquals(None, a.tail) 1499 self.assertEquals(None, a.get('hoi')) 1500 self.assertEquals('a', a.tag) 1501 self.assertEquals(0, len(a)) 1502 self.assertXML(_bytes('<a></a>'), 1503 a) 1504 self.assertXML(_bytes('<b><c></c></b>'), 1505 b)
1506
1507 - def test_clear_tail(self):
1508 ElementTree = self.etree.ElementTree 1509 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1510 doc = ElementTree(file=f) 1511 a = doc.getroot() 1512 a.clear() 1513 self.assertXML( 1514 _bytes('<a></a>'), 1515 a)
1516
1517 - def test_insert(self):
1518 Element = self.etree.Element 1519 SubElement = self.etree.SubElement 1520 1521 a = Element('a') 1522 b = SubElement(a, 'b') 1523 c = SubElement(a, 'c') 1524 d = Element('d') 1525 a.insert(0, d) 1526 1527 self.assertEquals( 1528 d, 1529 a[0]) 1530 1531 self.assertXML( 1532 _bytes('<a><d></d><b></b><c></c></a>'), 1533 a) 1534 1535 e = Element('e') 1536 a.insert(2, e) 1537 self.assertEquals( 1538 e, 1539 a[2]) 1540 self.assertXML( 1541 _bytes('<a><d></d><b></b><e></e><c></c></a>'), 1542 a)
1543
1544 - def test_insert_beyond_index(self):
1545 Element = self.etree.Element 1546 SubElement = self.etree.SubElement 1547 1548 a = Element('a') 1549 b = SubElement(a, 'b') 1550 c = Element('c') 1551 1552 a.insert(2, c) 1553 self.assertEquals( 1554 c, 1555 a[1]) 1556 self.assertXML( 1557 _bytes('<a><b></b><c></c></a>'), 1558 a)
1559
1560 - def test_insert_negative(self):
1561 Element = self.etree.Element 1562 SubElement = self.etree.SubElement 1563 1564 a = Element('a') 1565 b = SubElement(a, 'b') 1566 c = SubElement(a, 'c') 1567 1568 d = Element('d') 1569 a.insert(-1, d) 1570 self.assertEquals( 1571 d, 1572 a[-2]) 1573 self.assertXML( 1574 _bytes('<a><b></b><d></d><c></c></a>'), 1575 a)
1576
1577 - def test_insert_tail(self):
1578 Element = self.etree.Element 1579 SubElement = self.etree.SubElement 1580 1581 a = Element('a') 1582 b = SubElement(a, 'b') 1583 1584 c = Element('c') 1585 c.tail = 'C2' 1586 1587 a.insert(0, c) 1588 self.assertXML( 1589 _bytes('<a><c></c>C2<b></b></a>'), 1590 a)
1591
1592 - def test_remove(self):
1593 Element = self.etree.Element 1594 SubElement = self.etree.SubElement 1595 1596 a = Element('a') 1597 b = SubElement(a, 'b') 1598 c = SubElement(a, 'c') 1599 1600 a.remove(b) 1601 self.assertEquals( 1602 c, 1603 a[0]) 1604 self.assertXML( 1605 _bytes('<a><c></c></a>'), 1606 a)
1607
1608 - def test_remove_ns(self):
1609 Element = self.etree.Element 1610 SubElement = self.etree.SubElement 1611 1612 a = Element('{http://test}a') 1613 b = SubElement(a, '{http://test}b') 1614 c = SubElement(a, '{http://test}c') 1615 1616 a.remove(b) 1617 self.assertXML( 1618 _bytes('<ns0:a xmlns:ns0="http://test"><ns0:c></ns0:c></ns0:a>'), 1619 a) 1620 self.assertXML( 1621 _bytes('<ns0:b xmlns:ns0="http://test"></ns0:b>'), 1622 b)
1623
1624 - def test_remove_nonexisting(self):
1625 Element = self.etree.Element 1626 SubElement = self.etree.SubElement 1627 1628 a = Element('a') 1629 b = SubElement(a, 'b') 1630 c = SubElement(a, 'c') 1631 d = Element('d') 1632 self.assertRaises( 1633 ValueError, a.remove, d)
1634
1635 - def test_remove_tail(self):
1636 Element = self.etree.Element 1637 SubElement = self.etree.SubElement 1638 1639 a = Element('a') 1640 b = SubElement(a, 'b') 1641 b.tail = 'b2' 1642 a.remove(b) 1643 self.assertXML( 1644 _bytes('<a></a>'), 1645 a) 1646 self.assertEquals('b2', b.tail)
1647
1648 - def _test_getchildren(self):
1649 Element = self.etree.Element 1650 SubElement = self.etree.SubElement 1651 1652 a = Element('a') 1653 b = SubElement(a, 'b') 1654 c = SubElement(a, 'c') 1655 d = SubElement(b, 'd') 1656 e = SubElement(c, 'e') 1657 self.assertXML( 1658 _bytes('<a><b><d></d></b><c><e></e></c></a>'), 1659 a) 1660 self.assertEquals( 1661 [b, c], 1662 a.getchildren()) 1663 self.assertEquals( 1664 [d], 1665 b.getchildren()) 1666 self.assertEquals( 1667 [], 1668 d.getchildren())
1669
1670 - def test_makeelement(self):
1671 Element = self.etree.Element 1672 1673 a = Element('a') 1674 b = a.makeelement('c', {'hoi':'dag'}) 1675 self.assertXML( 1676 _bytes('<c hoi="dag"></c>'), 1677 b)
1678 1679 required_versions_ET['test_iter'] = (1,3)
1680 - def test_iter(self):
1681 Element = self.etree.Element 1682 SubElement = self.etree.SubElement 1683 1684 a = Element('a') 1685 b = SubElement(a, 'b') 1686 c = SubElement(a, 'c') 1687 d = SubElement(b, 'd') 1688 e = SubElement(c, 'e') 1689 1690 self.assertEquals( 1691 [a, b, d, c, e], 1692 list(a.iter())) 1693 self.assertEquals( 1694 [d], 1695 list(d.iter()))
1696
1697 - def test_getiterator(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(b, 'd') 1705 e = SubElement(c, 'e') 1706 1707 self.assertEquals( 1708 [a, b, d, c, e], 1709 list(a.getiterator())) 1710 self.assertEquals( 1711 [d], 1712 list(d.getiterator()))
1713
1714 - def test_getiterator_empty(self):
1715 Element = self.etree.Element 1716 SubElement = self.etree.SubElement 1717 1718 a = Element('a') 1719 b = SubElement(a, 'b') 1720 c = SubElement(a, 'c') 1721 d = SubElement(b, 'd') 1722 e = SubElement(c, 'e') 1723 1724 self.assertEquals( 1725 [], 1726 list(a.getiterator('none'))) 1727 self.assertEquals( 1728 [], 1729 list(e.getiterator('none'))) 1730 self.assertEquals( 1731 [e], 1732 list(e.getiterator()))
1733
1734 - def test_getiterator_filter(self):
1735 Element = self.etree.Element 1736 SubElement = self.etree.SubElement 1737 1738 a = Element('a') 1739 b = SubElement(a, 'b') 1740 c = SubElement(a, 'c') 1741 d = SubElement(b, 'd') 1742 e = SubElement(c, 'e') 1743 1744 self.assertEquals( 1745 [a], 1746 list(a.getiterator('a'))) 1747 a2 = SubElement(e, 'a') 1748 self.assertEquals( 1749 [a, a2], 1750 list(a.getiterator('a'))) 1751 self.assertEquals( 1752 [a2], 1753 list(c.getiterator('a')))
1754
1755 - def test_getiterator_filter_all(self):
1756 Element = self.etree.Element 1757 SubElement = self.etree.SubElement 1758 1759 a = Element('a') 1760 b = SubElement(a, 'b') 1761 c = SubElement(a, 'c') 1762 d = SubElement(b, 'd') 1763 e = SubElement(c, 'e') 1764 1765 self.assertEquals( 1766 [a, b, d, c, e], 1767 list(a.getiterator('*')))
1768
1769 - def test_getiterator_filter_comment(self):
1770 Element = self.etree.Element 1771 Comment = self.etree.Comment 1772 SubElement = self.etree.SubElement 1773 1774 a = Element('a') 1775 b = SubElement(a, 'b') 1776 comment_b = Comment("TEST-b") 1777 b.append(comment_b) 1778 1779 self.assertEquals( 1780 [comment_b], 1781 list(a.getiterator(Comment))) 1782 1783 comment_a = Comment("TEST-a") 1784 a.append(comment_a) 1785 1786 self.assertEquals( 1787 [comment_b, comment_a], 1788 list(a.getiterator(Comment))) 1789 1790 self.assertEquals( 1791 [comment_b], 1792 list(b.getiterator(Comment)))
1793
1794 - def test_getiterator_filter_pi(self):
1795 Element = self.etree.Element 1796 PI = self.etree.ProcessingInstruction 1797 SubElement = self.etree.SubElement 1798 1799 a = Element('a') 1800 b = SubElement(a, 'b') 1801 pi_b = PI("TEST-b") 1802 b.append(pi_b) 1803 1804 self.assertEquals( 1805 [pi_b], 1806 list(a.getiterator(PI))) 1807 1808 pi_a = PI("TEST-a") 1809 a.append(pi_a) 1810 1811 self.assertEquals( 1812 [pi_b, pi_a], 1813 list(a.getiterator(PI))) 1814 1815 self.assertEquals( 1816 [pi_b], 1817 list(b.getiterator(PI)))
1818
1819 - def test_getiterator_with_text(self):
1820 Element = self.etree.Element 1821 SubElement = self.etree.SubElement 1822 1823 a = Element('a') 1824 a.text = 'a' 1825 b = SubElement(a, 'b') 1826 b.text = 'b' 1827 b.tail = 'b1' 1828 c = SubElement(a, 'c') 1829 c.text = 'c' 1830 c.tail = 'c1' 1831 d = SubElement(b, 'd') 1832 c.text = 'd' 1833 c.tail = 'd1' 1834 e = SubElement(c, 'e') 1835 e.text = 'e' 1836 e.tail = 'e1' 1837 1838 self.assertEquals( 1839 [a, b, d, c, e], 1840 list(a.getiterator()))
1841 #self.assertEquals( 1842 # [d], 1843 # list(d.getiterator())) 1844
1845 - def test_getiterator_filter_with_text(self):
1846 Element = self.etree.Element 1847 SubElement = self.etree.SubElement 1848 1849 a = Element('a') 1850 a.text = 'a' 1851 b = SubElement(a, 'b') 1852 b.text = 'b' 1853 b.tail = 'b1' 1854 c = SubElement(a, 'c') 1855 c.text = 'c' 1856 c.tail = 'c1' 1857 d = SubElement(b, 'd') 1858 c.text = 'd' 1859 c.tail = 'd1' 1860 e = SubElement(c, 'e') 1861 e.text = 'e' 1862 e.tail = 'e1' 1863 1864 self.assertEquals( 1865 [a], 1866 list(a.getiterator('a'))) 1867 a2 = SubElement(e, 'a') 1868 self.assertEquals( 1869 [a, a2], 1870 list(a.getiterator('a'))) 1871 self.assertEquals( 1872 [a2], 1873 list(e.getiterator('a')))
1874
1875 - def test_getslice(self):
1876 Element = self.etree.Element 1877 SubElement = self.etree.SubElement 1878 1879 a = Element('a') 1880 b = SubElement(a, 'b') 1881 c = SubElement(a, 'c') 1882 d = SubElement(a, 'd') 1883 1884 self.assertEquals( 1885 [b, c], 1886 a[0:2]) 1887 self.assertEquals( 1888 [b, c, d], 1889 a[:]) 1890 self.assertEquals( 1891 [b, c, d], 1892 a[:10]) 1893 self.assertEquals( 1894 [b], 1895 a[0:1]) 1896 self.assertEquals( 1897 [], 1898 a[10:12])
1899
1900 - def test_getslice_negative(self):
1901 Element = self.etree.Element 1902 SubElement = self.etree.SubElement 1903 1904 a = Element('a') 1905 b = SubElement(a, 'b') 1906 c = SubElement(a, 'c') 1907 d = SubElement(a, 'd') 1908 1909 self.assertEquals( 1910 [d], 1911 a[-1:]) 1912 self.assertEquals( 1913 [c, d], 1914 a[-2:]) 1915 self.assertEquals( 1916 [c], 1917 a[-2:-1]) 1918 self.assertEquals( 1919 [b, c], 1920 a[-3:-1]) 1921 self.assertEquals( 1922 [b, c], 1923 a[-3:2])
1924
1925 - def test_getslice_step(self):
1926 Element = self.etree.Element 1927 SubElement = self.etree.SubElement 1928 1929 a = Element('a') 1930 b = SubElement(a, 'b') 1931 c = SubElement(a, 'c') 1932 d = SubElement(a, 'd') 1933 e = SubElement(a, 'e') 1934 1935 self.assertEquals( 1936 [e,d,c,b], 1937 a[::-1]) 1938 self.assertEquals( 1939 [b,d], 1940 a[::2]) 1941 self.assertEquals( 1942 [e,c], 1943 a[::-2]) 1944 self.assertEquals( 1945 [d,c], 1946 a[-2:0:-1]) 1947 self.assertEquals( 1948 [e], 1949 a[:1:-2])
1950
1951 - def test_getslice_text(self):
1952 ElementTree = self.etree.ElementTree 1953 1954 f = BytesIO('<a><b>B</b>B1<c>C</c>C1</a>') 1955 doc = ElementTree(file=f) 1956 a = doc.getroot() 1957 b = a[0] 1958 c = a[1] 1959 self.assertEquals( 1960 [b, c], 1961 a[:]) 1962 self.assertEquals( 1963 [b], 1964 a[0:1]) 1965 self.assertEquals( 1966 [c], 1967 a[1:])
1968
1969 - def test_comment_getitem_getslice(self):
1970 Element = self.etree.Element 1971 Comment = self.etree.Comment 1972 SubElement = self.etree.SubElement 1973 1974 a = Element('a') 1975 b = SubElement(a, 'b') 1976 foo = Comment('foo') 1977 a.append(foo) 1978 c = SubElement(a, 'c') 1979 self.assertEquals( 1980 [b, foo, c], 1981 a[:]) 1982 self.assertEquals( 1983 foo, 1984 a[1]) 1985 a[1] = new = Element('new') 1986 self.assertEquals( 1987 new, 1988 a[1]) 1989 self.assertXML( 1990 _bytes('<a><b></b><new></new><c></c></a>'), 1991 a)
1992
1993 - def test_delslice(self):
1994 Element = self.etree.Element 1995 SubElement = self.etree.SubElement 1996 1997 a = Element('a') 1998 b = SubElement(a, 'b') 1999 c = SubElement(a, 'c') 2000 d = SubElement(a, 'd') 2001 e = SubElement(a, 'e') 2002 2003 del a[1:3] 2004 self.assertEquals( 2005 [b, e], 2006 list(a))
2007
2008 - def test_delslice_negative1(self):
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 d = SubElement(a, 'd') 2016 e = SubElement(a, 'e') 2017 2018 del a[1:-1] 2019 self.assertEquals( 2020 [b, e], 2021 list(a))
2022
2023 - def test_delslice_negative2(self):
2024 Element = self.etree.Element 2025 SubElement = self.etree.SubElement 2026 2027 a = Element('a') 2028 b = SubElement(a, 'b') 2029 c = SubElement(a, 'c') 2030 d = SubElement(a, 'd') 2031 e = SubElement(a, 'e') 2032 2033 del a[-3:-1] 2034 self.assertEquals( 2035 [b, e], 2036 list(a))
2037
2038 - def test_delslice_step(self):
2039 Element = self.etree.Element 2040 SubElement = self.etree.SubElement 2041 2042 a = Element('a') 2043 b = SubElement(a, 'b') 2044 c = SubElement(a, 'c') 2045 d = SubElement(a, 'd') 2046 e = SubElement(a, 'e') 2047 2048 del a[1::2] 2049 self.assertEquals( 2050 [b, d], 2051 list(a))
2052
2053 - def test_delslice_step_negative(self):
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(a, 'd') 2061 e = SubElement(a, 'e') 2062 2063 del a[::-1] 2064 self.assertEquals( 2065 [], 2066 list(a))
2067
2068 - def test_delslice_step_negative2(self):
2069 Element = self.etree.Element 2070 SubElement = self.etree.SubElement 2071 2072 a = Element('a') 2073 b = SubElement(a, 'b') 2074 c = SubElement(a, 'c') 2075 d = SubElement(a, 'd') 2076 e = SubElement(a, 'e') 2077 2078 del a[::-2] 2079 self.assertEquals( 2080 [b, d], 2081 list(a))
2082
2083 - def test_delslice_child_tail(self):
2084 ElementTree = self.etree.ElementTree 2085 f = BytesIO('<a><b></b>B2<c></c>C2<d></d>D2<e></e>E2</a>') 2086 doc = ElementTree(file=f) 2087 a = doc.getroot() 2088 del a[1:3] 2089 self.assertXML( 2090 _bytes('<a><b></b>B2<e></e>E2</a>'), 2091 a)
2092
2093 - def test_delslice_tail(self):
2094 XML = self.etree.XML 2095 a = XML(_bytes('<a><b></b>B2<c></c>C2</a>')) 2096 b, c = a 2097 2098 del a[:] 2099 2100 self.assertEquals("B2", b.tail) 2101 self.assertEquals("C2", c.tail)
2102
2103 - def test_delslice_memory(self):
2104 # this could trigger a crash 2105 Element = self.etree.Element 2106 SubElement = self.etree.SubElement 2107 a = Element('a') 2108 b = SubElement(a, 'b') 2109 c = SubElement(b, 'c') 2110 del b # no more reference to b 2111 del a[:] 2112 self.assertEquals('c', c.tag)
2113
2114 - def test_setslice(self):
2115 Element = self.etree.Element 2116 SubElement = self.etree.SubElement 2117 2118 a = Element('a') 2119 b = SubElement(a, 'b') 2120 c = SubElement(a, 'c') 2121 d = SubElement(a, 'd') 2122 2123 e = Element('e') 2124 f = Element('f') 2125 g = Element('g') 2126 2127 s = [e, f, g] 2128 a[1: