4
# Contact: blais@furius.ca
5
# Revision: $Revision: 4132 $
6
# Date: $Date: 2005-12-03 03:13:12 +0100 (Sat, 03 Dec 2005) $
7
# Copyright: This module has been placed in the public domain.
10
Test the `Publisher` facade and the ``publish_*`` convenience functions.
14
from types import StringType, DictType
15
import DocutilsTestSupport # must be imported before docutils
17
from docutils import core, nodes, io
24
This is a test document with a broken reference: nonexistent_
26
pseudoxml_output = """\
27
<document ids="test-document" names="test\ document" source="<string>" title="Test Document">
31
This is a test document with a broken reference: \n\
32
<problematic ids="id2" refid="id1">
34
<section classes="system-messages">
36
Docutils System Messages
37
<system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
39
Unknown target name: "nonexistent".
41
exposed_pseudoxml_output = """\
42
<document ids="test-document" internal:refnames="{u\'nonexistent\': [<reference: <#text: u\'nonexistent\'>>]}" names="test\ document" source="<string>" title="Test Document">
46
This is a test document with a broken reference: \n\
47
<problematic ids="id2" refid="id1">
49
<section classes="system-messages">
51
Docutils System Messages
52
<system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
54
Unknown target name: "nonexistent".
58
class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec):
60
settings_default_overrides = {
62
'warning_stream': io.NullOutput()}
64
def test_publish_doctree(self):
65
# Test `publish_doctree` and `publish_from_doctree`.
67
# Produce the document tree.
68
doctree = core.publish_doctree(
69
source=test_document, reader_name='standalone',
70
parser_name='restructuredtext', settings_spec=self,
71
settings_overrides={'expose_internals':
72
['refnames', 'do_not_expose'],
74
self.assert_(isinstance(doctree, nodes.document))
76
# Confirm that transforms have been applied (in this case, the
77
# DocTitle transform):
78
self.assert_(isinstance(doctree[0], nodes.title))
79
self.assert_(isinstance(doctree[1], nodes.paragraph))
80
# Confirm that the Messages transform has not yet been applied:
81
self.assertEquals(len(doctree), 2)
83
# The `do_not_expose` attribute may not show up in the
84
# pseudoxml output because the expose_internals transform may
85
# not be applied twice.
86
doctree.do_not_expose = 'test'
87
# Write out the document:
88
output = core.publish_from_doctree(
89
doctree, writer_name='pseudoxml',
91
settings_overrides={'expose_internals':
92
['refnames', 'do_not_expose'],
94
self.assertEquals(output, exposed_pseudoxml_output)
96
# Test publishing parts using document as the source.
97
parts = core.publish_parts(
98
reader_name='doctree', source_class=io.DocTreeInput,
99
source=doctree, source_path='test', writer_name='html',
101
self.assert_(isinstance(parts, DictType))
103
def test_publish_pickle(self):
104
# Test publishing a document tree with pickling and unpickling.
106
# Produce the document tree.
107
doctree = core.publish_doctree(
108
source=test_document,
109
reader_name='standalone',
110
parser_name='restructuredtext',
112
self.assert_(isinstance(doctree, nodes.document))
114
# Pickle the document. Note: if this fails, some unpickleable
115
# reference has been added somewhere within the document tree.
116
# If so, you need to fix that.
118
# Note: Please do not remove this test, this is an important
119
# requirement, applications will be built on the assumption
120
# that we can pickle the document.
122
# Remove the reporter and the transformer before pickling.
123
doctree.reporter = None
124
doctree.transformer = None
126
doctree_pickled = pickle.dumps(doctree)
127
self.assert_(isinstance(doctree_pickled, StringType))
130
# Unpickle the document.
131
doctree_zombie = pickle.loads(doctree_pickled)
132
self.assert_(isinstance(doctree_zombie, nodes.document))
134
# Write out the document:
135
output = core.publish_from_doctree(
136
doctree_zombie, writer_name='pseudoxml',
138
self.assertEquals(output, pseudoxml_output)
141
if __name__ == '__main__':