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="document('foo')"/>
<xsl:message terminate="no">bar</xsl:message>
<xsl:value-of select="$baz"/>
</xsl:template>
</xsl:stylesheet>
'''
xml = r'''<!DOCTYPE message SYSTEM "test.dtd">
<message>bye bye!</message>'''
class ProblemListener:
def __init__(self, problems):
self.problems = problems
self.savedNodes = []
def problem(self, where, what,
sourceNode, styleNode,
msg, uri, lineNo, offset):
for key, value in self.problems[0].items():
assert locals()[key] == value
self.problems = self.problems[1:]
if sourceNode: self.savedNodes.append(sourceNode)
if styleNode: self.savedNodes.append(styleNode)
class ListenerException(Exception):
pass
class RaisingListener:
def __init__(self):
pass
def problem(self, where, what,
sourceNode, styleNode,
msg, uri, lineNo, offset):
raise ListenerException
class _CompareObject:
def __init__(self, attributes):
self.attributes = attributes
def __cmp__(self, other):
for key, value in self.attributes.items():
try:
if self.attributes[key] != getattr(other, key):
return -1
except AttributeError:
return -1
return 0
class _RegexEqual:
def __init__(self, pattern):
import re
self.pattern = re.compile(pattern)
def __cmp__(self, other):
if self.pattern.match(other) is not None:
return 0
else:
return -1
class ProblemListenerTestCase(unittest.TestCase):
def checkDefaultRemoval(self):
save = Pyana.defaultProblemListenerFactory
del Pyana.defaultProblemListenerFactory
self.assertRaises(KeyError, Pyana.Transformer)
Pyana.defaultProblemListenerFactory = save
def check(self):
problems = [
{ # foo cannot be opened
'where' : Pyana.eXSLPROCESSOR,
'what' : Pyana.eWARNING,
'sourceNode' : _CompareObject({'nodeName' : 'message'}),
'styleNode': None,
# msg
'uri' : _RegexEqual('.*?Python String'),
'lineNo' : 6,
'offset' : 49
},
{ # "bar" message
'where' : Pyana.eXSLPROCESSOR,
'what' : Pyana.eMESSAGE,
'sourceNode' : _CompareObject({'nodeName' : 'message'}),
'styleNode' : None,
'msg' : "bar",
'uri' : _RegexEqual('.*?Python String'),
'lineNo' : 7,
'offset' : 37},
{ # "baz" variable not defined
'where' : Pyana.eXSLPROCESSOR,
'what' : Pyana.eERROR,
'sourceNode' : _CompareObject({'nodeName' : 'message'}),
'styleNode' : None,
'msg' : "The variable 'baz' is not defined.",
'uri' : _RegexEqual('.*?Python String'),
'lineNo' : 8,
'offset' : 38}]
t = Pyana.Transformer()
t.useValidation = 1
problemListener = ProblemListener(problems)
t.setProblemListener(problemListener)
self.assertRaises(Pyana.ProcessorError, t.transform2String, xml, xsl)
assert problemListener.problems == []
for node in problemListener.savedNodes:
self.assertRaises(TypeError, getattr, node, 'nodeName')
t.setProblemListener(RaisingListener())
self.assertRaises(ListenerException, t.transform2String, xml, xsl)
def getTestSuites(type):
return unittest.TestSuite([
unittest.makeSuite(ProblemListenerTestCase, type)
])
|