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