Apply [ 2714873 ] Fix for the overwritting of document attributes.
[docutils.git] / test / test_transforms / test_doctitle.py
blobc00113e63b1c1c9a0f8274f7dfe228f408c5d185
1 #! /usr/bin/env python
3 # $Id$
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
7 """
8 Tests for docutils.transforms.frontmatter.DocTitle.
9 """
11 from __init__ import DocutilsTestSupport
12 from docutils.transforms.frontmatter import DocTitle, SectionSubTitle
13 from docutils.parsers.rst import Parser, Directive
14 from docutils.parsers.rst.directives import register_directive
16 # dummy directive to test attribute merging:
17 class AddNameToDocumentTitle(Directive):
18 required_arguments = 0
19 optional_arguments = 0
20 final_argument_whitespace = True
21 option_spec = { }
22 has_content = False
24 def run(self):
25 document = self.state_machine.document
26 document['names'].append('Name')
27 return []
29 register_directive('add-name-to-title', AddNameToDocumentTitle)
31 def suite():
32 parser = Parser()
33 s = DocutilsTestSupport.TransformTestSuite(parser)
34 s.generateTests(totest)
35 return s
37 totest = {}
39 totest['section_headers'] = ((DocTitle, SectionSubTitle), [
40 ["""\
41 .. test title promotion
43 Title
44 =====
46 Paragraph.
47 """,
48 """\
49 <document ids="title" names="title" source="test data" title="Title">
50 <title>
51 Title
52 <comment xml:space="preserve">
53 test title promotion
54 <paragraph>
55 Paragraph.
56 """],
57 ["""\
58 Title
59 =====
60 Paragraph (no blank line).
61 """,
62 """\
63 <document ids="title" names="title" source="test data" title="Title">
64 <title>
65 Title
66 <paragraph>
67 Paragraph (no blank line).
68 """],
69 ["""\
70 Paragraph.
72 Title
73 =====
75 Paragraph.
76 """,
77 """\
78 <document source="test data">
79 <paragraph>
80 Paragraph.
81 <section ids="title" names="title">
82 <title>
83 Title
84 <paragraph>
85 Paragraph.
86 """],
87 ["""\
88 Title
89 =====
91 Subtitle
92 --------
94 .. title:: Another Title
96 Test title, subtitle, and title metadata.
97 """,
98 """\
99 <document ids="title" names="title" source="test data" title="Another Title">
100 <title>
101 Title
102 <subtitle ids="subtitle" names="subtitle">
103 Subtitle
104 <paragraph>
105 Test title, subtitle, and title metadata.
106 """],
107 ["""\
108 Title
109 ====
111 Test short underline.
112 """,
113 """\
114 <document ids="title" names="title" source="test data" title="Title">
115 <title>
116 Title
117 <system_message level="2" line="2" source="test data" type="WARNING">
118 <paragraph>
119 Title underline too short.
120 <literal_block xml:space="preserve">
121 Title
122 ====
123 <paragraph>
124 Test short underline.
125 """],
126 ["""\
127 =======
128 Long Title
129 =======
131 Test long title and space normalization.
132 The system_message should move after the document title
133 (it was before the beginning of the section).
134 """,
135 """\
136 <document ids="long-title" names="long\ title" source="test data" title="Long Title">
137 <title>
138 Long Title
139 <system_message level="2" line="1" source="test data" type="WARNING">
140 <paragraph>
141 Title overline too short.
142 <literal_block xml:space="preserve">
143 =======
144 Long Title
145 =======
146 <paragraph>
147 Test long title and space normalization.
148 The system_message should move after the document title
149 (it was before the beginning of the section).
150 """],
151 ["""\
152 .. Test multiple second-level titles.
154 Title 1
155 =======
156 Paragraph 1.
158 Title 2
159 -------
160 Paragraph 2.
162 Title 3
163 -------
164 Paragraph 3.
165 """,
166 """\
167 <document ids="title-1" names="title\ 1" source="test data" title="Title 1">
168 <title>
169 Title 1
170 <comment xml:space="preserve">
171 Test multiple second-level titles.
172 <paragraph>
173 Paragraph 1.
174 <section ids="title-2" names="title\ 2">
175 <title>
176 Title 2
177 <paragraph>
178 Paragraph 2.
179 <section ids="title-3" names="title\ 3">
180 <title>
181 Title 3
182 <paragraph>
183 Paragraph 3.
184 """],
185 ["""\
186 .. |foo| replace:: bar
188 .. _invisible target:
190 Title
191 =====
192 This title should be the document title despite the
193 substitution_definition.
194 """,
195 """\
196 <document ids="title" names="title" source="test data" title="Title">
197 <title>
198 Title
199 <substitution_definition names="foo">
201 <target ids="invisible-target" names="invisible\ target">
202 <paragraph>
203 This title should be the document title despite the
204 substitution_definition.
205 """],
206 ["""\
207 (Because of this paragraph, the following is not a doc title.)
209 ===============
210 Section Title
211 ===============
213 Subtitle
214 ========
216 -----------------
217 Another Section
218 -----------------
220 Another Subtitle
221 ----------------
223 """,
224 """\
225 <document source="test data">
226 <paragraph>
227 (Because of this paragraph, the following is not a doc title.)
228 <section ids="section-title" names="section\ title">
229 <title>
230 Section Title
231 <subtitle ids="subtitle" names="subtitle">
232 Subtitle
233 <section ids="another-section" names="another\ section">
234 <title>
235 Another Section
236 <subtitle ids="another-subtitle" names="another\ subtitle">
237 Another Subtitle
238 """],
239 ["""\
240 -----
241 Title
242 -----
244 This is a document, it flows nicely, so the attributes of it are at the
245 bottom.
247 .. add-name-to-title::
249 """,
250 """\
251 <document ids="title" names="Name title" source="test data" title="Title">
252 <title>
253 Title
254 <paragraph>
255 This is a document, it flows nicely, so the attributes of it are at the
256 bottom.
257 """]
261 if __name__ == '__main__':
262 import unittest
263 unittest.main(defaultTest='suite')