import StringIO
import os
import Pyana
import unittest
import sys
def xalanDOM(input):
return Pyana.Transformer().parseSource(input).getDocument()
class DOMTestCase(unittest.TestCase):
def setUp(self):
self.t = Pyana.Transformer()
def checkDocument(self):
fruit = r'''
<fruit:fruit-category name="tropical" xmlns:fruit="http://www.sweetapp.com/fruit">
<fruit:fruit name="pineapple"/>
<fruit:fruit-category name="citrus">
<fruit:fruit name="lemon"/>
<fruit:fruit name="lime"/>
<fruit:fruit name="orange"/>
</fruit:fruit-category>
</fruit:fruit-category>'''
self._checkDocument(xalanDOM(fruit))
def _checkDocument(self, document):
# http://www.python.org/doc/current/lib/dom-document-objects.html#dom-document-objects
assert repr(document).startswith('<Document')
# documentElement
assert document.documentElement.nodeName == u"fruit:fruit-category"
"""
This routines are unless until Xerces DOM support is readded
if usesXerces:
# getElementsByTagName
elements = document.getElementsByTagName(u'locations')
assert len(elements) == 0
elements = document.getElementsByTagName(u'fruit:fruit')
assert elements[0].getAttribute(u"name") == u"pineapple"
assert len(elements) == 4
# getElementsByTagNameNS
elements = document.getElementsByTagNameNS(u'http://www.sweetapp.com/fruit', 'locations')
assert len(elements) == 0
elements = document.getElementsByTagNameNS(u'http://www.sweetapp.com/fruit', 'fruit')
assert elements[0].getAttribute(u"name") == u"pineapple"
assert len(elements) == 4
else:
# getElementsByTagName
self.assertRaises(Pyana.DOMException, document.getElementsByTagName, u'fruit:fruit')
# getElementsByTagNameNS
self.assertRaises(Pyana.DOMException, document.getElementsByTagNameNS, u'http://www.sweetapp.com/fruit', u'fruit')
"""
def checkElement(self):
fruit = r'''
<fruit:fruit-category name="tropical" xmlns:fruit="http://www.sweetapp.com/fruit">
<fruit:fruit name="pineapple"/>
<fruit:fruit-category name="citrus">
<fruit:fruit name="lemon"/>
<fruit:fruit name="lime"/>
<fruit:fruit name="orange"/>
</fruit:fruit-category>
</fruit:fruit-category>'''
self._checkElement(xalanDOM(fruit))
def _checkElement(self, document):
# http://www.python.org/doc/current/lib/dom-element-objects.html#dom-element-objects
documentElement = document.documentElement
assert repr(documentElement).startswith('<Element')
# tagName
assert documentElement.tagName == u'fruit:fruit-category'
# getElementsByTagName(tagName)
# getElementsByTagNameNS(tagName)
# getAttribute(attname)
assert documentElement.getAttribute(u"name") == u"tropical"
assert documentElement.getAttribute(u"foo") == u""
self.assertRaises(TypeError, documentElement.getAttribute, 5)
# getAttributeNode(attrname)
# getAttributeNS(namespaceURI, localName)
# getAttributeNodeNS(namespaceURI, localName)
def checkNode(self):
contacts = r'''
<con:contacts xmlns:con="http://www.sweetapp.com/contacts">
<!-- Stores information about contacts -->
<con:person name="Brian Quinlan" sex="Male">
<con:birthdate year="1974" month="12" day="11"/>
<con:phone>6047288693</con:phone>
<con:website><![CDATA[http://www.sweetapp.com/]]></con:website>
</con:person>
</con:contacts>'''
self._checkNode(xalanDOM(contacts))
def _checkNode(self, document):
# see http://www.python.org/doc/current/lib/dom-node-objects.html#dom-node-object
documentElement = document.documentElement
comment = documentElement.firstChild.nextSibling
brianElement = comment.nextSibling.nextSibling
birthdateElement = brianElement.firstChild.nextSibling
phoneElement = birthdateElement.nextSibling.nextSibling
phoneText = phoneElement.firstChild
# attributes
# childNodes
assert len(comment.childNodes) == 0
assert phoneElement.childNodes[0].isSameNode(phoneText)
# lastChild
assert comment.lastChild is None
assert phoneElement.lastChild.isSameNode(phoneText)
# firstChild
assert comment.firstChild is None
assert phoneElement.firstChild.isSameNode(phoneText)
# localName
assert documentElement.localName == u'contacts'
# namespaceURI
assert documentElement.namespaceURI == u'http://www.sweetapp.com/contacts'
# nextSibling
assert documentElement.nextSibling is None
assert birthdateElement.nextSibling.nextSibling.isSameNode(phoneElement)
# nodeName
assert documentElement.nodeName == 'con:contacts'
# nodeType
assert document.nodeType == Pyana.DOCUMENT_NODE
assert documentElement.nodeType == Pyana.ELEMENT_NODE
assert comment.nodeType == Pyana.COMMENT_NODE
# nodeValue
assert documentElement.nodeValue == ''
assert phoneText.nodeValue == '6047288693'
# parentNode
assert document.parentNode is None
assert documentElement.parentNode.isSameNode(document)
assert brianElement.parentNode.isSameNode(documentElement)
# prefix
assert documentElement.prefix == u'con'
assert comment.prefix == ''
# previousSibling
assert documentElement.previousSibling is None
assert phoneElement.previousSibling.previousSibling.isSameNode(birthdateElement)
# hasAttributes()
assert not phoneElement.hasAttributes()
assert brianElement.hasAttributes()
self.assertRaises(TypeError, comment.hasAttributes, 5)
# hasChildNodes()
assert documentElement.hasChildNodes()
assert not comment.hasChildNodes()
self.assertRaises(TypeError, comment.hasChildNodes, 5)
# isSameNode()
assert not comment.isSameNode(None)
def checkNodeList(self):
xml = r'''<a><b/><c/><d/></a>'''
for document in (xalanDOM(xml),):
documentElement = document.documentElement
assert len(documentElement.firstChild.childNodes) == 0
children = documentElement.childNodes
assert repr(children).startswith('<NodeList')
assert len(children) == 3
assert children[0].nodeName == u'b'
assert children[1].nodeName == u'c'
assert children[2].nodeName == u'd'
try:
assert children[3]
except IndexError: pass
else: assert 0, "expected IndexError"
def getTestSuites(type):
return unittest.TestSuite([
unittest.makeSuite(DOMTestCase, type)
])
if __name__ == '__main__':
unittest.TestProgram()
|