4 # Author: Martin Blais <blais@furius.ca>
5 # Copyright: This module has been placed in the public domain.
8 Test the `Publisher` facade and the ``publish_*`` convenience functions.
12 import DocutilsTestSupport
# must be imported before docutils
14 from docutils
import core
, nodes
, io
15 from docutils
._compat
import b
, bytes
, u_prefix
22 This is a test document with a broken reference: nonexistent_
24 pseudoxml_output
= b("""\
25 <document ids="test-document" names="test\ document" source="<string>" title="Test Document">
29 This is a test document with a broken reference: \n\
30 <problematic ids="id2" refid="id1">
32 <section classes="system-messages">
34 Docutils System Messages
35 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
37 Unknown target name: "nonexistent".
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">
44 This is a test document with a broken reference: \n\
45 <problematic ids="id2" refid="id1">
47 <section classes="system-messages">
49 Docutils System Messages
50 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
52 Unknown target name: "nonexistent".
56 class PublishDoctreeTestCase(DocutilsTestSupport
.StandardTestCase
, docutils
.SettingsSpec
):
58 settings_default_overrides
= {
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'],
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',
89 settings_overrides
={'expose_internals':
90 ['refnames', 'do_not_expose'],
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',
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',
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
))
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',
136 self
.assertEquals(output
, pseudoxml_output
)
139 if __name__
== '__main__':