"""The classic example.
Executes a simple transformation and prints the string result
"""
"""
class Listener:
def trace(self, event):
print 'trace'
print '\tevent:', event
print '\tevent.styleNode:', event.styleNode
print '\tevent.styleNode.uri:', event.styleNode.uri
try: print '\tevent.styleNode.text: %s' % event.styleNode.text
except AttributeError: pass
try: print '\tevent.styleNode.currentPattern: %s' % event.styleNode.currentPattern
except AttributeError: pass
def selected(self, event):
print 'selected'
print '\tevent:', event
print '\tevent.styleNode:', event.styleNode
print '\tevent.attributeName:', event.attributeName
print '\tevent.xpathExpression:', event.xpathExpression
print '\tevent.sourceNode:', event.sourceNode
t = Pyana.Transformer()
t.setTraceListener(Listener())
print t.transform2String(source=inputExampleXML, style=inputExampleXSL)
"""
import Pyana
import unittest
inputXSL = r'''
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="message">I say:<xsl:value-of select="child::text()"/></xsl:template>
</xsl:stylesheet>
'''
inputXML = r'''
<message>Hello World!</message>
'''
class TraceProblemRaiser:
def trace(self, event):
raise ValueError('foo bar')
class SelectedProblemRaiser:
def selected(self, event):
raise ValueError('foo bar')
class GeneratedProblemRaiser:
def generated(self, event):
raise ValueError('foo bar')
class CheckTraceListener(unittest.TestCase):
def __init__(self):
self.traceEvents = []
self.styleNodes = []
self.eventNumber = 0
def trace(self, event):
self.traceEvents.append(event)
self.styleNodes.append(event.styleNode)
repr(event)
repr(event.styleNode)
if self.eventNumber == 0:
assert event.styleNode.uri.endswith("Python String")
assert event.styleNode.columnNumber == -1
assert event.styleNode.lineNumber == -1
assert event.styleNode.elementName == 'xsl:template'
assert event.styleNode.token == Pyana.ELEMNAME_TEMPLATE
assert event.styleNode.currentPattern == '/'
self.assertRaises(AttributeError, getattr, event.styleNode, 'text')
elif self.eventNumber == 3:
assert event.styleNode.token == Pyana.ELEMNAME_TEXT_LITERAL_RESULT
assert event.styleNode.text == 'I say:'
self.assertRaises(AttributeError, getattr, event.styleNode, 'currentPattern')
self.eventNumber = self.eventNumber + 1
class CheckSelectedListener:
def __init__(self):
self.selectionEvents = []
self.styleNodes = []
self.sourceNodes = []
self.eventNumber = 0
def selected(self, event):
self.selectionEvents.append(event)
self.styleNodes.append(event.styleNode)
self.sourceNodes.append(event.sourceNode)
repr(event)
repr(event.styleNode)
if self.eventNumber == 1:
assert event.styleNode.token == Pyana.ELEMNAME_VALUE_OF
assert event.styleNode.columnNumber == 79
assert event.styleNode.lineNumber == 4
assert event.attributeName == 'select'
assert event.xpathExpression == 'child::text()'
assert event.sourceNode.localName == "message"
self.eventNumber = self.eventNumber + 1
class CheckGeneratedListener:
def __init__(self):
self.generatedEvents = []
self.eventNumber = 0
def generated(self, event):
self.generatedEvents.append(event)
repr(event)
if self.eventNumber == 1:
assert event.eventType == Pyana.EVENTTYPE_CHARACTERS
assert event.characters == 'I say:'
assert event.name == ''
assert event.data == ''
assert event.start == 0
assert event.length == 6
self.eventNumber = self.eventNumber + 1
class TraceListenerTestCase(unittest.TestCase):
def setUp(self):
self.t = Pyana.Transformer()
def checkExceptions(self):
t = Pyana.Transformer()
t.setTraceListener(None) # Doesn't call unimplemented methods
t.transform2String(source=inputXML, style=inputXSL)
t.setTraceListener(TraceProblemRaiser())
self.assertRaises(ValueError, t.transform2String, source=inputXML, style=inputXSL)
t.setTraceListener(SelectedProblemRaiser())
self.assertRaises(ValueError, t.transform2String, source=inputXML, style=inputXSL)
t.setTraceListener(GeneratedProblemRaiser())
self.assertRaises(ValueError, t.transform2String, source=inputXML, style=inputXSL)
def checkTrace(self):
t = Pyana.Transformer()
traceListener = CheckTraceListener()
t.setTraceListener(traceListener)
t.transform2String(source=inputXML, style=inputXSL)
for event in traceListener.traceEvents:
assert repr(event).find('dead') != -1
self.assertRaises(TypeError, getattr, event, 'styleNode')
for node in traceListener.styleNodes:
assert repr(node).find('dead') != -1
self.assertRaises(TypeError, getattr, node, 'uri')
def checkSelected(self):
t = Pyana.Transformer()
traceListener = CheckSelectedListener()
t.setTraceListener(traceListener)
t.transform2String(source=inputXML, style=inputXSL)
for event in traceListener.selectionEvents:
assert repr(event).find('dead') != -1
self.assertRaises(TypeError, getattr, event, 'styleNode')
for node in traceListener.styleNodes:
assert repr(node).find('dead') != -1
self.assertRaises(TypeError, getattr, node, 'uri')
for node in traceListener.sourceNodes:
assert repr(node).find('dead') != -1
self.assertRaises(TypeError, getattr, node, 'localName')
def checkGenerated(self):
t = Pyana.Transformer()
traceListener = CheckGeneratedListener()
t.setTraceListener(traceListener)
t.transform2String(source=inputXML, style=inputXSL)
for event in traceListener.generatedEvents:
assert repr(event).find('dead') != -1
self.assertRaises(TypeError, getattr, event, 'data')
def getTestSuites(type):
return unittest.TestSuite([
unittest.makeSuite(TraceListenerTestCase, type)
])
|