import StringIO
import os
import Pyana
import unittest
import sys
xsl = r'''
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="message">
<xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
'''
xml = r'''<!DOCTYPE message SYSTEM "test.dtd">
<messages><message>bye bye!</message></messages>foo'''
class ErrorHandler:
def __init__(self, expected):
self.expected = expected
def common(self, name, e):
assert self.expected[0] == (name,) + e.args
self.expected = self.expected[1:]
def warning(self, e):
self.common('warning', e)
def error(self, e):
self.common('error', e)
def fatalError(self, e):
self.common('fatalError', e)
def resetErrors(self):
pass
class HandlerException(Exception):
pass
class RaisingHandler(ErrorHandler):
def __init__(self):
pass
def common(self, name, e):
raise HandlerException, (name, e)
class ResetRaisingHandler(RaisingHandler):
def common(self, name, e):
pass
def resetErrors(self):
raise HandlerException, "resetErrors"
class ErrorHandlerTestCase(unittest.TestCase):
def checkDefaultRemoval(self):
save = Pyana.defaultErrorHandlerFactory
del Pyana.defaultErrorHandlerFactory
self.assertRaises(KeyError, Pyana.Transformer)
Pyana.defaultErrorHandlerFactory = save
def check(self):
t = Pyana.Transformer()
t.useValidation = 1
errorHandler = ErrorHandler(
[ # The following warning is generated because Xerces is attempting
# to load a schema. Whether or not this is a bug is still up in
# the air (the problem affects testXSLT too).
('error', "Unknown element 'messages'", 2, 11, None, 'Python String'),
('error', 'Root element different from DOCTYPE', 2, 11, None, 'Python String'),
('fatalError', 'Expected comment or processing instruction', 2, 49, None, 'Python String')]
)
t.setErrorHandler(errorHandler)
assert t.transform2String(xml, xsl) == 'bye bye!'
assert errorHandler.expected == []
t.setErrorHandler(RaisingHandler())
self.assertRaises(HandlerException, t.transform2String, xml, xsl)
t.setErrorHandler(RaisingHandler())
self.assertRaises(HandlerException, t.transform2String, xml, xsl)
def getTestSuites(type):
return unittest.TestSuite([
unittest.makeSuite(ErrorHandlerTestCase, type)
])
|