~ubuntu-branches/ubuntu/karmic/python-docutils/karmic

« back to all changes in this revision

Viewing changes to test/test_publisher.py

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Author: Martin Blais
 
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.
 
8
 
 
9
"""
 
10
Test the `Publisher` facade and the ``publish_*`` convenience functions.
 
11
"""
 
12
 
 
13
import pickle
 
14
from types import StringType, DictType
 
15
import DocutilsTestSupport              # must be imported before docutils
 
16
import docutils
 
17
from docutils import core, nodes, io
 
18
 
 
19
 
 
20
test_document = """\
 
21
Test Document
 
22
=============
 
23
 
 
24
This is a test document with a broken reference: nonexistent_
 
25
"""
 
26
pseudoxml_output = """\
 
27
<document ids="test-document" names="test\ document" source="<string>" title="Test Document">
 
28
    <title>
 
29
        Test Document
 
30
    <paragraph>
 
31
        This is a test document with a broken reference: \n\
 
32
        <problematic ids="id2" refid="id1">
 
33
            nonexistent_
 
34
    <section classes="system-messages">
 
35
        <title>
 
36
            Docutils System Messages
 
37
        <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
 
38
            <paragraph>
 
39
                Unknown target name: "nonexistent".
 
40
"""
 
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">
 
43
    <title>
 
44
        Test Document
 
45
    <paragraph>
 
46
        This is a test document with a broken reference: \n\
 
47
        <problematic ids="id2" refid="id1">
 
48
            nonexistent_
 
49
    <section classes="system-messages">
 
50
        <title>
 
51
            Docutils System Messages
 
52
        <system_message backrefs="id2" ids="id1" level="3" line="4" source="<string>" type="ERROR">
 
53
            <paragraph>
 
54
                Unknown target name: "nonexistent".
 
55
"""
 
56
 
 
57
 
 
58
class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase, docutils.SettingsSpec):
 
59
 
 
60
    settings_default_overrides = {
 
61
        '_disable_config': 1,
 
62
        'warning_stream': io.NullOutput()}
 
63
 
 
64
    def test_publish_doctree(self):
 
65
        # Test `publish_doctree` and `publish_from_doctree`.
 
66
 
 
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'],
 
73
                                'report_level': 5})
 
74
        self.assert_(isinstance(doctree, nodes.document))
 
75
 
 
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)
 
82
 
 
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',
 
90
            settings_spec=self,
 
91
            settings_overrides={'expose_internals':
 
92
                                ['refnames', 'do_not_expose'],
 
93
                                'report_level': 1})
 
94
        self.assertEquals(output, exposed_pseudoxml_output)
 
95
 
 
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',
 
100
           settings_spec=self)
 
101
        self.assert_(isinstance(parts, DictType))
 
102
 
 
103
    def test_publish_pickle(self):
 
104
        # Test publishing a document tree with pickling and unpickling.
 
105
        
 
106
        # Produce the document tree.
 
107
        doctree = core.publish_doctree(
 
108
            source=test_document,
 
109
            reader_name='standalone',
 
110
            parser_name='restructuredtext',
 
111
            settings_spec=self)
 
112
        self.assert_(isinstance(doctree, nodes.document))
 
113
 
 
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.
 
117
        #
 
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.
 
121
 
 
122
        # Remove the reporter and the transformer before pickling.
 
123
        doctree.reporter = None
 
124
        doctree.transformer = None
 
125
 
 
126
        doctree_pickled = pickle.dumps(doctree)
 
127
        self.assert_(isinstance(doctree_pickled, StringType))
 
128
        del doctree
 
129
 
 
130
        # Unpickle the document.
 
131
        doctree_zombie = pickle.loads(doctree_pickled)
 
132
        self.assert_(isinstance(doctree_zombie, nodes.document))
 
133
 
 
134
        # Write out the document:
 
135
        output = core.publish_from_doctree(
 
136
            doctree_zombie, writer_name='pseudoxml',
 
137
            settings_spec=self)
 
138
        self.assertEquals(output, pseudoxml_output)
 
139
 
 
140
 
 
141
if __name__ == '__main__':
 
142
    import unittest
 
143
    unittest.main()