Allow new XML formatting also in version 2.7.2
[docutils.git] / test / test_writers / test_docutils_xml.py
blobf3456dbd8243cf61e4641d8d280e520b73e204bf
1 #!/usr/bin/env python
3 # $Id$
4 # Author: Lea Wiemann <LeWiemann@gmail.com>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Test for docutils XML writer.
9 """
11 from __init__ import DocutilsTestSupport
13 import sys
14 import docutils
15 import docutils.core
17 # sample strings:
19 source = u"""\
20 Test
22 ----------
24 Test. \xe4\xf6\xfc\u20ac"""
26 xmldecl = u"""<?xml version="1.0" encoding="iso-8859-1"?>
27 """
29 doctypedecl = u"""\
30 <!DOCTYPE document PUBLIC "+//IDN docutils.sourceforge.net\
31 //DTD Docutils Generic//EN//XML"\
32 "http://docutils.sourceforge.net/docs/ref/docutils.dtd">
33 """
35 generatedby = u'<!-- Generated by Docutils %s -->\n' % docutils.__version__
37 bodynormal = u"""\
38 <document source="&lt;string&gt;"><paragraph>Test</paragraph>\
39 <transition/><paragraph>Test. \xe4\xf6\xfc&#8364;</paragraph>\
40 </document>"""
42 bodynewlines = u"""\
43 <document source="&lt;string&gt;">
44 <paragraph>Test</paragraph>
45 <transition/>
46 <paragraph>Test. \xe4\xf6\xfc&#8364;</paragraph>
47 </document>
48 """
50 bodynewlines_old = u"""\
51 <document source="&lt;string&gt;">
52 <paragraph>
53 Test
54 </paragraph>
55 <transition/>
56 <paragraph>
57 Test. \xe4\xf6\xfc&#8364;
58 </paragraph>
59 </document>
60 """
62 bodyindents = u"""\
63 <document source="&lt;string&gt;">
64 <paragraph>Test</paragraph>
65 <transition/>
66 <paragraph>Test. \xe4\xf6\xfc&#8364;</paragraph>
67 </document>
68 """
70 bodyindents_old = u"""\
71 <document source="&lt;string&gt;">
72 <paragraph>
73 Test
74 </paragraph>
75 <transition/>
76 <paragraph>
77 Test. \xe4\xf6\xfc&#8364;
78 </paragraph>
79 </document>
80 """
82 # New formatting introduced in versions 2.7.3 and 3.2.3 on 2011-11-18
83 # to fix http://bugs.python.org/issue4147
84 # (Some distributions ship also earlier versions with this patch.)
85 if (sys.version_info < (2, 7, 3) or
86 sys.version_info[0] == 3 and sys.version_info < (3, 2, 3)):
87 whitespace_fix = False
88 else:
89 whitespace_fix = True
91 def publish_xml(settings):
92 return docutils.core.publish_string(source=source.encode('utf8'),
93 reader_name='standalone',
94 writer_name='docutils_xml',
95 settings_overrides=settings)
98 class DocutilsXMLTestCase(DocutilsTestSupport.StandardTestCase):
100 settings = {'input_encoding': 'utf8',
101 'output_encoding': 'iso-8859-1',
102 '_disable_config': 1}
104 def test_publish(self):
105 for self.settings['xml_declaration'] in True, False:
106 for self.settings['doctype_declaration'] in True, False:
107 expected = u''
108 if self.settings['xml_declaration']:
109 expected += xmldecl
110 if self.settings['doctype_declaration']:
111 expected += doctypedecl
112 expected += generatedby
113 expected += bodynormal
114 result = publish_xml(self.settings)
115 self.assertEqual(result, expected.encode('latin1'))
117 def test_publish_indents(self):
118 self.settings['indents'] = True
119 self.settings['newlines'] = False
120 self.settings['xml_declaration'] = False
121 self.settings['doctype_declaration'] = False
122 result = publish_xml(self.settings)
124 # New formatting introduced in versions 2.7.3 and 3.2.3
125 if whitespace_fix:
126 expected = (generatedby + bodyindents).encode('latin1')
127 else:
128 expected = (generatedby + bodyindents_old).encode('latin1')
129 # Some distributions patch also earlier versions:
130 if (result != expected and not whitespace_fix):
131 expected = (generatedby + bodyindents).encode('latin1')
133 self.assertEqual(result, expected)
135 def test_publish_newlines(self):
136 self.settings['newlines'] = True
137 self.settings['indents'] = False
138 self.settings['xml_declaration'] = False
139 self.settings['doctype_declaration'] = False
140 result = publish_xml(self.settings)
142 # New formatting introduced in versions 2.7.3 and 3.2.3
143 if whitespace_fix:
144 expected = (generatedby + bodynewlines).encode('latin1')
145 else:
146 expected = (generatedby + bodynewlines_old).encode('latin1')
147 # Some distributions patch also earlier versions:
148 if (result != expected and not whitespace_fix):
149 expected = (generatedby + bodynewlines).encode('latin1')
151 self.assertEqual(result, expected)
154 if __name__ == '__main__':
155 import unittest
156 unittest.main()