Ignore config file settings when testing the ODT writer.
[docutils.git] / docutils / test / test_writers / test_odt.py
blob4a83eb9786cabfcafc00695f33b47b003e4f9517
1 #!/usr/bin/env python
3 # $Id$
4 # Author: Dave Kuhlman <dkuhlman@rexx.com>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests for docutils odtwriter.
10 Instructions for adding a new test:
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.
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.
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.
27 4. Run your test. Your new test should pass.
29 5. If any other tests fail, that's a possible regression.
31 """
33 import sys
34 import os
35 import StringIO
36 import zipfile
37 from xml.dom import minidom
38 import tempfile
40 from __init__ import DocutilsTestSupport
42 import docutils
43 import docutils.core
44 from docutils._compat import BytesIO
47 # Globals
48 TEMP_FILE_PATH = 'functional/output/'
49 INPUT_PATH = 'functional/input/'
50 EXPECTED_PATH = 'functional/expected/'
52 class DocutilsOdtTestCase(DocutilsTestSupport.StandardTestCase):
54 # Check to see if we can import the needed XML library.
55 # Report failure if we cannot.
56 def check_import(self):
57 WhichElementTree = ''
58 try:
59 # 1. Try to use lxml.
60 #from lxml import etree
61 #WhichElementTree = 'lxml'
62 raise ImportError('Ignoring lxml')
63 except ImportError, e:
64 try:
65 # 2. Try to use ElementTree from the Python standard library.
66 from xml.etree import ElementTree as etree
67 WhichElementTree = 'elementtree'
68 except ImportError, e:
69 try:
70 # 3. Try to use a version of ElementTree installed as a separate
71 # product.
72 from elementtree import ElementTree as etree
73 WhichElementTree = 'elementtree'
74 except ImportError, e:
75 s1 = '\nSkipped test of odf_odt writer. ' \
76 'In order to test odf_odt writer ' \
77 'must install either a version of Python containing ' \
78 'ElementTree (Python version >=2.5) or ' \
79 'install ElementTree.\n\n'
80 #self.fail(s1)
81 sys.stderr.write(s1)
82 return WhichElementTree
84 def process_test(self, input_filename, expected_filename,
85 save_output_name=None, settings_overrides=None):
86 if not self.check_import():
87 return
88 # Test that xmlcharrefreplace is the default output encoding
89 # error handler.
90 input_file = open(INPUT_PATH + input_filename, 'rb')
91 expected_file = open(EXPECTED_PATH + expected_filename, 'rb')
92 input = input_file.read()
93 expected = expected_file.read()
94 input_file.close()
95 expected_file.close()
96 if settings_overrides is None:
97 settings_overrides={}
98 settings_overrides['_disable_config'] = True
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)
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
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.assertEqual(self,
140 first, second, msg2)
143 # Unit test methods
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.
150 def test_odt_basic(self):
151 self.process_test('odt_basic.txt', 'odt_basic.odt',
152 save_output_name='odt_basic.odt'
155 def test_odt_nested_class(self):
156 self.process_test('odt_nested_class.txt',
157 'odt_nested_class.odt',
158 save_output_name='odt_nested_class.odt'
160 self.process_test('odt_unnested_class.txt',
161 'odt_unnested_class.odt',
162 save_output_name='odt_unnested_class.odt'
164 self.process_test('odt_no_class.txt',
165 'odt_no_class.odt',
166 save_output_name='odt_no_class.odt'
169 def test_odt_tables1(self):
170 self.process_test('odt_tables1.txt', 'odt_tables1.odt',
171 save_output_name='odt_tables1.odt'
174 def test_odt_custom_headfoot(self):
175 settings_overrides = {
176 'custom_header': 'Page %p% of %P%',
177 'custom_footer': 'Title: %t% Date: %d3% Time: %t4%',
179 self.process_test('odt_custom_headfoot.txt', 'odt_custom_headfoot.odt',
180 settings_overrides=settings_overrides,
181 save_output_name='odt_custom_headfoot.odt'
185 # Template for new tests.
186 # Also add functional/input/odt_xxxx.txt and
187 # functional/expected/odt_xxxx.odt
188 # Replace all xxxx with name of your test.
190 ## def test_odt_xxxx(self):
191 ## self.process_test('odt_xxxx.txt', 'odt_xxxx.odt')
194 # -----------------------------------------------------------------
197 if __name__ == '__main__':
198 import unittest
199 unittest.main()