|
1 | 1 | # test for xml.dom.minidom
|
2 | 2 |
|
| 3 | +import copy |
3 | 4 | import pickle
|
4 | 5 | from test.support import 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
|
@@ -1481,52 +1489,54 @@ def testSetIdAttributeNode(self):
|
1481 | 1489 | self.confirm(e.isSameNode(doc.getElementById("w"))
|
1482 | 1490 | and a2.isId)
|
1483 | 1491 |
|
| 1492 | + def assert_recursive_equal(self, doc, doc2): |
| 1493 | + stack = [(doc, doc2)] |
| 1494 | + while stack: |
| 1495 | + n1, n2 = stack.pop() |
| 1496 | + self.assertEqual(n1.nodeType, n2.nodeType) |
| 1497 | + self.assertEqual(len(n1.childNodes), len(n2.childNodes)) |
| 1498 | + self.assertEqual(n1.nodeName, n2.nodeName) |
| 1499 | + self.assertFalse(n1.isSameNode(n2)) |
| 1500 | + self.assertFalse(n2.isSameNode(n1)) |
| 1501 | + if n1.nodeType == Node.DOCUMENT_TYPE_NODE: |
| 1502 | + len(n1.entities) |
| 1503 | + len(n2.entities) |
| 1504 | + len(n1.notations) |
| 1505 | + len(n2.notations) |
| 1506 | + self.assertEqual(len(n1.entities), len(n2.entities)) |
| 1507 | + self.assertEqual(len(n1.notations), len(n2.notations)) |
| 1508 | + for i in range(len(n1.notations)): |
| 1509 | + # XXX this loop body doesn't seem to be executed? |
| 1510 | + no1 = n1.notations.item(i) |
| 1511 | + no2 = n1.notations.item(i) |
| 1512 | + self.assertEqual(no1.name, no2.name) |
| 1513 | + self.assertEqual(no1.publicId, no2.publicId) |
| 1514 | + self.assertEqual(no1.systemId, no2.systemId) |
| 1515 | + stack.append((no1, no2)) |
| 1516 | + for i in range(len(n1.entities)): |
| 1517 | + e1 = n1.entities.item(i) |
| 1518 | + e2 = n2.entities.item(i) |
| 1519 | + self.assertEqual(e1.notationName, e2.notationName) |
| 1520 | + self.assertEqual(e1.publicId, e2.publicId) |
| 1521 | + self.assertEqual(e1.systemId, e2.systemId) |
| 1522 | + stack.append((e1, e2)) |
| 1523 | + if n1.nodeType != Node.DOCUMENT_NODE: |
| 1524 | + self.assertTrue(n1.ownerDocument.isSameNode(doc)) |
| 1525 | + self.assertTrue(n2.ownerDocument.isSameNode(doc2)) |
| 1526 | + for i in range(len(n1.childNodes)): |
| 1527 | + stack.append((n1.childNodes[i], n2.childNodes[i])) |
| 1528 | + |
1484 | 1529 | def testPickledDocument(self):
|
1485 |
| - doc = parseString("<?xml version='1.0' encoding='us-ascii'?>\n" |
1486 |
| - "<!DOCTYPE doc PUBLIC 'http://xml.python.org/public'" |
1487 |
| - " 'http://xml.python.org/system' [\n" |
1488 |
| - " <!ELEMENT e EMPTY>\n" |
1489 |
| - " <!ENTITY ent SYSTEM 'http://xml.python.org/entity'>\n" |
1490 |
| - "]><doc attr='value'> text\n" |
1491 |
| - "<?pi sample?> <!-- comment --> <e/> </doc>") |
| 1530 | + doc = parseString(sample) |
1492 | 1531 | for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
|
1493 | 1532 | s = pickle.dumps(doc, proto)
|
1494 | 1533 | doc2 = pickle.loads(s)
|
1495 |
| - stack = [(doc, doc2)] |
1496 |
| - while stack: |
1497 |
| - n1, n2 = stack.pop() |
1498 |
| - self.confirm(n1.nodeType == n2.nodeType |
1499 |
| - and len(n1.childNodes) == len(n2.childNodes) |
1500 |
| - and n1.nodeName == n2.nodeName |
1501 |
| - and not n1.isSameNode(n2) |
1502 |
| - and not n2.isSameNode(n1)) |
1503 |
| - if n1.nodeType == Node.DOCUMENT_TYPE_NODE: |
1504 |
| - len(n1.entities) |
1505 |
| - len(n2.entities) |
1506 |
| - len(n1.notations) |
1507 |
| - len(n2.notations) |
1508 |
| - self.confirm(len(n1.entities) == len(n2.entities) |
1509 |
| - and len(n1.notations) == len(n2.notations)) |
1510 |
| - for i in range(len(n1.notations)): |
1511 |
| - # XXX this loop body doesn't seem to be executed? |
1512 |
| - no1 = n1.notations.item(i) |
1513 |
| - no2 = n1.notations.item(i) |
1514 |
| - self.confirm(no1.name == no2.name |
1515 |
| - and no1.publicId == no2.publicId |
1516 |
| - and no1.systemId == no2.systemId) |
1517 |
| - stack.append((no1, no2)) |
1518 |
| - for i in range(len(n1.entities)): |
1519 |
| - e1 = n1.entities.item(i) |
1520 |
| - e2 = n2.entities.item(i) |
1521 |
| - self.confirm(e1.notationName == e2.notationName |
1522 |
| - and e1.publicId == e2.publicId |
1523 |
| - and e1.systemId == e2.systemId) |
1524 |
| - stack.append((e1, e2)) |
1525 |
| - if n1.nodeType != Node.DOCUMENT_NODE: |
1526 |
| - self.confirm(n1.ownerDocument.isSameNode(doc) |
1527 |
| - and n2.ownerDocument.isSameNode(doc2)) |
1528 |
| - for i in range(len(n1.childNodes)): |
1529 |
| - stack.append((n1.childNodes[i], n2.childNodes[i])) |
| 1534 | + self.assert_recursive_equal(doc, doc2) |
| 1535 | + |
| 1536 | + def testDeepcopiedDocument(self): |
| 1537 | + doc = parseString(sample) |
| 1538 | + doc2 = copy.deepcopy(doc) |
| 1539 | + self.assert_recursive_equal(doc, doc2) |
1530 | 1540 |
|
1531 | 1541 | def testSerializeCommentNodeWithDoubleHyphen(self):
|
1532 | 1542 | doc = create_doc_without_doctype()
|
|
0 commit comments