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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
def testRestriction(tester, doc, mapping, node, good):
# Any keys that are in the mapping but not in good are automaticly bad
bad = []
for key in mapping.keys():
if not key in good:
bad.append(key)
df = doc.createDocumentFragment()
# Make sure none of the good fail
for type in good:
try:
node.appendChild(mapping[type])
except:
tester.error('Didn\'t allow addition of %s' % type)
else:
df.appendChild(mapping[type])
# Add the good nodes in a DocFrag too
try:
node.appendChild(df)
except:
tester.error('Could not append DocumentFragment')
# And none of the bad work
for type in bad:
try:
node.appendChild(mapping[type])
except:
pass
else:
tester.error('Allowed addition of %s' % type)
def test(tester):
tester.startGroup('DOM Structure Model')
tester.startTest('Creating test environment')
from xml.dom import implementation
dt = implementation.createDocumentType('dt1', '', '')
doc = implementation.createDocument('', None, dt)
df = doc.createDocumentFragment()
element = doc.createElement('TestElement')
Nodes = {
'Document' : implementation.createDocument('','ROOT2', dt),
'DocType' : implementation.createDocumentType('dt2', '', ''),
'Element' : doc.createElement('tagName1'),
'Text' : doc.createTextNode('data'),
'Comment' : doc.createComment('data'),
'CDATA' : doc.createCDATASection('data'),
'ProcInstruct' : doc.createProcessingInstruction('target', 'data'),
'Attr' : doc.createAttribute('name'),
'EntityRef' : doc.createEntityReference('name'),
}
tester.testDone()
tester.startTest('Testing Document')
good = ['Element',
'ProcInstruct',
'Comment',
]
# Add duplicate Element & DocType
Nodes['Element2'] = doc.createElement('tagName2')
testRestriction(tester, doc, Nodes, doc, good)
# Remove added items
del Nodes['Element2']
tester.testDone()
tester.startTest('Testing DocumentFragment')
good = ['Element',
'ProcInstruct',
'Comment',
'Text',
'CDATA',
'EntityRef',
]
testRestriction(tester, doc, Nodes, df, good)
tester.testDone()
tester.startTest('Testing DocumentType')
good = [
]
testRestriction(tester, doc, Nodes, dt, good)
tester.testDone()
tester.startTest('Testing EntityReference')
good = ['Element',
'ProcInstruct',
'Comment',
'Text',
'CDATA',
'EntityRef'
]
ref = doc.createEntityReference('test')
testRestriction(tester, doc, Nodes, ref, good)
tester.testDone()
tester.startTest('Testing Element')
good = ['Element',
'ProcInstruct',
'Comment',
'Text',
'CDATA',
'EntityRef',
]
testRestriction(tester, doc, Nodes, element, good)
tester.testDone()
tester.startTest('Testing Attr')
good = ['Text',
'EntityRef',
]
testRestriction(tester, doc, Nodes, Nodes['Attr'], good)
tester.testDone()
tester.startTest('Testing Comment')
good = [
]
testRestriction(tester, doc, Nodes, Nodes['Comment'], good)
tester.testDone()
tester.startTest('Testing Text')
good = [
]
testRestriction(tester, doc, Nodes, Nodes['Text'], good)
tester.testDone()
tester.startTest('Testing CDATASection')
good = [
]
testRestriction(tester, doc, Nodes, Nodes['CDATA'], good)
tester.testDone()
return tester.groupDone()
if __name__ == '__main__':
import sys
import TestSuite
tester = TestSuite.TestSuite(0,1)
retVal = test(tester)
sys.exit(retVal)
|