1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#!/usr/bin/env python
# $Id: test_docutils_xml.py 5889 2009-04-01 20:00:21Z gbrandl $
# Author: Lea Wiemann <LeWiemann@gmail.com>
# Copyright: This module has been placed in the public domain.
"""
Test for docutils XML writer.
"""
from __init__ import DocutilsTestSupport
import sys
import docutils
import docutils.core
from docutils._compat import b
if sys.version_info[:3] in [(2, 7, 2), (3, 2, 2)]:
import xml.dom.minidom as minidom
if minidom.parseString('<x><y>z</y></x>').toprettyxml() == '<?xml version="1.0" ?>\n<x>\n\t<y>z</y>\n</x>\n':
sys_version_info = sys.version_info[:2] + (3, 'candidate', 0)
class sys:
version_info = sys_version_info
del sys_version_info
del minidom
class DocutilsXMLTestCase(DocutilsTestSupport.StandardTestCase):
input = b("""\
Test
----------
Test. \xc3\xa4\xc3\xb6\xc3\xbc\xe2\x82\xac""")
xmldecl = b('<?xml version="1.0" encoding="iso-8859-1"?>\n')
doctypedecl = b('<!DOCTYPE document PUBLIC "+//IDN docutils.sourceforge.net//DTD Docutils Generic//EN//XML" "http://docutils.sourceforge.net/docs/ref/docutils.dtd">\n')
generatedby = b('<!-- Generated by Docutils %s -->\n' % docutils.__version__)
bodynormal = b('<document source="<string>"><paragraph>Test</paragraph><transition/><paragraph>Test. \xe4\xf6\xfc€</paragraph></document>')
bodynewlines = b("""\
<document source="<string>">
<paragraph>Test</paragraph>
<transition/>
<paragraph>Test. \xe4\xf6\xfc€</paragraph>
</document>
""")
bodynewlines_old = b("""\
<document source="<string>">
<paragraph>
Test
</paragraph>
<transition/>
<paragraph>
Test. \xe4\xf6\xfc€
</paragraph>
</document>
""")
bodyindents = b("""\
<document source="<string>">
<paragraph>Test</paragraph>
<transition/>
<paragraph>Test. \xe4\xf6\xfc€</paragraph>
</document>
""")
bodyindents_old = b("""\
<document source="<string>">
<paragraph>
Test
</paragraph>
<transition/>
<paragraph>
Test. \xe4\xf6\xfc€
</paragraph>
</document>
""")
def test_publish(self):
settings = {'input_encoding': 'utf8',
'output_encoding': 'iso-8859-1',
'_disable_config': 1}
for settings['newlines'] in 0, 1:
for settings['indents'] in 0, 1:
for settings['xml_declaration'] in 0, 1:
for settings['doctype_declaration'] in 0, 1:
expected = b('')
if settings['xml_declaration']:
expected += self.xmldecl
if settings['doctype_declaration']:
expected += self.doctypedecl
expected += self.generatedby
if settings['indents']:
expected += self.bodyindents
elif settings['newlines']:
expected += self.bodynewlines
else:
expected += self.bodynormal
self.assertEqual(docutils.core.publish_string
(source=self.input,
reader_name='standalone',
writer_name='docutils_xml',
settings_overrides=settings),
expected)
# New formatting introduced in versions 2.7.3 and 3.2.3 on 2011-11-18
# to fix http://bugs.python.org/issue4147
if (sys.version_info < (2, 7, 3) or
sys.version_info[0] == 3 and sys.version_info < (3, 2, 3)):
bodynewlines = bodynewlines_old
bodyindents = bodyindents_old
if __name__ == '__main__':
import unittest
unittest.main()
|