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