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