Allow also non-ASCII whitespace characters around inline markup.
[docutils.git] / test / test_publisher.py
blob32e6f76aff3b497b809f4d56a4d8eb42d4a95c94
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 b, bytes, 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="{%s\'nonexistent\': [<reference: <#text: \'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
41 <title>
42 Test Document
43 <paragraph>
44 This is a test document with a broken reference: \n\
45 <problematic ids="id2" refid="id1">
46 nonexistent_
47 <section classes="system-messages">
48 <title>
49 Docutils System Messages
50 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
51 <paragraph>
52 Unknown target name: "nonexistent".
53 """ % u_prefix)
56 class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec):
58 settings_default_overrides = {
59 '_disable_config': 1,
60 'warning_stream': io.NullOutput()}
62 def test_publish_doctree(self):
63 # Test `publish_doctree` and `publish_from_doctree`.
65 # Produce the document tree.
66 doctree = core.publish_doctree(
67 source=test_document, reader_name='standalone',
68 parser_name='restructuredtext', settings_spec=self,
69 settings_overrides={'expose_internals':
70 ['refnames', 'do_not_expose'],
71 'report_level': 5})
72 self.assert_(isinstance(doctree, nodes.document))
74 # Confirm that transforms have been applied (in this case, the
75 # DocTitle transform):
76 self.assert_(isinstance(doctree[0], nodes.title))
77 self.assert_(isinstance(doctree[1], nodes.paragraph))
78 # Confirm that the Messages transform has not yet been applied:
79 self.assertEquals(len(doctree), 2)
81 # The `do_not_expose` attribute may not show up in the
82 # pseudoxml output because the expose_internals transform may
83 # not be applied twice.
84 doctree.do_not_expose = 'test'
85 # Write out the document:
86 output = core.publish_from_doctree(
87 doctree, writer_name='pseudoxml',
88 settings_spec=self,
89 settings_overrides={'expose_internals':
90 ['refnames', 'do_not_expose'],
91 'report_level': 1})
92 self.assertEquals(output, exposed_pseudoxml_output)
94 # Test publishing parts using document as the source.
95 parts = core.publish_parts(
96 reader_name='doctree', source_class=io.DocTreeInput,
97 source=doctree, source_path='test', writer_name='html',
98 settings_spec=self)
99 self.assert_(isinstance(parts, dict))
101 def test_publish_pickle(self):
102 # Test publishing a document tree with pickling and unpickling.
104 # Produce the document tree.
105 doctree = core.publish_doctree(
106 source=test_document,
107 reader_name='standalone',
108 parser_name='restructuredtext',
109 settings_spec=self)
110 self.assert_(isinstance(doctree, nodes.document))
112 # Pickle the document. Note: if this fails, some unpickleable
113 # reference has been added somewhere within the document tree.
114 # If so, you need to fix that.
116 # Note: Please do not remove this test, this is an important
117 # requirement, applications will be built on the assumption
118 # that we can pickle the document.
120 # Remove the reporter and the transformer before pickling.
121 doctree.reporter = None
122 doctree.transformer = None
124 doctree_pickled = pickle.dumps(doctree)
125 self.assert_(isinstance(doctree_pickled, bytes))
126 del doctree
128 # Unpickle the document.
129 doctree_zombie = pickle.loads(doctree_pickled)
130 self.assert_(isinstance(doctree_zombie, nodes.document))
132 # Write out the document:
133 output = core.publish_from_doctree(
134 doctree_zombie, writer_name='pseudoxml',
135 settings_spec=self)
136 self.assertEquals(output, pseudoxml_output)
139 if __name__ == '__main__':
140 import unittest
141 unittest.main()