| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
Test for docutils XML writer. |
| 9 |
|
| 10 |
.. Attention:: |
| 11 |
While the tests compare the output on the string-level, no guarantee |
| 12 |
is given against changes to identical XML representations like |
| 13 |
``<empty></empty>`` vs. ``<empty/>``. The sample strings in this test |
| 14 |
module mirrors the current behaviour of the docutils_xml writer. |
| 15 |
""" |
| 16 |
|
| 17 |
|
| 18 |
import unittest |
| 19 |
|
| 20 |
import docutils |
| 21 |
import docutils.core |
| 22 |
from docutils.writers import docutils_xml |
| 23 |
|
| 24 |
from io import StringIO |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
source = """\ |
| 30 |
Test |
| 31 |
|
| 32 |
---------- |
| 33 |
|
| 34 |
Test. \xe4\xf6\xfc\u20ac""" |
| 35 |
|
| 36 |
xmldecl = """<?xml version="1.0" encoding="iso-8859-1"?> |
| 37 |
""" |
| 38 |
|
| 39 |
doctypedecl = """\ |
| 40 |
<!DOCTYPE document PUBLIC "+//IDN docutils.sourceforge.net\ |
| 41 |
//DTD Docutils Generic//EN//XML"\ |
| 42 |
"http://docutils.sourceforge.net/docs/ref/docutils.dtd"> |
| 43 |
""" |
| 44 |
|
| 45 |
generatedby = '<!-- Generated by Docutils %s -->\n' % docutils.__version__ |
| 46 |
|
| 47 |
bodynormal = """\ |
| 48 |
<document source="<string>"><paragraph>Test</paragraph>\ |
| 49 |
<transition></transition><paragraph>Test. \xe4\xf6\xfc€</paragraph>\ |
| 50 |
</document>""" |
| 51 |
|
| 52 |
bodynewlines = """\ |
| 53 |
<document source="<string>"> |
| 54 |
<paragraph>Test</paragraph> |
| 55 |
<transition></transition> |
| 56 |
<paragraph>Test. \xe4\xf6\xfc€</paragraph> |
| 57 |
</document> |
| 58 |
""" |
| 59 |
|
| 60 |
bodyindents = """\ |
| 61 |
<document source="<string>"> |
| 62 |
<paragraph>Test</paragraph> |
| 63 |
<transition></transition> |
| 64 |
<paragraph>Test. \xe4\xf6\xfc€</paragraph> |
| 65 |
</document> |
| 66 |
""" |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
raw_xml_source = """\ |
| 72 |
.. raw:: xml |
| 73 |
|
| 74 |
<root> |
| 75 |
<child>Test \xe4\xf6\xfc\u20ac</child> |
| 76 |
> |
| 77 |
< |
| 78 |
|
| 79 |
</root> |
| 80 |
|
| 81 |
.. role:: xml(raw) |
| 82 |
:format: xml |
| 83 |
|
| 84 |
:xml:`<test>inline raw XML</test>`. |
| 85 |
""" |
| 86 |
|
| 87 |
raw_xml = """\ |
| 88 |
<document source="<string>"> |
| 89 |
<raw format="xml" xml:space="preserve"><root> |
| 90 |
<child>Test \xe4\xf6\xfc€</child> |
| 91 |
> |
| 92 |
< |
| 93 |
|
| 94 |
</root></raw> |
| 95 |
<paragraph><raw classes="xml" format="xml" xml:space="preserve">\ |
| 96 |
<test>inline raw XML</test></raw>.</paragraph> |
| 97 |
</document> |
| 98 |
""" |
| 99 |
|
| 100 |
invalid_raw_xml_source = """\ |
| 101 |
.. raw:: xml |
| 102 |
|
| 103 |
<root> |
| 104 |
<child>Test \xe4\xf6\xfc\u20ac</child> |
| 105 |
</mismatch> |
| 106 |
|
| 107 |
.. role:: xml(raw) |
| 108 |
:format: xml |
| 109 |
|
| 110 |
:xml:`<test>inline raw XML</test>`. |
| 111 |
""" |
| 112 |
|
| 113 |
invalid_raw_xml = """\ |
| 114 |
<document source="<string>"> |
| 115 |
<raw format="xml" xml:space="preserve"><root> |
| 116 |
<child>Test \xe4\xf6\xfc\u20ac</child> |
| 117 |
</mismatch></raw> |
| 118 |
<paragraph><raw classes="xml" format="xml" xml:space="preserve">\ |
| 119 |
<test>inline raw XML</test></raw>.</paragraph> |
| 120 |
</document> |
| 121 |
""" |
| 122 |
|
| 123 |
|
| 124 |
def publish_xml(settings, source): |
| 125 |
return docutils.core.publish_string(source=source, |
| 126 |
writer=docutils_xml.Writer(), |
| 127 |
settings_overrides=settings) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
class DocutilsXMLTestCase(unittest.TestCase): |
| 135 |
|
| 136 |
settings = {'input_encoding': 'utf-8', |
| 137 |
'output_encoding': 'iso-8859-1', |
| 138 |
'_disable_config': True, |
| 139 |
'indents': False, |
| 140 |
'newlines': True, |
| 141 |
'xml_declaration': False, |
| 142 |
'doctype_declaration': False, |
| 143 |
} |
| 144 |
|
| 145 |
def test_publish(self): |
| 146 |
settings = self.settings.copy() |
| 147 |
settings['newlines'] = False |
| 148 |
for settings['xml_declaration'] in True, False: |
| 149 |
for settings['doctype_declaration'] in True, False: |
| 150 |
expected = '' |
| 151 |
if settings['xml_declaration']: |
| 152 |
expected += xmldecl |
| 153 |
if settings['doctype_declaration']: |
| 154 |
expected += doctypedecl |
| 155 |
expected += generatedby |
| 156 |
expected += bodynormal |
| 157 |
result = publish_xml(settings, source) |
| 158 |
self.assertEqual(expected.encode('latin1'), result) |
| 159 |
|
| 160 |
def test_publish_indents(self): |
| 161 |
settings = self.settings.copy() |
| 162 |
settings['indents'] = True |
| 163 |
result = publish_xml(settings, source) |
| 164 |
expected = (generatedby + bodyindents).encode('latin1') |
| 165 |
self.assertEqual(expected, result) |
| 166 |
|
| 167 |
def test_publish_newlines(self): |
| 168 |
settings = self.settings.copy() |
| 169 |
result = publish_xml(settings, source) |
| 170 |
expected = (generatedby + bodynewlines).encode('latin1') |
| 171 |
self.assertEqual(expected, result) |
| 172 |
|
| 173 |
def test_raw_xml(self): |
| 174 |
result = publish_xml(self.settings, raw_xml_source) |
| 175 |
expected = (generatedby |
| 176 |
+ raw_xml).encode('latin1', 'xmlcharrefreplace') |
| 177 |
self.assertEqual(expected, result) |
| 178 |
|
| 179 |
def test_invalid_raw_xml(self): |
| 180 |
warnings = StringIO() |
| 181 |
settings = self.settings.copy() |
| 182 |
settings['warning_stream'] = warnings |
| 183 |
result = publish_xml(settings, invalid_raw_xml_source) |
| 184 |
expected = (generatedby |
| 185 |
+ invalid_raw_xml).encode('latin1', 'xmlcharrefreplace') |
| 186 |
self.assertEqual(expected, result) |
| 187 |
warnings.seek(0) |
| 188 |
self.assertEqual( |
| 189 |
['<string>:5: ' |
| 190 |
'(WARNING/2) Invalid raw XML in column 2, line offset 3:\n', |
| 191 |
'<root>\n', |
| 192 |
' <child>Test \xe4\xf6\xfc\u20ac</child>\n', |
| 193 |
'</mismatch>\n', |
| 194 |
'<string>:10: ' |
| 195 |
'(WARNING/2) Invalid raw XML in column 30, line offset 1:\n', |
| 196 |
'<test>inline raw XML</test>\n'], |
| 197 |
warnings.readlines()) |
| 198 |
settings['halt_level'] = 2 |
| 199 |
settings['warning_stream'] = '' |
| 200 |
with self.assertRaises(docutils.utils.SystemMessage): |
| 201 |
publish_xml(settings, invalid_raw_xml_source) |
| 202 |
|
| 203 |
|
| 204 |
if __name__ == '__main__': |
| 205 |
unittest.main() |