TestOutputTypes.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 » TestOutputTypes.py
import Pyana
import os
import unittest
from StringIO import StringIO
import xml.dom

class Writer:
    def __init__(self):
        self.buffer = ""

    def write(self, writeBuffer):
        self.buffer += str(writeBuffer)

class NoWriteMethodWriter:
    pass

class WriteException(Exception):
    pass

class RaisingWriter:
    def write(self, writeBuffer):
        raise WriteException
        
class WriterWithFlush(Writer):
    def flush(self):
        pass

class WriterWithBrokenFlush(WriterWithFlush):
    def flush(self):
        raise WriteException

outputTestXSL = r'''
<xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="a"><xsl:value-of select="child::text()"/></xsl:template>
</xsl:stylesheet>
'''

domOutputTestXSL = r'''
<xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/"><out><xsl:apply-templates/></out></xsl:template>
    
    <xsl:template match="a"><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>
'''

outputTestXML = r'''
<a>Success!</a>
'''

identityTransform = r'''
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Whenever you match any node or any attribute -->
  <xsl:template match="node()|@*">
    <!-- Copy the current node -->
    <xsl:copy>
      <!-- Including any attributes it has and any child nodes -->
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
'''

manyNodes = r'''
<a><b attr='foo'>Text</b>2<b><c><d>Test</d><d>Test</d></c></b>Test</a>'''


def alldescendants(node, nodes = None):
    if nodes is None:
        nodes = []
        
    if node is None:
        return nodes

    alldescendants(node.firstChild, nodes)
    alldescendants(node.nextSibling, nodes)
    
    nodes.append(node)
    return nodes
        
class OutputTypesTestCase(unittest.TestCase):
    def setUp(self):
        self.t = Pyana.Transformer()
        
    def checkStringOutput(self):
        assert self.t.transform2String(
                    outputTestXML,
                    outputTestXSL
                ) == 'Success!'

        assert self.t.transform2String(
                    source=outputTestXML,
                    style=outputTestXSL
                ) == 'Success!'

        assert self.t.transform2String(
                    '<nomatch/>',
                    outputTestXSL
                ) == ''

        # Check convenience version
        assert Pyana.transform2String(
                    outputTestXML,
                    outputTestXSL
                ) == 'Success!'        
                        
    def checkFileOutput(self):
        import tempfile
        
        self.assertRaises(
                    TypeError,
                    self.t.transform2File,
                    outputTestXML,
                    outputTestXSL,
                    5
                )
            
        filename = tempfile.mktemp()
        try:
            open(filename, 'wb').write("Success!")
            os.remove(filename)
        except IOError, e:
            raise IOError, 'Unable to create file, file output test skipped'
        else:
            assert self.t.transform2File(
                        outputTestXML,
                        outputTestXSL,
                        filename
                    ) == None

            
            assert open(filename,'rb').read() == "Success!"
            os.remove(filename)

            # Check convenience version
            assert Pyana.transform2File(
                        outputTestXML,
                        outputTestXSL,
                        filename
                    ) == None

            assert open(filename,'rb').read() == "Success!"
            os.remove(filename)
        
            assert self.t.transform2File(
                        source=outputTestXML,
                        style=outputTestXSL,
                        file=filename
                    ) == None
            
            assert open(filename,'rb').read() == "Success!"
            os.remove(filename)
                        
            assert self.t.transform2File(
                        StringIO(outputTestXML),
                        StringIO(outputTestXSL),
                        filename
                    ) == None
            
            assert open(filename,'rb').read() == "Success!"
            os.remove(filename)

            self.assertRaises(
                        Pyana.XSLError,
                        self.t.transform2File,
                        outputTestXML,
                        outputTestXSL,
                        '~/ImprobableDirName/testfile.xml'
                    )
        
    def checkWriterOutput(self):
        self.assertRaises(
                        AttributeError,
                        self.t.transform2Writer,
                        outputTestXML,
                        outputTestXSL,
                        {}
                    )
        
        writer = Writer()
        assert self.t.transform2Writer(
                        outputTestXML,
                        outputTestXSL,
                        writer
                    ) == None
        assert writer.buffer == "Success!"

        # Check convenience version
        writer = Writer()
        assert Pyana.transform2Writer(
                        outputTestXML,
                        outputTestXSL,
                        writer
                    ) == None
        assert writer.buffer == "Success!"

        writer = Writer()
        assert self.t.transform2Writer(
                        source=outputTestXML,
                        style=outputTestXSL,
                        writer=writer
                    ) == None
        assert writer.buffer == "Success!"

        self.assertRaises(
                        AttributeError,
                        self.t.transform2Writer,
                        outputTestXML,
                        outputTestXSL,
                        NoWriteMethodWriter()
                    )

        self.assertRaises(
                        WriteException,
                        self.t.transform2Writer,
                        outputTestXML,
                        outputTestXSL,
                        RaisingWriter()
                    )                        


        writer = WriterWithFlush()        
        assert self.t.transform2Writer(
                        outputTestXML,
                        outputTestXSL,
                        writer
                    ) == None
        assert writer.buffer == "Success!"
        
        # Exceptions thrown in flush don't get propogated by Xalan
        writer = WriterWithBrokenFlush()
        assert self.t.transform2Writer(
                        outputTestXML,
                        outputTestXSL,
                        writer
                    ) == None         
        assert writer.buffer == "Success!"

    def checkDOMOutput(self):
        self.assertRaises(
                        NotImplementedError,
                        self.t.transform2DOM,
                        outputTestXML,
                        outputTestXSL)
        
def getTestSuites(type):
    return unittest.TestSuite([
            unittest.makeSuite(OutputTypesTestCase, 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.