1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
from xml.dom import DOMException
from xml.dom import NO_MODIFICATION_ALLOWED_ERR
def get_exception_name(code):
import types
from xml import dom
for (name,value) in vars(dom).items():
if (type(value) == types.IntType
and value == code):
return name
def test(tester):
tester.startGroup('NodeList')
tester.startTest('Checking syntax')
try:
from xml.dom import NodeList
from xml.dom.NodeList import NodeList
except:
tester.error('Error in syntax',1)
tester.testDone()
tester.startTest('Creating the test environment')
try:
from xml.dom import implementation
dt = implementation.createDocumentType('','','')
doc = implementation.createDocument('','ROOT',dt)
except:
tester.error('Error creating document')
nodes = []
try:
for ctr in range(3):
nodes.append(doc.createElement('Node%d' %ctr))
except:
tester.error("Error creating nodes")
e = doc.createElement('PARENT')
try:
for n in nodes:
e.appendChild(n)
except:
tester.error('Error appending nodes')
nl = e.childNodes
tester.testDone()
tester.startTest("Testing attributes")
if nl.length != 3:
tester.error('length reports wrong amount')
try:
nl.length = 5
except DOMException, x:
if x.code != NO_MODIFICATION_ALLOWED_ERR:
name = get_exception_name(x.code)
tester.error("Wrong exception '%s', expected NOMODIFICATION_ALLOWED_ERR" % name)
else:
tester.error('length not read-only')
tester.testDone()
tester.startTest("Testing item()")
if nl.item(0).nodeName != nodes[0].nodeName:
tester.error("Item returns wrong item")
if nl.item(3) != None:
tester.error("Item returns something on invalid index")
tester.testDone()
return tester.groupDone()
if __name__ == '__main__':
import sys
import TestSuite
tester = TestSuite.TestSuite()
retVal = test(tester)
sys.exit(retVal)
|