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.
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.
37
from xml.dom import minidom
40
from __init__ import DocutilsTestSupport
44
from docutils._compat import BytesIO
48
TEMP_FILE_PATH = '/tmp'
49
INPUT_PATH = 'functional/input/'
50
EXPECTED_PATH = 'functional/expected/'
53
class DocutilsOdtTestCase(DocutilsTestSupport.StandardTestCase):
56
# Check to see if we can import the needed XML library.
57
# Report failure if we cannot.
58
def check_import(self):
62
#from lxml import etree
63
#WhichElementTree = 'lxml'
64
raise ImportError('Ignoring lxml')
65
except ImportError, e:
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:
72
# 3. Try to use a version of ElementTree installed as a separate
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'
84
return WhichElementTree
86
def process_test(self, input_filename, expected_filename,
87
save_output_name=None, settings_overrides=None):
88
if not self.check_import():
90
# Test that xmlcharrefreplace is the default output encoding
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()
98
if settings_overrides is None:
99
settings_overrides={ }
100
result = docutils.core.publish_string(
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)))
109
filename = '%s%s%s' % (TEMP_FILE_PATH, os.sep, save_output_name,)
110
outfile = open(filename, 'w')
111
outfile.write(result)
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)
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()
130
def assertEqual(self, first, second, msg=None):
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,
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_tables1(self):
156
self.process_test('odt_tables1.txt', 'odt_tables1.odt',
157
#save_output_name='odt_tables1.odt'
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%',
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'
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.
176
## def test_odt_xxxx(self):
177
## self.process_test('odt_xxxx.txt', 'odt_xxxx.odt')
180
# -----------------------------------------------------------------
183
if __name__ == '__main__':