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
21 This is a test document with a broken reference: nonexistent_
23 pseudoxml_output
= """\
24 <document ids="test-document" names="test\ document" source="<string>" title="Test Document">
28 This is a test document with a broken reference: \n\
29 <problematic ids="id2" refid="id1">
31 <section classes="system-messages">
33 Docutils System Messages
34 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
36 Unknown target name: "nonexistent".
38 exposed_pseudoxml_output
= """\
39 <document ids="test-document" internal:refnames="{u\'nonexistent\': [<reference: <#text: \'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
43 This is a test document with a broken reference: \n\
44 <problematic ids="id2" refid="id1">
46 <section classes="system-messages">
48 Docutils System Messages
49 <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
51 Unknown target name: "nonexistent".
55 class PublishDoctreeTestCase(DocutilsTestSupport
.StandardTestCase
, docutils
.SettingsSpec
):
57 settings_default_overrides
= {
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'],
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',
88 settings_overrides
={'expose_internals':
89 ['refnames', 'do_not_expose'],
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',
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',
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))
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',
135 self
.assertEquals(output
, pseudoxml_output
)
138 if __name__
== '__main__':