48
48
def test_version(self):
49
self.assert_(isinstance(etree.__version__, _unicode))
50
self.assert_(isinstance(etree.LXML_VERSION, tuple))
49
self.assertTrue(isinstance(etree.__version__, _unicode))
50
self.assertTrue(isinstance(etree.LXML_VERSION, tuple))
51
51
self.assertEqual(len(etree.LXML_VERSION), 4)
52
self.assert_(isinstance(etree.LXML_VERSION[0], int))
53
self.assert_(isinstance(etree.LXML_VERSION[1], int))
54
self.assert_(isinstance(etree.LXML_VERSION[2], int))
55
self.assert_(isinstance(etree.LXML_VERSION[3], int))
56
self.assert_(etree.__version__.startswith(
52
self.assertTrue(isinstance(etree.LXML_VERSION[0], int))
53
self.assertTrue(isinstance(etree.LXML_VERSION[1], int))
54
self.assertTrue(isinstance(etree.LXML_VERSION[2], int))
55
self.assertTrue(isinstance(etree.LXML_VERSION[3], int))
56
self.assertTrue(etree.__version__.startswith(
57
57
str(etree.LXML_VERSION[0])))
59
59
def test_c_api(self):
60
60
if hasattr(self.etree, '__pyx_capi__'):
61
61
# newer Pyrex compatible C-API
62
self.assert_(isinstance(self.etree.__pyx_capi__, dict))
63
self.assert_(len(self.etree.__pyx_capi__) > 0)
62
self.assertTrue(isinstance(self.etree.__pyx_capi__, dict))
63
self.assertTrue(len(self.etree.__pyx_capi__) > 0)
65
65
# older C-API mechanism
66
self.assert_(hasattr(self.etree, '_import_c_api'))
66
self.assertTrue(hasattr(self.etree, '_import_c_api'))
68
68
def test_element_names(self):
69
69
Element = self.etree.Element
70
70
el = Element('name')
71
self.assertEquals(el.tag, 'name')
71
self.assertEqual(el.tag, 'name')
72
72
el = Element('{}name')
73
self.assertEquals(el.tag, 'name')
73
self.assertEqual(el.tag, 'name')
75
75
def test_element_name_empty(self):
76
76
Element = self.etree.Element
245
245
self.etree.strip_attributes(root, 'a')
246
self.assertEquals(_bytes('<test b="10" c="20"><x b="2"></x></test>'),
246
self.assertEqual(_bytes('<test b="10" c="20"><x b="2"></x></test>'),
247
247
self._writeElement(root))
250
250
self.etree.strip_attributes(root, 'b', 'c')
251
self.assertEquals(_bytes('<test a="5"><x a="4"></x></test>'),
251
self.assertEqual(_bytes('<test a="5"><x a="4"></x></test>'),
252
252
self._writeElement(root))
254
254
def test_strip_attributes_ns(self):
259
259
self.etree.strip_attributes(root, 'a')
261
261
_bytes('<test xmlns:n="http://test/ns" b="10" c="20" n:a="5"><x n:b="2"></x></test>'),
262
262
self._writeElement(root))
265
265
self.etree.strip_attributes(root, '{http://test/ns}a', 'c')
267
267
_bytes('<test xmlns:n="http://test/ns" a="6" b="10"><x a="4" n:b="2"></x></test>'),
268
268
self._writeElement(root))
271
271
self.etree.strip_attributes(root, '{http://test/ns}*')
273
273
_bytes('<test xmlns:n="http://test/ns" a="6" b="10" c="20"><x a="4"></x></test>'),
274
274
self._writeElement(root))
281
281
self.etree.strip_elements(root, 'a')
282
self.assertEquals(_bytes('<test><x></x></test>'),
282
self.assertEqual(_bytes('<test><x></x></test>'),
283
283
self._writeElement(root))
286
286
self.etree.strip_elements(root, 'b', 'c', 'X', 'Y', 'Z')
287
self.assertEquals(_bytes('<test><a></a><x><a></a></x></test>'),
287
self.assertEqual(_bytes('<test><a></a><x><a></a></x></test>'),
288
288
self._writeElement(root))
291
291
self.etree.strip_elements(root, 'c')
292
self.assertEquals(_bytes('<test><a><b></b></a><x><a><b></b></a></x></test>'),
292
self.assertEqual(_bytes('<test><a><b></b></a><x><a><b></b></a></x></test>'),
293
293
self._writeElement(root))
295
295
def test_strip_elements_ns(self):
300
300
self.etree.strip_elements(root, 'a')
301
self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>C</b>BT</n:a>AT<x>X</x>XT</test>'),
301
self.assertEqual(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>C</b>BT</n:a>AT<x>X</x>XT</test>'),
302
302
self._writeElement(root))
305
305
self.etree.strip_elements(root, '{urn:a}b', 'c')
306
self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>C</b>BT</n:a>AT<x>X<a>A<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
306
self.assertEqual(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>C</b>BT</n:a>AT<x>X<a>A<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
307
307
self._writeElement(root))
310
310
self.etree.strip_elements(root, '{urn:a}*', 'c')
311
self.assertEquals(_bytes('<test>TEST<x>X<a>A<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
311
self.assertEqual(_bytes('<test>TEST<x>X<a>A<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
312
312
self._writeElement(root))
315
315
self.etree.strip_elements(root, '{urn:a}*', 'c', with_tail=False)
316
self.assertEquals(_bytes('<test>TESTAT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
316
self.assertEqual(_bytes('<test>TESTAT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
317
317
self._writeElement(root))
319
319
def test_strip_tags(self):
324
324
self.etree.strip_tags(root, 'a')
325
self.assertEquals(_bytes('<test>TESTA<b>B<c></c>CT</b>BTAT<x>XA<b></b>BT<c></c>CTAT</x>XT</test>'),
325
self.assertEqual(_bytes('<test>TESTA<b>B<c></c>CT</b>BTAT<x>XA<b></b>BT<c></c>CTAT</x>XT</test>'),
326
326
self._writeElement(root))
329
329
self.etree.strip_tags(root, 'b', 'c', 'X', 'Y', 'Z')
330
self.assertEquals(_bytes('<test>TEST<a>ABCTBT</a>AT<x>X<a>ABTCT</a>AT</x>XT</test>'),
330
self.assertEqual(_bytes('<test>TEST<a>ABCTBT</a>AT<x>X<a>ABTCT</a>AT</x>XT</test>'),
331
331
self._writeElement(root))
334
334
self.etree.strip_tags(root, 'c')
335
self.assertEquals(_bytes('<test>TEST<a>A<b>BCT</b>BT</a>AT<x>X<a>A<b></b>BTCT</a>AT</x>XT</test>'),
335
self.assertEqual(_bytes('<test>TEST<a>A<b>BCT</b>BT</a>AT<x>X<a>A<b></b>BTCT</a>AT</x>XT</test>'),
336
336
self._writeElement(root))
338
338
def test_strip_tags_pi_comment(self):
345
345
self.etree.strip_tags(root, PI)
346
self.assertEquals(_bytes('<!--comment1-->\n<?PI1?>\n<test>TEST<!--comment2-->XT</test>\n<!--comment3-->\n<?PI1?>'),
346
self.assertEqual(_bytes('<!--comment1-->\n<?PI1?>\n<test>TEST<!--comment2-->XT</test>\n<!--comment3-->\n<?PI1?>'),
347
347
self._writeElement(root))
350
350
self.etree.strip_tags(root, Comment)
351
self.assertEquals(_bytes('<!--comment1-->\n<?PI1?>\n<test>TESTXT<?PI2?></test>\n<!--comment3-->\n<?PI1?>'),
351
self.assertEqual(_bytes('<!--comment1-->\n<?PI1?>\n<test>TESTXT<?PI2?></test>\n<!--comment3-->\n<?PI1?>'),
352
352
self._writeElement(root))
355
355
self.etree.strip_tags(root, PI, Comment)
356
self.assertEquals(_bytes('<!--comment1-->\n<?PI1?>\n<test>TESTXT</test>\n<!--comment3-->\n<?PI1?>'),
356
self.assertEqual(_bytes('<!--comment1-->\n<?PI1?>\n<test>TESTXT</test>\n<!--comment3-->\n<?PI1?>'),
357
357
self._writeElement(root))
360
360
self.etree.strip_tags(root, Comment, PI)
361
self.assertEquals(_bytes('<!--comment1-->\n<?PI1?>\n<test>TESTXT</test>\n<!--comment3-->\n<?PI1?>'),
361
self.assertEqual(_bytes('<!--comment1-->\n<?PI1?>\n<test>TESTXT</test>\n<!--comment3-->\n<?PI1?>'),
362
362
self._writeElement(root))
364
364
def test_strip_tags_pi_comment_all(self):
372
372
self.etree.strip_tags(ElementTree(root), PI)
373
self.assertEquals(_bytes('<!--comment1-->\n<test>TEST<!--comment2-->XT</test>\n<!--comment3-->'),
373
self.assertEqual(_bytes('<!--comment1-->\n<test>TEST<!--comment2-->XT</test>\n<!--comment3-->'),
374
374
self._writeElement(root))
377
377
self.etree.strip_tags(ElementTree(root), Comment)
378
self.assertEquals(_bytes('<?PI1?>\n<test>TESTXT<?PI2?></test>\n<?PI1?>'),
378
self.assertEqual(_bytes('<?PI1?>\n<test>TESTXT<?PI2?></test>\n<?PI1?>'),
379
379
self._writeElement(root))
382
382
self.etree.strip_tags(ElementTree(root), PI, Comment)
383
self.assertEquals(_bytes('<test>TESTXT</test>'),
383
self.assertEqual(_bytes('<test>TESTXT</test>'),
384
384
self._writeElement(root))
387
387
self.etree.strip_tags(ElementTree(root), Comment, PI)
388
self.assertEquals(_bytes('<test>TESTXT</test>'),
388
self.assertEqual(_bytes('<test>TESTXT</test>'),
389
389
self._writeElement(root))
391
391
def test_strip_tags_doc_style(self):
407
407
self.etree.strip_tags(root, 'a')
408
self.assertEquals(re.sub(_bytes('</?a[^>]*>'), _bytes(''), xml).replace(_bytes('<br/>'), _bytes('<br></br>')),
408
self.assertEqual(re.sub(_bytes('</?a[^>]*>'), _bytes(''), xml).replace(_bytes('<br/>'), _bytes('<br></br>')),
409
409
self._writeElement(root))
412
412
self.etree.strip_tags(root, 'a', 'br')
413
self.assertEquals(re.sub(_bytes('</?a[^>]*>'), _bytes(''),
413
self.assertEqual(re.sub(_bytes('</?a[^>]*>'), _bytes(''),
414
414
re.sub(_bytes('<br[^>]*>'), _bytes(''), xml)),
415
415
self._writeElement(root))
422
422
self.etree.strip_tags(root, 'a')
423
self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>CT</b>BT</n:a>AT<x>XA<b xmlns="urn:a"></b>BT<c xmlns="urn:x"></c>CTAT</x>XT</test>'),
423
self.assertEqual(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>CT</b>BT</n:a>AT<x>XA<b xmlns="urn:a"></b>BT<c xmlns="urn:x"></c>CTAT</x>XT</test>'),
424
424
self._writeElement(root))
427
427
self.etree.strip_tags(root, '{urn:a}b', 'c')
428
self.assertEquals(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>CT</b>BT</n:a>AT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
428
self.assertEqual(_bytes('<test>TEST<n:a xmlns:n="urn:a">A<b>B<c xmlns="urn:c"></c>CT</b>BT</n:a>AT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
429
429
self._writeElement(root))
432
432
self.etree.strip_tags(root, '{urn:a}*', 'c')
433
self.assertEquals(_bytes('<test>TESTA<b>B<c xmlns="urn:c"></c>CT</b>BTAT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
433
self.assertEqual(_bytes('<test>TESTA<b>B<c xmlns="urn:c"></c>CT</b>BTAT<x>X<a>ABT<c xmlns="urn:x"></c>CT</a>AT</x>XT</test>'),
434
434
self._writeElement(root))
436
436
def test_strip_tags_and_remove(self):
437
437
# previously crashed
438
438
HTML = self.etree.HTML
439
439
root = HTML(_bytes('<div><h1>title</h1> <b>foo</b> <p>boo</p></div>'))[0][0]
440
self.assertEquals(_bytes('<div><h1>title</h1> <b>foo</b> <p>boo</p></div>'),
440
self.assertEqual(_bytes('<div><h1>title</h1> <b>foo</b> <p>boo</p></div>'),
441
441
self.etree.tostring(root))
442
442
self.etree.strip_tags(root, 'b')
443
self.assertEquals(_bytes('<div><h1>title</h1> foo <p>boo</p></div>'),
443
self.assertEqual(_bytes('<div><h1>title</h1> foo <p>boo</p></div>'),
444
444
self.etree.tostring(root))
445
445
root.remove(root[0])
446
self.assertEquals(_bytes('<div><p>boo</p></div>'),
446
self.assertEqual(_bytes('<div><p>boo</p></div>'),
447
447
self.etree.tostring(root))
449
449
def test_pi(self):
456
456
a.append(ProcessingInstruction('foo', 'some more text'))
457
self.assertEquals(a[0].target, 'foo')
458
self.assertEquals(a[0].text, 'some more text')
457
self.assertEqual(a[0].target, 'foo')
458
self.assertEqual(a[0].text, 'some more text')
460
460
def test_pi_parse(self):
461
461
XML = self.etree.XML
462
462
root = XML(_bytes("<test><?mypi my test ?></test>"))
463
self.assertEquals(root[0].target, "mypi")
464
self.assertEquals(root[0].text, "my test ")
463
self.assertEqual(root[0].target, "mypi")
464
self.assertEqual(root[0].text, "my test ")
466
466
def test_pi_pseudo_attributes_get(self):
467
467
XML = self.etree.XML
468
468
root = XML(_bytes("<test><?mypi my='1' test=\" abc \" quotes=\"' '\" only names ?></test>"))
469
self.assertEquals(root[0].target, "mypi")
470
self.assertEquals(root[0].get('my'), "1")
471
self.assertEquals(root[0].get('test'), " abc ")
472
self.assertEquals(root[0].get('quotes'), "' '")
473
self.assertEquals(root[0].get('only'), None)
474
self.assertEquals(root[0].get('names'), None)
475
self.assertEquals(root[0].get('nope'), None)
469
self.assertEqual(root[0].target, "mypi")
470
self.assertEqual(root[0].get('my'), "1")
471
self.assertEqual(root[0].get('test'), " abc ")
472
self.assertEqual(root[0].get('quotes'), "' '")
473
self.assertEqual(root[0].get('only'), None)
474
self.assertEqual(root[0].get('names'), None)
475
self.assertEqual(root[0].get('nope'), None)
477
477
def test_pi_pseudo_attributes_attrib(self):
478
478
XML = self.etree.XML
479
479
root = XML(_bytes("<test><?mypi my='1' test=\" abc \" quotes=\"' '\" only names ?></test>"))
480
self.assertEquals(root[0].target, "mypi")
481
self.assertEquals(root[0].attrib['my'], "1")
482
self.assertEquals(root[0].attrib['test'], " abc ")
483
self.assertEquals(root[0].attrib['quotes'], "' '")
480
self.assertEqual(root[0].target, "mypi")
481
self.assertEqual(root[0].attrib['my'], "1")
482
self.assertEqual(root[0].attrib['test'], " abc ")
483
self.assertEqual(root[0].attrib['quotes'], "' '")
484
484
self.assertRaises(KeyError, root[0].attrib.__getitem__, 'only')
485
485
self.assertRaises(KeyError, root[0].attrib.__getitem__, 'names')
486
486
self.assertRaises(KeyError, root[0].attrib.__getitem__, 'nope')
493
493
b = copy.deepcopy(a)
494
494
b.text = "ANOTHER"
496
self.assertEquals('ONE', a.text)
497
self.assertEquals('ANOTHER', b.text)
496
self.assertEqual('ONE', a.text)
497
self.assertEqual('ANOTHER', b.text)
499
499
def test_deepcopy_elementtree_pi(self):
500
500
XML = self.etree.XML
501
501
tostring = self.etree.tostring
502
502
root = XML(_bytes("<?mypi my test ?><test/><!--comment -->"))
503
503
tree1 = self.etree.ElementTree(root)
504
self.assertEquals(_bytes("<?mypi my test ?><test/><!--comment -->"),
504
self.assertEqual(_bytes("<?mypi my test ?><test/><!--comment -->"),
507
507
tree2 = copy.deepcopy(tree1)
508
self.assertEquals(_bytes("<?mypi my test ?><test/><!--comment -->"),
508
self.assertEqual(_bytes("<?mypi my test ?><test/><!--comment -->"),
511
511
root2 = copy.deepcopy(tree1.getroot())
512
self.assertEquals(_bytes("<test/>"),
512
self.assertEqual(_bytes("<test/>"),
515
515
def test_deepcopy_elementtree_dtd(self):
518
518
xml = _bytes('<!DOCTYPE test [\n<!ENTITY entity "tasty">\n]>\n<test/>')
520
520
tree1 = self.etree.ElementTree(root)
521
self.assertEquals(xml, tostring(tree1))
521
self.assertEqual(xml, tostring(tree1))
523
523
tree2 = copy.deepcopy(tree1)
524
self.assertEquals(xml, tostring(tree2))
524
self.assertEqual(xml, tostring(tree2))
526
526
root2 = copy.deepcopy(tree1.getroot())
527
self.assertEquals(_bytes("<test/>"),
527
self.assertEqual(_bytes("<test/>"),
530
530
def test_attribute_set(self):
600
600
f = BytesIO('<a><!--A--><b><!-- B --><c/></b><!--C--></a>')
601
601
events = list(iterparse(f, events=('end', 'comment')))
602
602
root = events[-1][1]
603
self.assertEquals(6, len(events))
604
self.assertEquals(['A', ' B ', 'c', 'b', 'C', 'a'],
603
self.assertEqual(6, len(events))
604
self.assertEqual(['A', ' B ', 'c', 'b', 'C', 'a'],
605
605
[ name(*item) for item in events ])
607
607
_bytes('<a><!--A--><b><!-- B --><c/></b><!--C--></a>'),
622
622
f = BytesIO('<?pia a?><a><?pib b?><b><?pic c?><c/></b><?pid d?></a><?pie e?>')
623
623
events = list(iterparse(f, events=('end', 'pi')))
624
624
root = events[-2][1]
625
self.assertEquals(8, len(events))
626
self.assertEquals([('pia','a'), ('pib','b'), ('pic','c'), 'c', 'b',
625
self.assertEqual(8, len(events))
626
self.assertEqual([('pia','a'), ('pib','b'), ('pic','c'), 'c', 'b',
627
627
('pid','d'), 'a', ('pie','e')],
628
628
[ name(*item) for item in events ])
630
630
_bytes('<?pia a?><a><?pib b?><b><?pic c?><c/></b><?pid d?></a><?pie e?>'),
631
631
tostring(ElementTree(root)))
638
638
events = list(iterparse(f, remove_comments=True,
639
639
events=('end', 'comment')))
640
640
root = events[-1][1]
641
self.assertEquals(3, len(events))
642
self.assertEquals(['c', 'b', 'a'],
641
self.assertEqual(3, len(events))
642
self.assertEqual(['c', 'b', 'a'],
643
643
[ el.tag for (event, el) in events ])
645
645
_bytes('<a><b><c/></b></a>'),
710
710
iterator = iterparse(f, tag="{}b", events=('start', 'end'))
711
711
events = list(iterator)
712
712
root = iterator.root
713
self.assertEquals([], events)
713
self.assertEqual([], events)
715
715
def test_iterparse_tag_ns_all(self):
716
716
iterparse = self.etree.iterparse
717
717
f = BytesIO('<a xmlns="urn:test:1"><b><d/></b><c/></a>')
718
718
iterator = iterparse(f, tag="{urn:test:1}*", events=('start', 'end'))
719
719
events = list(iterator)
720
self.assertEquals(8, len(events))
720
self.assertEqual(8, len(events))
722
722
def test_iterparse_tag_ns_empty_all(self):
723
723
iterparse = self.etree.iterparse
724
724
f = BytesIO('<a xmlns="urn:test:1"><b><d/></b><c/></a>')
725
725
iterator = iterparse(f, tag="{}*", events=('start', 'end'))
726
726
events = list(iterator)
727
self.assertEquals([], events)
727
self.assertEqual([], events)
729
729
f = BytesIO('<a><b><d/></b><c/></a>')
730
730
iterator = iterparse(f, tag="{}*", events=('start', 'end'))
731
731
events = list(iterator)
732
self.assertEquals(8, len(events))
732
self.assertEqual(8, len(events))
734
734
def test_iterparse_encoding_error(self):
735
735
text = _str('SĆøk pĆ„ nettet')
785
785
root = parser.close()
787
self.assertEquals(root.tag, "root")
788
self.assertEquals(len(root), 1)
789
self.assertEquals(root[0].tag, "a")
790
self.assertEquals(root[0].get("test"), "works")
791
self.assertEquals(len(root[0]), 1)
792
self.assertEquals(root[0][0].tag, "othertag")
787
self.assertEqual(root.tag, "root")
788
self.assertEqual(len(root), 1)
789
self.assertEqual(root[0].tag, "a")
790
self.assertEqual(root[0].get("test"), "works")
791
self.assertEqual(len(root[0]), 1)
792
self.assertEqual(root[0][0].tag, "othertag")
793
793
# FIXME: would be nice to get some errors logged ...
794
#self.assert_(len(parser.error_log) > 0, "error log is empty")
794
#self.assertTrue(len(parser.error_log) > 0, "error log is empty")
796
796
def test_elementtree_parser_target_type_error(self):
797
assertEquals = self.assertEquals
797
assertEqual = self.assertEqual
798
798
assertFalse = self.assertFalse
893
893
parser.feed(_bytes('<!--a--><root>A<!--b--><sub/><!--c-->B</root><!--d-->'))
894
894
done = parser.close()
896
self.assertEquals("DONE", done)
897
self.assertEquals(["comment-a", "start-root", "data-A", "comment-b",
896
self.assertEqual("DONE", done)
897
self.assertEqual(["comment-a", "start-root", "data-A", "comment-b",
898
898
"start-sub", "end-sub", "comment-c", "data-B",
899
899
"end-root", "comment-d"],
1244
1244
xml = _bytes('<!DOCTYPE doc SYSTEM "test"><doc>&myentity;</doc>')
1245
1245
tree = parse(BytesIO(xml), parser)
1246
1246
root = tree.getroot()
1247
self.assertEquals(root[0].tag, Entity)
1248
self.assertEquals(root[0].text, "&myentity;")
1249
self.assertEquals(root[0].tail, None)
1250
self.assertEquals(root[0].name, "myentity")
1247
self.assertEqual(root[0].tag, Entity)
1248
self.assertEqual(root[0].text, "&myentity;")
1249
self.assertEqual(root[0].tail, None)
1250
self.assertEqual(root[0].name, "myentity")
1252
self.assertEquals(_bytes('<doc>&myentity;</doc>'),
1252
self.assertEqual(_bytes('<doc>&myentity;</doc>'),
1253
1253
tostring(root))
1255
1255
def test_entity_restructure(self):
1263
1263
parser = self.etree.XMLParser(resolve_entities=False)
1264
1264
root = etree.fromstring(xml, parser)
1265
self.assertEquals([ el.tag for el in root ],
1265
self.assertEqual([ el.tag for el in root ],
1266
1266
['child1', 'child2', 'child3'])
1268
1268
root[0] = root[-1]
1269
self.assertEquals([ el.tag for el in root ],
1269
self.assertEqual([ el.tag for el in root ],
1270
1270
['child3', 'child2'])
1271
self.assertEquals(root[0][0].text, ' ')
1272
self.assertEquals(root[0][0].name, 'nbsp')
1271
self.assertEqual(root[0][0].text, ' ')
1272
self.assertEqual(root[0][0].name, 'nbsp')
1274
1274
def test_entity_append(self):
1275
1275
Entity = self.etree.Entity
1279
1279
root = Element("root")
1280
1280
root.append( Entity("test") )
1282
self.assertEquals(root[0].tag, Entity)
1283
self.assertEquals(root[0].text, "&test;")
1284
self.assertEquals(root[0].tail, None)
1285
self.assertEquals(root[0].name, "test")
1282
self.assertEqual(root[0].tag, Entity)
1283
self.assertEqual(root[0].text, "&test;")
1284
self.assertEqual(root[0].tail, None)
1285
self.assertEqual(root[0].name, "test")
1287
self.assertEquals(_bytes('<root>&test;</root>'),
1287
self.assertEqual(_bytes('<root>&test;</root>'),
1288
1288
tostring(root))
1290
1290
def test_entity_values(self):
1291
1291
Entity = self.etree.Entity
1292
self.assertEquals(Entity("test").text, '&test;')
1293
self.assertEquals(Entity("#17683").text, '䔓')
1294
self.assertEquals(Entity("#x1768").text, 'ᝨ')
1295
self.assertEquals(Entity("#x98AF").text, '颯')
1292
self.assertEqual(Entity("test").text, '&test;')
1293
self.assertEqual(Entity("#17683").text, '䔓')
1294
self.assertEqual(Entity("#x1768").text, 'ᝨ')
1295
self.assertEqual(Entity("#x98AF").text, '颯')
1297
1297
def test_entity_error(self):
1298
1298
Entity = self.etree.Entity
1347
1347
parser = self.etree.XMLParser(strip_cdata=False)
1348
1348
root = self.etree.XML(_bytes('<root><![CDATA[test]]></root>'), parser)
1350
self.assertEquals('test', root.text)
1351
self.assertEquals(_bytes('<root><![CDATA[test]]></root>'),
1350
self.assertEqual('test', root.text)
1351
self.assertEqual(_bytes('<root><![CDATA[test]]></root>'),
1352
1352
tostring(root))
1354
1354
def test_cdata_xpath(self):
1355
1355
tostring = self.etree.tostring
1356
1356
parser = self.etree.XMLParser(strip_cdata=False)
1357
1357
root = self.etree.XML(_bytes('<root><![CDATA[test]]></root>'), parser)
1358
self.assertEquals(_bytes('<root><![CDATA[test]]></root>'),
1358
self.assertEqual(_bytes('<root><![CDATA[test]]></root>'),
1359
1359
tostring(root))
1361
self.assertEquals(['test'], root.xpath('//text()'))
1361
self.assertEqual(['test'], root.xpath('//text()'))
1363
1363
# TypeError in etree, AssertionError in ElementTree;
1364
1364
def test_setitem_assert(self):
1962
1962
tree, dic = parseid(BytesIO(xml_text))
1963
1963
root = tree.getroot()
1964
1964
root2 = XML(xml_text)
1965
self.assertEquals(self._writeElement(root),
1965
self.assertEqual(self._writeElement(root),
1966
1966
self._writeElement(root2))
1968
1968
"chapter1" : root[0],
1969
1969
"xmlid" : root[3],
1970
1970
"warn1" : root[4]
1972
self.assert_("chapter1" in dic)
1973
self.assert_("warn1" in dic)
1974
self.assert_("xmlid" in dic)
1972
self.assertTrue("chapter1" in dic)
1973
self.assertTrue("warn1" in dic)
1974
self.assertTrue("xmlid" in dic)
1975
1975
self._checkIDDict(dic, expected)
1977
1977
def test_XMLDTDID(self):
1997
1997
root, dic = XMLDTDID(xml_text)
1998
1998
root2 = XML(xml_text)
1999
self.assertEquals(self._writeElement(root),
1999
self.assertEqual(self._writeElement(root),
2000
2000
self._writeElement(root2))
2002
2002
"chapter1" : root[0],
2003
2003
"xmlid" : root[3],
2004
2004
"warn1" : root[4]
2006
self.assert_("chapter1" in dic)
2007
self.assert_("warn1" in dic)
2008
self.assert_("xmlid" in dic)
2006
self.assertTrue("chapter1" in dic)
2007
self.assertTrue("warn1" in dic)
2008
self.assertTrue("xmlid" in dic)
2009
2009
self._checkIDDict(dic, expected)
2011
2011
def test_XMLDTDID_empty(self):
2023
2023
root, dic = XMLDTDID(xml_text)
2024
2024
root2 = XML(xml_text)
2025
self.assertEquals(self._writeElement(root),
2025
self.assertEqual(self._writeElement(root),
2026
2026
self._writeElement(root2))
2028
2028
self._checkIDDict(dic, expected)
2030
2030
def _checkIDDict(self, dic, expected):
2031
self.assertEquals(len(dic),
2031
self.assertEqual(len(dic),
2033
self.assertEquals(sorted(dic.items()),
2033
self.assertEqual(sorted(dic.items()),
2034
2034
sorted(expected.items()))
2035
2035
if sys.version_info < (3,):
2036
self.assertEquals(sorted(dic.iteritems()),
2036
self.assertEqual(sorted(dic.iteritems()),
2037
2037
sorted(expected.iteritems()))
2038
self.assertEquals(sorted(dic.keys()),
2038
self.assertEqual(sorted(dic.keys()),
2039
2039
sorted(expected.keys()))
2040
2040
if sys.version_info < (3,):
2041
self.assertEquals(sorted(dic.iterkeys()),
2041
self.assertEqual(sorted(dic.iterkeys()),
2042
2042
sorted(expected.iterkeys()))
2043
2043
if sys.version_info < (3,):
2044
self.assertEquals(sorted(dic.values()),
2044
self.assertEqual(sorted(dic.values()),
2045
2045
sorted(expected.values()))
2046
self.assertEquals(sorted(dic.itervalues()),
2046
self.assertEqual(sorted(dic.itervalues()),
2047
2047
sorted(expected.itervalues()))
2049
2049
def test_namespaces(self):
2150
2150
two.append(baz)
2151
2151
del one # make sure the source document is deallocated
2153
self.assertEquals('{%s}baz' % ns_href, baz.tag)
2153
self.assertEqual('{%s}baz' % ns_href, baz.tag)
2155
2155
_bytes('<root xmlns:ns="%s"><ns:baz/></root>' % ns_href),
2156
2156
self.etree.tostring(two))
2158
2158
def test_namespace_cleanup(self):
2159
2159
xml = _bytes('<foo xmlns="F" xmlns:x="x"><bar xmlns:ns="NS" xmlns:b="b" xmlns="B"><ns:baz/></bar></foo>')
2160
2160
root = self.etree.fromstring(xml)
2161
self.assertEquals(xml,
2161
self.assertEqual(xml,
2162
2162
self.etree.tostring(root))
2163
2163
self.etree.cleanup_namespaces(root)
2165
2165
_bytes('<foo xmlns="F"><bar xmlns:ns="NS" xmlns="B"><ns:baz/></bar></foo>'),
2166
2166
self.etree.tostring(root))
2191
self.assertEquals(re, e.nsmap)
2192
self.assertEquals(r, s.nsmap)
2191
self.assertEqual(re, e.nsmap)
2192
self.assertEqual(r, s.nsmap)
2194
2194
def test_html_prefix_nsmap(self):
2195
2195
etree = self.etree
2196
2196
el = etree.HTML('<hha:page-description>aa</hha:page-description>').find('.//page-description')
2197
self.assertEquals({'hha': None}, el.nsmap)
2197
self.assertEqual({'hha': None}, el.nsmap)
2199
2199
def test_getiterator_filter_multiple(self):
2200
2200
Element = self.etree.Element
2375
2375
ElementTree = self.etree.ElementTree
2376
2376
QName = self.etree.QName
2377
2377
tree = ElementTree(XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')))
2378
self.assertEquals(tree.find(QName("c")), tree.getroot()[2])
2378
self.assertEqual(tree.find(QName("c")), tree.getroot()[2])
2380
2380
def test_elementtree_findall_qname(self):
2381
2381
XML = self.etree.XML
2382
2382
ElementTree = self.etree.ElementTree
2383
2383
QName = self.etree.QName
2384
2384
tree = ElementTree(XML(_bytes('<a><b><c/></b><b/><c><b/></c></a>')))
2385
self.assertEquals(len(list(tree.findall(QName("c")))), 1)
2385
self.assertEqual(len(list(tree.findall(QName("c")))), 1)
2387
2387
def test_elementtree_findall_ns_qname(self):
2388
2388
XML = self.etree.XML
2390
2390
QName = self.etree.QName
2391
2391
tree = ElementTree(XML(
2392
2392
_bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')))
2393
self.assertEquals(len(list(tree.findall(QName("b")))), 2)
2394
self.assertEquals(len(list(tree.findall(QName("X", "b")))), 1)
2393
self.assertEqual(len(list(tree.findall(QName("b")))), 2)
2394
self.assertEqual(len(list(tree.findall(QName("X", "b")))), 1)
2396
2396
def test_findall_ns(self):
2397
2397
XML = self.etree.XML
2398
2398
root = XML(_bytes('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>'))
2399
self.assertEquals(len(root.findall(".//{X}b")), 2)
2400
self.assertEquals(len(root.findall(".//{X}*")), 2)
2401
self.assertEquals(len(root.findall(".//b")), 3)
2399
self.assertEqual(len(root.findall(".//{X}b")), 2)
2400
self.assertEqual(len(root.findall(".//{X}*")), 2)
2401
self.assertEqual(len(root.findall(".//b")), 3)
2403
2403
def test_findall_syntax_error(self):
2404
2404
XML = self.etree.XML
2632
2632
Element = self.etree.Element
2633
2633
SubElement = self.etree.SubElement
2634
2634
el = Element("test")
2635
self.assertEquals(None, el.sourceline)
2635
self.assertEqual(None, el.sourceline)
2637
2637
child = SubElement(el, "test")
2638
self.assertEquals(None, el.sourceline)
2639
self.assertEquals(None, child.sourceline)
2638
self.assertEqual(None, el.sourceline)
2639
self.assertEqual(None, child.sourceline)
2641
2641
def test_XML_base_url_docinfo(self):
2642
2642
etree = self.etree
2643
2643
root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
2644
2644
docinfo = root.getroottree().docinfo
2645
self.assertEquals(docinfo.URL, "http://no/such/url")
2645
self.assertEqual(docinfo.URL, "http://no/such/url")
2647
2647
def test_XML_set_base_url_docinfo(self):
2648
2648
etree = self.etree
2649
2649
root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
2650
2650
docinfo = root.getroottree().docinfo
2651
self.assertEquals(docinfo.URL, "http://no/such/url")
2651
self.assertEqual(docinfo.URL, "http://no/such/url")
2652
2652
docinfo.URL = "https://secret/url"
2653
self.assertEquals(docinfo.URL, "https://secret/url")
2653
self.assertEqual(docinfo.URL, "https://secret/url")
2655
2655
def test_parse_stringio_base_url(self):
2656
2656
etree = self.etree
2657
2657
tree = etree.parse(BytesIO("<root/>"), base_url="http://no/such/url")
2658
2658
docinfo = tree.docinfo
2659
self.assertEquals(docinfo.URL, "http://no/such/url")
2659
self.assertEqual(docinfo.URL, "http://no/such/url")
2661
2661
def test_parse_base_url_docinfo(self):
2662
2662
etree = self.etree
2663
2663
tree = etree.parse(fileInTestDir('include/test_xinclude.xml'),
2664
2664
base_url="http://no/such/url")
2665
2665
docinfo = tree.docinfo
2666
self.assertEquals(docinfo.URL, "http://no/such/url")
2666
self.assertEqual(docinfo.URL, "http://no/such/url")
2668
2668
def test_HTML_base_url_docinfo(self):
2669
2669
etree = self.etree
2670
2670
root = etree.HTML(_bytes("<html/>"), base_url="http://no/such/url")
2671
2671
docinfo = root.getroottree().docinfo
2672
self.assertEquals(docinfo.URL, "http://no/such/url")
2672
self.assertEqual(docinfo.URL, "http://no/such/url")
2674
2674
def test_docinfo_public(self):
2675
2675
etree = self.etree
2683
2683
tree = etree.parse(BytesIO(xml))
2684
2684
docinfo = tree.docinfo
2685
self.assertEquals(docinfo.encoding, "ascii")
2686
self.assertEquals(docinfo.xml_version, "1.0")
2687
self.assertEquals(docinfo.public_id, pub_id)
2688
self.assertEquals(docinfo.system_url, sys_id)
2689
self.assertEquals(docinfo.root_name, 'html')
2690
self.assertEquals(docinfo.doctype, doctype_string)
2685
self.assertEqual(docinfo.encoding, "ascii")
2686
self.assertEqual(docinfo.xml_version, "1.0")
2687
self.assertEqual(docinfo.public_id, pub_id)
2688
self.assertEqual(docinfo.system_url, sys_id)
2689
self.assertEqual(docinfo.root_name, 'html')
2690
self.assertEqual(docinfo.doctype, doctype_string)
2692
2692
def test_docinfo_system(self):
2693
2693
etree = self.etree
2699
2699
tree = etree.parse(BytesIO(xml))
2700
2700
docinfo = tree.docinfo
2701
self.assertEquals(docinfo.encoding, "UTF-8")
2702
self.assertEquals(docinfo.xml_version, "1.0")
2703
self.assertEquals(docinfo.public_id, None)
2704
self.assertEquals(docinfo.system_url, sys_id)
2705
self.assertEquals(docinfo.root_name, 'html')
2706
self.assertEquals(docinfo.doctype, doctype_string)
2701
self.assertEqual(docinfo.encoding, "UTF-8")
2702
self.assertEqual(docinfo.xml_version, "1.0")
2703
self.assertEqual(docinfo.public_id, None)
2704
self.assertEqual(docinfo.system_url, sys_id)
2705
self.assertEqual(docinfo.root_name, 'html')
2706
self.assertEqual(docinfo.doctype, doctype_string)
2708
2708
def test_docinfo_empty(self):
2709
2709
etree = self.etree
2710
2710
xml = _bytes('<html><body></body></html>')
2711
2711
tree = etree.parse(BytesIO(xml))
2712
2712
docinfo = tree.docinfo
2713
self.assertEquals(docinfo.encoding, "UTF-8")
2714
self.assertEquals(docinfo.xml_version, "1.0")
2715
self.assertEquals(docinfo.public_id, None)
2716
self.assertEquals(docinfo.system_url, None)
2717
self.assertEquals(docinfo.root_name, 'html')
2718
self.assertEquals(docinfo.doctype, '')
2713
self.assertEqual(docinfo.encoding, "UTF-8")
2714
self.assertEqual(docinfo.xml_version, "1.0")
2715
self.assertEqual(docinfo.public_id, None)
2716
self.assertEqual(docinfo.system_url, None)
2717
self.assertEqual(docinfo.root_name, 'html')
2718
self.assertEqual(docinfo.doctype, '')
2720
2720
def test_docinfo_name_only(self):
2721
2721
etree = self.etree
2722
2722
xml = _bytes('<!DOCTYPE root><root></root>')
2723
2723
tree = etree.parse(BytesIO(xml))
2724
2724
docinfo = tree.docinfo
2725
self.assertEquals(docinfo.encoding, "UTF-8")
2726
self.assertEquals(docinfo.xml_version, "1.0")
2727
self.assertEquals(docinfo.public_id, None)
2728
self.assertEquals(docinfo.system_url, None)
2729
self.assertEquals(docinfo.root_name, 'root')
2730
self.assertEquals(docinfo.doctype, '<!DOCTYPE root>')
2725
self.assertEqual(docinfo.encoding, "UTF-8")
2726
self.assertEqual(docinfo.xml_version, "1.0")
2727
self.assertEqual(docinfo.public_id, None)
2728
self.assertEqual(docinfo.system_url, None)
2729
self.assertEqual(docinfo.root_name, 'root')
2730
self.assertEqual(docinfo.doctype, '<!DOCTYPE root>')
2732
2732
def test_doctype_name_only_roundtrip(self):
2733
2733
etree = self.etree
2734
2734
xml = _bytes('<!DOCTYPE root>\n<root/>')
2735
2735
tree = etree.parse(BytesIO(xml))
2736
self.assertEquals(xml, etree.tostring(tree))
2736
self.assertEqual(xml, etree.tostring(tree))
2738
2738
def test_doctype_output_override(self):
2739
2739
etree = self.etree
2744
2744
xml = _bytes('<!DOCTYPE root>\n<root/>')
2745
2745
tree = etree.parse(BytesIO(xml))
2746
self.assertEquals(xml.replace(_bytes('<!DOCTYPE root>'), doctype_string),
2746
self.assertEqual(xml.replace(_bytes('<!DOCTYPE root>'), doctype_string),
2747
2747
etree.tostring(tree, doctype=doctype_string))
2749
2749
def test_xml_base(self):
2750
2750
etree = self.etree
2751
2751
root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
2752
self.assertEquals(root.base, "http://no/such/url")
2752
self.assertEqual(root.base, "http://no/such/url")
2754
2754
root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2755
2755
root.base = "https://secret/url"
2756
self.assertEquals(root.base, "https://secret/url")
2756
self.assertEqual(root.base, "https://secret/url")
2758
2758
root.get('{http://www.w3.org/XML/1998/namespace}base'),
2759
2759
"https://secret/url")
2761
2761
def test_xml_base_attribute(self):
2762
2762
etree = self.etree
2763
2763
root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
2764
self.assertEquals(root.base, "http://no/such/url")
2764
self.assertEqual(root.base, "http://no/such/url")
2766
2766
root.get('{http://www.w3.org/XML/1998/namespace}base'), None)
2767
2767
root.set('{http://www.w3.org/XML/1998/namespace}base',
2768
2768
"https://secret/url")
2769
self.assertEquals(root.base, "https://secret/url")
2769
self.assertEqual(root.base, "https://secret/url")
2771
2771
root.get('{http://www.w3.org/XML/1998/namespace}base'),
2772
2772
"https://secret/url")
2775
2775
etree = self.etree
2776
2776
root = etree.HTML(_bytes("<html><body></body></html>"),
2777
2777
base_url="http://no/such/url")
2778
self.assertEquals(root.base, "http://no/such/url")
2778
self.assertEqual(root.base, "http://no/such/url")
2780
2780
def test_html_base_tag(self):
2781
2781
etree = self.etree
2782
2782
root = etree.HTML(_bytes('<html><head><base href="http://no/such/url"></head></html>'))
2783
self.assertEquals(root.base, "http://no/such/url")
2783
self.assertEqual(root.base, "http://no/such/url")
2785
2785
def test_parse_fileobject_unicode(self):
2786
2786
# parse from a file object that returns unicode strings
2787
2787
f = LargeFileLikeUnicode()
2788
2788
tree = self.etree.parse(f)
2789
2789
root = tree.getroot()
2790
self.assert_(root.tag.endswith('root'))
2790
self.assertTrue(root.tag.endswith('root'))
2792
2792
def test_dtd_io(self):
2793
2793
# check that DTDs that go in also go back out
2904
2904
c = SubElement(a, 'c')
2906
2906
result = tostring(a)
2907
self.assertEquals(result, _bytes("<a><b/><c/></a>"))
2907
self.assertEqual(result, _bytes("<a><b/><c/></a>"))
2909
2909
result = tostring(a, pretty_print=False)
2910
self.assertEquals(result, _bytes("<a><b/><c/></a>"))
2910
self.assertEqual(result, _bytes("<a><b/><c/></a>"))
2912
2912
result = tostring(a, pretty_print=True)
2913
self.assertEquals(result, _bytes("<a>\n <b/>\n <c/>\n</a>\n"))
2913
self.assertEqual(result, _bytes("<a>\n <b/>\n <c/>\n</a>\n"))
2915
2915
def test_tostring_with_tail(self):
2916
2916
tostring = self.etree.tostring
2924
2924
c = SubElement(a, 'c')
2926
2926
result = tostring(a)
2927
self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))
2927
self.assertEqual(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))
2929
2929
result = tostring(a, with_tail=False)
2930
self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>"))
2930
self.assertEqual(result, _bytes("<a><b/>bTAIL<c/></a>"))
2932
2932
result = tostring(a, with_tail=True)
2933
self.assertEquals(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))
2933
self.assertEqual(result, _bytes("<a><b/>bTAIL<c/></a>aTAIL"))
2935
2935
def test_standalone(self):
2936
2936
tostring = self.etree.tostring
2939
2939
Element = self.etree.Element
2941
2941
tree = Element("root").getroottree()
2942
self.assertEquals(None, tree.docinfo.standalone)
2942
self.assertEqual(None, tree.docinfo.standalone)
2944
2944
tree = XML(_bytes("<root/>")).getroottree()
2945
self.assertEquals(None, tree.docinfo.standalone)
2945
self.assertEqual(None, tree.docinfo.standalone)
2947
2947
tree = XML(_bytes(
2948
2948
"<?xml version='1.0' encoding='ASCII' standalone='yes'?>\n<root/>"
2949
2949
)).getroottree()
2950
self.assertEquals(True, tree.docinfo.standalone)
2950
self.assertEqual(True, tree.docinfo.standalone)
2952
2952
tree = XML(_bytes(
2953
2953
"<?xml version='1.0' encoding='ASCII' standalone='no'?>\n<root/>"
2954
2954
)).getroottree()
2955
self.assertEquals(False, tree.docinfo.standalone)
2955
self.assertEqual(False, tree.docinfo.standalone)
2957
2957
def test_tostring_standalone(self):
2958
2958
tostring = self.etree.tostring
2962
2962
root = XML(_bytes("<root/>"))
2964
2964
tree = ElementTree(root)
2965
self.assertEquals(None, tree.docinfo.standalone)
2965
self.assertEqual(None, tree.docinfo.standalone)
2967
2967
result = tostring(root, xml_declaration=True, encoding="ASCII")
2968
self.assertEquals(result, _bytes(
2968
self.assertEqual(result, _bytes(
2969
2969
"<?xml version='1.0' encoding='ASCII'?>\n<root/>"))
2971
2971
result = tostring(root, xml_declaration=True, encoding="ASCII",
2972
2972
standalone=True)
2973
self.assertEquals(result, _bytes(
2973
self.assertEqual(result, _bytes(
2974
2974
"<?xml version='1.0' encoding='ASCII' standalone='yes'?>\n<root/>"))
2976
2976
tree = ElementTree(XML(result))
2977
self.assertEquals(True, tree.docinfo.standalone)
2977
self.assertEqual(True, tree.docinfo.standalone)
2979
2979
result = tostring(root, xml_declaration=True, encoding="ASCII",
2980
2980
standalone=False)
2981
self.assertEquals(result, _bytes(
2981
self.assertEqual(result, _bytes(
2982
2982
"<?xml version='1.0' encoding='ASCII' standalone='no'?>\n<root/>"))
2984
2984
tree = ElementTree(XML(result))
2985
self.assertEquals(False, tree.docinfo.standalone)
2985
self.assertEqual(False, tree.docinfo.standalone)
2987
2987
def test_tostring_standalone_in_out(self):
2988
2988
tostring = self.etree.tostring
2993
2993
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n<root/>"))
2995
2995
tree = ElementTree(root)
2996
self.assertEquals(True, tree.docinfo.standalone)
2996
self.assertEqual(True, tree.docinfo.standalone)
2998
2998
result = tostring(root, xml_declaration=True, encoding="ASCII")
2999
self.assertEquals(result, _bytes(
2999
self.assertEqual(result, _bytes(
3000
3000
"<?xml version='1.0' encoding='ASCII'?>\n<root/>"))
3002
3002
result = tostring(root, xml_declaration=True, encoding="ASCII",
3003
3003
standalone=True)
3004
self.assertEquals(result, _bytes(
3004
self.assertEqual(result, _bytes(
3005
3005
"<?xml version='1.0' encoding='ASCII' standalone='yes'?>\n<root/>"))
3007
3007
def test_tostring_method_text_encoding(self):
3066
3066
b = SubElement(a, 'b')
3067
3067
c = SubElement(a, 'c')
3068
3068
d = SubElement(c, 'd')
3069
self.assert_(isinstance(tounicode(b), _unicode))
3070
self.assert_(isinstance(tounicode(c), _unicode))
3071
self.assertEquals(_bytes('<b></b>'),
3069
self.assertTrue(isinstance(tounicode(b), _unicode))
3070
self.assertTrue(isinstance(tounicode(c), _unicode))
3071
self.assertEqual(_bytes('<b></b>'),
3072
3072
canonicalize(tounicode(b)))
3073
self.assertEquals(_bytes('<c><d></d></c>'),
3073
self.assertEqual(_bytes('<c><d></d></c>'),
3074
3074
canonicalize(tounicode(c)))
3076
3076
def test_tounicode_none(self):
3102
3102
c = SubElement(a, 'c')
3104
3104
result = tounicode(a)
3105
self.assertEquals(result, "<a><b/><c/></a>")
3105
self.assertEqual(result, "<a><b/><c/></a>")
3107
3107
result = tounicode(a, pretty_print=False)
3108
self.assertEquals(result, "<a><b/><c/></a>")
3108
self.assertEqual(result, "<a><b/><c/></a>")
3110
3110
result = tounicode(a, pretty_print=True)
3111
self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>\n")
3111
self.assertEqual(result, "<a>\n <b/>\n <c/>\n</a>\n")
3113
3113
def test_tostring_unicode(self):
3114
3114
tostring = self.etree.tostring
3132
3132
b = SubElement(a, 'b')
3133
3133
c = SubElement(a, 'c')
3134
3134
d = SubElement(c, 'd')
3135
self.assert_(isinstance(tostring(b, encoding=_unicode), _unicode))
3136
self.assert_(isinstance(tostring(c, encoding=_unicode), _unicode))
3137
self.assertEquals(_bytes('<b></b>'),
3135
self.assertTrue(isinstance(tostring(b, encoding=_unicode), _unicode))
3136
self.assertTrue(isinstance(tostring(c, encoding=_unicode), _unicode))
3137
self.assertEqual(_bytes('<b></b>'),
3138
3138
canonicalize(tostring(b, encoding=_unicode)))
3139
self.assertEquals(_bytes('<c><d></d></c>'),
3139
self.assertEqual(_bytes('<c><d></d></c>'),
3140
3140
canonicalize(tostring(c, encoding=_unicode)))
3142
3142
def test_tostring_unicode_none(self):
3169
3169
c = SubElement(a, 'c')
3171
3171
result = tostring(a, encoding=_unicode)
3172
self.assertEquals(result, "<a><b/><c/></a>")
3172
self.assertEqual(result, "<a><b/><c/></a>")
3174
3174
result = tostring(a, encoding=_unicode, pretty_print=False)
3175
self.assertEquals(result, "<a><b/><c/></a>")
3175
self.assertEqual(result, "<a><b/><c/></a>")
3177
3177
result = tostring(a, encoding=_unicode, pretty_print=True)
3178
self.assertEquals(result, "<a>\n <b/>\n <c/>\n</a>\n")
3178
self.assertEqual(result, "<a>\n <b/>\n <c/>\n</a>\n")
3180
3180
def test_pypy_proxy_collect(self):
3181
3181
root = etree.Element('parent')
3182
3182
etree.SubElement(root, 'child')
3184
self.assertEquals(len(root), 1)
3185
self.assertEquals(root[0].tag, 'child')
3184
self.assertEqual(len(root), 1)
3185
self.assertEqual(root[0].tag, 'child')
3187
3187
# in PyPy, GC used to kill the Python proxy instance without cleanup
3189
self.assertEquals(len(root), 1)
3190
self.assertEquals(root[0].tag, 'child')
3189
self.assertEqual(len(root), 1)
3190
self.assertEqual(root[0].tag, 'child')
3192
3192
# helper methods
3329
3329
tree.write_c14n(f)
3330
3330
s = f.getvalue()
3331
self.assertEquals(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3331
self.assertEqual(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3334
3334
tree.write_c14n(f, with_comments=True)
3335
3335
s = f.getvalue()
3336
self.assertEquals(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3336
self.assertEqual(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3339
3339
tree.write_c14n(f, with_comments=False)
3340
3340
s = f.getvalue()
3341
self.assertEquals(_bytes('<a><b></b></a>'),
3341
self.assertEqual(_bytes('<a><b></b></a>'),
3344
3344
def test_c14n_tostring_with_comments(self):
3345
3345
tree = self.parse(_bytes('<!--hi--><a><!--ho--><b/></a><!--hu-->'))
3346
3346
s = etree.tostring(tree, method='c14n')
3347
self.assertEquals(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3347
self.assertEqual(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3349
3349
s = etree.tostring(tree, method='c14n', with_comments=True)
3350
self.assertEquals(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3350
self.assertEqual(_bytes('<!--hi-->\n<a><!--ho--><b></b></a>\n<!--hu-->'),
3352
3352
s = etree.tostring(tree, method='c14n', with_comments=False)
3353
self.assertEquals(_bytes('<a><b></b></a>'),
3353
self.assertEqual(_bytes('<a><b></b></a>'),
3356
3356
def test_c14n_element_tostring_with_comments(self):
3357
3357
tree = self.parse(_bytes('<!--hi--><a><!--ho--><b/></a><!--hu-->'))
3358
3358
s = etree.tostring(tree.getroot(), method='c14n')
3359
self.assertEquals(_bytes('<a><!--ho--><b></b></a>'),
3359
self.assertEqual(_bytes('<a><!--ho--><b></b></a>'),
3361
3361
s = etree.tostring(tree.getroot(), method='c14n', with_comments=True)
3362
self.assertEquals(_bytes('<a><!--ho--><b></b></a>'),
3362
self.assertEqual(_bytes('<a><!--ho--><b></b></a>'),
3364
3364
s = etree.tostring(tree.getroot(), method='c14n', with_comments=False)
3365
self.assertEquals(_bytes('<a><b></b></a>'),
3365
self.assertEqual(_bytes('<a><b></b></a>'),
3368
3368
def test_c14n_exclusive(self):
3372
3372
tree.write_c14n(f)
3373
3373
s = f.getvalue()
3374
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3374
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3377
3377
tree.write_c14n(f, exclusive=False)
3378
3378
s = f.getvalue()
3379
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3379
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3382
3382
tree.write_c14n(f, exclusive=True)
3383
3383
s = f.getvalue()
3384
self.assertEquals(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
3384
self.assertEqual(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
3388
3388
tree.write_c14n(f, exclusive=True, inclusive_ns_prefixes=['z'])
3389
3389
s = f.getvalue()
3390
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:z="http://cde"><z:b></z:b></a>'),
3390
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:z="http://cde"><z:b></z:b></a>'),
3393
3393
def test_c14n_tostring_exclusive(self):
3394
3394
tree = self.parse(_bytes(
3395
3395
'<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b/></a>'))
3396
3396
s = etree.tostring(tree, method='c14n')
3397
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3397
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3399
3399
s = etree.tostring(tree, method='c14n', exclusive=False)
3400
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3400
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3402
3402
s = etree.tostring(tree, method='c14n', exclusive=True)
3403
self.assertEquals(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
3403
self.assertEqual(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
3406
3406
s = etree.tostring(tree, method='c14n', exclusive=True, inclusive_ns_prefixes=['y'])
3407
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd"><z:b xmlns:z="http://cde"></z:b></a>'),
3407
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd"><z:b xmlns:z="http://cde"></z:b></a>'),
3410
3410
def test_c14n_element_tostring_exclusive(self):
3411
3411
tree = self.parse(_bytes(
3412
3412
'<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b/></a>'))
3413
3413
s = etree.tostring(tree.getroot(), method='c14n')
3414
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3414
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3416
3416
s = etree.tostring(tree.getroot(), method='c14n', exclusive=False)
3417
self.assertEquals(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3417
self.assertEqual(_bytes('<a xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"><z:b></z:b></a>'),
3419
3419
s = etree.tostring(tree.getroot(), method='c14n', exclusive=True)
3420
self.assertEquals(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
3420
self.assertEqual(_bytes('<a xmlns="http://abc"><z:b xmlns:z="http://cde"></z:b></a>'),
3423
3423
s = etree.tostring(tree.getroot()[0], method='c14n', exclusive=False)
3424
self.assertEquals(_bytes('<z:b xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"></z:b>'),
3424
self.assertEqual(_bytes('<z:b xmlns="http://abc" xmlns:y="http://bcd" xmlns:z="http://cde"></z:b>'),
3426
3426
s = etree.tostring(tree.getroot()[0], method='c14n', exclusive=True)
3427
self.assertEquals(_bytes('<z:b xmlns:z="http://cde"></z:b>'),
3427
self.assertEqual(_bytes('<z:b xmlns:z="http://cde"></z:b>'),
3430
3430
s = etree.tostring(tree.getroot()[0], method='c14n', exclusive=True, inclusive_ns_prefixes=['y'])
3431
self.assertEquals(_bytes('<z:b xmlns:y="http://bcd" xmlns:z="http://cde"></z:b>'),
3431
self.assertEqual(_bytes('<z:b xmlns:y="http://bcd" xmlns:z="http://cde"></z:b>'),
3434
3434
def test_c14n_tostring_inclusive_ns_prefixes(self):
3486
3486
tree.write(f, compression=9)
3487
3487
s = f.getvalue()
3488
self.assert_(len(s) <= len(s0))
3488
self.assertTrue(len(s) <= len(s0))
3489
3489
gzfile = gzip.GzipFile(fileobj=BytesIO(s))
3491
3491
s9 = gzfile.read()
3495
self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
3495
self.assertEqual(_bytes('<a>'+'<b/>'*200+'</a>'),
3497
self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
3497
self.assertEqual(_bytes('<a>'+'<b/>'*200+'</a>'),
3499
self.assertEquals(_bytes('<a>'+'<b/>'*200+'</a>'),
3499
self.assertEqual(_bytes('<a>'+'<b/>'*200+'</a>'),
3502
3502
def test_write_file(self):
3566
3566
e = sys.exc_info()[1]
3567
3567
logs = e.error_log
3569
self.assert_([ log for log in logs
3569
self.assertTrue([ log for log in logs
3570
3570
if 'mismatch' in log.message ])
3571
self.assert_([ log for log in logs
3571
self.assertTrue([ log for log in logs
3572
3572
if 'PARSER' in log.domain_name])
3573
self.assert_([ log for log in logs
3573
self.assertTrue([ log for log in logs
3574
3574
if 'ERR_TAG_NAME_MISMATCH' in log.type_name ])
3575
self.assert_([ log for log in logs
3575
self.assertTrue([ log for log in logs
3576
3576
if 1 == log.line ])
3577
self.assert_([ log for log in logs
3577
self.assertTrue([ log for log in logs
3578
3578
if 15 == log.column ])
3580
3580
def _test_python_error_logging(self):