latex-writer: Handle class arguments for block-level elements
[docutils.git] / test / test_publisher.py
blob0bbd17f1a5cdcff44472d6ae84744fa117c66a62
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 PublisherTests(DocutilsTestSupport.StandardTestCase):
58 def test_input_error_handling(self):
59 # core.publish_cmdline(argv=['nonexisting/path'])
60 # exits with a short message, if `traceback` is False,
62 # pass IOErrors to calling application if `traceback` is True
63 try:
64 core.publish_cmdline(argv=['nonexisting/path'],
65 settings_overrides={'traceback': True})
66 except IOError, e:
67 self.assertTrue(isinstance(e, io.InputError))
70 def test_output_error_handling(self):
71 # pass IOErrors to calling application if `traceback` is True
72 try:
73 core.publish_cmdline(argv=['data/include.txt', 'nonexisting/path'],
74 settings_overrides={'traceback': True})
75 except IOError, e:
76 self.assertTrue(isinstance(e, io.OutputError))
79 class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec):
81 settings_default_overrides = {
82 '_disable_config': True,
83 'warning_stream': io.NullOutput()}
85 def test_publish_doctree(self):
86 # Test `publish_doctree` and `publish_from_doctree`.
88 # Produce the document tree.
89 doctree = core.publish_doctree(
90 source=test_document, reader_name='standalone',
91 parser_name='restructuredtext', settings_spec=self,
92 settings_overrides={'expose_internals':
93 ['refnames', 'do_not_expose'],
94 'report_level': 5})
95 self.assertTrue(isinstance(doctree, nodes.document))
97 # Confirm that transforms have been applied (in this case, the
98 # DocTitle transform):
99 self.assertTrue(isinstance(doctree[0], nodes.title))
100 self.assertTrue(isinstance(doctree[1], nodes.paragraph))
101 # Confirm that the Messages transform has not yet been applied:
102 self.assertEqual(len(doctree), 2)
104 # The `do_not_expose` attribute may not show up in the
105 # pseudoxml output because the expose_internals transform may
106 # not be applied twice.
107 doctree.do_not_expose = 'test'
108 # Write out the document:
109 output = core.publish_from_doctree(
110 doctree, writer_name='pseudoxml',
111 settings_spec=self,
112 settings_overrides={'expose_internals':
113 ['refnames', 'do_not_expose'],
114 'report_level': 1})
115 self.assertEqual(output, exposed_pseudoxml_output)
117 # Test publishing parts using document as the source.
118 parts = core.publish_parts(
119 reader_name='doctree', source_class=io.DocTreeInput,
120 source=doctree, source_path='test', writer_name='html',
121 settings_spec=self)
122 self.assertTrue(isinstance(parts, dict))
124 def test_publish_pickle(self):
125 # Test publishing a document tree with pickling and unpickling.
127 # Produce the document tree.
128 doctree = core.publish_doctree(
129 source=test_document,
130 reader_name='standalone',
131 parser_name='restructuredtext',
132 settings_spec=self)
133 self.assertTrue(isinstance(doctree, nodes.document))
135 # Pickle the document. Note: if this fails, some unpickleable
136 # reference has been added somewhere within the document tree.
137 # If so, you need to fix that.
139 # Note: Please do not remove this test, this is an important
140 # requirement, applications will be built on the assumption
141 # that we can pickle the document.
143 # Remove the reporter and the transformer before pickling.
144 doctree.reporter = None
145 doctree.transformer = None
147 doctree_pickled = pickle.dumps(doctree)
148 self.assertTrue(isinstance(doctree_pickled, bytes))
149 del doctree
151 # Unpickle the document.
152 doctree_zombie = pickle.loads(doctree_pickled)
153 self.assertTrue(isinstance(doctree_zombie, nodes.document))
155 # Write out the document:
156 output = core.publish_from_doctree(
157 doctree_zombie, writer_name='pseudoxml',
158 settings_spec=self)
159 self.assertEqual(output, pseudoxml_output)
162 if __name__ == '__main__':
163 import unittest
164 unittest.main()