Debian Codegraph @

test/test_transforms/test_sectnum.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_sectnum.py 10303 2026-03-27 08:41:10Z milde $
4 # Authors: David Goodger <[email protected]>; Dmitry Jemerov
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for `docutils.transforms.parts.SectNum` (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['section_numbers'] = ((SectionIDs, Substitutions), [
50 ["""\
51 .. sectnum::
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 <section ids="title-1" names="title\\ 1">
72 <title auto="1">
73 <generated classes="sectnum">
74 1\u00a0\u00a0\u00a0
75 Title 1
76 <paragraph>
77 Paragraph 1.
78 <section ids="title-2" names="title\\ 2">
79 <title auto="1">
80 <generated classes="sectnum">
81 1.1\u00a0\u00a0\u00a0
82 Title 2
83 <paragraph>
84 Paragraph 2.
85 <section ids="title-3" names="title\\ 3">
86 <title auto="1">
87 <generated classes="sectnum">
88 1.1.1\u00a0\u00a0\u00a0
89 Title 3
90 <paragraph>
91 Paragraph 3.
92 <section ids="title-4" names="title\\ 4">
93 <title auto="1">
94 <generated classes="sectnum">
95 1.2\u00a0\u00a0\u00a0
96 Title 4
97 <paragraph>
98 Paragraph 4.
99 """],
100 ["""\
101 .. sectnum::
102
103 **Bold Title**
104 ==============
105 Paragraph 1.
106 """,
107 """\
108 <document source="test data">
109 <section ids="bold-title" names="bold\\ title">
110 <title auto="1">
111 <generated classes="sectnum">
112 1\u00a0\u00a0\u00a0
113 <strong>
114 Bold Title
115 <paragraph>
116 Paragraph 1.
117 """],
118 ["""\
119 .. sectnum:: :depth: 2
120
121 Title 1
122 =======
123 Paragraph 1.
124
125 Title 2
126 -------
127 Paragraph 2.
128
129 Title 3
130 ```````
131 Paragraph 3.
132
133 Title 4
134 -------
135 Paragraph 4.
136 """,
137 """\
138 <document source="test data">
139 <section ids="title-1" names="title\\ 1">
140 <title auto="1">
141 <generated classes="sectnum">
142 1\u00a0\u00a0\u00a0
143 Title 1
144 <paragraph>
145 Paragraph 1.
146 <section ids="title-2" names="title\\ 2">
147 <title auto="1">
148 <generated classes="sectnum">
149 1.1\u00a0\u00a0\u00a0
150 Title 2
151 <paragraph>
152 Paragraph 2.
153 <section ids="title-3" names="title\\ 3">
154 <title>
155 Title 3
156 <paragraph>
157 Paragraph 3.
158 <section ids="title-4" names="title\\ 4">
159 <title auto="1">
160 <generated classes="sectnum">
161 1.2\u00a0\u00a0\u00a0
162 Title 4
163 <paragraph>
164 Paragraph 4.
165 """],
166 ["""\
167 .. contents::
168 .. sectnum:: :depth: 2
169
170 Title 1
171 =======
172 Paragraph 1.
173
174 Title 2
175 -------
176 Paragraph 2.
177
178 Title 3
179 ```````
180 Paragraph 3.
181
182 Title 4
183 -------
184 Paragraph 4.
185 """,
186 """\
187 <document source="test data">
188 <topic classes="contents" ids="contents" names="contents">
189 <title>
190 Contents
191 <bullet_list classes="auto-toc">
192 <list_item>
193 <paragraph>
194 <reference ids="toc-entry-1" refid="title-1">
195 <generated classes="sectnum">
196 1\u00a0\u00a0\u00a0
197 Title 1
198 <bullet_list classes="auto-toc">
199 <list_item>
200 <paragraph>
201 <reference ids="toc-entry-2" refid="title-2">
202 <generated classes="sectnum">
203 1.1\u00a0\u00a0\u00a0
204 Title 2
205 <bullet_list>
206 <list_item>
207 <paragraph>
208 <reference ids="toc-entry-3" refid="title-3">
209 Title 3
210 <list_item>
211 <paragraph>
212 <reference ids="toc-entry-4" refid="title-4">
213 <generated classes="sectnum">
214 1.2\u00a0\u00a0\u00a0
215 Title 4
216 <section ids="title-1" names="title\\ 1">
217 <title auto="1" refid="toc-entry-1">
218 <generated classes="sectnum">
219 1\u00a0\u00a0\u00a0
220 Title 1
221 <paragraph>
222 Paragraph 1.
223 <section ids="title-2" names="title\\ 2">
224 <title auto="1" refid="toc-entry-2">
225 <generated classes="sectnum">
226 1.1\u00a0\u00a0\u00a0
227 Title 2
228 <paragraph>
229 Paragraph 2.
230 <section ids="title-3" names="title\\ 3">
231 <title refid="toc-entry-3">
232 Title 3
233 <paragraph>
234 Paragraph 3.
235 <section ids="title-4" names="title\\ 4">
236 <title auto="1" refid="toc-entry-4">
237 <generated classes="sectnum">
238 1.2\u00a0\u00a0\u00a0
239 Title 4
240 <paragraph>
241 Paragraph 4.
242 """],
243 ["""\
244 .. sectnum::
245 :prefix: Arbitrary-
246
247 Title 1
248 =======
249 Paragraph 1.
250
251 Title 2
252 -------
253 Paragraph 2.
254
255 Title 3
256 ```````
257 Paragraph 3.
258
259 Title 4
260 -------
261 Paragraph 4.
262 """,
263 """\
264 <document source="test data">
265 <section ids="title-1" names="title\\ 1">
266 <title auto="1">
267 <generated classes="sectnum">
268 Arbitrary-1\u00a0\u00a0\u00a0
269 Title 1
270 <paragraph>
271 Paragraph 1.
272 <section ids="title-2" names="title\\ 2">
273 <title auto="1">
274 <generated classes="sectnum">
275 Arbitrary-1.1\u00a0\u00a0\u00a0
276 Title 2
277 <paragraph>
278 Paragraph 2.
279 <section ids="title-3" names="title\\ 3">
280 <title auto="1">
281 <generated classes="sectnum">
282 Arbitrary-1.1.1\u00a0\u00a0\u00a0
283 Title 3
284 <paragraph>
285 Paragraph 3.
286 <section ids="title-4" names="title\\ 4">
287 <title auto="1">
288 <generated classes="sectnum">
289 Arbitrary-1.2\u00a0\u00a0\u00a0
290 Title 4
291 <paragraph>
292 Paragraph 4.
293 """],
294 ["""\
295 .. sectnum::
296 :start: 3
297 \n\
298 Title 1
299 =======
300 Paragraph 1.
301
302 Title 2
303 -------
304 Paragraph 2.
305
306 Title 3
307 ```````
308 Paragraph 3.
309
310 Title 4
311 -------
312 Paragraph 4.
313 """,
314 """\
315 <document source="test data">
316 <section ids="title-1" names="title\\ 1">
317 <title auto="1">
318 <generated classes="sectnum">
319 3\u00a0\u00a0\u00a0
320 Title 1
321 <paragraph>
322 Paragraph 1.
323 <section ids="title-2" names="title\\ 2">
324 <title auto="1">
325 <generated classes="sectnum">
326 3.1\u00a0\u00a0\u00a0
327 Title 2
328 <paragraph>
329 Paragraph 2.
330 <section ids="title-3" names="title\\ 3">
331 <title auto="1">
332 <generated classes="sectnum">
333 3.1.1\u00a0\u00a0\u00a0
334 Title 3
335 <paragraph>
336 Paragraph 3.
337 <section ids="title-4" names="title\\ 4">
338 <title auto="1">
339 <generated classes="sectnum">
340 3.2\u00a0\u00a0\u00a0
341 Title 4
342 <paragraph>
343 Paragraph 4.
344 """],
345 ["""\
346 .. sectnum::
347 :prefix: (5.9.
348 :suffix: )
349 :start: 3
350 \n\
351 Title 1
352 =======
353 Paragraph 1.
354
355 Title 2
356 -------
357 Paragraph 2.
358
359 Title 3
360 ```````
361 Paragraph 3.
362
363 Title 4
364 -------
365 Paragraph 4.
366 """,
367 """\
368 <document source="test data">
369 <section ids="title-1" names="title\\ 1">
370 <title auto="1">
371 <generated classes="sectnum">
372 (5.9.3)\u00a0\u00a0\u00a0
373 Title 1
374 <paragraph>
375 Paragraph 1.
376 <section ids="title-2" names="title\\ 2">
377 <title auto="1">
378 <generated classes="sectnum">
379 (5.9.3.1)\u00a0\u00a0\u00a0
380 Title 2
381 <paragraph>
382 Paragraph 2.
383 <section ids="title-3" names="title\\ 3">
384 <title auto="1">
385 <generated classes="sectnum">
386 (5.9.3.1.1)\u00a0\u00a0\u00a0
387 Title 3
388 <paragraph>
389 Paragraph 3.
390 <section ids="title-4" names="title\\ 4">
391 <title auto="1">
392 <generated classes="sectnum">
393 (5.9.3.2)\u00a0\u00a0\u00a0
394 Title 4
395 <paragraph>
396 Paragraph 4.
397 """],
398 ])
399
400
401 if __name__ == '__main__':
402 unittest.main()