1
##############################################################################
3
# Copyright (c) 2007 Zope Corporation and Contributors.
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTLAR PURPOSE.
13
##############################################################################
14
"""Testing all XML Locale functionality.
16
$Id: test_rml.py 84586 2008-03-11 19:51:31Z ctheune $
24
from z3c.rml import rml2pdf, attr
28
return ('gs', '-q', '-sNOPAUSE', '-sDEVICE=png256',
29
'-sOutputFile=%s[Page-%%d].png' % path[:-4],
33
class RMLRenderingTestCase(unittest.TestCase):
35
def __init__(self, inPath, outPath):
37
self._outPath = outPath
38
unittest.TestCase.__init__(self)
41
# Switch file opener for Image attibute
42
self._fileOpen = attr.File.open
43
def testOpen(img, filename):
44
# cleanup win paths like:
45
# ....\\input\\file:///D:\\trunk\\...
46
if sys.platform[:3].lower() == "win":
47
filename.replace('/', '\\')
48
if filename.startswith('file:///'):
49
filename = filename[len('file:///'):]
50
path = os.path.join(os.path.dirname(self._inPath), filename)
51
return open(path, 'rb')
52
attr.File.open = testOpen
53
import z3c.rml.tests.module
54
sys.modules['module'] = z3c.rml.tests.module
55
sys.modules['mymodule'] = z3c.rml.tests.module
58
attr.File.open = self._fileOpen
59
del sys.modules['module']
60
del sys.modules['mymodule']
63
rml2pdf.go(self._inPath, self._outPath)
66
class ComparePDFTestCase(unittest.TestCase):
70
def __init__(self, basePath, testPath):
71
self._basePath = basePath
72
self._testPath = testPath
73
unittest.TestCase.__init__(self)
75
def assertSameImage(self, baseImage, testImage):
76
base = PIL.Image.open(baseImage).getdata()
77
test = PIL.Image.open(testImage).getdata()
78
for i in range(len(base)):
79
if (base[i] - test[i]) != 0:
80
self.fail('Image is not the same.')
83
# Convert the base PDF to image(s)
84
status = subprocess.Popen(gs_command(self._basePath)).wait()
87
# Convert the test PDF to image(s)
88
status = subprocess.Popen(gs_command(self._testPath)).wait()
91
# Go through all pages and ensure their equality
94
baseImage = self._basePath[:-4] + '[Page-%i].png' %n
95
testImage = self._testPath[:-4] + '[Page-%i].png' %n
96
if os.path.exists(baseImage) and os.path.exists(testImage):
97
self.assertSameImage(baseImage, testImage)
104
suite = unittest.TestSuite()
105
inputDir = os.path.join(os.path.dirname(z3c.rml.tests.__file__), 'input')
106
outputDir = os.path.join(os.path.dirname(z3c.rml.tests.__file__), 'output')
107
expectDir = os.path.join(os.path.dirname(z3c.rml.tests.__file__), 'expected')
108
for filename in os.listdir(inputDir):
109
if not filename.endswith(".rml"):
111
inPath = os.path.join(inputDir, filename)
112
outPath = os.path.join(outputDir, filename[:-4] + '.pdf')
113
expectPath = os.path.join(expectDir, filename[:-4] + '.pdf')
115
# ** Test RML to PDF rendering **
116
# Create new type, so that we can get test matching
117
TestCase = type(filename[:-4], (RMLRenderingTestCase,), {})
118
case = TestCase(inPath, outPath)
121
# ** Test PDF rendering correctness **
122
TestCase = type('compare-'+filename[:-4], (ComparePDFTestCase,), {})
123
case = TestCase(expectPath, outPath)
128
if __name__ == '__main__':
129
unittest.main(defaultTest='test_suite')