Debian Codegraph @

test/test_transforms/test_doctitle.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_doctitle.py 10308 2026-03-28 18:54:25Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for docutils.transforms.frontmatter.DocTitle.
9 """
10
11 from pathlib import Path
12 import sys
13 import unittest
14
15 if __name__ == '__main__':
16 # prepend the "docutils root" to the Python library path
17 # so we import the local `docutils` package.
18 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser, Directive
22 from docutils.parsers.rst.directives import register_directive
23 from docutils.transforms.frontmatter import DocTitle, SectionSubTitle
24 from docutils.transforms.universal import TestMessages
25 from docutils.utils import new_document
26
27
28 # dummy directive to test attribute merging:
29 class AddNameToDocumentTitle(Directive):
30 required_arguments = 0
31 optional_arguments = 0
32 final_argument_whitespace = True
33 option_spec = {}
34 has_content = False
35
36 def run(self):
37 document = self.state_machine.document
38 document['names'].append('Name')
39 return []
40
41
42 class TransformTestCase(unittest.TestCase):
43
44 maxDiff = None
45
46 def test_transforms(self):
47 parser = Parser()
48 settings = get_default_settings(Parser)
49 settings.legacy_ids = False
50 settings.warning_stream = ''
51 register_directive('add-name-to-title', AddNameToDocumentTitle)
52 for name, (transforms, cases) in totest.items():
53 for casenum, (case_input, case_expected) in enumerate(cases):
54 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
55 document = new_document('test data', settings.copy())
56 parser.parse(case_input, document)
57 # Don't do a ``populate_from_components()`` because that
58 # would enable the Transformer's default transforms.
59 document.transformer.add_transforms(transforms)
60 document.transformer.add_transform(TestMessages)
61 document.transformer.apply_transforms()
62 output = document.pformat()
63 self.assertEqual(case_expected, output)
64
65
66 totest = {}
67
68 totest['section_headers'] = ((DocTitle, SectionSubTitle), [
69 ["""\
70 .. test title promotion
71
72 Title
73 =====
74
75 Paragraph.
76 """,
77 """\
78 <document names="title" source="test data" title="Title">
79 <title>
80 Title
81 <comment xml:space="preserve">
82 test title promotion
83 <paragraph>
84 Paragraph.
85 """],
86 ["""\
87 Title
88 =====
89 Paragraph (no blank line).
90 """,
91 """\
92 <document names="title" source="test data" title="Title">
93 <title>
94 Title
95 <paragraph>
96 Paragraph (no blank line).
97 """],
98 ["""\
99 Paragraph.
100
101 Title
102 =====
103
104 Paragraph.
105 """,
106 """\
107 <document source="test data">
108 <paragraph>
109 Paragraph.
110 <section names="title">
111 <title>
112 Title
113 <paragraph>
114 Paragraph.
115 """],
116 ["""\
117 Title
118 =====
119
120 Subtitle
121 --------
122
123 .. title:: Another Title
124
125 Test title, subtitle, and title metadata.
126 """,
127 """\
128 <document names="title" source="test data" title="Another Title">
129 <title>
130 Title
131 <subtitle names="subtitle">
132 Subtitle
133 <paragraph>
134 Test title, subtitle, and title metadata.
135 """],
136 ["""\
137 Title
138 ====
139
140 Test short underline.
141 """,
142 """\
143 <document names="title" source="test data" title="Title">
144 <title>
145 Title
146 <system_message level="2" line="2" source="test data" type="WARNING">
147 <paragraph>
148 Title underline too short.
149 <literal_block xml:space="preserve">
150 Title
151 ====
152 <paragraph>
153 Test short underline.
154 """],
155 ["""\
156 =======
157 Long Title
158 =======
159
160 Test long title and space normalization.
161 The system_message should move after the document title
162 (it was before the beginning of the section).
163 """,
164 """\
165 <document names="long\\ title" source="test data" title="Long Title">
166 <title>
167 Long Title
168 <system_message level="2" line="1" source="test data" type="WARNING">
169 <paragraph>
170 Title overline too short.
171 <literal_block xml:space="preserve">
172 =======
173 Long Title
174 =======
175 <paragraph>
176 Test long title and space normalization.
177 The system_message should move after the document title
178 (it was before the beginning of the section).
179 """],
180 ["""\
181 .. Test multiple second-level titles.
182
183 Title 1
184 =======
185 Paragraph 1.
186
187 Title 2
188 -------
189 Paragraph 2.
190
191 Title 3
192 -------
193 Paragraph 3.
194 """,
195 """\
196 <document names="title\\ 1" source="test data" title="Title 1">
197 <title>
198 Title 1
199 <comment xml:space="preserve">
200 Test multiple second-level titles.
201 <paragraph>
202 Paragraph 1.
203 <section names="title\\ 2">
204 <title>
205 Title 2
206 <paragraph>
207 Paragraph 2.
208 <section names="title\\ 3">
209 <title>
210 Title 3
211 <paragraph>
212 Paragraph 3.
213 """],
214 ["""\
215 .. |foo| replace:: bar
216
217 .. _invisible target:
218
219 Title
220 =====
221 This title should be the document title despite the
222 substitution_definition.
223 """,
224 """\
225 <document names="title" source="test data" title="Title">
226 <title>
227 Title
228 <substitution_definition names="foo">
229 bar
230 <target ids="invisible-target" names="invisible\\ target">
231 <paragraph>
232 This title should be the document title despite the
233 substitution_definition.
234 """],
235 ["""\
236 (Because of this paragraph, the following is not a doc title.)
237
238 ===============
239 Section Title
240 ===============
241
242 Subtitle
243 ========
244
245 -----------------
246 Another Section
247 -----------------
248
249 Another Subtitle
250 ----------------
251
252 """,
253 """\
254 <document source="test data">
255 <paragraph>
256 (Because of this paragraph, the following is not a doc title.)
257 <section names="section\\ title">
258 <title>
259 Section Title
260 <subtitle names="subtitle">
261 Subtitle
262 <section names="another\\ section">
263 <title>
264 Another Section
265 <subtitle names="another\\ subtitle">
266 Another Subtitle
267 """],
268 ["""\
269 -----
270 Title
271 -----
272
273 This is a document, it flows nicely, so the attributes of it are at the
274 bottom.
275
276 .. add-name-to-title::
277
278 """,
279 """\
280 <document names="Name title" source="test data" title="Title">
281 <title>
282 Title
283 <paragraph>
284 This is a document, it flows nicely, so the attributes of it are at the
285 bottom.
286 """]
287 ])
288
289
290 if __name__ == '__main__':
291 unittest.main()