TestTraceListener.py :  » Language-Interface » Pyana » Pyana-0.9.2 » Test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Pyana 
Pyana » Pyana 0.9.2 » Test » TestTraceListener.py
"""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)
                ])
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.