diff options
author | Fred Drake <fdrake@acm.org> | 2000-11-21 22:02:43 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2000-11-21 22:02:43 (GMT) |
commit | a1bde802f1ef3ab49b821b2bb63f0f008f81a9d8 (patch) | |
tree | 7e2d29cf28a7986bc3f30dd0f3ed542a80bcdb10 /Lib | |
parent | 4ccf4a1e8a3141e34fba73f08d678b47b3ddb81d (diff) | |
download | cpython-a1bde802f1ef3ab49b821b2bb63f0f008f81a9d8.zip cpython-a1bde802f1ef3ab49b821b2bb63f0f008f81a9d8.tar.gz cpython-a1bde802f1ef3ab49b821b2bb63f0f008f81a9d8.tar.bz2 |
testInsertBefore(): Rewritten to actually test insertBefore() somewhat.
testAAA(),
testAAB(): Added checks that the results are right.
testTooManyDocumentElements(): Added code to actually test this.
testCloneElementDeep()
testCloneElementShallow(): Filled these in with test code.
_testCloneElementCopiesAttributes(),
_setupCloneElement(): Helper functions used with the other
testCloneElement*() functions.
testCloneElementShallowCopiesAttributes(): No longer a separate test;
_setupCloneElement() uses _testCloneElementCopiesAttributes() to
test that this is always done.
testNormalize(): Added to check Node.normalize().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_minidom.py | 117 |
1 files changed, 103 insertions, 14 deletions
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index ce7170d..5bdee80 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -37,16 +37,36 @@ def testGetElementsByTagName(): dom.unlink() def testInsertBefore(): - dom = parse(tstfile) - docel = dom.documentElement - #docel.insertBefore( dom.createProcessingInstruction("a", "b"), - # docel.childNodes[1]) - - #docel.insertBefore( dom.createProcessingInstruction("a", "b"), - # docel.childNodes[0]) - - #confirm( docel.childNodes[0].tet == "a") - #confirm( docel.childNodes[2].tet == "a") + dom = parseString("<doc><foo/></doc>") + root = dom.documentElement + elem = root.childNodes[0] + nelem = dom.createElement("element") + root.insertBefore(nelem, elem) + confirm(len(root.childNodes) == 2 + and root.childNodes[0] is nelem + and root.childNodes[1] is elem + and root.firstChild is nelem + and root.lastChild is elem + and root.toxml() == "<doc><element/><foo/></doc>" + , "testInsertBefore -- node properly placed in tree") + nelem = dom.createElement("element") + root.insertBefore(nelem, None) + confirm(len(root.childNodes) == 3 + and root.childNodes[1] is elem + and root.childNodes[2] is nelem + and root.lastChild is nelem + and nelem.previousSibling is elem + and root.toxml() == "<doc><element/><foo/><element/></doc>" + , "testInsertBefore -- node properly placed in tree") + nelem2 = dom.createElement("bar") + root.insertBefore(nelem2, nelem) + confirm(len(root.childNodes) == 4 + and root.childNodes[2] is nelem2 + and root.childNodes[3] is nelem + and nelem2.nextSibling is nelem + and nelem.previousSibling is nelem2 + and root.toxml() == "<doc><element/><foo/><bar/><element/></doc>" + , "testInsertBefore -- node properly placed in tree") dom.unlink() def testAppendChild(): @@ -77,6 +97,7 @@ def testAAA(): dom = parseString("<abc/>") el = dom.documentElement el.setAttribute("spam", "jam2") + confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA") dom.unlink() def testAAB(): @@ -84,6 +105,7 @@ def testAAB(): el = dom.documentElement el.setAttribute("spam", "jam") el.setAttribute("spam", "jam2") + confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB") dom.unlink() def testAddAttr(): @@ -242,7 +264,18 @@ def testWriteText(): pass def testDocumentElement(): pass -def testTooManyDocumentElements(): pass +def testTooManyDocumentElements(): + doc = parseString("<doc/>") + elem = doc.createElement("extra") + try: + doc.appendChild(elem) + except TypeError: + print "Caught expected exception when adding extra document element." + else: + print "Failed to catch expected exception when" \ + " adding extra document element." + elem.unlink() + doc.unlink() def testCreateElementNS(): pass @@ -290,11 +323,54 @@ def testFirstChild(): pass def testHasChildNodes(): pass -def testCloneElementShallow(): pass +def testCloneElementShallow(): + dom, clone = _setupCloneElement(0) + confirm(len(clone.childNodes) == 0 + and clone.parentNode is None + and clone.toxml() == '<doc attr="value"/>' + , "testCloneElementShallow") + dom.unlink() -def testCloneElementShallowCopiesAttributes(): pass +def testCloneElementDeep(): + dom, clone = _setupCloneElement(1) + confirm(len(clone.childNodes) == 1 + and clone.parentNode is None + and clone.toxml() == '<doc attr="value"><foo/></doc>' + , "testCloneElementDeep") + dom.unlink() -def testCloneElementDeep(): pass +def _setupCloneElement(deep): + dom = parseString("<doc attr='value'><foo/></doc>") + root = dom.documentElement + clone = root.cloneNode(deep) + _testCloneElementCopiesAttributes( + root, clone, "testCloneElement" + (deep and "Deep" or "Shallow")) + # mutilate the original so shared data is detected + root.tagName = root.nodeName = "MODIFIED" + root.setAttribute("attr", "NEW VALUE") + root.setAttribute("added", "VALUE") + return dom, clone + +def _testCloneElementCopiesAttributes(e1, e2, test): + attrs1 = e1.attributes + attrs2 = e2.attributes + keys1 = attrs1.keys() + keys2 = attrs2.keys() + keys1.sort() + keys2.sort() + confirm(keys1 == keys2, "clone of element has same attribute keys") + for i in range(len(keys1)): + a1 = attrs1.item(i) + a2 = attrs2.item(i) + confirm(a1 is not a2 + and a1.value == a2.value + and a1.nodeValue == a2.nodeValue + and a1.namespaceURI == a2.namespaceURI + and a1.localName == a2.localName + , "clone of attribute node has proper attribute values") + confirm(a2.ownerElement is e2, + "clone of attribute node correctly owned") + def testCloneDocumentShallow(): pass @@ -308,6 +384,19 @@ def testClonePIShallow(): pass def testClonePIDeep(): pass +def testNormalize(): + doc = parseString("<doc/>") + root = doc.documentElement + root.appendChild(doc.createTextNode("first")) + root.appendChild(doc.createTextNode("second")) + confirm(len(root.childNodes) == 2, "testNormalize -- preparation") + doc.normalize() + confirm(len(root.childNodes) == 1 + and root.firstChild is root.lastChild + and root.firstChild.data == "firstsecond" + , "testNormalize -- result") + doc.unlink() + def testSiblings(): doc = parseString("<doc><?pi?>text?<elm/></doc>") root = doc.documentElement |