#!/usr/bin/env python3 # $Id: test_docutils_xml.py 9914 2024-08-21 13:45:08Z milde $ # Author: Lea Wiemann # Copyright: This module has been placed in the public domain. """ Test for docutils XML writer. .. Attention:: While the tests compare the output on the string-level, no guarantee is given against changes to identical XML representations like ```` vs. ````. The sample strings in this test module mirrors the current behaviour of the docutils_xml writer. """ import unittest import docutils import docutils.core from docutils.writers import docutils_xml from io import StringIO # sample strings # -------------- source = """\ Test ---------- Test. \xe4\xf6\xfc\u20ac""" xmldecl = """ """ doctypedecl = """\ """ generatedby = '\n' % docutils.__version__ bodynormal = """\ Test\ Test. \xe4\xf6\xfc€\ """ bodynewlines = """\ Test Test. \xe4\xf6\xfc€ """ bodyindents = """\ Test Test. \xe4\xf6\xfc€ """ # raw XML # """"""" raw_xml_source = """\ .. raw:: xml Test \xe4\xf6\xfc\u20ac > < .. role:: xml(raw) :format: xml :xml:`inline raw XML`. """ raw_xml = """\ Test \xe4\xf6\xfc€ > < \ inline raw XML. """ invalid_raw_xml_source = """\ .. raw:: xml Test \xe4\xf6\xfc\u20ac .. role:: xml(raw) :format: xml :xml:`inline raw XML</test>`. """ invalid_raw_xml = """\ Test \xe4\xf6\xfc\u20ac \ inline raw XML</test>. """ def publish_xml(settings, source): return docutils.core.publish_string(source=source, writer=docutils_xml.Writer(), settings_overrides=settings) # XML Test Case # ------------- class DocutilsXMLTestCase(unittest.TestCase): settings = {'input_encoding': 'utf-8', 'output_encoding': 'iso-8859-1', '_disable_config': True, 'indents': False, 'newlines': True, 'xml_declaration': False, 'doctype_declaration': False, } def test_publish(self): settings = self.settings.copy() settings['newlines'] = False for settings['xml_declaration'] in True, False: for settings['doctype_declaration'] in True, False: expected = '' if settings['xml_declaration']: expected += xmldecl if settings['doctype_declaration']: expected += doctypedecl expected += generatedby expected += bodynormal result = publish_xml(settings, source) self.assertEqual(expected.encode('latin1'), result) def test_publish_indents(self): settings = self.settings.copy() settings['indents'] = True result = publish_xml(settings, source) expected = (generatedby + bodyindents).encode('latin1') self.assertEqual(expected, result) def test_publish_newlines(self): settings = self.settings.copy() result = publish_xml(settings, source) expected = (generatedby + bodynewlines).encode('latin1') self.assertEqual(expected, result) def test_raw_xml(self): result = publish_xml(self.settings, raw_xml_source) expected = (generatedby + raw_xml).encode('latin1', 'xmlcharrefreplace') self.assertEqual(expected, result) def test_invalid_raw_xml(self): warnings = StringIO() settings = self.settings.copy() settings['warning_stream'] = warnings result = publish_xml(settings, invalid_raw_xml_source) expected = (generatedby + invalid_raw_xml).encode('latin1', 'xmlcharrefreplace') self.assertEqual(expected, result) warnings.seek(0) self.assertEqual( [':5: ' '(WARNING/2) Invalid raw XML in column 2, line offset 3:\n', '\n', ' Test \xe4\xf6\xfc\u20ac\n', '\n', ':10: ' '(WARNING/2) Invalid raw XML in column 30, line offset 1:\n', 'inline raw XML</test>\n'], warnings.readlines()) settings['halt_level'] = 2 # convert info messages to exceptions settings['warning_stream'] = '' with self.assertRaises(docutils.utils.SystemMessage): publish_xml(settings, invalid_raw_xml_source) if __name__ == '__main__': unittest.main()