|
1 | 1 | # test for xml.dom.minidom
|
2 | 2 |
|
| 3 | +import copy |
3 | 4 | import pickle
|
4 | 5 | from test.support import run_unittest, findfile
|
5 | 6 | import unittest
|
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | tstfile = findfile("test.xml", subdir="xmltestdata")
|
| 15 | +sample = ("<?xml version='1.0' encoding='us-ascii'?>\n" |
| 16 | + "<!DOCTYPE doc PUBLIC 'http://xml.python.org/public'" |
| 17 | + " 'http://xml.python.org/system' [\n" |
| 18 | + " <!ELEMENT e EMPTY>\n" |
| 19 | + " <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>\n" |
| 20 | + "]><doc attr='value'> text\n" |
| 21 | + "<?pi sample?> <!-- comment --> <e/> </doc>") |
14 | 22 |
|
15 | 23 | # The tests of DocumentType importing use these helpers to construct
|
16 | 24 | # the documents to work with, since not all DOM builders actually
|
@@ -1466,52 +1474,54 @@ def testSetIdAttributeNode(self):
|
1466 | 1474 | self.confirm(e.isSameNode(doc.getElementById("w"))
|
1467 | 1475 | and a2.isId)
|
1468 | 1476 |
|
| 1477 | + def assert_recursive_equal(self, doc, doc2): |
| 1478 | + stack = [(doc, doc2)] |
| 1479 | + while stack: |
| 1480 | + n1, n2 = stack.pop() |
| 1481 | + self.assertEqual(n1.nodeType, n2.nodeType) |
| 1482 | + self.assertEqual(len(n1.childNodes), len(n2.childNodes)) |
| 1483 | + self.assertEqual(n1.nodeName, n2.nodeName) |
| 1484 | + self.assertFalse(n1.isSameNode(n2)) |
| 1485 | + self.assertFalse(n2.isSameNode(n1)) |
| 1486 | + if n1.nodeType == Node.DOCUMENT_TYPE_NODE: |
| 1487 | + len(n1.entities) |
| 1488 | + len(n2.entities) |
| 1489 | + len(n1.notations) |
| 1490 | + len(n2.notations) |
| 1491 | + self.assertEqual(len(n1.entities), len(n2.entities)) |
| 1492 | + self.assertEqual(len(n1.notations), len(n2.notations)) |
| 1493 | + for i in range(len(n1.notations)): |
| 1494 | + # XXX this loop body doesn't seem to be executed? |
| 1495 | + no1 = n1.notations.item(i) |
| 1496 | + no2 = n1.notations.item(i) |
| 1497 | + self.assertEqual(no1.name, no2.name) |
| 1498 | + self.assertEqual(no1.publicId, no2.publicId) |
| 1499 | + self.assertEqual(no1.systemId, no2.systemId) |
| 1500 | + stack.append((no1, no2)) |
| 1501 | + for i in range(len(n1.entities)): |
| 1502 | + e1 = n1.entities.item(i) |
| 1503 | + e2 = n2.entities.item(i) |
| 1504 | + self.assertEqual(e1.notationName, e2.notationName) |
| 1505 | + self.assertEqual(e1.publicId, e2.publicId) |
| 1506 | + self.assertEqual(e1.systemId, e2.systemId) |
| 1507 | + stack.append((e1, e2)) |
| 1508 | + if n1.nodeType != Node.DOCUMENT_NODE: |
| 1509 | + self.assertTrue(n1.ownerDocument.isSameNode(doc)) |
| 1510 | + self.assertTrue(n2.ownerDocument.isSameNode(doc2)) |
| 1511 | + for i in range(len(n1.childNodes)): |
| 1512 | + stack.append((n1.childNodes[i], n2.childNodes[i])) |
| 1513 | + |
1469 | 1514 | def testPickledDocument(self):
|
1470 |
| - doc = parseString("<?xml version='1.0' encoding='us-ascii'?>\n" |
1471 |
| - "<!DOCTYPE doc PUBLIC 'http://xml.python.org/public'" |
1472 |
| - " 'http://xml.python.org/system' [\n" |
1473 |
| - " <!ELEMENT e EMPTY>\n" |
1474 |
| - " <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>\n" |
1475 |
| - "]><doc attr='value'> text\n" |
1476 |
| - "<?pi sample?> <!-- comment --> <e/> </doc>") |
| 1515 | + doc = parseString(sample) |
1477 | 1516 | for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
1478 | 1517 | s = pickle.dumps(doc, proto)
|
1479 | 1518 | doc2 = pickle.loads(s)
|
1480 |
| - stack = [(doc, doc2)] |
1481 |
| - while stack: |
1482 |
| - n1, n2 = stack.pop() |
1483 |
| - self.confirm(n1.nodeType == n2.nodeType |
1484 |
| - and len(n1.childNodes) == len(n2.childNodes) |
1485 |
| - and n1.nodeName == n2.nodeName |
1486 |
| - and not n1.isSameNode(n2) |
1487 |
| - and not n2.isSameNode(n1)) |
1488 |
| - if n1.nodeType == Node.DOCUMENT_TYPE_NODE: |
1489 |
| - len(n1.entities) |
1490 |
| - len(n2.entities) |
1491 |
| - len(n1.notations) |
1492 |
| - len(n2.notations) |
1493 |
| - self.confirm(len(n1.entities) == len(n2.entities) |
1494 |
| - and len(n1.notations) == len(n2.notations)) |
1495 |
| - for i in range(len(n1.notations)): |
1496 |
| - # XXX this loop body doesn't seem to be executed? |
1497 |
| - no1 = n1.notations.item(i) |
1498 |
| - no2 = n1.notations.item(i) |
1499 |
| - self.confirm(no1.name == no2.name |
1500 |
| - and no1.publicId == no2.publicId |
1501 |
| - and no1.systemId == no2.systemId) |
1502 |
| - stack.append((no1, no2)) |
1503 |
| - for i in range(len(n1.entities)): |
1504 |
| - e1 = n1.entities.item(i) |
1505 |
| - e2 = n2.entities.item(i) |
1506 |
| - self.confirm(e1.notationName == e2.notationName |
1507 |
| - and e1.publicId == e2.publicId |
1508 |
| - and e1.systemId == e2.systemId) |
1509 |
| - stack.append((e1, e2)) |
1510 |
| - if n1.nodeType != Node.DOCUMENT_NODE: |
1511 |
| - self.confirm(n1.ownerDocument.isSameNode(doc) |
1512 |
| - and n2.ownerDocument.isSameNode(doc2)) |
1513 |
| - for i in range(len(n1.childNodes)): |
1514 |
| - stack.append((n1.childNodes[i], n2.childNodes[i])) |
| 1519 | + self.assert_recursive_equal(doc, doc2) |
| 1520 | + |
| 1521 | + def testDeepcopiedDocument(self): |
| 1522 | + doc = parseString(sample) |
| 1523 | + doc2 = copy.deepcopy(doc) |
| 1524 | + self.assert_recursive_equal(doc, doc2) |
1515 | 1525 |
|
1516 | 1526 | def testSerializeCommentNodeWithDoubleHyphen(self):
|
1517 | 1527 | doc = create_doc_without_doctype()
|
|
0 commit comments