Drop 2.4 and 2.5 compatibility code, part 2.
[docutils.git] / docutils / test / test_publisher.py
blobe0d904c6a0bdd4f55c3ab3a63f13103b9fd8402c
1 #!/usr/bin/env python
3 # $Id$
4 # Author: Martin Blais <blais@furius.ca>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Test the `Publisher` facade and the ``publish_*`` convenience functions.
9 """
11 import pickle
12 import DocutilsTestSupport # must be imported before docutils
13 import docutils
14 from docutils import core, nodes, io
15 from docutils._compat import u_prefix
18 test_document = """\
19 Test Document
20 =============
22 This is a test document with a broken reference: nonexistent_
23 """
24 pseudoxml_output = b"""\
25 <document ids="test-document" names="test\ document" source="<string>" title="Test Document">
26 <title>
27 Test Document
28 <paragraph>
29 This is a test document with a broken reference: \n\
30 <problematic ids="id2" refid="id1">
31 nonexistent_
32 <section classes="system-messages">
33 <title>
34 Docutils System Messages
35 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
36 <paragraph>
37 Unknown target name: "nonexistent".
38 """
39 exposed_pseudoxml_output = (b"""\
40 <document ids="test-document" internal:refnames="{"""
41 + u_prefix
42 + b"""\'nonexistent\': [<reference: <#text: \'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
43 <title>
44 Test Document
45 <paragraph>
46 This is a test document with a broken reference: \n\
47 <problematic ids="id2" refid="id1">
48 nonexistent_
49 <section classes="system-messages">
50 <title>
51 Docutils System Messages
52 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
53 <paragraph>
54 Unknown target name: "nonexistent".
55 """) # % u_prefix # %-expansion not supported in bytes in 3.3 and 3.4
58 class PublisherTests(DocutilsTestSupport.StandardTestCase):
60 def test_input_error_handling(self):
61 # core.publish_cmdline(argv=['nonexisting/path'])
62 # exits with a short message, if `traceback` is False,
64 # pass IOErrors to calling application if `traceback` is True
65 try:
66 core.publish_cmdline(argv=['nonexisting/path'],
67 settings_overrides={'traceback': True})
68 except IOError, e:
69 self.assertTrue(isinstance(e, io.InputError))
72 def test_output_error_handling(self):
73 # pass IOErrors to calling application if `traceback` is True
74 try:
75 core.publish_cmdline(argv=['data/include.txt', 'nonexisting/path'],
76 settings_overrides={'traceback': True})
77 except IOError, e:
78 self.assertTrue(isinstance(e, io.OutputError))
81 class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec):
83 settings_default_overrides = {
84 '_disable_config': True,
85 'warning_stream': io.NullOutput()}
87 def test_publish_doctree(self):
88 # Test `publish_doctree` and `publish_from_doctree`.
90 # Produce the document tree.
91 doctree = core.publish_doctree(
92 source=test_document, reader_name='standalone',
93 parser_name='restructuredtext', settings_spec=self,
94 settings_overrides={'expose_internals':
95 ['refnames', 'do_not_expose'],
96 'report_level': 5})
97 self.assertTrue(isinstance(doctree, nodes.document))
99 # Confirm that transforms have been applied (in this case, the
100 # DocTitle transform):
101 self.assertTrue(isinstance(doctree[0], nodes.title))
102 self.assertTrue(isinstance(doctree[1], nodes.paragraph))
103 # Confirm that the Messages transform has not yet been applied:
104 self.assertEqual(len(doctree), 2)
106 # The `do_not_expose` attribute may not show up in the
107 # pseudoxml output because the expose_internals transform may
108 # not be applied twice.
109 doctree.do_not_expose = 'test'
110 # Write out the document:
111 output = core.publish_from_doctree(
112 doctree, writer_name='pseudoxml',
113 settings_spec=self,
114 settings_overrides={'expose_internals':
115 ['refnames', 'do_not_expose'],
116 'report_level': 1})
117 self.assertEqual(output, exposed_pseudoxml_output)
119 # Test publishing parts using document as the source.
120 parts = core.publish_parts(
121 reader_name='doctree', source_class=io.DocTreeInput,
122 source=doctree, source_path='test', writer_name='html',
123 settings_spec=self)
124 self.assertTrue(isinstance(parts, dict))
126 def test_publish_pickle(self):
127 # Test publishing a document tree with pickling and unpickling.
129 # Produce the document tree.
130 doctree = core.publish_doctree(
131 source=test_document,
132 reader_name='standalone',
133 parser_name='restructuredtext',
134 settings_spec=self)
135 self.assertTrue(isinstance(doctree, nodes.document))
137 # Pickle the document. Note: if this fails, some unpickleable
138 # reference has been added somewhere within the document tree.
139 # If so, you need to fix that.
141 # Note: Please do not remove this test, this is an important
142 # requirement, applications will be built on the assumption
143 # that we can pickle the document.
145 # Remove the reporter and the transformer before pickling.
146 doctree.reporter = None
147 doctree.transformer = None
149 doctree_pickled = pickle.dumps(doctree)
150 self.assertTrue(isinstance(doctree_pickled, bytes))
151 del doctree
153 # Unpickle the document.
154 doctree_zombie = pickle.loads(doctree_pickled)
155 self.assertTrue(isinstance(doctree_zombie, nodes.document))
157 # Write out the document:
158 output = core.publish_from_doctree(
159 doctree_zombie, writer_name='pseudoxml',
160 settings_spec=self)
161 self.assertEqual(output, pseudoxml_output)
164 if __name__ == '__main__':
165 import unittest
166 unittest.main()