#!/usr/bin/env python # $Id: test_docutils_xml.py 7966 2016-08-18 13:06:09Z 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. """ from StringIO import StringIO from __init__ import DocutilsTestSupport # must be imported before docutils import docutils import docutils.core # sample strings # -------------- source = u"""\ Test ---------- Test. \xe4\xf6\xfc\u20ac""" xmldecl = u""" """ doctypedecl = u"""\ """ generatedby = u'\n' % docutils.__version__ bodynormal = u"""\ Test\ Test. \xe4\xf6\xfc€\ """ bodynewlines = u"""\ Test Test. \xe4\xf6\xfc€ """ bodyindents = u"""\ Test Test. \xe4\xf6\xfc€ """ # raw XML # """"""" raw_xml_source = u"""\ .. raw:: xml Test \xe4\xf6\xfc\u20ac > < .. role:: xml(raw) :format: xml :xml:`inline raw XML`. """ raw_xml = u"""\ Test \xe4\xf6\xfc€ > < \ inline raw XML. """ invalid_raw_xml_source = u"""\ .. raw:: xml Test \xe4\xf6\xfc\u20ac .. role:: xml(raw) :format: xml :xml:`inline raw XML</test>`. """ invalid_raw_xml = u"""\ Test \xe4\xf6\xfc\u20ac \ inline raw XML</test>. """ def publish_xml(settings, source): return docutils.core.publish_string(source=source.encode('utf8'), reader_name='standalone', writer_name='docutils_xml', settings_overrides=settings) # XML Test Case # ------------- class DocutilsXMLTestCase(DocutilsTestSupport.StandardTestCase): settings = {'input_encoding': 'utf8', '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 = u'' if settings['xml_declaration']: expected += xmldecl if settings['doctype_declaration']: expected += doctypedecl expected += generatedby expected += bodynormal result = publish_xml(settings, source) self.assertEqual(result, expected.encode('latin1')) def test_publish_indents(self): settings = self.settings.copy() settings['indents'] = True result = publish_xml(settings, source) expected = (generatedby + bodyindents).encode('latin1') self.assertEqual(result, expected) def test_publish_newlines(self): settings = self.settings.copy() result = publish_xml(settings, source) expected = (generatedby + bodynewlines).encode('latin1') self.assertEqual(result, expected) def test_raw_xml(self): result = publish_xml(self.settings, raw_xml_source) expected = (generatedby + raw_xml).encode('latin1', 'xmlcharrefreplace') self.assertEqual(result, expected) 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(result, expected) warnings.seek(0) self.assertEqual(warnings.readlines(), [u':5: ' u'(WARNING/2) Invalid raw XML in column 2, line offset 3:\n', u'\n', u' Test \xe4\xf6\xfc\u20ac\n', u'\n', u':10: ' u'(WARNING/2) Invalid raw XML in column 30, line offset 1:\n', u'inline raw XML</test>\n']) # abort with SystemMessage if halt_level is "info": settings['halt_level'] = 2 settings['warning_stream'] = '' self.assertRaises(docutils.utils.SystemMessage, publish_xml, settings, invalid_raw_xml_source) if __name__ == '__main__': import unittest unittest.main()