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().
This commit is contained in:
Fred Drake 2000-11-21 22:02:43 +00:00
parent 4ccf4a1e8a
commit a1bde802f1
1 changed files with 103 additions and 14 deletions

View File

@ -37,16 +37,36 @@ def testGetElementsByTagName():
dom.unlink() dom.unlink()
def testInsertBefore(): def testInsertBefore():
dom = parse(tstfile) dom = parseString("<doc><foo/></doc>")
docel = dom.documentElement root = dom.documentElement
#docel.insertBefore( dom.createProcessingInstruction("a", "b"), elem = root.childNodes[0]
# docel.childNodes[1]) nelem = dom.createElement("element")
root.insertBefore(nelem, elem)
#docel.insertBefore( dom.createProcessingInstruction("a", "b"), confirm(len(root.childNodes) == 2
# docel.childNodes[0]) and root.childNodes[0] is nelem
and root.childNodes[1] is elem
#confirm( docel.childNodes[0].tet == "a") and root.firstChild is nelem
#confirm( docel.childNodes[2].tet == "a") 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() dom.unlink()
def testAppendChild(): def testAppendChild():
@ -77,6 +97,7 @@ def testAAA():
dom = parseString("<abc/>") dom = parseString("<abc/>")
el = dom.documentElement el = dom.documentElement
el.setAttribute("spam", "jam2") el.setAttribute("spam", "jam2")
confirm(el.toxml() == '<abc spam="jam2"/>', "testAAA")
dom.unlink() dom.unlink()
def testAAB(): def testAAB():
@ -84,6 +105,7 @@ def testAAB():
el = dom.documentElement el = dom.documentElement
el.setAttribute("spam", "jam") el.setAttribute("spam", "jam")
el.setAttribute("spam", "jam2") el.setAttribute("spam", "jam2")
confirm(el.toxml() == '<abc spam="jam2"/>', "testAAB")
dom.unlink() dom.unlink()
def testAddAttr(): def testAddAttr():
@ -242,7 +264,18 @@ def testWriteText(): pass
def testDocumentElement(): 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 def testCreateElementNS(): pass
@ -290,11 +323,54 @@ def testFirstChild(): pass
def testHasChildNodes(): 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 _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 testCloneElementDeep(): pass
def testCloneDocumentShallow(): pass def testCloneDocumentShallow(): pass
@ -308,6 +384,19 @@ def testClonePIShallow(): pass
def testClonePIDeep(): 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(): def testSiblings():
doc = parseString("<doc><?pi?>text?<elm/></doc>") doc = parseString("<doc><?pi?>text?<elm/></doc>")
root = doc.documentElement root = doc.documentElement