Debian Codegraph @

test/test_transforms/test_transitions.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_transitions.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 Test module for misc.Transitions transform.
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 import rst, docutils_xml
22 from docutils.transforms.misc import Transitions
23 from docutils.transforms.universal import TestMessages
24 from docutils.utils import new_document
25
26
27 class TransformTestCase(unittest.TestCase):
28
29 maxDiff = None
30
31 def test_transforms(self):
32 parser = rst.Parser()
33 settings = get_default_settings(rst.Parser)
34 settings.legacy_ids = False
35 settings.warning_stream = ''
36 for name, (transforms, cases) in totest.items():
37 for casenum, (case_input, case_expected) in enumerate(cases):
38 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
39 document = new_document('test data', settings.copy())
40 parser.parse(case_input, document)
41 # Don't do a ``populate_from_components()`` because that
42 # would enable the Transformer's default transforms.
43 document.transformer.add_transforms(transforms)
44 document.transformer.add_transform(TestMessages)
45 document.transformer.apply_transforms()
46 output = document.pformat()
47 self.assertEqual(case_expected, output)
48
49 def test_transfroms_x(self):
50 # test samples given as Docutils XML in `totest_x`
51 # (for document trees that cannot be generated from rST)
52 parser = docutils_xml.Parser()
53 settings = get_default_settings(docutils_xml.Parser)
54 settings.warning_stream = ''
55 for name, (transforms, cases) in totest_x.items():
56 for casenum, (case_input, case_expected) in enumerate(cases):
57 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
58 document = new_document('test data', settings.copy())
59 parser.parse(case_input, document)
60 # Don't do a ``populate_from_components()`` because that
61 # would enable the Transformer's default transforms.
62 document.transformer.add_transforms(transforms)
63 document.transformer.add_transform(TestMessages)
64 document.transformer.apply_transforms()
65 output = document.pformat()
66 self.assertEqual(case_expected, output)
67
68
69 totest = {} # rST samples and expected pseudoXML
70 totest_x = {} # XML samples and expected pseudoXML
71
72 totest['transitions'] = ((Transitions,), [
73 ["""\
74 Section 1
75 =========
76
77 Subsection 1
78 ------------
79
80 A transition at the end of a section is moved behind the section.
81
82 ----------
83
84 Section 2
85 =========
86
87 Some text.
88 """,
89 """\
90 <document source="test data">
91 <section names="section\\ 1">
92 <title>
93 Section 1
94 <section names="subsection\\ 1">
95 <title>
96 Subsection 1
97 <paragraph>
98 A transition at the end of a section is moved behind the section.
99 <transition>
100 <section names="section\\ 2">
101 <title>
102 Section 2
103 <paragraph>
104 Some text.
105 """],
106 ["""\
107 A paragraph.
108
109 ----------
110
111 Section 1
112 =========
113
114 Paragraph.
115 """,
116 """\
117 <document source="test data">
118 <paragraph>
119 A paragraph.
120 <transition>
121 <section names="section\\ 1">
122 <title>
123 Section 1
124 <paragraph>
125 Paragraph.
126 """],
127 ["""\
128 --------
129
130 A system message warns about invalid placement of transitions.
131 """,
132 """\
133 <document source="test data">
134 <transition>
135 <system_message level="2" line="1" source="test data" type="WARNING">
136 <paragraph>
137 Transition at the start of the document.
138 <paragraph>
139 A system message warns about invalid placement of transitions.
140 """],
141 ["""\
142 rST and Doctree specifications say ...
143
144 --------
145
146 --------
147
148 ... that two transitions should not be adjacent.
149 """,
150 """\
151 <document source="test data">
152 <paragraph>
153 rST and Doctree specifications say ...
154 <transition>
155 <transition>
156 <system_message level="2" line="5" source="test data" type="WARNING">
157 <paragraph>
158 At least one body element should separate transitions.
159 <paragraph>
160 ... that two transitions should not be adjacent.
161 """],
162 ["""\
163 The specs also say that a section or document
164 may not end with a transition.
165
166 --------
167 """,
168 """\
169 <document source="test data">
170 <paragraph>
171 The specs also say that a section or document
172 may not end with a transition.
173 <transition>
174 <system_message level="2" line="4" source="test data" type="WARNING">
175 <paragraph>
176 Transition at the end of the document.
177 """],
178 ["""\
179 Moving and invisible elements don't count.
180
181 ----------
182
183 .. meta:: :keywords: transition test
184 .. footer:: will move away
185 .. _anchor:
186 .. |substitution reference| replace:: is invisible
187 """,
188 """\
189 <document source="test data">
190 <meta content="transition test" name="keywords">
191 <decoration>
192 <footer>
193 <paragraph>
194 will move away
195 <paragraph>
196 Moving and invisible elements don't count.
197 <transition>
198 <system_message level="2" line="3" source="test data" type="WARNING">
199 <paragraph>
200 Transition at the end of the document.
201 <target ids="anchor" names="anchor">
202 <substitution_definition names="substitution\\ reference">
203 is invisible
204 """],
205 ["""\
206 Sections with transitions at beginning and end.
207
208 Section 1
209 =========
210
211 ----------
212
213 .. Comment after transition.
214
215 Section 2
216 =========
217
218 Some text before the transition.
219
220 ----------
221 """,
222 """\
223 <document source="test data">
224 <paragraph>
225 Sections with transitions at beginning and end.
226 <section names="section\\ 1">
227 <title>
228 Section 1
229 <transition>
230 <system_message level="2" line="6" source="test data" type="WARNING">
231 <paragraph>
232 Transition at the start of the section.
233 <comment xml:space="preserve">
234 Comment after transition.
235 <section names="section\\ 2">
236 <title>
237 Section 2
238 <paragraph>
239 Some text before the transition.
240 <transition>
241 <system_message level="2" line="15" source="test data" type="WARNING">
242 <paragraph>
243 Transition at the end of the document.
244 """],
245 ["""\
246 A paragraph and two transitions.
247
248 ----------
249
250 ----------
251 """,
252 """\
253 <document source="test data">
254 <paragraph>
255 A paragraph and two transitions.
256 <transition>
257 <transition>
258 <system_message level="2" line="5" source="test data" type="WARNING">
259 <paragraph>
260 At least one body element should separate transitions.
261 <system_message level="2" line="5" source="test data" type="WARNING">
262 <paragraph>
263 Transition at the end of the document.
264 """],
265 ["""\
266 A paragraph, two transitions, and a blank line.
267
268 ----------
269
270 ----------
271
272 """,
273 """\
274 <document source="test data">
275 <paragraph>
276 A paragraph, two transitions, and a blank line.
277 <transition>
278 <transition>
279 <system_message level="2" line="5" source="test data" type="WARNING">
280 <paragraph>
281 At least one body element should separate transitions.
282 <system_message level="2" line="5" source="test data" type="WARNING">
283 <paragraph>
284 Transition at the end of the document.
285 """],
286 ["""\
287 ----------
288
289 Document beginning with a transition.
290 """,
291 """\
292 <document source="test data">
293 <transition>
294 <system_message level="2" line="1" source="test data" type="WARNING">
295 <paragraph>
296 Transition at the start of the document.
297 <paragraph>
298 Document beginning with a transition.
299 """],
300 ["""\
301 .. class:: classy
302 .. meta:: :keywords: transition test
303 .. footer:: will move away
304 .. _anchor:
305 .. |substitution reference| replace:: is invisible
306
307 ----------
308
309 Document beginning with a transition (title, moving elements,
310 and invisible elements don't count).
311 """,
312 """\
313 <document source="test data">
314 <meta content="transition test" name="keywords">
315 <decoration>
316 <footer>
317 <paragraph>
318 will move away
319 <target ids="anchor" names="anchor">
320 <substitution_definition names="substitution\\ reference">
321 is invisible
322 <transition classes="classy">
323 <system_message level="2" line="7" source="test data" type="WARNING">
324 <paragraph>
325 Transition at the start of the document.
326 <paragraph>
327 Document beginning with a transition (title, moving elements,
328 and invisible elements don't count).
329 """],
330 ["""\
331 Section 1
332 =========
333
334 ----------
335
336 ----------
337
338 ----------
339
340 Section 2
341 =========
342 """,
343 """\
344 <document source="test data">
345 <section names="section\\ 1">
346 <title>
347 Section 1
348 <transition>
349 <system_message level="2" line="4" source="test data" type="WARNING">
350 <paragraph>
351 Transition at the start of the section.
352 <transition>
353 <transition>
354 <system_message level="2" line="8" source="test data" type="WARNING">
355 <paragraph>
356 At least one body element should separate transitions.
357 <section names="section\\ 2">
358 <title>
359 Section 2
360 """],
361 ["""\
362 ----------
363
364 ----------
365
366 ----------
367 """,
368 # The placement of <system_message>s in this sample is an implementation
369 # detail, see the remarks in the preceding test nr. 11.
370 """\
371 <document source="test data">
372 <transition>
373 <system_message level="2" line="1" source="test data" type="WARNING">
374 <paragraph>
375 Transition at the start of the document.
376 <transition>
377 <transition>
378 <system_message level="2" line="5" source="test data" type="WARNING">
379 <paragraph>
380 At least one body element should separate transitions.
381 <system_message level="2" line="5" source="test data" type="WARNING">
382 <paragraph>
383 Transition at the end of the document.
384 """],
385 ])
386
387
388 totest_x['transitions extra'] = ((Transitions,), [
389 # currently, a <transition> is only valid inside <document> or <section>
390 ["""\
391 <document>
392 <paragraph>Some text.</paragraph>
393 <transition />
394 <paragraph>Some text.</paragraph>
395 <block_quote>
396 <paragraph>Some text.</paragraph>
397 <transition />
398 <paragraph>Some text.</paragraph>
399 </block_quote>
400 <paragraph>A <transition /> in a paragraph.</paragraph>
401 </document>
402 """,
403 """\
404 <document source="test data">
405 <paragraph>
406 Some text.
407 <transition>
408 <paragraph>
409 Some text.
410 <block_quote>
411 <paragraph>
412 Some text.
413 <transition>
414 <system_message level="2" line="7" source="test data" type="WARNING">
415 <paragraph>
416 Transition only valid as child of <document> or <section>.
417 <paragraph>
418 Some text.
419 <paragraph>
420 A \n\
421 <transition>
422 in a paragraph.
423 <system_message level="2" line="10" source="test data" type="WARNING">
424 <paragraph>
425 Transition only valid as child of <document> or <section>.
426 """],
427 ])
428
429
430 if __name__ == '__main__':
431 unittest.main()