~ubuntu-branches/ubuntu/trusty/z3c.rml/trusty

« back to all changes in this revision

Viewing changes to src/z3c/rml/tests/test_rml.py

  • Committer: Bazaar Package Importer
  • Author(s): Gediminas Paulauskas
  • Date: 2011-01-05 22:34:45 UTC
  • Revision ID: james.westby@ubuntu.com-20110105223445-wkcn61jbbuqid38s
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2007 Zope Corporation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
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.
 
12
#
 
13
##############################################################################
 
14
"""Testing all XML Locale functionality.
 
15
 
 
16
$Id: test_rml.py 84586 2008-03-11 19:51:31Z ctheune $
 
17
"""
 
18
import os
 
19
import PIL
 
20
import subprocess
 
21
import unittest
 
22
import sys
 
23
import z3c.rml.tests
 
24
from z3c.rml import rml2pdf, attr
 
25
 
 
26
 
 
27
def gs_command(path):
 
28
    return ('gs', '-q', '-sNOPAUSE', '-sDEVICE=png256',
 
29
            '-sOutputFile=%s[Page-%%d].png' % path[:-4],
 
30
            path, '-c', 'quit')
 
31
 
 
32
 
 
33
class RMLRenderingTestCase(unittest.TestCase):
 
34
 
 
35
    def __init__(self, inPath, outPath):
 
36
        self._inPath = inPath
 
37
        self._outPath = outPath
 
38
        unittest.TestCase.__init__(self)
 
39
 
 
40
    def setUp(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
 
56
 
 
57
    def tearDown(self):
 
58
        attr.File.open = self._fileOpen
 
59
        del sys.modules['module']
 
60
        del sys.modules['mymodule']
 
61
 
 
62
    def runTest(self):
 
63
        rml2pdf.go(self._inPath, self._outPath)
 
64
 
 
65
 
 
66
class ComparePDFTestCase(unittest.TestCase):
 
67
 
 
68
    level = 2
 
69
 
 
70
    def __init__(self, basePath, testPath):
 
71
        self._basePath = basePath
 
72
        self._testPath = testPath
 
73
        unittest.TestCase.__init__(self)
 
74
 
 
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.')
 
81
 
 
82
    def runTest(self):
 
83
        # Convert the base PDF to image(s)
 
84
        status = subprocess.Popen(gs_command(self._basePath)).wait()
 
85
        if status:
 
86
            return
 
87
        # Convert the test PDF to image(s)
 
88
        status = subprocess.Popen(gs_command(self._testPath)).wait()
 
89
        if status:
 
90
            return
 
91
        # Go through all pages and ensure their equality
 
92
        n = 1
 
93
        while True:
 
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)
 
98
            else:
 
99
                break
 
100
            n += 1
 
101
 
 
102
 
 
103
def test_suite():
 
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"):
 
110
           continue
 
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')
 
114
 
 
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)
 
119
       suite.addTest(case)
 
120
 
 
121
       # ** Test PDF rendering correctness **
 
122
       TestCase = type('compare-'+filename[:-4], (ComparePDFTestCase,), {})
 
123
       case = TestCase(expectPath, outPath)
 
124
       suite.addTest(case)
 
125
 
 
126
   return suite
 
127
 
 
128
if __name__ == '__main__':
 
129
    unittest.main(defaultTest='test_suite')