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, 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 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_extra(self):
901 Element = self.etree.Element 902 903 el = Element('tag', {'foo': 'Foo', 'bar': 'Bar'}, baz='Baz') 904 self.assertEqual('Foo', el.attrib['foo']) 905 self.assertEqual('Bar', el.attrib['bar']) 906 self.assertEqual('Baz', el.attrib['baz'])
907
908 - def test_element_with_attributes_extra_duplicate(self):
909 Element = self.etree.Element 910 911 el = Element('tag', {'foo': 'Foo', 'bar': 'Bar'}, bar='Baz') 912 self.assertEqual('Foo', el.attrib['foo']) 913 self.assertEqual('Baz', el.attrib['bar'])
914
915 - def test_element_with_attributes_ns(self):
916 Element = self.etree.Element 917 918 el = Element('tag', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 919 self.assertEqual('Foo', el.attrib['{ns1}foo']) 920 self.assertEqual('Bar', el.attrib['{ns2}bar'])
921
922 - def test_subelement_with_attributes(self):
923 Element = self.etree.Element 924 SubElement = self.etree.SubElement 925 926 el = Element('tag') 927 SubElement(el, 'foo', {'foo':'Foo'}, baz="Baz") 928 self.assertEqual("Baz", el[0].attrib['baz']) 929 self.assertEqual('Foo', el[0].attrib['foo'])
930
931 - def test_subelement_with_attributes_ns(self):
932 Element = self.etree.Element 933 SubElement = self.etree.SubElement 934 935 el = Element('tag') 936 SubElement(el, 'foo', {'{ns1}foo':'Foo', '{ns2}bar':'Bar'}) 937 self.assertEqual('Foo', el[0].attrib['{ns1}foo']) 938 self.assertEqual('Bar', el[0].attrib['{ns2}bar'])
939
940 - def test_write(self):
941 ElementTree = self.etree.ElementTree 942 XML = self.etree.XML 943 944 for i in range(10): 945 f = BytesIO() 946 root = XML(_bytes('<doc%s>This is a test.</doc%s>' % (i, i))) 947 tree = ElementTree(element=root) 948 tree.write(f) 949 data = f.getvalue() 950 self.assertEqual( 951 _bytes('<doc%s>This is a test.</doc%s>' % (i, i)), 952 canonicalize(data))
953 954 required_versions_ET['test_write_method_html'] = (1,3)
955 - def test_write_method_html(self):
956 ElementTree = self.etree.ElementTree 957 Element = self.etree.Element 958 SubElement = self.etree.SubElement 959 960 html = Element('html') 961 body = SubElement(html, 'body') 962 p = SubElement(body, 'p') 963 p.text = "html" 964 SubElement(p, 'br').tail = "test" 965 966 tree = ElementTree(element=html) 967 f = BytesIO() 968 tree.write(f, method="html") 969 data = f.getvalue().replace(_bytes('\n'),_bytes('')) 970 971 self.assertEqual(_bytes('<html><body><p>html<br>test</p></body></html>'), 972 data)
973 974 required_versions_ET['test_write_method_text'] = (1,3)
975 - def test_write_method_text(self):
976 ElementTree = self.etree.ElementTree 977 Element = self.etree.Element 978 SubElement = self.etree.SubElement 979 980 a = Element('a') 981 a.text = "A" 982 a.tail = "tail" 983 b = SubElement(a, 'b') 984 b.text = "B" 985 b.tail = "TAIL" 986 c = SubElement(a, 'c') 987 c.text = "C" 988 989 tree = ElementTree(element=a) 990 f = BytesIO() 991 tree.write(f, method="text") 992 data = f.getvalue() 993 994 self.assertEqual(_bytes('ABTAILCtail'), 995 data)
996
997 - def test_write_fail(self):
998 ElementTree = self.etree.ElementTree 999 XML = self.etree.XML 1000 1001 tree = ElementTree( XML(_bytes('<doc>This is a test.</doc>')) ) 1002 self.assertRaises(IOError, tree.write, 1003 "definitely////\\-\\nonexisting\\-\\////FILE")
1004 1005 # this could trigger a crash, apparently because the document 1006 # reference was prematurely garbage collected
1007 - def test_crash(self):
1008 Element = self.etree.Element 1009 1010 element = Element('tag') 1011 for i in range(10): 1012 element.attrib['key'] = 'value' 1013 value = element.attrib['key'] 1014 self.assertEqual(value, 'value')
1015 1016 # from doctest; for some reason this caused crashes too
1017 - def test_write_ElementTreeDoctest(self):
1018 Element = self.etree.Element 1019 ElementTree = self.etree.ElementTree 1020 1021 f = BytesIO() 1022 for i in range(10): 1023 element = Element('tag%s' % i) 1024 self._check_element(element) 1025 tree = ElementTree(element) 1026 tree.write(f) 1027 self._check_element_tree(tree)
1028
1029 - def test_subelement_reference(self):
1030 Element = self.etree.Element 1031 SubElement = self.etree.SubElement 1032 1033 el = Element('foo') 1034 el2 = SubElement(el, 'bar') 1035 el3 = SubElement(el2, 'baz') 1036 1037 al = Element('foo2') 1038 al2 = SubElement(al, 'bar2') 1039 al3 = SubElement(al2, 'baz2') 1040 1041 # now move al2 into el 1042 el.append(al2) 1043 1044 # now change al3 directly 1045 al3.text = 'baz2-modified' 1046 1047 # it should have changed through this route too 1048 self.assertEqual( 1049 'baz2-modified', 1050 el[1][0].text)
1051
1052 - def test_set_text(self):
1053 Element = self.etree.Element 1054 SubElement = self.etree.SubElement 1055 1056 a = Element('a') 1057 b = SubElement(a, 'b') 1058 a.text = 'hoi' 1059 self.assertEqual( 1060 'hoi', 1061 a.text) 1062 self.assertEqual( 1063 'b', 1064 a[0].tag)
1065
1066 - def test_set_text2(self):
1067 Element = self.etree.Element 1068 SubElement = self.etree.SubElement 1069 1070 a = Element('a') 1071 a.text = 'hoi' 1072 b = SubElement(a ,'b') 1073 self.assertEqual( 1074 'hoi', 1075 a.text) 1076 self.assertEqual( 1077 'b', 1078 a[0].tag)
1079
1080 - def test_set_text_none(self):
1081 Element = self.etree.Element 1082 1083 a = Element('a') 1084 1085 a.text = 'foo' 1086 a.text = None 1087 1088 self.assertEqual( 1089 None, 1090 a.text) 1091 self.assertXML(_bytes('<a></a>'), a)
1092
1093 - def test_set_text_empty(self):
1094 Element = self.etree.Element 1095 1096 a = Element('a') 1097 self.assertEqual(None, a.text) 1098 1099 a.text = '' 1100 self.assertEqual('', a.text) 1101 self.assertXML(_bytes('<a></a>'), a)
1102
1103 - def test_tail1(self):
1104 Element = self.etree.Element 1105 SubElement = self.etree.SubElement 1106 1107 a = Element('a') 1108 a.tail = 'dag' 1109 self.assertEqual('dag', 1110 a.tail) 1111 b = SubElement(a, 'b') 1112 b.tail = 'hoi' 1113 self.assertEqual('hoi', 1114 b.tail) 1115 self.assertEqual('dag', 1116 a.tail)
1117
1118 - def test_tail_append(self):
1119 Element = self.etree.Element 1120 1121 a = Element('a') 1122 b = Element('b') 1123 b.tail = 'b_tail' 1124 a.append(b) 1125 self.assertEqual('b_tail', 1126 b.tail)
1127
1128 - def test_tail_set_twice(self):
1129 Element = self.etree.Element 1130 SubElement = self.etree.SubElement 1131 1132 a = Element('a') 1133 b = SubElement(a, 'b') 1134 b.tail = 'foo' 1135 b.tail = 'bar' 1136 self.assertEqual('bar', 1137 b.tail) 1138 self.assertXML(_bytes('<a><b></b>bar</a>'), a)
1139
1140 - def test_tail_set_none(self):
1141 Element = self.etree.Element 1142 a = Element('a') 1143 a.tail = 'foo' 1144 a.tail = None 1145 self.assertEqual( 1146 None, 1147 a.tail) 1148 self.assertXML(_bytes('<a></a>'), a)
1149 1150 required_versions_ET['test_extend'] = (1,3)
1151 - def test_extend(self):
1152 root = self.etree.Element('foo') 1153 for i in range(3): 1154 element = self.etree.SubElement(root, 'a%s' % i) 1155 element.text = "text%d" % i 1156 element.tail = "tail%d" % i 1157 1158 elements = [] 1159 for i in range(3): 1160 new_element = self.etree.Element("test%s" % i) 1161 new_element.text = "TEXT%s" % i 1162 new_element.tail = "TAIL%s" % i 1163 elements.append(new_element) 1164 1165 root.extend(elements) 1166 1167 self.assertEqual( 1168 ["a0", "a1", "a2", "test0", "test1", "test2"], 1169 [ el.tag for el in root ]) 1170 self.assertEqual( 1171 ["text0", "text1", "text2", "TEXT0", "TEXT1", "TEXT2"], 1172 [ el.text for el in root ]) 1173 self.assertEqual( 1174 ["tail0", "tail1", "tail2", "TAIL0", "TAIL1", "TAIL2"], 1175 [ el.tail for el in root ])
1176
1177 - def test_comment(self):
1178 Element = self.etree.Element 1179 SubElement = self.etree.SubElement 1180 Comment = self.etree.Comment 1181 1182 a = Element('a') 1183 a.append(Comment('foo')) 1184 self.assertEqual(a[0].tag, Comment) 1185 self.assertEqual(a[0].text, 'foo')
1186 1187 # ElementTree < 1.3 adds whitespace around comments 1188 required_versions_ET['test_comment_text'] = (1,3)
1189 - def test_comment_text(self):
1190 Element = self.etree.Element 1191 SubElement = self.etree.SubElement 1192 Comment = self.etree.Comment 1193 tostring = self.etree.tostring 1194 1195 a = Element('a') 1196 a.append(Comment('foo')) 1197 self.assertEqual(a[0].text, 'foo') 1198 1199 self.assertEqual( 1200 _bytes('<a><!--foo--></a>'), 1201 tostring(a)) 1202 1203 a[0].text = "TEST" 1204 self.assertEqual(a[0].text, 'TEST') 1205 1206 self.assertEqual( 1207 _bytes('<a><!--TEST--></a>'), 1208 tostring(a))
1209 1210 # ElementTree < 1.3 adds whitespace around comments 1211 required_versions_ET['test_comment_whitespace'] = (1,3)
1212 - def test_comment_whitespace(self):
1213 Element = self.etree.Element 1214 SubElement = self.etree.SubElement 1215 Comment = self.etree.Comment 1216 tostring = self.etree.tostring 1217 1218 a = Element('a') 1219 a.append(Comment(' foo ')) 1220 self.assertEqual(a[0].text, ' foo ') 1221 self.assertEqual( 1222 _bytes('<a><!-- foo --></a>'), 1223 tostring(a))
1224
1225 - def test_comment_nonsense(self):
1226 Comment = self.etree.Comment 1227 c = Comment('foo') 1228 self.assertEqual({}, c.attrib) 1229 self.assertEqual([], list(c.keys())) 1230 self.assertEqual([], list(c.items())) 1231 self.assertEqual(None, c.get('hoi')) 1232 self.assertEqual(0, len(c)) 1233 # should not iterate 1234 for i in c: 1235 pass
1236
1237 - def test_pi(self):
1238 # lxml.etree separates target and text 1239 Element = self.etree.Element 1240 SubElement = self.etree.SubElement 1241 ProcessingInstruction = self.etree.ProcessingInstruction 1242 1243 a = Element('a') 1244 a.append(ProcessingInstruction('foo', 'some more text')) 1245 self.assertEqual(a[0].tag, ProcessingInstruction) 1246 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1247 a)
1248
1249 - def test_processinginstruction(self):
1250 # lxml.etree separates target and text 1251 Element = self.etree.Element 1252 SubElement = self.etree.SubElement 1253 ProcessingInstruction = self.etree.PI 1254 1255 a = Element('a') 1256 a.append(ProcessingInstruction('foo', 'some more text')) 1257 self.assertEqual(a[0].tag, ProcessingInstruction) 1258 self.assertXML(_bytes("<a><?foo some more text?></a>"), 1259 a)
1260
1261 - def test_pi_nonsense(self):
1262 ProcessingInstruction = self.etree.ProcessingInstruction 1263 pi = ProcessingInstruction('foo') 1264 self.assertEqual({}, pi.attrib) 1265 self.assertEqual([], list(pi.keys())) 1266 self.assertEqual([], list(pi.items())) 1267 self.assertEqual(None, pi.get('hoi')) 1268 self.assertEqual(0, len(pi)) 1269 # should not iterate 1270 for i in pi: 1271 pass
1272
1273 - def test_setitem(self):
1274 Element = self.etree.Element 1275 SubElement = self.etree.SubElement 1276 1277 a = Element('a') 1278 b = SubElement(a, 'b') 1279 c = Element('c') 1280 a[0] = c 1281 self.assertEqual( 1282 c, 1283 a[0]) 1284 self.assertXML(_bytes('<a><c></c></a>'), 1285 a) 1286 self.assertXML(_bytes('<b></b>'), 1287 b)
1288
1289 - def test_setitem2(self):
1290 Element = self.etree.Element 1291 SubElement = self.etree.SubElement 1292 1293 a = Element('a') 1294 for i in range(5): 1295 b = SubElement(a, 'b%s' % i) 1296 c = SubElement(b, 'c') 1297 for i in range(5): 1298 d = Element('d') 1299 e = SubElement(d, 'e') 1300 a[i] = d 1301 self.assertXML( 1302 _bytes('<a><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d><d><e></e></d></a>'), 1303 a) 1304 self.assertXML(_bytes('<c></c>'), 1305 c)
1306
1307 - def test_setitem_replace(self):
1308 Element = self.etree.Element 1309 SubElement = self.etree.SubElement 1310 1311 a = Element('a') 1312 SubElement(a, 'b') 1313 d = Element('d') 1314 a[0] = d 1315 self.assertXML(_bytes('<a><d></d></a>'), a)
1316
1317 - def test_setitem_indexerror(self):
1318 Element = self.etree.Element 1319 SubElement = self.etree.SubElement 1320 1321 a = Element('a') 1322 b = SubElement(a, 'b') 1323 1324 self.assertRaises(IndexError, operator.setitem, a, 1, Element('c'))
1325
1326 - def test_setitem_tail(self):
1327 Element = self.etree.Element 1328 SubElement = self.etree.SubElement 1329 1330 a = Element('a') 1331 b = SubElement(a, 'b') 1332 b.tail = 'B2' 1333 c = Element('c') 1334 c.tail = 'C2' 1335 1336 a[0] = c 1337 self.assertXML( 1338 _bytes('<a><c></c>C2</a>'), 1339 a)
1340
1341 - def test_tag_write(self):
1342 Element = self.etree.Element 1343 SubElement = self.etree.SubElement 1344 1345 a = Element('a') 1346 b = SubElement(a, 'b') 1347 1348 a.tag = 'c' 1349 1350 self.assertEqual( 1351 'c', 1352 a.tag) 1353 1354 self.assertXML( 1355 _bytes('<c><b></b></c>'), 1356 a)
1357
1358 - def test_tag_reset_ns(self):
1359 Element = self.etree.Element 1360 SubElement = self.etree.SubElement 1361 tostring = self.etree.tostring 1362 1363 a = Element('{a}a') 1364 b1 = SubElement(a, '{a}b') 1365 b2 = SubElement(a, '{b}b') 1366 1367 self.assertEqual('{a}b', b1.tag) 1368 1369 b1.tag = 'c' 1370 1371 # can't use C14N here! 1372 self.assertEqual('c', b1.tag) 1373 self.assertEqual(_bytes('<c'), tostring(b1)[:2]) 1374 self.assertTrue(_bytes('<c') in tostring(a))
1375
1376 - def test_tag_reset_root_ns(self):
1377 Element = self.etree.Element 1378 SubElement = self.etree.SubElement 1379 tostring = self.etree.tostring 1380 1381 a = Element('{a}a') 1382 b1 = SubElement(a, '{a}b') 1383 b2 = SubElement(a, '{b}b') 1384 1385 a.tag = 'c' 1386 1387 self.assertEqual( 1388 'c', 1389 a.tag) 1390 1391 # can't use C14N here! 1392 self.assertEqual('c', a.tag) 1393 self.assertEqual(_bytes('<c'), tostring(a)[:2])
1394
1395 - def test_tag_str_subclass(self):
1396 Element = self.etree.Element 1397 1398 class strTest(str): 1399 pass
1400 1401 a = Element("a") 1402 a.tag = strTest("TAG") 1403 self.assertXML(_bytes('<TAG></TAG>'), 1404 a) 1405
1406 - def test_delitem(self):
1407 Element = self.etree.Element 1408 SubElement = self.etree.SubElement 1409 1410 a = Element('a') 1411 b = SubElement(a, 'b') 1412 c = SubElement(a, 'c') 1413 d = SubElement(a, 'd') 1414 1415 del a[1] 1416 self.assertXML( 1417 _bytes('<a><b></b><d></d></a>'), 1418 a) 1419 1420 del a[0] 1421 self.assertXML( 1422 _bytes('<a><d></d></a>'), 1423 a) 1424 1425 del a[0] 1426 self.assertXML( 1427 _bytes('<a></a>'), 1428 a) 1429 # move deleted element into other tree afterwards 1430 other = Element('other') 1431 other.append(c) 1432 self.assertXML( 1433 _bytes('<other><c></c></other>'), 1434 other)
1435
1436 - def test_del_insert(self):
1437 Element = self.etree.Element 1438 SubElement = self.etree.SubElement 1439 1440 a = Element('a') 1441 b = SubElement(a, 'b') 1442 bs = SubElement(b, 'bs') 1443 c = SubElement(a, 'c') 1444 cs = SubElement(c, 'cs') 1445 1446 el = a[0] 1447 self.assertXML( 1448 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1449 a) 1450 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1451 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1452 1453 del a[0] 1454 self.assertXML( 1455 _bytes('<a><c><cs></cs></c></a>'), 1456 a) 1457 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1458 self.assertXML(_bytes('<c><cs></cs></c>'), c) 1459 1460 a.insert(0, el) 1461 self.assertXML( 1462 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1463 a) 1464 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1465 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1466
1467 - def test_del_setitem(self):
1468 Element = self.etree.Element 1469 SubElement = self.etree.SubElement 1470 1471 a = Element('a') 1472 b = SubElement(a, 'b') 1473 bs = SubElement(b, 'bs') 1474 c = SubElement(a, 'c') 1475 cs = SubElement(c, 'cs') 1476 1477 el = a[0] 1478 del a[0] 1479 a[0] = el 1480 self.assertXML( 1481 _bytes('<a><b><bs></bs></b></a>'), 1482 a) 1483 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1484 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1485
1486 - def test_del_setslice(self):
1487 Element = self.etree.Element 1488 SubElement = self.etree.SubElement 1489 1490 a = Element('a') 1491 b = SubElement(a, 'b') 1492 bs = SubElement(b, 'bs') 1493 c = SubElement(a, 'c') 1494 cs = SubElement(c, 'cs') 1495 1496 el = a[0] 1497 del a[0] 1498 a[0:0] = [el] 1499 self.assertXML( 1500 _bytes('<a><b><bs></bs></b><c><cs></cs></c></a>'), 1501 a) 1502 self.assertXML(_bytes('<b><bs></bs></b>'), b) 1503 self.assertXML(_bytes('<c><cs></cs></c>'), c)
1504
1505 - def test_replace_slice_tail(self):
1506 XML = self.etree.XML 1507 a = XML(_bytes('<a><b></b>B2<c></c>C2</a>')) 1508 b, c = a 1509 1510 a[:] = [] 1511 1512 self.assertEqual("B2", b.tail) 1513 self.assertEqual("C2", c.tail)
1514
1515 - def test_merge_namespaced_subtree_as_slice(self):
1516 XML = self.etree.XML 1517 root = XML(_bytes( 1518 '<foo><bar xmlns:baz="http://huhu"><puh><baz:bump1 /><baz:bump2 /></puh></bar></foo>')) 1519 root[:] = root.findall('.//puh') # delete bar from hierarchy 1520 1521 # previously, this lost a namespace declaration on bump2 1522 result = self.etree.tostring(root) 1523 foo = self.etree.fromstring(result) 1524 1525 self.assertEqual('puh', foo[0].tag) 1526 self.assertEqual('{http://huhu}bump1', foo[0][0].tag) 1527 self.assertEqual('{http://huhu}bump2', foo[0][1].tag)
1528
1529 - def test_delitem_tail(self):
1530 ElementTree = self.etree.ElementTree 1531 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1532 doc = ElementTree(file=f) 1533 a = doc.getroot() 1534 del a[0] 1535 self.assertXML( 1536 _bytes('<a><c></c>C2</a>'), 1537 a)
1538
1539 - def test_clear(self):
1540 Element = self.etree.Element 1541 1542 a = Element('a') 1543 a.text = 'foo' 1544 a.tail = 'bar' 1545 a.set('hoi', 'dag') 1546 a.clear() 1547 self.assertEqual(None, a.text) 1548 self.assertEqual(None, a.tail) 1549 self.assertEqual(None, a.get('hoi')) 1550 self.assertEqual('a', a.tag)
1551
1552 - def test_clear_sub(self):
1553 Element = self.etree.Element 1554 SubElement = self.etree.SubElement 1555 1556 a = Element('a') 1557 a.text = 'foo' 1558 a.tail = 'bar' 1559 a.set('hoi', 'dag') 1560 b = SubElement(a, 'b') 1561 c = SubElement(b, 'c') 1562 a.clear() 1563 self.assertEqual(None, a.text) 1564 self.assertEqual(None, a.tail) 1565 self.assertEqual(None, a.get('hoi')) 1566 self.assertEqual('a', a.tag) 1567 self.assertEqual(0, len(a)) 1568 self.assertXML(_bytes('<a></a>'), 1569 a) 1570 self.assertXML(_bytes('<b><c></c></b>'), 1571 b)
1572
1573 - def test_clear_tail(self):
1574 ElementTree = self.etree.ElementTree 1575 f = BytesIO('<a><b></b>B2<c></c>C2</a>') 1576 doc = ElementTree(file=f) 1577 a = doc.getroot() 1578 a.clear() 1579 self.assertXML( 1580 _bytes('<a></a>'), 1581 a)
1582
1583 - def test_insert(self):
1584 Element = self.etree.Element 1585 SubElement = self.etree.SubElement 1586 1587 a = Element('a') 1588 b = SubElement(a, 'b') 1589 c = SubElement(a, 'c') 1590 d = Element('d') 1591 a.insert(0, d) 1592 1593 self.assertEqual( 1594 d, 1595 a[0]) 1596 1597 self.assertXML( 1598 _bytes('<a><d></d><b></b><c></c></a>'), 1599 a) 1600 1601 e = Element('e') 1602 a.insert(2, e) 1603 self.assertEqual( 1604 e, 1605 a[2]) 1606 self.assertXML( 1607 _bytes('<a><d></d><b></b><e></e><c></c></a>'), 1608 a)
1609
1610 - def test_insert_beyond_index(self):
1611 Element = self.etree.Element 1612 SubElement = self.etree.SubElement 1613 1614 a = Element('a') 1615 b = SubElement(a, 'b') 1616 c = Element('c') 1617 1618 a.insert(2, c) 1619 self.assertEqual( 1620 c, 1621 a[1]) 1622 self.assertXML( 1623 _bytes('<a><b></b><c></c></a>'), 1624 a)
1625
1626 - def test_insert_negative(self):
1627 Element = self.etree.Element 1628 SubElement = self.etree.SubElement 1629 1630 a = Element('a') 1631 b = SubElement(a, 'b') 1632 c = SubElement(a, 'c') 1633 1634 d = Element('d') 1635 a.insert(-1, d) 1636 self.assertEqual( 1637 d, 1638 a[-2]) 1639 self.assertXML( 1640 _bytes('<a><b></b><d></d><c></c></a>'), 1641 a)
1642
1643 - def test_insert_tail(self):
1644 Element = self.etree.Element 1645 SubElement = self.etree.SubElement 1646 1647 a = Element('a') 1648 b = SubElement(a, 'b') 1649 1650 c = Element('c') 1651 c.tail = 'C2' 1652 1653 a.insert(0, c) 1654 self.assertXML( 1655 _bytes('<a><c></c>C2<b></b></a>'), 1656 a)
1657
1658 - def test_remove(self):
1659 Element = self.etree.Element 1660 SubElement = self.etree.SubElement 1661 1662 a = Element('a') 1663 b = SubElement(a, 'b') 1664 c = SubElement(a, 'c') 1665 1666 a.remove(b) 1667 self.assertEqual( 1668 c, 1669 a[0]) 1670 self.assertXML( 1671 _bytes('<a><c></c></a>'), 1672 a)
1673
1674 - def test_remove_ns(self):
1675 Element = self.etree.Element 1676 SubElement = self.etree.SubElement 1677 1678 a = Element('{http://test}a') 1679 b = SubElement(a, '{http://test}b') 1680 c = SubElement(a, '{http://test}c') 1681 1682 a.remove(b) 1683 self.assertXML( 1684 _bytes('<ns0:a xmlns:ns0="http://test"><ns0:c></ns0:c></ns0:a>'), 1685 a) 1686 self.assertXML( 1687 _bytes('<ns0:b xmlns:ns0="http://test"></ns0:b>'), 1688 b)
1689
1690 - def test_remove_nonexisting(self):
1691 Element = self.etree.Element 1692 SubElement = self.etree.SubElement 1693 1694 a = Element('a') 1695 b = SubElement(a, 'b') 1696 c = SubElement(a, 'c') 1697 d = Element('d') 1698 self.assertRaises( 1699 ValueError, a.remove, d)
1700
1701 - def test_remove_tail(self):
1702 Element = self.etree.Element 1703 SubElement = self.etree.SubElement 1704 1705 a = Element('a') 1706 b = SubElement(a, 'b') 1707 b.tail = 'b2' 1708 a.remove(b) 1709 self.assertXML( 1710 _bytes('<a></a>'), 1711 a) 1712 self.assertEqual('b2', b.tail)
1713
1714 - def _test_getchildren(self):
1715 Element = self.etree.Element 1716 SubElement = self.etree.SubElement 1717 1718 a = Element('a') 1719 b = SubElement(a, 'b') 1720 c = SubElement(a, 'c') 1721 d = SubElement(b, 'd') 1722 e = SubElement(c, 'e') 1723 self.assertXML( 1724 _bytes('<a><b><d></d></b><c><e></e></c></a>'), 1725 a) 1726 self.assertEqual( 1727 [b, c], 1728 a.getchildren()) 1729 self.assertEqual( 1730 [d], 1731 b.getchildren()) 1732 self.assertEqual( 1733 [], 1734 d.getchildren())
1735
1736 - def test_makeelement(self):
1737 Element = self.etree.Element 1738 1739 a = Element('a') 1740 b = a.makeelement('c', {'hoi':'dag'}) 1741 self.assertXML( 1742 _bytes('<c hoi="dag"></c>'), 1743 b)
1744 1745 required_versions_ET['test_iter'] = (1,3)
1746 - def test_iter(self):
1747 Element = self.etree.Element 1748 SubElement = self.etree.SubElement 1749 1750 a = Element('a') 1751 b = SubElement(a, 'b') 1752 c = SubElement(a, 'c') 1753 d = SubElement(b, 'd') 1754 e = SubElement(c, 'e') 1755 1756 self.assertEqual( 1757 [a, b, d, c, e], 1758 list(a.iter())) 1759 self.assertEqual( 1760 [d], 1761 list(d.iter()))
1762
1763 - def test_getiterator(self):
1764 Element = self.etree.Element 1765 SubElement = self.etree.SubElement 1766 1767 a = Element('a') 1768 b = SubElement(a, 'b') 1769 c = SubElement(a, 'c') 1770 d = SubElement(b, 'd') 1771 e = SubElement(c, 'e') 1772 1773 self.assertEqual( 1774 [a, b, d, c, e], 1775 list(a.getiterator())) 1776 self.assertEqual( 1777 [d], 1778 list(d.getiterator()))
1779
1780 - def test_getiterator_empty(self):
1781 Element = self.etree.Element 1782 SubElement = self.etree.SubElement 1783 1784 a = Element('a') 1785 b = SubElement(a, 'b') 1786 c = SubElement(a, 'c') 1787 d = SubElement(b, 'd') 1788 e = SubElement(c, 'e') 1789 1790 self.assertEqual( 1791 [], 1792 list(a.getiterator('none'))) 1793 self.assertEqual( 1794 [], 1795 list(e.getiterator('none'))) 1796 self.assertEqual( 1797 [e], 1798 list(e.getiterator()))
1799
1800 - def test_getiterator_filter(self):
1801 Element = self.etree.Element 1802 SubElement = self.etree.SubElement 1803 1804 a = Element('a') 1805 b = SubElement(a, 'b') 1806 c = SubElement(a, 'c') 1807 d = SubElement(b, 'd') 1808 e = SubElement(c, 'e') 1809 1810 self.assertEqual( 1811 [a], 1812 list(a.getiterator('a'))) 1813 a2 = SubElement(e, 'a') 1814 self.assertEqual( 1815 [a, a2], 1816 list(a.getiterator('a'))) 1817 self.assertEqual( 1818 [a2], 1819 list(c.getiterator('a')))
1820
1821 - def test_getiterator_filter_all(self):
1822 Element = self.etree.Element 1823 SubElement = self.etree.SubElement 1824 1825 a = Element('a') 1826 b = SubElement(a, 'b') 1827 c = SubElement(a, 'c') 1828 d = SubElement(b, 'd') 1829 e = SubElement(c, 'e') 1830 1831 self.assertEqual( 1832 [a, b, d, c, e], 1833 list(a.getiterator('*')))
1834
1835 - def test_getiterator_filter_comment(self):
1836 Element = self.etree.Element 1837 Comment = self.etree.Comment 1838 SubElement = self.etree.SubElement 1839 1840 a = Element('a') 1841 b = SubElement(a, 'b') 1842 comment_b = Comment("TEST-b") 1843 b.append(comment_b) 1844 1845 self.assertEqual( 1846 [comment_b], 1847 list(a.getiterator(Comment))) 1848 1849 comment_a = Comment("TEST-a") 1850 a.append(comment_a) 1851 1852 self.assertEqual( 1853 [comment_b, comment_a], 1854 list(a.getiterator(Comment))) 1855 1856 self.assertEqual( 1857 [comment_b], 1858 list(b.getiterator(Comment)))
1859
1860 - def test_getiterator_filter_pi(self):
1861 Element = self.etree.Element 1862 PI = self.etree.ProcessingInstruction 1863 SubElement = self.etree.SubElement 1864 1865 a = Element('a') 1866 b = SubElement(a, 'b') 1867 pi_b = PI("TEST-b") 1868 b.append(pi_b) 1869 1870 self.assertEqual( 1871 [pi_b], 1872 list(a.getiterator(PI))) 1873 1874 pi_a = PI("TEST-a") 1875 a.append(pi_a) 1876 1877 self.assertEqual( 1878 [pi_b, pi_a], 1879 list(a.getiterator(PI))) 1880 1881 self.assertEqual( 1882 [pi_b], 1883 list(b.getiterator(PI)))
1884
1885 - def test_getiterator_with_text(self):
1886 Element = self.etree.Element 1887 SubElement = self.etree.SubElement 1888 1889 a = Element('a') 1890 a.text = 'a' 1891 b = SubElement(a, 'b') 1892 b.text = 'b' 1893 b.tail = 'b1' 1894 c = SubElement(a, 'c') 1895 c.text = 'c' 1896 c.tail = 'c1' 1897 d = SubElement(b, 'd') 1898 c.text = 'd' 1899 c.tail = 'd1' 1900 e = SubElement(c, 'e') 1901 e.text = 'e' 1902 e.tail = 'e1' 1903 1904 self.assertEqual( 1905 [a, b, d, c, e], 1906 list(a.getiterator()))
1907 #self.assertEqual( 1908 # [d], 1909 # list(d.getiterator())) 1910
1911 - def test_getiterator_filter_with_text(self):
1912 Element = self.etree.Element 1913 SubElement = self.etree.SubElement 1914 1915 a = Element('a') 1916 a.text = 'a' 1917 b = SubElement(a, 'b') 1918 b.text = 'b' 1919 b.tail = 'b1' 1920 c = SubElement(a, 'c') 1921 c.text = 'c' 1922 c.tail = 'c1' 1923 d = SubElement(b, 'd') 1924 c.text = 'd' 1925 c.tail = 'd1' 1926 e = SubElement(c, 'e') 1927 e.text = 'e' 1928 e.tail = 'e1' 1929 1930 self.assertEqual( 1931 [a], 1932 list(a.getiterator('a'))) 1933 a2 = SubElement(e, 'a') 1934 self.assertEqual( 1935 [a, a2], 1936 list(a.getiterator('a'))) 1937 self.assertEqual( 1938 [a2], 1939 list(e.getiterator('a')))
1940
1941 - def test_getslice(self):
1942 Element = self.etree.Element 1943 SubElement = self.etree.SubElement 1944 1945 a = Element('a') 1946 b = SubElement(a, 'b') 1947 c = SubElement(a, 'c') 1948 d = SubElement(a, 'd') 1949 1950 self.assertEqual( 1951 [b, c], 1952 a[0:2]) 1953 self.assertEqual( 1954 [b, c, d], 1955 a[:]) 1956 self.assertEqual( 1957 [b, c, d], 1958 a[:10]) 1959 self.assertEqual( 1960 [b], 1961 a[0:1]) 1962 self.assertEqual( 1963 [], 1964 a[10:12])
1965
1966 - def test_getslice_negative(self):
1967 Element = self.etree.Element 1968 SubElement = self.etree.SubElement 1969 1970 a = Element('a') 1971 b = SubElement(a, 'b') 1972 c = SubElement(a, 'c') 1973 d = SubElement(a, 'd') 1974 1975 self.assertEqual( 1976 [d], 1977 a[-1:]) 1978 self.assertEqual( 1979 [c, d], 1980 a[-2:]) 1981 self.assertEqual( 1982 [c], 1983 a[-2:-1]) 1984 self.assertEqual( 1985 [b, c], 1986 a[-3:-1]) 1987 self.assertEqual( 1988 [b, c], 1989 a[-3:2])
1990
1991 - def test_getslice_step(self):
1992 Element = self.etree.Element 1993 SubElement = self.etree.SubElement 1994 1995 a = Element('a') 1996 b = SubElement(a, 'b') 1997 c = SubElement(a, 'c') 1998 d = SubElement(a, 'd') 1999 e = SubElement(a, 'e') 2000 2001 self.assertEqual( 2002 [e,d,c,b], 2003 a[::-1]) 2004 self.assertEqual( 2005 [b,d], 2006 a[::2]) 2007 self.assertEqual( 2008 [e,c], 2009 a[::-2]) 2010 self.assertEqual( 2011 [d,c], 2012 a[-2:0:-1]) 2013 self.assertEqual( 2014 [e], 2015 a[:1:-2])
2016
2017 - def test_getslice_text(self):
2018 ElementTree = self.etree.ElementTree 2019 2020 f = BytesIO('<a><b>B</b>B1<c>C</c>C1</a>') 2021 doc = ElementTree(file=f) 2022 a = doc.getroot() 2023 b = a[0] 2024 c = a[1] 2025 self.assertEqual( 2026 [b, c], 2027 a[:]) 2028 self.assertEqual( 2029 [b], 2030 a[0:1]) 2031 self.assertEqual( 2032 [c], 2033 a[1:])
2034
2035 - def test_comment_getitem_getslice(self):
2036 Element = self.etree.Element 2037 Comment = self.etree.Comment 2038 SubElement = self.etree.SubElement 2039 2040 a = Element('a') 2041 b = SubElement(a, 'b') 2042 foo = Comment('foo') 2043 a.append(foo) 2044 c = SubElement(a, 'c') 2045 self.assertEqual( 2046 [b, foo, c], 2047 a[:]) 2048 self.assertEqual( 2049 foo, 2050 a[1]) 2051 a[1] = new = Element('new') 2052 self.assertEqual( 2053 new, 2054 a[1]) 2055 self.assertXML( 2056 _bytes('<a><b></b><new></new><c></c></a>'), 2057 a)
2058
2059 - def test_delslice(self):