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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
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('Element')
tester.startTest('Testing syntax')
try:
from xml.dom import Element
from xml.dom.Element import Element
except:
tester.tester.error('Error in syntax', 1)
tester.testDone()
tester.startTest('Creating test environment')
from xml.dom import DOMException
from xml.dom import INVALID_CHARACTER_ERR
from xml.dom import WRONG_DOCUMENT_ERR
from xml.dom import INUSE_ATTRIBUTE_ERR
from xml.dom import NOT_FOUND_ERR
from xml.dom import NO_MODIFICATION_ALLOWED_ERR
from xml.dom import implementation
dt = implementation.createDocumentType('','','')
doc = implementation.createDocument('','ROOT',dt)
doc_nons = implementation.createDocument('','ROOT',dt)
ns = 'www.fourthought.com'
e_ns = doc.createElementNS(ns, 'TEST')
e = doc.createElement('TEST')
tester.testDone()
tester.startTest('Testing attributes')
if e.tagName != 'TEST':
tester.error("tagName failed")
try:
e.tagName = "test"
except DOMException, exc:
if exc.code != NO_MODIFICATION_ALLOWED_ERR:
tester.error('Wrong exception on read-only violation')
tester.testDone()
tester.startTest('Testing setAttribute()')
if e.setAttribute('Attr1','Value 1') != None:
tester.error('setAttribvute returns a value')
if e.attributes.length != 1:
tester.error('setAttribute did not add the attribute')
e.setAttribute('Attr1','Value 2');
if e.attributes.length != 1:
tester.error('setAttribute did not replace the attribute')
try:
e.setAttribute('A<ttr','Value3')
except DOMException , x:
if x.code != INVALID_CHARACTER_ERR:
tester.error('Wrong exception on illegal character')
else:
tester.error('setAttribute allowed illegal characters')
tester.testDone()
tester.startTest('Testing getAttribute()')
if e.getAttribute('Attr1') != 'Value 2':
tester.error('getAttribute returned the worng string')
if e.getAttribute('Attr2') != '':
tester.error('getAttribute returns value for non-existant attribute')
tester.testDone()
tester.startTest('Testing hasAttribute()')
if not e.hasAttribute('Attr1'):
tester.error('hasAttribute didn\'t find the attribute')
if e.hasAttribute('Attr2'):
tester.error('hasAttribute found a non-existant attribute')
tester.testDone()
tester.startTest('Testing removeAttribute()')
if e.removeAttribute('Attr1') != None:
tester.error('removeAttribute returns something')
if e.attributes.length != 0:
tester.error('removeAttribute did not remove it')
tester.testDone()
tester.startTest('Testing setAttributeNode()')
a = doc.createAttribute('attrNode1');
if e.setAttributeNode(a) != None:
tester.error('setAttributeNode returns a value')
a1 = doc.createAttribute('attrNode1')
if e.setAttributeNode(a1).nodeName != a.nodeName:
tester.error('setAttribute does not return the replaced value')
tester.testDone()
tester.startTest('Testing getAttributeNode()')
if e.getAttributeNode('attrNode1').nodeName != a1.nodeName:
tester.error('getAttributeNode does not return the correct value')
if e.getAttributeNode('attrNode2') != None:
tester.error('getAttributeNode returns a value when it shouldn;t')
tester.testDone()
tester.startTest('Testing removeAttributeNode()')
if e.removeAttributeNode(a1).nodeName != a1.nodeName:
tester.error('removeAttributeNode does not return the correct value')
if e.attributes.length != 0:
tester.error('removeAttributeNode does not actually remove it')
a2 = doc.createAttribute('attrNode2')
try:
e.removeAttributeNode(a2)
except DOMException, x:
if x.code != NOT_FOUND_ERR:
raise x
tester.testDone()
tester.startTest('Testing getElementsByTagName()')
e2 = doc.createElement('TAG1')
e3 = doc.createElement('TAG1')
e4 = doc.createElement('TAG2')
e5 = doc.createElement('TAG3')
e.appendChild(e2);
e2.appendChild(e3);
e.appendChild(e4);
e2.appendChild(e5);
tagList = e.getElementsByTagName('TAG1')
if tagList.length != 2:
tester.error('getElementsByTagName returned the worng number')
tagList = e.getElementsByTagName('*')
if tagList.length != 4:
tester.error('getElementsByTagName(*) returned the wrong number');
tester.testDone()
tester.startTest('Testing setAttributeNS()')
if e.setAttributeNS('www.fourthought.com','ft:Attr1','Value 1') != None:
tester.error('setAttributeNS returns a value')
if e.attributes.length != 1:
tester.error('setAttributeNS did not add the attribute')
e.setAttributeNS('www.fourthought.com','ft:Attr1','Value 2')
if e.attributes.length != 1:
tester.error('setAttributeNS did not replace the attribute')
try:
e.setAttributeNS('www.fourthought.com','ft:Attr<2>','Value3')
except DOMException , x:
if x.code != INVALID_CHARACTER_ERR:
tester.error('Wrong exception on illegal character: %s' % str(x.code))
else:
tester.error('setAttributeNS allowed illegal characters')
tester.testDone()
tester.startTest('Testing getAttributeNS()')
if e.getAttributeNS('www.fourthought.com','Attr1') != 'Value 2':
tester.error('getAttributeNS returned the worng string')
if e.getAttributeNS('www.fourthought.com','Attr2') != '':
tester.error('getAttributeNS returns value for non-existant attribute')
tester.testDone()
tester.startTest('Testing hasAttributeNS()')
if not e.hasAttributeNS('www.fourthought.com','Attr1'):
tester.error('hasAttributeNS didn\'t find the attribute')
if e.hasAttributeNS('www.fourthought.com','Attr2'):
tester.error('hasAttributeNS found a non-existant attribute')
tester.testDone()
tester.startTest('Testing removeAttributeNS()')
if e.removeAttributeNS('www.fourthought.com','Attr1') != None:
tester.error('removeAttributeNS returns something')
if e.attributes.length != 0:
tester.error('removeAttributeNS did not remove it')
tester.testDone()
tester.startTest('Testing setAttributeNodeNS()')
attr = doc.createAttributeNS('www.fourthought.com','ft:ns1')
attr.value = 'TEST'
if e.setAttributeNodeNS(attr) != None:
tester.error('setAttributeNodeNS returns a value')
attr1 = doc.createAttributeNS('www.fourthought.com','ft:ns1')
if e.setAttributeNodeNS(attr1).nodeName != attr.nodeName:
tester.error('setAttributeNS does not return the replaced value')
tester.testDone()
tester.startTest('Testing getAttributeNodeNS()')
if e.getAttributeNodeNS('www.fourthought.com', 'ns1').nodeName != attr1.nodeName:
tester.error('getAttributeNodeNS does not return the correct value')
if e.getAttributeNodeNS('www.fourthought.com','ns2') != None:
tester.error('getAttributeNodeNS returns a value when it shouldn;t')
tester.testDone()
tester.startTest('Testing getElementsByTagNameNS()')
eNs = doc.createElementNS('www.fourthought.com','ft:ns')
e.appendChild(eNs)
rt = e.getElementsByTagNameNS('www.fourthought.com','ns')
if len(rt) != 1:
tester.error('failed with specified namespace and localName')
rt = e.getElementsByTagNameNS('www.fourthought.com','*')
if len(rt) != 1:
tester.error('failed with specified namespace and * localName')
rt = e.getElementsByTagNameNS('*','ns')
if len(rt) != 1:
print rt
tester.error('failed with * namespace and specified localName')
rt = e.getElementsByTagNameNS('*','*')
if len(rt) != 5:
print rt
tester.error('failed with * namespace and localName')
tester.testDone()
tester.startTest('Testing ext.ReleaseNode()')
from xml.dom import ext
ext.ReleaseNode(e)
if e.childNodes.length != 0:
tester.error('ReleaseNode did not remove from parent')
if e5.parentNode != None:
tester.error('ReleaseNode did not set parent to None')
tester.testDone()
tester.startTest('Test cloneNode()')
e.setAttribute('ATT1','VALUE 1')
e6 = e.cloneNode(1)
if e.attributes.length != e6.attributes.length:
tester.error("cloneNode didn't do the right number of attributes")
a1 = e.attributes.item(0)
a2 = e6.attributes.item(0)
if a1.name != a2.name:
tester.error('cloneNode did not copy the attribute names')
if a1.value != a2.value:
tester.error('cloneNode did not copy the attribute values')
tester.testDone()
return tester.groupDone()
if __name__ == '__main__':
import sys
import TestSuite
tester = TestSuite.TestSuite()
retVal = test(tester)
sys.exit(retVal)
|