TestProblemListener.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 » TestProblemListener.py
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)
                ])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.