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): 
  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({'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_keys(self):
443 XML = self.etree.XML 444 445 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 446 keys = list(root.attrib.keys()) 447 keys.sort() 448 self.assertEquals(['alpha', 'beta', 'gamma'], keys)
449
450 - def test_attribute_keys2(self):
451 XML = self.etree.XML 452 453 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 454 keys = list(root.keys()) 455 keys.sort() 456 self.assertEquals(['alpha', 'beta', 'gamma'], keys)
457
458 - def test_attribute_items2(self):
459 XML = self.etree.XML 460 461 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 462 items = list(root.items()) 463 items.sort() 464 self.assertEquals( 465 [('alpha','Alpha'), ('beta','Beta'), ('gamma','Gamma')], 466 items)
467
468 - def test_attribute_keys_ns(self):
469 XML = self.etree.XML 470 471 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 472 keys = list(root.keys()) 473 keys.sort() 474 self.assertEquals(['bar', '{http://ns.codespeak.net/test}baz'], 475 keys)
476
477 - def test_attribute_values(self):
478 XML = self.etree.XML 479 480 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 481 values = list(root.attrib.values()) 482 values.sort() 483 self.assertEquals(['Alpha', 'Beta', 'Gamma'], values)
484
485 - def test_attribute_values_ns(self):
486 XML = self.etree.XML 487 488 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 489 values = list(root.attrib.values()) 490 values.sort() 491 self.assertEquals( 492 ['Bar', 'Baz'], values)
493
494 - def test_attribute_items(self):
495 XML = self.etree.XML 496 497 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma"/>')) 498 items = list(root.attrib.items()) 499 items.sort() 500 self.assertEquals([ 501 ('alpha', 'Alpha'), 502 ('beta', 'Beta'), 503 ('gamma', 'Gamma'), 504 ], 505 items)
506
507 - def test_attribute_items_ns(self):
508 XML = self.etree.XML 509 510 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 511 items = list(root.attrib.items()) 512 items.sort() 513 self.assertEquals( 514 [('bar', 'Bar'), ('{http://ns.codespeak.net/test}baz', 'Baz')], 515 items)
516
517 - def test_attribute_str(self):
518 XML = self.etree.XML 519 520 expected = "{'{http://ns.codespeak.net/test}baz': 'Baz', 'bar': 'Bar'}" 521 alternative = "{'bar': 'Bar', '{http://ns.codespeak.net/test}baz': 'Baz'}" 522 523 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 524 try: 525 self.assertEquals(expected, str(root.attrib)) 526 except AssertionError: 527 self.assertEquals(alternative, str(root.attrib))
528
529 - def test_attribute_contains(self):
530 XML = self.etree.XML 531 532 root = XML(_bytes('<foo bar="Bar" xmlns:ns="http://ns.codespeak.net/test" ns:baz="Baz" />')) 533 self.assertEquals( 534 True, 'bar' in root.attrib) 535 self.assertEquals( 536 False, 'baz' in root.attrib) 537 self.assertEquals( 538 False, 'hah' in root.attrib) 539 self.assertEquals( 540 True, 541 '{http://ns.codespeak.net/test}baz' in root.attrib)
542
543 - def test_attribute_set(self):
544 Element = self.etree.Element 545 546 root = Element("root") 547 root.set("attr", "TEST") 548 self.assertEquals("TEST", root.get("attr"))
549
550 - def test_attribute_iterator(self):
551 XML = self.etree.XML 552 553 root = XML(_bytes('<doc alpha="Alpha" beta="Beta" gamma="Gamma" />')) 554 result = [] 555 for key in root.attrib: 556 result.append(key) 557 result.sort() 558 self.assertEquals(['alpha', 'beta', 'gamma'], result)
559
560 - def test_attribute_manipulation(self):
561 Element = self.etree.Element 562 563 a = Element('a') 564 a.attrib['foo'] = 'Foo' 565 a.attrib['bar'] = 'Bar' 566 self.assertEquals('Foo', a.attrib['foo']) 567 del a.attrib['foo'] 568 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')
569
570 - def test_del_attribute_ns(self):
571 Element = self.etree.Element 572 573 a = Element('a') 574 a.attrib['{http://a/}foo'] = 'Foo' 575 a.attrib['{http://a/}bar'] = 'Bar' 576 self.assertEquals(None, a.get('foo')) 577 self.assertEquals('Foo', a.get('{http://a/}foo')) 578 self.assertEquals('Foo', a.attrib['{http://a/}foo']) 579 580 self.assertRaises(KeyError, operator.delitem, a.attrib, 'foo') 581 self.assertEquals('Foo', a.attrib['{http://a/}foo']) 582 583 del a.attrib['{http://a/}foo'] 584 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')
585
586 - def test_del_attribute_ns_parsed(self):
587 XML = self.etree.XML 588 589 a = XML(_bytes('<a xmlns:nsa="http://a/" nsa:foo="FooNS" foo="Foo" />')) 590 591 self.assertEquals('Foo', a.attrib['foo']) 592 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 593 594 del a.attrib['foo'] 595 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 596 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 597 self.assertRaises(KeyError, operator.delitem, a.attrib, 'foo') 598 599 del a.attrib['{http://a/}foo'] 600 self.assertRaises(KeyError, operator.getitem, a.attrib, '{http://a/}foo') 601 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 602 603 a = XML(_bytes('<a xmlns:nsa="http://a/" foo="Foo" nsa:foo="FooNS" />')) 604 605 self.assertEquals('Foo', a.attrib['foo']) 606 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 607 608 del a.attrib['foo'] 609 self.assertEquals('FooNS', a.attrib['{http://a/}foo']) 610 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo') 611 612 del a.attrib['{http://a/}foo'] 613 self.assertRaises(KeyError, operator.getitem, a.attrib, '{http://a/}foo') 614 self.assertRaises(KeyError, operator.getitem, a.attrib, 'foo')
615
616 - def test_XML(self):
617 XML = self.etree.XML 618 619 root = XML(_bytes('<doc>This is a text.</doc>')) 620 self.assertEquals(0, len(root)) 621 self.assertEquals('This is a text.', root.text)
622
623 - def test_XMLID(self):
624 XMLID = self.etree.XMLID 625 XML = self.etree.XML 626 xml_text = _bytes(''' 627 <document> 628 <h1 id="chapter1">...</h1> 629 <p id="note1" class="note">...</p> 630 <p>Regular paragraph.</p> 631 <p xml:id="xmlid">XML:ID paragraph.</p> 632 <p id="warn1" class="warning">...</p> 633 </document> 634 ''') 635 636 root, dic = XMLID(xml_text) 637 root2 = XML(xml_text) 638 self.assertEquals(self._writeElement(root), 639 self._writeElement(root2)) 640 expected = { 641 "chapter1" : root[0], 642 "note1" : root[1], 643 "warn1" : root[4] 644 } 645 self.assertEquals(dic, expected)
646
647 - def test_fromstring(self):
648 fromstring = self.etree.fromstring 649 650 root = fromstring('<doc>This is a text.</doc>') 651 self.assertEquals(0, len(root)) 652 self.assertEquals('This is a text.', root.text)
653 654 required_versions_ET['test_fromstringlist'] = (1,3)
655 - def test_fromstringlist(self):
656 fromstringlist = self.etree.fromstringlist 657 658 root = fromstringlist(["<do", "c>T", "hi", "s is", 659 " a text.<", "/doc", ">"]) 660 self.assertEquals(0, len(root)) 661 self.assertEquals('This is a text.', root.text)
662 663 required_versions_ET['test_fromstringlist_characters'] = (1,3)
664 - def test_fromstringlist_characters(self):
665 fromstringlist = self.etree.fromstringlist 666 667 root = fromstringlist(list('<doc>This is a text.</doc>')) 668 self.assertEquals(0, len(root)) 669 self.assertEquals('This is a text.', root.text)
670 671 required_versions_ET['test_fromstringlist_single'] = (1,3)
672 - def test_fromstringlist_single(self):
673 fromstringlist = self.etree.fromstringlist 674 675 root = fromstringlist(['<doc>This is a text.</doc>']) 676 self.assertEquals(0, len(root)) 677 self.assertEquals('This is a text.', root.text)
678
679 - def test_iselement(self):
680 iselement = self.etree.iselement 681 Element = self.etree.Element 682 ElementTree = self.etree.ElementTree 683 XML = self.etree.XML 684 Comment = self.etree.Comment 685 ProcessingInstruction = self.etree.ProcessingInstruction 686 687 el = Element('hoi') 688 self.assert_(iselement(el)) 689 690 el2 = XML(_bytes('<foo/>')) 691 self.assert_(iselement(el2)) 692 693 tree = ElementTree(element=Element('dag')) 694 self.assert_(not iselement(tree)) 695 self.assert_(iselement(tree.getroot())) 696 697 c = Comment('test') 698 self.assert_(iselement(c)) 699 700 p = ProcessingInstruction("test", "some text") 701 self.assert_(iselement(p))
702
703 - def test_iteration(self):
704 XML = self.etree.XML 705 706 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 707 result = [] 708 for el in root: 709 result.append(el.tag) 710 self.assertEquals(['one', 'two', 'three'], result)
711
712 - def test_iteration_empty(self):
713 XML = self.etree.XML 714 715 root = XML(_bytes('<doc></doc>')) 716 result = [] 717 for el in root: 718 result.append(el.tag) 719 self.assertEquals([], result)
720
721 - def test_iteration_text_only(self):
722 XML = self.etree.XML 723 724 root = XML(_bytes('<doc>Text</doc>')) 725 result = [] 726 for el in root: 727 result.append(el.tag) 728 self.assertEquals([], result)
729
730 - def test_iteration_crash(self):
731 # this would cause a crash in the past 732 fromstring = self.etree.fromstring 733 root = etree.fromstring('<html><p></p>x</html>') 734 for elem in root: 735 elem.tail = ''
736
737 - def test_iteration_reversed(self):
738 XML = self.etree.XML 739 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 740 result = [] 741 for el in reversed(root): 742 result.append(el.tag) 743 self.assertEquals(['three', 'two', 'one'], result)
744
745 - def test_iteration_subelement(self):
746 XML = self.etree.XML 747 748 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 749 result = [] 750 add = True 751 for el in root: 752 result.append(el.tag) 753 if add: 754 self.etree.SubElement(root, 'four') 755 add = False 756 self.assertEquals(['one', 'two', 'three', 'four'], result)
757
758 - def test_iteration_del_child(self):
759 XML = self.etree.XML 760 761 root = XML(_bytes('<doc><one/><two>Two</two>Hm<three/></doc>')) 762 result = [] 763 for el in root: 764 result.append(el.tag) 765 del root[-1] 766 self.assertEquals(['one', 'two'], result)
767
768 - def test_iteration_double(self):
769 XML = self.etree.XML 770 771 root = XML(_bytes('<doc><one/><two/></doc>')) 772 result = [] 773 for el0 in root: 774 result.append(el0.tag) 775 for el1 in root: 776 result.append(el1.tag) 777 self.assertEquals(['one','one', 'two', 'two', 'one', 'two'], result)
778 779 required_versions_ET['test_itertext'] = (1,3)
780 - def test_itertext(self):
781 # ET 1.3+ 782 XML = self.etree.XML 783 root = XML(_bytes("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")) 784 785 text = list(root.itertext()) 786 self.assertEquals(["RTEXT", "ATAIL", "CTEXT", "CTAIL"], 787 text)
788 789 required_versions_ET['test_itertext_child'] = (1,3)
790 - def test_itertext_child(self):
791 # ET 1.3+ 792 XML = self.etree.XML 793 root = XML(_bytes("<root>RTEXT<a></a>ATAIL<b/><c>CTEXT</c>CTAIL</root>")) 794 795 text = list(root[2].itertext()) 796 self.assertEquals(["CTEXT"], 797 text)
798
799 - def test_findall(self):
800 XML = self.etree.XML 801 root = XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')) 802 self.assertEquals(len(list(root.findall("c"))), 1) 803 self.assertEquals(len(list(root.findall(".//c"))), 2) 804 self.assertEquals(len(list(root.findall(".//b"))), 3) 805 self.assertEquals(len(list(root.findall(".//b"))[0]), 1) 806 self.assertEquals(len(list(root.findall(".//b"))[1]), 0) 807 self.assertEquals(len(list(root.findall(".//b"))[2]), 0)
808
809 - def test_findall_ns(self):
810 XML = self.etree.XML 811 root = XML(_bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')) 812 self.assertEquals(len(list(root.findall(".//{X}b"))), 2) 813 self.assertEquals(len(list(root.findall(".//b"))), 3) 814 self.assertEquals(len(list(root.findall("b"))), 2)
815
816 - def test_element_with_attributes_keywords(self):
817 Element = self.etree.Element 818 819 el = Element('tag', foo='Foo', bar='Bar') 820 self.assertEquals('Foo', el.attrib['foo']) 821 self.assertEquals('Bar', el.attrib['bar'])
822
823 - def test_element_with_attributes(self):
824 Element = self.etree.Element 825 826 el = Element('tag', {'foo':'Foo', 'bar':'Bar'}) 827 self.assertEquals('Foo', el.attrib['foo']) 828 self.assertEquals('Bar', el.attrib['bar'])
829
830 - def test_element_with_attributes_ns(self):
831 Element = self.etree.Element 832 833 el = Element('tag', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 834 self.assertEquals('Foo', el.attrib['{ns1}foo']) 835 self.assertEquals('Bar', el.attrib['{ns2}bar'])
836
837 - def test_subelement_with_attributes(self):
838 Element = self.etree.Element 839 SubElement = self.etree.SubElement 840 841 el = Element('tag') 842 SubElement(el, 'foo', {'foo':'Foo'}, baz="Baz") 843 self.assertEquals("Baz", el[0].attrib['baz']) 844 self.assertEquals('Foo', el[0].attrib['foo'])
845
846 - def test_subelement_with_attributes_ns(self):
847 Element = self.etree.Element 848 SubElement = self.etree.SubElement 849 850 el = Element('tag') 851 SubElement(el, 'foo', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 852 self.assertEquals('Foo', el[0].attrib['{ns1}foo']) 853 self.assertEquals('Bar', el[0].attrib['{ns2}bar'])
854
855 - def test_write(self):
856 ElementTree = self.etree.ElementTree 857 XML = self.etree.XML 858 859 for i in range(10): 860 f = BytesIO() 861 root = XML(_bytes('<doc%s>This is a test.</doc%s>' % (i, i))) 862 tree = ElementTree(element=root) 863 tree.write(f) 864 data = f.getvalue() 865 self.assertEquals( 866 _bytes('<doc%s>This is a test.</doc%s>' % (i, i)), 867 canonicalize(data))
868 869 required_versions_ET['test_write_method_html'] = (1,3)
870 - def test_write_method_html(self):
871 ElementTree = self.etree.ElementTree 872 Element = self.etree.Element 873 SubElement = self.etree.SubElement 874 875 html = Element('html') 876 body = SubElement(html, 'body') 877 p = SubElement(body, 'p') 878 p.text = "html" 879 SubElement(p, 'br').tail = "test" 880 881 tree = ElementTree(element=html) 882 f = BytesIO() 883 tree.write(f, method="html") 884 data = f.getvalue().replace(_bytes('\n'),_bytes('')) 885 886 self.assertEquals(_bytes('<html><body><p>html<br>test</p></body></html>'), 887 data)
888 889 required_versions_ET['test_write_method_text'] = (1,3)
890 - def test_write_method_text(self):
891 ElementTree = self.etree.ElementTree 892 Element = self.etree.Element 893 SubElement = self.etree.SubElement 894 895 a = Element('a') 896 a.text = "A" 897 a.tail = "tail" 898 b = SubElement(a, 'b') 899 b.text = "B" 900 b.tail = "TAIL" 901 c = SubElement(a, 'c') 902 c.text = "C" 903 904 tree = ElementTree(element=a) 905 f = BytesIO() 906 tree.write(f, method="text") 907 data = f.getvalue() 908 909 self.assertEquals(_bytes('ABTAILCtail'), 910 data)
911
912 - def test_write_fail(self):
913 ElementTree = self.etree.ElementTree 914 XML = self.etree.XML 915 916 tree = ElementTree( XML(_bytes('<doc>This is a test.</doc>')) ) 917 self.assertRaises(IOError, tree.write, 918 "definitely////\\-\\nonexisting\\-\\////FILE")
919 920 # this could trigger a crash, apparently because the document 921 # reference was prematurely garbage collected
922 - def test_crash(self):
923 Element = self.etree.Element 924 925 element = Element('tag') 926 for i in range(10): 927 element.attrib['key'] = 'value' 928 value = element.attrib['key'] 929 self.assertEquals(value, 'value')
930 931 # from doctest; for some reason this caused crashes too
932 - def test_write_ElementTreeDoctest(self):
933 Element = self.etree.Element 934 ElementTree = self.etree.ElementTree 935 936 f = BytesIO() 937 for i in range(10): 938 element = Element('tag%s' % i) 939 self._check_element(element) 940 tree = ElementTree(element) 941 tree.write(f) 942 self._check_element_tree(tree)
943
944 - def test_subelement_reference(self):
945 Element = self.etree.Element 946 SubElement = self.etree.SubElement 947 948 el = Element('foo') 949 el2 = SubElement(el, 'bar') 950 el3 = SubElement(el2, 'baz') 951 952 al = Element('foo2') 953 al2 = SubElement(al, 'bar2') 954 al3 = SubElement(al2, 'baz2') 955 956 # now move al2 into el 957 el.append(al2) 958 959 # now change al3 directly 960 al3.text = 'baz2-modified' 961 962 # it should have changed through this route too 963 self.assertEquals( 964 'baz2-modified', 965 el[1][0].text)
966
967 - def test_set_text(self):
968 Element = self.etree.Element 969 SubElement = self.etree.SubElement 970 971 a = Element('a') 972 b = SubElement(a, 'b') 973 a.text = 'hoi' 974 self.assertEquals( 975 'hoi', 976 a.text) 977 self.assertEquals( 978 'b', 979 a[0].tag)
980
981 - def test_set_text2(self):
982 Element = self.etree.Element 983 SubElement = self.etree.SubElement 984 985 a = Element('a') 986 a.text = 'hoi' 987 b = SubElement(a ,'b') 988 self.assertEquals( 989 'hoi', 990 a.text) 991 self.assertEquals( 992 'b', 993 a[0].tag)
994
995 - def test_set_text_none(self):
996 Element = self.etree.Element 997 998 a = Element('a') 999 1000 a.text = 'foo' 1001 a.text = None 1002 1003 self.assertEquals( 1004 None, 1005 a.text) 1006 self.assertXML(_bytes('<a></a>'), a)
1007
1008 - def test_set_text_empty(self):
1009 Element = self.etree.Element 1010 1011 a = Element('a') 1012 self.assertEquals(None, a.text) 1013 1014 a.text = '' 1015 self.assertEquals('', a.text) 1016 self.assertXML(_bytes('<a></a>'), a)
1017
1018 - def test_tail1(self):
1019 Element = self.etree.Element 1020 SubElement = self.etree.SubElement 1021 1022 a = Element('a') 1023 a.tail = 'dag' 1024 self.assertEquals('dag', 1025 a.tail) 1026 b = SubElement(a, 'b') 1027 b.tail = 'hoi' 1028 self.assertEquals('hoi', 1029 b.tail) 1030 self.assertEquals('dag', 1031 a.tail)
1032
1033 - def test_tail_append(self):
1034 Element = self.etree.Element 1035 1036 a = Element('a') 1037 b = Element('b') 1038 b.tail = 'b_tail' 1039 a.append(b) 1040 self.assertEquals('b_tail', 1041 b.tail)
1042
1043 - def test_tail_set_twice(self):
1044 Element = self.etree.Element 1045 SubElement = self.etree.SubElement 1046 1047 a = Element('a') 1048 b = SubElement(a, 'b') 1049 b.tail = 'foo' 1050 b.tail = 'bar' 1051 self.assertEquals('bar', 1052 b.tail) 1053 self.assertXML(_bytes('<a><b></b>bar</a>'), a)
1054
1055 - def test_tail_set_none(self):
1056 Element = self.etree.Element 1057 a = Element('a') 1058 a.tail = 'foo' 1059 a.tail = None 1060 self.assertEquals( 1061 None, 1062 a.tail) 1063 self.assertXML(_bytes('<a></a>'), a)
1064 1065 required_versions_ET['test_extend'] = (1,3)
1066 - def test_extend(self):
1067 root = self.etree.Element('foo') 1068 for i in range(3): 1069 element = self.etree.SubElement(root, 'a%s' % i) 1070 element.text = "text%d" % i 1071 element.tail = "tail%d" % i 1072 1073 elements = [] 1074 for i in range(3): 1075 new_element = self.etree.Element("test%s" % i) 1076 new_element.text = "TEXT%s" % i 1077 new_element.tail = "TAIL%s" % i 1078 elements.append(new_element) 1079 1080 root.extend(elements) 1081 1082 self.assertEquals( 1083 ["a0", "a1", "a2", "test0", "test1", "test2"], 1084 [ el.tag for el in root ]) 1085 self.assertEquals( 1086 ["text0", "text1", "text2", "TEXT0", "TEXT1", "TEXT2"], 1087 [ el.text for el in root ]) 1088 self.assertEquals( 1089 ["tail0", "tail1", "tail2", "TAIL0", "TAIL1", "TAIL2"], 1090 [ el.tail for el in root ])
1091
1092 - def test_comment(self):
1093 Element = self.etree.Element 1094 SubElement = self.etree.SubElement 1095 Comment = self.etree.Comment 1096 1097 a = Element('a') 1098 a.append(Comment('foo')) 1099 self.assertEquals(a[0].tag, Comment) 1100 self.assertEquals(a[0].text, 'foo')
1101 1102 # ElementTree < 1.3 adds whitespace around comments 1103 required_versions_ET['test_comment_text'] = (1,3)
1104 - def test_comment_text(self):
1105 Element = self.etree.Element 1106 SubElement = self.etree.SubElement 1107 Comment = self.etree.Comment 1108 tostring = self.etree.tostring 1109 1110 a = Element('a') 1111 a.append(Comment('foo')) 1112 self.assertEquals(a[0].text, 'foo') 1113 1114 self.assertEquals( 1115 _bytes('<a><!--foo--></a>'), 1116 tostring(a)) 1117 1118 a[0].text = "TEST" 1119 self.assertEquals(a[0].text, 'TEST') 1120 1121 self.assertEquals( 1122 _bytes('<a><!--TEST--></a>'), 1123 tostring(a))
1124 1125 # ElementTree < 1.3 adds whitespace around comments 1126 required_versions_ET['test_comment_whitespace'] = (1,3)
1127 - def test_comment_whitespace(self):
1128 Element = self.etree.Element 1129 SubElement = self.etree.SubElement 1130 Comment = self.etree.Comment 1131 tostring = self.etree.tostring 1132 1133 a = Element('a') 1134 a.append(Comment(' foo ')) 1135 self.assertEquals(a[0].text, ' foo ') 1136 self.assertEquals( 1137 _bytes('<a><!-- foo --></a>'), 1138 tostring(a))
1139
1140 - def test_comment_nonsense(self):
1141 Comment = self.etree.Comment 1142 c = Comment('foo') 1143 self.assertEquals({}, c.attrib) 1144 self.assertEquals([], list(c.keys())) 1145 self.assertEquals([], list(c.items())) 1146 self.assertEquals(None, c.get('hoi')) 1147 self.assertEquals(0, len(c)) 1148 # should not iterate 1149 for i in c: 1150 pass
1151
1152 - def test_pi(self):
1153 # lxml.etree separates target and text 1154 Element = self.etree.Element 1155 SubElement = self.etree.SubElement 1156 ProcessingInstruction = self.etree.ProcessingInstruction 1157 1158 a = Element('a') 1159 a.append(ProcessingInstruction('foo', 'some more text')) 1160 self.assertEquals(a[0].tag, ProcessingInstruction) 1161 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1162 a)
1163
1164 - def test_processinginstruction(self):
1165 # lxml.etree separates target and text 1166 Element = self.etree.Element 1167 SubElement = self.etree.SubElement 1168 ProcessingInstruction = self.etree.PI 1169 1170 a = Element('a') 1171 a.append(ProcessingInstruction('foo', 'some more text')) 1172 self.assertEquals(a[0].tag, ProcessingInstruction) 1173 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1174 a)
1175
1176 - def test_pi_nonsense(self):
1177 ProcessingInstruction = self.etree.ProcessingInstruction 1178 pi = ProcessingInstruction('foo') 1179 self.assertEquals({}, pi.attrib) 1180 self.assertEquals([], list(pi.keys())) 1181 self.assertEquals([], list(pi.items())) 1182 self.assertEquals(None, pi.get('hoi')) 1183 self.assertEquals(0, len(pi)) 1184 # should not iterate 1185 for i in pi: 1186 pass
1187
1188 - def test_setitem(self):
1189 Element = self.etree.Element 1190 SubElement = self.etree.SubElement 1191 1192 a = Element('a') 1193 b = SubElement(a, 'b') 1194 c = Element('c') 1195 a[0] = c 1196 self.assertEquals( 1197 c, 1198 a[0]) 1199 self.assertXML(_bytes('<a><c></c></a>'), 1200 a) 1201 self.assertXML(_bytes('<b></b>'), 1202 b)
1203
1204 - def test_setitem2(self):
1205 Element = self.etree.Element 1206 SubElement = self.etree.SubElement 1207 1208 a = Element('a') 1209 for i in range(5): 1210 b = SubElement(a, 'b%s' % i) 1211 c = SubElement(b, 'c') 1212 for i in range(5): 1213 d = Element('d') 1214 e = SubElement(d, 'e') 1215 a[i] = d 1216 self.assertXML( 1217 _bytes('<a><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d></a>'), 1218 a) 1219 self.assertXML(_bytes('<c></c>'), 1220 c)
1221
1222 - def test_setitem_replace(self):
1223 Element = self.etree.Element 1224 SubElement = self.etree.SubElement 1225 1226 a = Element('a') 1227 SubElement(a, 'b') 1228 d = Element('d') 1229 a[0] = d 1230 self.assertXML(_bytes('<a><d></d></a>'), a)
1231
1232 - def test_setitem_indexerror(self):
1233 Element = self.etree.Element 1234 SubElement = self.etree.SubElement 1235 1236 a = Element('a') 1237 b = SubElement(a, 'b') 1238 1239 self.assertRaises(IndexError, operator.setitem, a, 1, Element('c'))
1240
1241 - def test_setitem_tail(self):
1242 Element = self.etree.Element 1243 SubElement = self.etree.SubElement 1244 1245 a = Element('a') 1246 b = SubElement(a, 'b') 1247 b.tail = 'B2' 1248 c = Element('c') 1249 c.tail = 'C2' 1250 1251 a[0] = c 1252 self.assertXML( 1253 _bytes('<a><c></c>C2</a>'), 1254 a)
1255
1256 - def test_tag_write(self):
1257 Element = self.etree.Element 1258 SubElement = self.etree.SubElement 1259 1260 a = Element('a') 1261 b = SubElement(a, 'b') 1262 1263 a.tag = 'c' 1264 1265 self.assertEquals( 1266 'c', 1267 a.tag) 1268 1269 self.assertXML( 1270 _bytes('<c><b></b></c>'), 1271 a)
1272
1273 - def test_tag_reset_ns(self):
1274 Element = self.etree.Element 1275 SubElement = self.etree.SubElement 1276 tostring = self.etree.tostring 1277 1278 a = Element('{a}a') 1279 b1 = SubElement(a, '{a}b') 1280 b2 = SubElement(a, '{b}b') 1281 1282 self.assertEquals('{a}b', b1.tag) 1283 1284 b1.tag = 'c' 1285 1286 # can't use C14N here! 1287 self.assertEquals('c', b1.tag) 1288 self.assertEquals(_bytes('<c'), tostring(b1)[:2]) 1289 self.assert_(_bytes('<c') in tostring(a))
1290
1291 - def test_tag_reset_root_ns(self):
1292 Element = self.etree.Element 1293 SubElement = self.etree.SubElement 1294 tostring = self.etree.tostring 1295 1296 a = Element('{a}a') 1297 b1 = SubElement(a, '{a}b') 1298 b2 = SubElement(a, '{b}b') 1299 1300 a.tag = 'c' 1301 1302 self.assertEquals( 1303 'c', 1304 a.tag) 1305 1306 # can't use C14N here! 1307 self.assertEquals('c', a.tag) 1308 self.assertEquals(_bytes('<c'), tostring(a)[:2])
1309
1310 - def test_tag_str_subclass(self):
1311 Element = self.etree.Element 1312 1313 class strTest(str): 1314 pass
1315 1316 a = Element("a") 1317 a.tag = strTest("TAG") 1318 self.assertXML(_bytes('<TAG></TAG>'), 1319 a) 1320
1321 - def test_delitem(self):
1322 Element = self.etree.Element 1323 SubElement = self.etree.SubElement 1324 1325 a = Element('a') 1326 b = SubElement(a, 'b') 1327 c = SubElement(a, 'c') 1328 d = SubElement(a, 'd') 1329 1330 del a[1] 1331 self.assertXML( 1332 _bytes('<a><b></b><d></d></a>'), 1333 a) 1334 1335 del a[0] 1336 self.assertXML( 1337 _bytes('<a><d></d></a>'), 1338 a) 1339 1340 del a[0] 1341 self.assertXML( 1342 _bytes('<a></a>'), 1343 a) 1344 # move deleted element into other tree afterwards 1345 other = Element('other') 1346 other.append(c) 1347 self.assertXML( 1348 _bytes('<other><c></c></other>'), 1349 other)
1350
1351 - def test_del_insert(self):
1352 Element = self.etree.Element 1353 SubElement = self.etree.SubElement 1354 1355 a = Element('a') 1356 b = SubElement(a, 'b') 1357 bs = SubElement(b, 'bs') 1358 c = SubElement(a, 'c') 1359 cs = SubElement(c, 'cs') 1360 1361 el = a[0] 1362 self.assertXML( 1363 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1364 a) 1365 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1366 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1367 1368 del a[0] 1369 self.assertXML( 1370 _bytes('<a><c><cs></cs></c></a>'), 1371 a) 1372 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1373 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1374 1375 a.insert(0, el) 1376 self.assertXML( 1377 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1378 a) 1379 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1380 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1381
1382 - def test_del_setitem(self):
1383 Element = self.etree.Element 1384 SubElement = self.etree.SubElement 1385 1386 a = Element('a') 1387 b = SubElement(a, 'b') 1388 bs = SubElement(b, 'bs') 1389 c = SubElement(a, 'c') 1390 cs = SubElement(c, 'cs') 1391 1392 el = a[0] 1393 del a[0] 1394 a[0] = el 1395 self.assertXML( 1396 _bytes('<a><b><bs></bs></b></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_setslice(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:0] = [el] 1414 self.assertXML( 1415 _bytes('<a><b><bs></bs></b><c><cs></cs></c></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_replace_slice_tail(self):
1421 XML = self.etree.XML 1422 a = XML(_bytes('<a><b></b>B2<c></c>C2</a>')) 1423 b, c = a 1424 1425 a[:] = [] 1426 1427 self.assertEquals("B2", b.tail) 1428 self.assertEquals("C2", c.tail)
1429
1430 - def test_merge_namespaced_subtree_as_slice(self):
1431 XML = self.etree.XML 1432 root = XML(_bytes( 1433 '<foo><bar xmlns:baz="http://huhu"><puh><baz:bump1 /><baz:bump2 /></puh></bar></foo>')) 1434 root[:] = root.findall('.//puh') # delete bar from hierarchy 1435 1436 # previously, this lost a namespace declaration on bump2 1437 result = self.etree.tostring(root) 1438 foo = self.etree.fromstring(result) 1439 1440 self.assertEquals('puh', foo[0].tag) 1441 self.assertEquals('{http://huhu}bump1', foo[0][0].tag) 1442 self.assertEquals('{http://huhu}bump2', foo[0][1].tag)
1443
1444 - def test_delitem_tail(self):
1445 ElementTree = self.etree.ElementTree 1446 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1447 doc = ElementTree(file=f) 1448 a = doc.getroot() 1449 del a[0] 1450 self.assertXML( 1451 _bytes('<a><c></c>C2</a>'), 1452 a)
1453
1454 - def test_clear(self):
1455 Element = self.etree.Element 1456 1457 a = Element('a') 1458 a.text = 'foo' 1459 a.tail = 'bar' 1460 a.set('hoi', 'dag') 1461 a.clear() 1462 self.assertEquals(None, a.text) 1463 self.assertEquals(None, a.tail) 1464 self.assertEquals(None, a.get('hoi')) 1465 self.assertEquals('a', a.tag)
1466
1467 - def test_clear_sub(self):
1468 Element = self.etree.Element 1469 SubElement = self.etree.SubElement 1470 1471 a = Element('a') 1472 a.text = 'foo' 1473 a.tail = 'bar' 1474 a.set('hoi', 'dag') 1475 b = SubElement(a, 'b') 1476 c = SubElement(b, 'c') 1477 a.clear() 1478 self.assertEquals(None, a.text) 1479 self.assertEquals(None, a.tail) 1480 self.assertEquals(None, a.get('hoi')) 1481 self.assertEquals('a', a.tag) 1482 self.assertEquals(0, len(a)) 1483 self.assertXML(_bytes('<a></a>'), 1484 a) 1485 self.assertXML(_bytes('<b><c></c></b>'), 1486 b)
1487
1488 - def test_clear_tail(self):
1489 ElementTree = self.etree.ElementTree 1490 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1491 doc = ElementTree(file=f) 1492 a = doc.getroot() 1493 a.clear() 1494 self.assertXML( 1495 _bytes('<a></a>'), 1496 a)
1497
1498 - def test_insert(self):
1499 Element = self.etree.Element 1500 SubElement = self.etree.SubElement 1501 1502 a = Element('a') 1503 b = SubElement(a, 'b') 1504 c = SubElement(a, 'c') 1505 d = Element('d') 1506 a.insert(0, d) 1507 1508 self.assertEquals( 1509 d, 1510 a[0]) 1511 1512 self.assertXML( 1513 _bytes('<a><d></d><b></b><c></c></a>'), 1514 a) 1515 1516 e = Element('e') 1517 a.insert(2, e) 1518 self.assertEquals( 1519 e, 1520 a[2]) 1521 self.assertXML( 1522 _bytes('<a><d></d><b></b><e></e><c></c></a>'), 1523 a)
1524
1525 - def test_insert_beyond_index(self):
1526 Element = self.etree.Element 1527 SubElement = self.etree.SubElement 1528 1529 a = Element('a') 1530 b = SubElement(a, 'b') 1531 c = Element('c') 1532 1533 a.insert(2, c) 1534 self.assertEquals( 1535 c, 1536 a[1]) 1537 self.assertXML( 1538 _bytes('<a><b></b><c></c></a>'), 1539 a)
1540
1541 - def test_insert_negative(self):
1542 Element = self.etree.Element 1543 SubElement = self.etree.SubElement 1544 1545 a = Element('a') 1546 b = SubElement(a, 'b') 1547 c = SubElement(a, 'c') 1548 1549 d = Element('d') 1550 a.insert(-1, d) 1551 self.assertEquals( 1552 d, 1553 a[-2]) 1554 self.assertXML( 1555 _bytes('<a><b></b><d></d><c></c></a>'), 1556 a)
1557
1558 - def test_insert_tail(self):
1559 Element = self.etree.Element 1560 SubElement = self.etree.SubElement 1561 1562 a = Element('a') 1563 b = SubElement(a, 'b') 1564 1565 c = Element('c') 1566 c.tail = 'C2' 1567 1568 a.insert(0, c) 1569 self.assertXML( 1570 _bytes('<a><c></c>C2<b></b></a>'), 1571 a)
1572
1573 - def test_remove(self):
1574 Element = self.etree.Element 1575 SubElement = self.etree.SubElement 1576 1577 a = Element('a') 1578 b = SubElement(a, 'b') 1579 c = SubElement(a, 'c') 1580 1581 a.remove(b) 1582 self.assertEquals( 1583 c, 1584 a[0]) 1585 self.assertXML( 1586 _bytes('<a><c></c></a>'), 1587 a)
1588
1589 - def test_remove_ns(self):
1590 Element = self.etree.Element 1591 SubElement = self.etree.SubElement 1592 1593 a = Element('{http://test}a') 1594 b = SubElement(a, '{http://test}b') 1595 c = SubElement(a, '{http://test}c') 1596 1597 a.remove(b) 1598 self.assertXML( 1599 _bytes('<ns0:a xmlns:ns0="http://test"><ns0:c></ns0:c></ns0:a>'), 1600 a) 1601 self.assertXML( 1602 _bytes('<ns0:b xmlns:ns0="http://test"></ns0:b>'), 1603 b)
1604
1605 - def test_remove_nonexisting(self):
1606 Element = self.etree.Element 1607 SubElement = self.etree.SubElement 1608 1609 a = Element('a') 1610 b = SubElement(a, 'b') 1611 c = SubElement(a, 'c') 1612 d = Element('d') 1613 self.assertRaises( 1614 ValueError, a.remove, d)
1615
1616 - def test_remove_tail(self):
1617 Element = self.etree.Element 1618 SubElement = self.etree.SubElement 1619 1620 a = Element('a') 1621 b = SubElement(a, 'b') 1622 b.tail = 'b2' 1623 a.remove(b) 1624 self.assertXML( 1625 _bytes('<a></a>'), 1626 a) 1627 self.assertEquals('b2', b.tail)
1628
1629 - def _test_getchildren(self):
1630 Element = self.etree.Element 1631 SubElement = self.etree.SubElement 1632 1633 a = Element('a') 1634 b = SubElement(a, 'b') 1635 c = SubElement(a, 'c') 1636 d = SubElement(b, 'd') 1637 e = SubElement(c, 'e') 1638 self.assertXML( 1639 _bytes('<a><b><d></d></b><c><e></e></c></a>'), 1640 a) 1641 self.assertEquals( 1642 [b, c], 1643 a.getchildren()) 1644 self.assertEquals( 1645 [d], 1646 b.getchildren()) 1647 self.assertEquals( 1648 [], 1649 d.getchildren())
1650
1651 - def test_makeelement(self):
1652 Element = self.etree.Element 1653 1654 a = Element('a') 1655 b = a.makeelement('c', {'hoi':'dag'}) 1656 self.assertXML( 1657 _bytes('<c hoi="dag"></c>'), 1658 b)
1659 1660 required_versions_ET['test_iter'] = (1,3)
1661 - def test_iter(self):
1662 Element = self.etree.Element 1663 SubElement = self.etree.SubElement 1664 1665 a = Element('a') 1666 b = SubElement(a, 'b') 1667 c = SubElement(a, 'c') 1668 d = SubElement(b, 'd') 1669 e = SubElement(c, 'e') 1670 1671 self.assertEquals( 1672 [a, b, d, c, e], 1673 list(a.iter())) 1674 self.assertEquals( 1675 [d], 1676 list(d.iter()))
1677
1678 - def test_getiterator(self):
1679 Element = self.etree.Element 1680 SubElement = self.etree.SubElement 1681 1682 a = Element('a') 1683 b = SubElement(a, 'b') 1684 c = SubElement(a, 'c') 1685 d = SubElement(b, 'd') 1686 e = SubElement(c, 'e') 1687 1688 self.assertEquals( 1689 [a, b, d, c, e], 1690 list(a.getiterator())) 1691 self.assertEquals( 1692 [d], 1693 list(d.getiterator()))
1694
1695 - def test_getiterator_empty(self):
1696 Element = self.etree.Element 1697 SubElement = self.etree.SubElement 1698 1699 a = Element('a') 1700 b = SubElement(a, 'b') 1701 c = SubElement(a, 'c') 1702 d = SubElement(b, 'd') 1703 e = SubElement(c, 'e') 1704 1705 self.assertEquals( 1706 [], 1707 list(a.getiterator('none'))) 1708 self.assertEquals( 1709 [], 1710 list(e.getiterator('none'))) 1711 self.assertEquals( 1712 [e], 1713 list(e.getiterator()))
1714
1715 - def test_getiterator_filter(self):
1716 Element = self.etree.Element 1717 SubElement = self.etree.SubElement 1718 1719 a = Element('a') 1720 b = SubElement(a, 'b') 1721 c = SubElement(a, 'c') 1722 d = SubElement(b, 'd') 1723 e = SubElement(c, 'e') 1724 1725 self.assertEquals( 1726 [a], 1727 list(a.getiterator('a'))) 1728 a2 = SubElement(e, 'a') 1729 self.assertEquals( 1730 [a, a2], 1731 list(a.getiterator('a'))) 1732 self.assertEquals( 1733 [a2], 1734 list(c.getiterator('a')))
1735
1736 - def test_getiterator_filter_all(self):
1737 Element = self.etree.Element 1738 SubElement = self.etree.SubElement 1739 1740 a = Element('a') 1741 b = SubElement(a, 'b') 1742 c = SubElement(a, 'c') 1743 d = SubElement(b, 'd') 1744 e = SubElement(c, 'e') 1745 1746 self.assertEquals( 1747 [a, b, d, c, e], 1748 list(a.getiterator('*')))
1749
1750 - def test_getiterator_filter_comment(self):
1751 Element = self.etree.Element 1752 Comment = self.etree.Comment 1753 SubElement = self.etree.SubElement 1754 1755 a = Element('a') 1756 b = SubElement(a, 'b') 1757 comment_b = Comment("TEST-b") 1758 b.append(comment_b) 1759 1760 self.assertEquals( 1761 [comment_b], 1762 list(a.getiterator(Comment))) 1763 1764 comment_a = Comment("TEST-a") 1765 a.append(comment_a) 1766 1767 self.assertEquals( 1768 [comment_b, comment_a], 1769 list(a.getiterator(Comment))) 1770 1771 self.assertEquals( 1772 [comment_b], 1773 list(b.getiterator(Comment)))
1774
1775 - def test_getiterator_filter_pi(self):
1776 Element = self.etree.Element 1777 PI = self.etree.ProcessingInstruction 1778 SubElement = self.etree.SubElement 1779 1780 a = Element('a') 1781 b = SubElement(a, 'b') 1782 pi_b = PI("TEST-b") 1783 b.append(pi_b) 1784 1785 self.assertEquals( 1786 [pi_b], 1787 list(a.getiterator(PI))) 1788 1789 pi_a = PI("TEST-a") 1790 a.append(pi_a) 1791 1792 self.assertEquals( 1793 [pi_b, pi_a], 1794 list(a.getiterator(PI))) 1795 1796 self.assertEquals( 1797 [pi_b], 1798 list(b.getiterator(PI)))
1799
1800 - def test_getiterator_with_text(self):
1801 Element = self.etree.Element 1802 SubElement = self.etree.SubElement 1803 1804 a = Element('a') 1805 a.text = 'a' 1806 b = SubElement(a, 'b') 1807 b.text = 'b' 1808 b.tail = 'b1' 1809 c = SubElement(a, 'c') 1810 c.text = 'c' 1811 c.tail = 'c1' 1812 d = SubElement(b, 'd') 1813 c.text = 'd' 1814 c.tail = 'd1' 1815 e = SubElement(c, 'e') 1816 e.text = 'e' 1817 e.tail = 'e1' 1818 1819 self.assertEquals( 1820 [a, b, d, c, e], 1821 list(a.getiterator()))
1822 #self.assertEquals( 1823 # [d], 1824 # list(d.getiterator())) 1825
1826 - def test_getiterator_filter_with_text(self):
1827 Element = self.etree.Element 1828 SubElement = self.etree.SubElement 1829 1830 a = Element('a') 1831 a.text = 'a' 1832 b = SubElement(a, 'b') 1833 b.text = 'b' 1834 b.tail = 'b1' 1835 c = SubElement(a, 'c') 1836 c.text = 'c' 1837 c.tail = 'c1' 1838 d = SubElement(b, 'd') 1839 c.text = 'd' 1840 c.tail = 'd1' 1841 e = SubElement(c, 'e') 1842 e.text = 'e' 1843 e.tail = 'e1' 1844 1845 self.assertEquals( 1846 [a], 1847 list(a.getiterator('a'))) 1848 a2 = SubElement(e, 'a') 1849 self.assertEquals( 1850 [a, a2], 1851 list(a.getiterator('a'))) 1852 self.assertEquals( 1853 [a2], 1854 list(e.getiterator('a')))
1855
1856 - def test_getslice(self):
1857 Element = self.etree.Element 1858 SubElement = self.etree.SubElement 1859 1860 a = Element('a') 1861 b = SubElement(a, 'b') 1862 c = SubElement(a, 'c') 1863 d = SubElement(a, 'd') 1864 1865 self.assertEquals( 1866 [b, c], 1867 a[0:2]) 1868 self.assertEquals( 1869 [b, c, d], 1870 a[:]) 1871 self.assertEquals( 1872 [b, c, d], 1873 a[:10]) 1874 self.assertEquals( 1875 [b], 1876 a[0:1]) 1877 self.assertEquals( 1878 [], 1879 a[10:12])
1880
1881 - def test_getslice_negative(self):
1882 Element = self.etree.Element 1883 SubElement = self.etree.SubElement 1884 1885 a = Element('a') 1886 b = SubElement(a, 'b') 1887 c = SubElement(a, 'c') 1888 d = SubElement(a, 'd') 1889 1890 self.assertEquals( 1891 [d], 1892 a[-1:]) 1893 self.assertEquals( 1894 [c, d], 1895 a[-2:]) 1896 self.assertEquals( 1897 [c], 1898 a[-2:-1]) 1899 self.assertEquals( 1900 [b, c], 1901 a[-3:-1]) 1902 self.assertEquals( 1903 [b, c], 1904 a[-3:2])
1905
1906 - def test_getslice_step(self):
1907 Element = self.etree.Element 1908 SubElement = self.etree.SubElement 1909 1910 a = Element('a') 1911 b = SubElement(a, 'b') 1912 c = SubElement(a, 'c') 1913 d = SubElement(a, 'd') 1914 e = SubElement(a, 'e') 1915 1916 self.assertEquals( 1917 [e,d,c,b], 1918 a[::-1]) 1919 self.assertEquals( 1920 [b,d], 1921 a[::2]) 1922 self.assertEquals( 1923 [e,c], 1924 a[::-2]) 1925 self.assertEquals( 1926 [d,c], 1927 a[-2:0:-1]) 1928 self.assertEquals( 1929 [e], 1930 a[:1:-2])
1931
1932 - def test_getslice_text(self):
1933 ElementTree = self.etree.ElementTree 1934 1935 f = BytesIO('<a><b>B</b>B1<c>C</c>C1</a>') 1936 doc = ElementTree(file=f) 1937 a = doc.getroot() 1938 b = a[0] 1939 c = a[1] 1940 self.assertEquals( 1941 [b, c], 1942 a[:]) 1943 self.assertEquals( 1944 [b], 1945 a[0:1]) 1946 self.assertEquals( 1947 [c], 1948 a[1:])
1949
1950 - def test_comment_getitem_getslice(self):
1951 Element = self.etree.Element 1952 Comment = self.etree.Comment 1953 SubElement = self.etree.SubElement 1954 1955 a = Element('a') 1956 b = SubElement(a, 'b') 1957 foo = Comment('foo') 1958 a.append(foo) 1959 c = SubElement(a, 'c') 1960 self.assertEquals( 1961 [b, foo, c], 1962 a[:]) 1963 self.assertEquals( 1964 foo, 1965 a[1]) 1966 a[1] = new = Element('new') 1967 self.assertEquals( 1968 new, 1969 a[1]) 1970 self.assertXML( 1971 _bytes('<a><b></b><new></new><c></c></a>'), 1972 a)
1973
1974 - def test_delslice(self):
1975 Element = self.etree.Element 1976 SubElement = self.etree.SubElement 1977 1978 a = Element('a') 1979 b = SubElement(a, 'b') 1980 c = SubElement(a, 'c') 1981 d = SubElement(a, 'd') 1982 e = SubElement(a, 'e') 1983 1984 del a[1:3] 1985 self.assertEquals( 1986 [b, e], 1987 list(a))
1988
1989 - def test_delslice_negative1(self):
1990 Element = self.etree.Element 1991 SubElement = self.etree.SubElement 1992 1993 a = Element('a') 1994 b = SubElement(a, 'b') 1995 c = SubElement(a, 'c') 1996 d = SubElement(a, 'd') 1997 e = SubElement(a, 'e') 1998 1999 del a[1:-1] 2000 self.assertEquals( 2001 [b, e], 2002 list(a))
2003
2004 - def test_delslice_negative2(self):
2005 Element = self.etree.Element 2006 SubElement = self.etree.SubElement 2007 2008 a = Element('a') 2009 b = SubElement(a, 'b') 2010 c = SubElement(a, 'c') 2011 d = SubElement(a, 'd') 2012 e = SubElement(a, 'e') 2013 2014 del a[-3:-1] 2015 self.assertEquals( 2016 [b, e], 2017 list(a))
2018
2019 - def test_delslice_step(self):
2020 Element = self.etree.Element 2021 SubElement = self.etree.SubElement 2022 2023 a = Element('a') 2024 b =