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)
])
|