Debian Codegraph @

test/test_transforms/test_contents.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_contents.py 10303 2026-03-27 08:41:10Z 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.parts.Contents` (via
9 `docutils.transforms.universal.LastReaderPending`).
10 """
11
12 from pathlib import Path
13 import sys
14 import unittest
15
16 if __name__ == '__main__':
17 # prepend the "docutils root" to the Python library path
18 # so we import the local `docutils` package.
19 sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
20
21 from docutils.frontend import get_default_settings
22 from docutils.parsers.rst import Parser
23 from docutils.transforms.references import SectionIDs, Substitutions
24 from docutils.transforms.universal import TestMessages
25 from docutils.utils import new_document
26
27
28 class TransformTestCase(unittest.TestCase):
29 def test_transforms(self):
30 parser = Parser()
31 settings = get_default_settings(Parser)
32 settings.warning_stream = ''
33 for name, (transforms, cases) in totest.items():
34 for casenum, (case_input, case_expected) in enumerate(cases):
35 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
36 document = new_document('test data', settings.copy())
37 parser.parse(case_input, document)
38 # Don't do a ``populate_from_components()`` because that
39 # would enable the Transformer's default transforms.
40 document.transformer.add_transforms(transforms)
41 document.transformer.add_transform(TestMessages)
42 document.transformer.apply_transforms()
43 output = document.pformat()
44 self.assertEqual(case_expected, output)
45
46
47 totest = {}
48
49 totest['tables_of_contents'] = ((SectionIDs, Substitutions,), [
50 ["""\
51 .. contents::
52
53 Title 1
54 =======
55 Paragraph 1.
56
57 Title_ 2
58 --------
59 Paragraph 2.
60
61 _`Title` 3
62 ``````````
63 Paragraph 3.
64
65 Title 4
66 -------
67 Paragraph 4.
68 """,
69 """\
70 <document source="test data">
71 <topic classes="contents" ids="contents" names="contents">
72 <title>
73 Contents
74 <bullet_list>
75 <list_item>
76 <paragraph>
77 <reference ids="toc-entry-1" refid="title-1">
78 Title 1
79 <bullet_list>
80 <list_item>
81 <paragraph>
82 <reference ids="toc-entry-2" refid="title-2">
83 Title
84 2
85 <bullet_list>
86 <list_item>
87 <paragraph>
88 <reference ids="toc-entry-3" refid="title-3">
89 Title
90 3
91 <list_item>
92 <paragraph>
93 <reference ids="toc-entry-4" refid="title-4">
94 Title 4
95 <section ids="title-1" names="title\\ 1">
96 <title refid="toc-entry-1">
97 Title 1
98 <paragraph>
99 Paragraph 1.
100 <section ids="title-2" names="title\\ 2">
101 <title>
102 <reference name="Title" refname="title">
103 Title
104 2
105 <paragraph>
106 Paragraph 2.
107 <section ids="title-3" names="title\\ 3">
108 <title refid="toc-entry-3">
109 <target ids="title" names="title">
110 Title
111 3
112 <paragraph>
113 Paragraph 3.
114 <section ids="title-4" names="title\\ 4">
115 <title refid="toc-entry-4">
116 Title 4
117 <paragraph>
118 Paragraph 4.
119 """],
120 ["""\
121 .. contents:: Table of Contents
122
123 Title 1
124 =======
125 Paragraph 1.
126
127 Title 2
128 -------
129 Paragraph 2.
130 """,
131 """\
132 <document source="test data">
133 <topic classes="contents" ids="table-of-contents" names="table\\ of\\ contents">
134 <title>
135 Table of Contents
136 <bullet_list>
137 <list_item>
138 <paragraph>
139 <reference ids="toc-entry-1" refid="title-1">
140 Title 1
141 <bullet_list>
142 <list_item>
143 <paragraph>
144 <reference ids="toc-entry-2" refid="title-2">
145 Title 2
146 <section ids="title-1" names="title\\ 1">
147 <title refid="toc-entry-1">
148 Title 1
149 <paragraph>
150 Paragraph 1.
151 <section ids="title-2" names="title\\ 2">
152 <title refid="toc-entry-2">
153 Title 2
154 <paragraph>
155 Paragraph 2.
156 """],
157 ["""\
158 .. contents:: There's an image in Title 2
159
160 Title 1
161 =======
162 Paragraph 1.
163
164 |Title 2|
165 =========
166 Paragraph 2.
167
168 .. |Title 2| image:: title2.png
169 """,
170 """\
171 <document source="test data">
172 <topic classes="contents" ids="there-s-an-image-in-title-2" names="there's\\ an\\ image\\ in\\ title\\ 2">
173 <title>
174 There's an image in Title 2
175 <bullet_list>
176 <list_item>
177 <paragraph>
178 <reference ids="toc-entry-1" refid="title-1">
179 Title 1
180 <list_item>
181 <paragraph>
182 <reference ids="toc-entry-2" refid="title-2">
183 Title 2
184 <section ids="title-1" names="title\\ 1">
185 <title refid="toc-entry-1">
186 Title 1
187 <paragraph>
188 Paragraph 1.
189 <section ids="title-2" names="title\\ 2">
190 <title refid="toc-entry-2">
191 <image alt="Title 2" uri="title2.png">
192 <paragraph>
193 Paragraph 2.
194 <substitution_definition names="Title\\ 2">
195 <image alt="Title 2" uri="title2.png">
196 """], # emacs cruft: "
197 ["""\
198 .. contents::
199 :depth: 2
200
201 Title 1
202 =======
203 Paragraph 1.
204
205 Title 2
206 -------
207 Paragraph 2.
208
209 Title 3
210 ```````
211 Paragraph 3.
212
213 Title 4
214 -------
215 Paragraph 4.
216 """,
217 """\
218 <document source="test data">
219 <topic classes="contents" ids="contents" names="contents">
220 <title>
221 Contents
222 <bullet_list>
223 <list_item>
224 <paragraph>
225 <reference ids="toc-entry-1" refid="title-1">
226 Title 1
227 <bullet_list>
228 <list_item>
229 <paragraph>
230 <reference ids="toc-entry-2" refid="title-2">
231 Title 2
232 <list_item>
233 <paragraph>
234 <reference ids="toc-entry-3" refid="title-4">
235 Title 4
236 <section ids="title-1" names="title\\ 1">
237 <title refid="toc-entry-1">
238 Title 1
239 <paragraph>
240 Paragraph 1.
241 <section ids="title-2" names="title\\ 2">
242 <title refid="toc-entry-2">
243 Title 2
244 <paragraph>
245 Paragraph 2.
246 <section ids="title-3" names="title\\ 3">
247 <title>
248 Title 3
249 <paragraph>
250 Paragraph 3.
251 <section ids="title-4" names="title\\ 4">
252 <title refid="toc-entry-3">
253 Title 4
254 <paragraph>
255 Paragraph 4.
256 """],
257 ["""\
258 Title 1
259 =======
260
261 .. contents::
262 :local:
263
264 Paragraph 1.
265
266 Title 2
267 -------
268 Paragraph 2.
269
270 Title 3
271 ```````
272 Paragraph 3.
273
274 Title 4
275 -------
276 Paragraph 4.
277 """,
278 """\
279 <document source="test data">
280 <section ids="title-1" names="title\\ 1">
281 <title>
282 Title 1
283 <topic classes="contents local" ids="contents" names="contents">
284 <bullet_list>
285 <list_item>
286 <paragraph>
287 <reference ids="toc-entry-1" refid="title-2">
288 Title 2
289 <bullet_list>
290 <list_item>
291 <paragraph>
292 <reference ids="toc-entry-2" refid="title-3">
293 Title 3
294 <list_item>
295 <paragraph>
296 <reference ids="toc-entry-3" refid="title-4">
297 Title 4
298 <paragraph>
299 Paragraph 1.
300 <section ids="title-2" names="title\\ 2">
301 <title refid="toc-entry-1">
302 Title 2
303 <paragraph>
304 Paragraph 2.
305 <section ids="title-3" names="title\\ 3">
306 <title refid="toc-entry-2">
307 Title 3
308 <paragraph>
309 Paragraph 3.
310 <section ids="title-4" names="title\\ 4">
311 <title refid="toc-entry-3">
312 Title 4
313 <paragraph>
314 Paragraph 4.
315 """],
316 ["""\
317 .. contents::
318 :local:
319
320 Test duplicate name "Contents".
321
322 Section
323 --------
324 Paragraph.
325 """,
326 """\
327 <document source="test data">
328 <topic classes="contents local" ids="contents" names="contents">
329 <bullet_list>
330 <list_item>
331 <paragraph>
332 <reference ids="toc-entry-1" refid="section">
333 Section
334 <paragraph>
335 Test duplicate name "Contents".
336 <section ids="section" names="section">
337 <title refid="toc-entry-1">
338 Section
339 <paragraph>
340 Paragraph.
341 """],
342 ["""\
343 .. contents::
344 :backlinks: top
345
346 Section
347 --------
348 Paragraph.
349 """,
350 """\
351 <document source="test data">
352 <topic classes="contents" ids="contents" names="contents">
353 <title>
354 Contents
355 <bullet_list>
356 <list_item>
357 <paragraph>
358 <reference ids="toc-entry-1" refid="section">
359 Section
360 <section ids="section" names="section">
361 <title refid="contents">
362 Section
363 <paragraph>
364 Paragraph.
365 """],
366 ["""\
367 .. contents::
368 :backlinks: none
369
370 Section
371 --------
372 Paragraph.
373 """,
374 """\
375 <document source="test data">
376 <topic classes="contents" ids="contents" names="contents">
377 <title>
378 Contents
379 <bullet_list>
380 <list_item>
381 <paragraph>
382 <reference ids="toc-entry-1" refid="section">
383 Section
384 <section ids="section" names="section">
385 <title>
386 Section
387 <paragraph>
388 Paragraph.
389 """],
390 ["""\
391 .. contents::
392
393 Degenerate case, no table of contents generated.
394 """,
395 """\
396 <document source="test data">
397 <paragraph>
398 Degenerate case, no table of contents generated.
399 """],
400 ["""\
401 Title 1
402 =======
403
404 Paragraph 1.
405
406 .. sidebar:: Contents
407
408 .. contents::
409 :local:
410
411 Title 2
412 -------
413 Paragraph 2.
414
415 Title 3
416 ```````
417 Paragraph 3.
418 """,
419 """\
420 <document source="test data">
421 <section ids="title-1" names="title\\ 1">
422 <title>
423 Title 1
424 <paragraph>
425 Paragraph 1.
426 <sidebar>
427 <title>
428 Contents
429 <topic classes="contents local" ids="contents" names="contents">
430 <bullet_list>
431 <list_item>
432 <paragraph>
433 <reference ids="toc-entry-1" refid="title-2">
434 Title 2
435 <bullet_list>
436 <list_item>
437 <paragraph>
438 <reference ids="toc-entry-2" refid="title-3">
439 Title 3
440 <section ids="title-2" names="title\\ 2">
441 <title refid="toc-entry-1">
442 Title 2
443 <paragraph>
444 Paragraph 2.
445 <section ids="title-3" names="title\\ 3">
446 <title refid="toc-entry-2">
447 Title 3
448 <paragraph>
449 Paragraph 3.
450 """],
451 ])
452
453
454 if __name__ == '__main__':
455 unittest.main()