4 # Author: Lea Wiemann <LeWiemann@gmail.com>
5 # Copyright: This module has been placed in the public domain.
8 Test for docutils XML writer.
11 from __init__
import DocutilsTestSupport
24 Test. \xe4\xf6\xfc\u20ac"""
26 xmldecl
= u
"""<?xml version="1.0" encoding="iso-8859-1"?>
30 <!DOCTYPE document PUBLIC "+//IDN docutils.sourceforge.net\
31 //DTD Docutils Generic//EN//XML"\
32 "http://docutils.sourceforge.net/docs/ref/docutils.dtd">
35 generatedby
= u
'<!-- Generated by Docutils %s -->\n' % docutils
.__version
__
38 <document source="<string>"><paragraph>Test</paragraph>\
39 <transition/><paragraph>Test. \xe4\xf6\xfc€</paragraph>\
43 <document source="<string>">
44 <paragraph>Test</paragraph>
46 <paragraph>Test. \xe4\xf6\xfc€</paragraph>
50 bodynewlines_old
= u
"""\
51 <document source="<string>">
57 Test. \xe4\xf6\xfc€
63 <document source="<string>">
64 <paragraph>Test</paragraph>
66 <paragraph>Test. \xe4\xf6\xfc€</paragraph>
70 bodyindents_old
= u
"""\
71 <document source="<string>">
77 Test. \xe4\xf6\xfc€
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
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:
108 if self
.settings
['xml_declaration']:
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
126 expected
= (generatedby
+ bodyindents
).encode('latin1')
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
144 expected
= (generatedby
+ bodynewlines
).encode('latin1')
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__':