~ubuntu-branches/ubuntu/trusty/python-docutils/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/no-test-skipping.diff/test/test_writers/test_odt.py

  • Committer: Package Import Robot
  • Author(s): Jakub Wilk
  • Date: 2012-03-03 01:40:35 UTC
  • Revision ID: package-import@ubuntu.com-20120303014035-ni524m4xktevt3bn
Tags: 0.8.1-6
* Fix a typo in a patch description.
* Bump standards version to 3.9.3 (no changes needed).
* Don't mention “Dive Into Python” in python*-roman package descriptions
  (closes: #661555). Thanks to Konrad Schöbel for the bug report.
* Remove references to http://diveintopython.org/ from debian/copyright.
* Do not skip tests because of failed imports, as they should never happen.
  (no-test-skipping.diff)
* Check only *.py files when looking for unguarded use of __file__.
* Fix paths in the auto-generated manual pages.
* Split section “Usage” into “Synopsis” and “Description” in the manual
  pages.
* Fool DocutilsXMLTestCase into thinking that Python version is 2.7.3/3.2.3
  when it's in fact 2.7.2/3.2.2 with patched
  xml.dom.minidom.Document.toprettyxml() method (closes: #645369).
  (fix-docutilsxmltestcase.diff).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# $Id: test_odt.py 6989 2011-03-04 23:44:32Z dkuhlman $
 
4
# Author: Dave Kuhlman <dkuhlman@rexx.com>
 
5
# Copyright: This module has been placed in the public domain.
 
6
 
 
7
"""
 
8
Tests for docutils odtwriter.
 
9
 
 
10
Instructions for adding a new test:
 
11
 
 
12
1. Add a new method to class DocutilsOdtTestCase (below) named
 
13
   test_odt_xxxx, where xxxx describes your new feature.  See
 
14
   test_odt_basic for an example.
 
15
 
 
16
2. Add a new input reST (.txt) file in test/functional/input. This
 
17
   file should contain the smallest amount of reST that tests your
 
18
   new feature.  Name this file odt_xxxx.txt.
 
19
 
 
20
3. Convert your input reST (.txt) file to an ODF (.odt) file using
 
21
   rst2odt.py.  Place this ODF (.odt) file in
 
22
   test/functional/expected.  Name this file odt_xxxx.odt.
 
23
   You can also pass parameter save_output_name='filename' to method
 
24
   process_test() in order to produce expected output.
 
25
   See and modify variable TEMP_FILE_PATH for destination.
 
26
 
 
27
4. Run your test.  Your new test should pass.
 
28
 
 
29
5. If any other tests fail, that's a possible regression.
 
30
 
 
31
"""
 
32
 
 
33
import sys
 
34
import os
 
35
import StringIO
 
36
import zipfile
 
37
from xml.dom import minidom
 
38
import tempfile
 
39
 
 
40
from __init__ import DocutilsTestSupport
 
41
 
 
42
import docutils
 
43
import docutils.core
 
44
from docutils._compat import BytesIO
 
45
 
 
46
#
 
47
# Globals
 
48
TEMP_FILE_PATH = '/tmp'
 
49
INPUT_PATH = 'functional/input/'
 
50
EXPECTED_PATH = 'functional/expected/'
 
51
 
 
52
 
 
53
class DocutilsOdtTestCase(DocutilsTestSupport.StandardTestCase):
 
54
 
 
55
    #
 
56
    # Check to see if we can import the needed XML library.
 
57
    # Report failure if we cannot.
 
58
    def check_import(self):
 
59
        WhichElementTree = ''
 
60
        try:
 
61
            # 1. Try to use lxml.
 
62
            #from lxml import etree
 
63
            #WhichElementTree = 'lxml'
 
64
            raise ImportError('Ignoring lxml')
 
65
        except ImportError, e:
 
66
            try:
 
67
                # 2. Try to use ElementTree from the Python standard library.
 
68
                from xml.etree import ElementTree as etree
 
69
                WhichElementTree = 'elementtree'
 
70
            except ImportError, e:
 
71
                try:
 
72
                    # 3. Try to use a version of ElementTree installed as a separate
 
73
                    #    product.
 
74
                    from elementtree import ElementTree as etree
 
75
                    WhichElementTree = 'elementtree'
 
76
                except ImportError, e:
 
77
                    s1 = '\nSkipped test of odf_odt writer.  ' \
 
78
                         'In order to test odf_odt writer ' \
 
79
                         'must install either a version of Python containing ' \
 
80
                         'ElementTree (Python version >=2.5) or ' \
 
81
                         'install ElementTree.\n\n'
 
82
                    #self.fail(s1)
 
83
                    sys.stderr.write(s1)
 
84
        return WhichElementTree
 
85
 
 
86
    def process_test(self, input_filename, expected_filename, 
 
87
            save_output_name=None, settings_overrides=None):
 
88
        if not self.check_import():
 
89
            return
 
90
        # Test that xmlcharrefreplace is the default output encoding
 
91
        # error handler.
 
92
        input_file = open(INPUT_PATH + input_filename, 'rb')
 
93
        expected_file = open(EXPECTED_PATH + expected_filename, 'rb')
 
94
        input = input_file.read()
 
95
        expected = expected_file.read()
 
96
        input_file.close()
 
97
        expected_file.close()
 
98
        if settings_overrides is None:
 
99
            settings_overrides={ }
 
100
        result = docutils.core.publish_string(
 
101
            source=input,
 
102
            reader_name='standalone',
 
103
            writer_name='odf_odt',
 
104
            settings_overrides=settings_overrides)
 
105
##         msg = 'file length not equal: expected length: %d  actual length: %d' % (
 
106
##             len(expected), len(result), )
 
107
##         self.assertEqual(str(len(result)), str(len(expected)))
 
108
        if save_output_name:
 
109
            filename = '%s%s%s' % (TEMP_FILE_PATH, os.sep, save_output_name,)
 
110
            outfile = open(filename, 'w')
 
111
            outfile.write(result)
 
112
            outfile.close()
 
113
        content1 = self.extract_file(result, 'content.xml')
 
114
        content2 = self.extract_file(expected, 'content.xml')
 
115
        msg = 'content.xml not equal: expected len: %d  actual len: %d' % (
 
116
            len(content2), len(content1), )
 
117
        self.assertEqual(content1, content2, msg)
 
118
 
 
119
    def extract_file(self, payload, filename):
 
120
        payloadfile = BytesIO()
 
121
        payloadfile.write(payload)
 
122
        payloadfile.seek(0)
 
123
        zfile = zipfile.ZipFile(payloadfile, 'r')
 
124
        content1 = zfile.read(filename)
 
125
        doc = minidom.parseString(content1)
 
126
        #content2 = doc.toprettyxml(indent='  ')
 
127
        content2 = doc.toxml()
 
128
        return content2
 
129
 
 
130
    def assertEqual(self, first, second, msg=None):
 
131
        if msg is None:
 
132
            msg2 = msg
 
133
        else:
 
134
            sep = '+' * 60
 
135
            msg1 = '\n%s\nresult:\n%s\n%s\nexpected:\n%s\n%s' % (
 
136
                sep, first, sep, second, sep, )
 
137
            #msg2 = '%s\n%s' % (msg1, msg, )
 
138
            msg2 = '%s' % (msg, )
 
139
        DocutilsTestSupport.StandardTestCase.failUnlessEqual(self,
 
140
            first, second, msg2)
 
141
 
 
142
    #
 
143
    # Unit test methods
 
144
    #
 
145
    # All test methods should be named "test_odt_xxxx", where
 
146
    #     xxxx is replaced with a name for the new test.
 
147
    # See instructions above in module doc-string.
 
148
    #
 
149
 
 
150
    def test_odt_basic(self):
 
151
        self.process_test('odt_basic.txt', 'odt_basic.odt',
 
152
            #save_output_name='odt_basic.odt'
 
153
            )
 
154
 
 
155
    def test_odt_tables1(self):
 
156
        self.process_test('odt_tables1.txt', 'odt_tables1.odt',
 
157
            #save_output_name='odt_tables1.odt'
 
158
            )
 
159
 
 
160
    def test_odt_custom_headfoot(self):
 
161
        settings_overrides = {
 
162
            'custom_header': 'Page %p% of %P%',
 
163
            'custom_footer': 'Title: %t%  Date: %d3%  Time: %t4%',
 
164
            }
 
165
        self.process_test('odt_custom_headfoot.txt', 'odt_custom_headfoot.odt',
 
166
            settings_overrides=settings_overrides,
 
167
            #save_output_name='odt_custom_headfoot.odt'
 
168
            )
 
169
 
 
170
    #
 
171
    # Template for new tests.
 
172
    # Also add functional/input/odt_xxxx.txt and
 
173
    #   functional/expected/odt_xxxx.odt
 
174
    # Replace all xxxx with name of your test.
 
175
    #
 
176
##     def test_odt_xxxx(self):
 
177
##         self.process_test('odt_xxxx.txt', 'odt_xxxx.odt')
 
178
 
 
179
 
 
180
# -----------------------------------------------------------------
 
181
 
 
182
 
 
183
if __name__ == '__main__':
 
184
    import unittest
 
185
    unittest.main()