Prepare for python 3.0: minimize "types" module where possible (gbrandl).
[docutils.git] / test / test_publisher.py
bloba95158fb54070f20e9a07a977dfd81425b0761ac
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
17 test_document = """\
18 Test Document
19 =============
21 This is a test document with a broken reference: nonexistent_
22 """
23 pseudoxml_output = """\
24 <document ids="test-document" names="test\ document" source="<string>" title="Test Document">
25 <title>
26 Test Document
27 <paragraph>
28 This is a test document with a broken reference: \n\
29 <problematic ids="id2" refid="id1">
30 nonexistent_
31 <section classes="system-messages">
32 <title>
33 Docutils System Messages
34 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
35 <paragraph>
36 Unknown target name: "nonexistent".
37 """
38 exposed_pseudoxml_output = """\
39 <document ids="test-document" internal:refnames="{u\'nonexistent\': [<reference: <#text: \'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
40 <title>
41 Test Document
42 <paragraph>
43 This is a test document with a broken reference: \n\
44 <problematic ids="id2" refid="id1">
45 nonexistent_
46 <section classes="system-messages">
47 <title>
48 Docutils System Messages
49 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
50 <paragraph>
51 Unknown target name: "nonexistent".
52 """
55 class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec):
57 settings_default_overrides = {
58 '_disable_config': 1,
59 'warning_stream': io.NullOutput()}
61 def test_publish_doctree(self):
62 # Test `publish_doctree` and `publish_from_doctree`.
64 # Produce the document tree.
65 doctree = core.publish_doctree(
66 source=test_document, reader_name='standalone',
67 parser_name='restructuredtext', settings_spec=self,
68 settings_overrides={'expose_internals':
69 ['refnames', 'do_not_expose'],
70 'report_level': 5})
71 self.assert_(isinstance(doctree, nodes.document))
73 # Confirm that transforms have been applied (in this case, the
74 # DocTitle transform):
75 self.assert_(isinstance(doctree[0], nodes.title))
76 self.assert_(isinstance(doctree[1], nodes.paragraph))
77 # Confirm that the Messages transform has not yet been applied:
78 self.assertEquals(len(doctree), 2)
80 # The `do_not_expose` attribute may not show up in the
81 # pseudoxml output because the expose_internals transform may
82 # not be applied twice.
83 doctree.do_not_expose = 'test'
84 # Write out the document:
85 output = core.publish_from_doctree(
86 doctree, writer_name='pseudoxml',
87 settings_spec=self,
88 settings_overrides={'expose_internals':
89 ['refnames', 'do_not_expose'],
90 'report_level': 1})
91 self.assertEquals(output, exposed_pseudoxml_output)
93 # Test publishing parts using document as the source.
94 parts = core.publish_parts(
95 reader_name='doctree', source_class=io.DocTreeInput,
96 source=doctree, source_path='test', writer_name='html',
97 settings_spec=self)
98 self.assert_(isinstance(parts, dict))
100 def test_publish_pickle(self):
101 # Test publishing a document tree with pickling and unpickling.
103 # Produce the document tree.
104 doctree = core.publish_doctree(
105 source=test_document,
106 reader_name='standalone',
107 parser_name='restructuredtext',
108 settings_spec=self)
109 self.assert_(isinstance(doctree, nodes.document))
111 # Pickle the document. Note: if this fails, some unpickleable
112 # reference has been added somewhere within the document tree.
113 # If so, you need to fix that.
115 # Note: Please do not remove this test, this is an important
116 # requirement, applications will be built on the assumption
117 # that we can pickle the document.
119 # Remove the reporter and the transformer before pickling.
120 doctree.reporter = None
121 doctree.transformer = None
123 doctree_pickled = pickle.dumps(doctree)
124 self.assert_(isinstance(doctree_pickled, str))
125 del doctree
127 # Unpickle the document.
128 doctree_zombie = pickle.loads(doctree_pickled)
129 self.assert_(isinstance(doctree_zombie, nodes.document))
131 # Write out the document:
132 output = core.publish_from_doctree(
133 doctree_zombie, writer_name='pseudoxml',
134 settings_spec=self)
135 self.assertEquals(output, pseudoxml_output)
138 if __name__ == '__main__':
139 import unittest
140 unittest.main()