Debian Codegraph @

test/test_parsers/test_rst/test_section_headers.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_section_headers.py 10308 2026-03-28 18:54:25Z milde $
3 # Author: David Goodger <[email protected]>
4 # Copyright: This module has been placed in the public domain.
5
6 """
7 Tests for states.py.
8 """
9
10 from pathlib import Path
11 import sys
12 import unittest
13
14 if __name__ == '__main__':
15 # prepend the "docutils root" to the Python library path
16 # so we import the local `docutils` package.
17 sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
18
19 from docutils.frontend import get_default_settings
20 from docutils.parsers.rst import Parser
21 from docutils.utils import new_document
22
23
24 class ParserTestCase(unittest.TestCase):
25
26 maxDiff = None
27
28 def test_parser(self):
29 parser = Parser()
30 settings = get_default_settings(Parser)
31 settings.legacy_ids = False
32 settings.warning_stream = ''
33 settings.halt_level = 5
34 for name, cases in totest.items():
35 for casenum, (case_input, case_expected) in enumerate(cases):
36 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
37 document = new_document('test data', settings.copy())
38 parser.parse(case_input, document)
39 output = document.pformat()
40 self.assertEqual(case_expected, output)
41
42
43 totest = {}
44
45 totest['section_headers'] = [
46 ["""\
47 Title
48 =====
49
50 Paragraph.
51 """,
52 """\
53 <document source="test data">
54 <section names="title">
55 <title>
56 Title
57 <paragraph>
58 Paragraph.
59 """],
60 ["""\
61 Title
62 =====
63 Paragraph (no blank line).
64 """,
65 """\
66 <document source="test data">
67 <section names="title">
68 <title>
69 Title
70 <paragraph>
71 Paragraph (no blank line).
72 """],
73 ["""\
74 Paragraph.
75
76 Title
77 =====
78
79 Paragraph.
80 """,
81 """\
82 <document source="test data">
83 <paragraph>
84 Paragraph.
85 <section names="title">
86 <title>
87 Title
88 <paragraph>
89 Paragraph.
90 """],
91 ["""\
92 Test unexpected section titles.
93
94 Title
95 =====
96 Paragraph.
97
98 -----
99 Title
100 -----
101 Paragraph.
102 """,
103 """\
104 <document source="test data">
105 <paragraph>
106 Test unexpected section titles.
107 <block_quote>
108 <system_message level="3" line="4" source="test data" type="ERROR">
109 <paragraph>
110 Unexpected section title.
111 <literal_block xml:space="preserve">
112 Title
113 =====
114 <paragraph>
115 Paragraph.
116 <system_message level="3" line="7" source="test data" type="ERROR">
117 <paragraph>
118 Unexpected section title or transition.
119 <literal_block xml:space="preserve">
120 -----
121 <system_message level="3" line="9" source="test data" type="ERROR">
122 <paragraph>
123 Unexpected section title.
124 <literal_block xml:space="preserve">
125 Title
126 -----
127 <paragraph>
128 Paragraph.
129 """],
130 ["""\
131 Title
132 ====
133
134 Test short underline.
135 """,
136 """\
137 <document source="test data">
138 <section names="title">
139 <title>
140 Title
141 <system_message level="2" line="2" source="test data" type="WARNING">
142 <paragraph>
143 Title underline too short.
144 <literal_block xml:space="preserve">
145 Title
146 ====
147 <paragraph>
148 Test short underline.
149 """],
150 ["""\
151 à with combining varia
152 ======================
153
154 Do not count combining chars in title column width.
155 """,
156 """\
157 <document source="test data">
158 <section names="a\u0300\\ with\\ combining\\ varia">
159 <title>
160 à with combining varia
161 <paragraph>
162 Do not count combining chars in title column width.
163 """],
164 ["""\
165 =====
166 Title
167 =====
168
169 Test overline title.
170 """,
171 """\
172 <document source="test data">
173 <section names="title">
174 <title>
175 Title
176 <paragraph>
177 Test overline title.
178 """],
179 ["""\
180 =======
181 Title
182 =======
183
184 Test overline title with inset.
185 """,
186 """\
187 <document source="test data">
188 <section names="title">
189 <title>
190 Title
191 <paragraph>
192 Test overline title with inset.
193 """],
194 ["""\
195 ========================
196 Test Missing Underline
197 """,
198 """\
199 <document source="test data">
200 <system_message level="3" line="1" source="test data" type="ERROR">
201 <paragraph>
202 Incomplete section title.
203 <literal_block xml:space="preserve">
204 ========================
205 Test Missing Underline
206 """],
207 ["""\
208 ========================
209 Test Missing Underline
210
211 """,
212 """\
213 <document source="test data">
214 <system_message level="3" line="1" source="test data" type="ERROR">
215 <paragraph>
216 Missing matching underline for section title overline.
217 <literal_block xml:space="preserve">
218 ========================
219 Test Missing Underline
220 """],
221 ["""\
222 =======
223 Title
224
225 Test missing underline, with paragraph.
226 """,
227 """\
228 <document source="test data">
229 <system_message level="3" line="1" source="test data" type="ERROR">
230 <paragraph>
231 Missing matching underline for section title overline.
232 <literal_block xml:space="preserve">
233 =======
234 Title
235 <paragraph>
236 Test missing underline, with paragraph.
237 """],
238 ["""\
239 =======
240 Long Title
241 =======
242
243 Test long title and space normalization.
244 """,
245 """\
246 <document source="test data">
247 <section names="long\\ title">
248 <title>
249 Long Title
250 <system_message level="2" line="1" source="test data" type="WARNING">
251 <paragraph>
252 Title overline too short.
253 <literal_block xml:space="preserve">
254 =======
255 Long Title
256 =======
257 <paragraph>
258 Test long title and space normalization.
259 """],
260 ["""\
261 =======
262 Title
263 -------
264
265 Paragraph.
266 """,
267 """\
268 <document source="test data">
269 <system_message level="3" line="1" source="test data" type="ERROR">
270 <paragraph>
271 Title overline & underline mismatch.
272 <literal_block xml:space="preserve">
273 =======
274 Title
275 -------
276 <paragraph>
277 Paragraph.
278 """],
279 ["""\
280 ========================
281
282 ========================
283
284 Test missing titles; blank line in-between.
285
286 ========================
287
288 ========================
289 """,
290 """\
291 <document source="test data">
292 <transition>
293 <transition>
294 <paragraph>
295 Test missing titles; blank line in-between.
296 <transition>
297 <transition>
298 """],
299 ["""\
300 ========================
301 ========================
302
303 Test missing titles; nothing in-between.
304
305 ========================
306 ========================
307 """,
308 """\
309 <document source="test data">
310 <system_message level="3" line="1" source="test data" type="ERROR">
311 <paragraph>
312 Invalid section title or transition marker.
313 <literal_block xml:space="preserve">
314 ========================
315 ========================
316 <paragraph>
317 Test missing titles; nothing in-between.
318 <system_message level="3" line="6" source="test data" type="ERROR">
319 <paragraph>
320 Invalid section title or transition marker.
321 <literal_block xml:space="preserve">
322 ========================
323 ========================
324 """],
325 ["""\
326 .. Test return to existing, highest-level section (Title 3).
327
328 Title 1
329 =======
330 Paragraph 1.
331
332 Title 2
333 -------
334 Paragraph 2.
335
336 Title 3
337 =======
338 Paragraph 3.
339
340 Title 4
341 -------
342 Paragraph 4.
343 """,
344 """\
345 <document source="test data">
346 <comment xml:space="preserve">
347 Test return to existing, highest-level section (Title 3).
348 <section names="title\\ 1">
349 <title>
350 Title 1
351 <paragraph>
352 Paragraph 1.
353 <section names="title\\ 2">
354 <title>
355 Title 2
356 <paragraph>
357 Paragraph 2.
358 <section names="title\\ 3">
359 <title>
360 Title 3
361 <paragraph>
362 Paragraph 3.
363 <section names="title\\ 4">
364 <title>
365 Title 4
366 <paragraph>
367 Paragraph 4.
368 """],
369 ["""\
370 Test return to existing, highest-level section (Title 3, with overlines).
371
372 =======
373 Title 1
374 =======
375 Paragraph 1.
376
377 -------
378 Title 2
379 -------
380 Paragraph 2.
381
382 =======
383 Title 3
384 =======
385 Paragraph 3.
386
387 -------
388 Title 4
389 -------
390 Paragraph 4.
391 """,
392 """\
393 <document source="test data">
394 <paragraph>
395 Test return to existing, highest-level section (Title 3, with overlines).
396 <section names="title\\ 1">
397 <title>
398 Title 1
399 <paragraph>
400 Paragraph 1.
401 <section names="title\\ 2">
402 <title>
403 Title 2
404 <paragraph>
405 Paragraph 2.
406 <section names="title\\ 3">
407 <title>
408 Title 3
409 <paragraph>
410 Paragraph 3.
411 <section names="title\\ 4">
412 <title>
413 Title 4
414 <paragraph>
415 Paragraph 4.
416 """],
417 ["""\
418 Test return to existing, higher-level section (Title 4).
419
420 Title 1
421 =======
422 Paragraph 1.
423
424 Title 2
425 -------
426 Paragraph 2.
427
428 Title 3
429 ```````
430 Paragraph 3.
431
432 Title 4
433 -------
434 Paragraph 4.
435 """,
436 """\
437 <document source="test data">
438 <paragraph>
439 Test return to existing, higher-level section (Title 4).
440 <section names="title\\ 1">
441 <title>
442 Title 1
443 <paragraph>
444 Paragraph 1.
445 <section names="title\\ 2">
446 <title>
447 Title 2
448 <paragraph>
449 Paragraph 2.
450 <section names="title\\ 3">
451 <title>
452 Title 3
453 <paragraph>
454 Paragraph 3.
455 <section names="title\\ 4">
456 <title>
457 Title 4
458 <paragraph>
459 Paragraph 4.
460 """],
461 ["""\
462 Test bad subsection order (Title 4).
463
464 Title 1
465 =======
466 Paragraph 1.
467
468 Title 2
469 -------
470 Paragraph 2.
471
472 Title 3
473 =======
474 Paragraph 3.
475
476 Title 4
477 ```````
478 Paragraph 4.
479 """,
480 """\
481 <document source="test data">
482 <paragraph>
483 Test bad subsection order (Title 4).
484 <section names="title\\ 1">
485 <title>
486 Title 1
487 <paragraph>
488 Paragraph 1.
489 <section names="title\\ 2">
490 <title>
491 Title 2
492 <paragraph>
493 Paragraph 2.
494 <section names="title\\ 3">
495 <title>
496 Title 3
497 <paragraph>
498 Paragraph 3.
499 <system_message level="3" line="15" source="test data" type="ERROR">
500 <paragraph>
501 Inconsistent title style: skip from level 1 to 3.
502 <literal_block xml:space="preserve">
503 Title 4
504 ```````
505 <paragraph>
506 Established title styles: = -
507 <paragraph>
508 Paragraph 4.
509 """],
510 ["""\
511 Test bad subsection order (Title 4, with overlines).
512
513 =======
514 Title 1
515 =======
516 Paragraph 1.
517
518 -------
519 Title 2
520 -------
521 Paragraph 2.
522
523 =======
524 Title 3
525 =======
526 Paragraph 3.
527
528 ```````
529 Title 4
530 ```````
531 Paragraph 4.
532 """,
533 """\
534 <document source="test data">
535 <paragraph>
536 Test bad subsection order (Title 4, with overlines).
537 <section names="title\\ 1">
538 <title>
539 Title 1
540 <paragraph>
541 Paragraph 1.
542 <section names="title\\ 2">
543 <title>
544 Title 2
545 <paragraph>
546 Paragraph 2.
547 <section names="title\\ 3">
548 <title>
549 Title 3
550 <paragraph>
551 Paragraph 3.
552 <system_message level="3" line="19" source="test data" type="ERROR">
553 <paragraph>
554 Inconsistent title style: skip from level 1 to 3.
555 <literal_block xml:space="preserve">
556 ```````
557 Title 4
558 ```````
559 <paragraph>
560 Established title styles: =/= -/-
561 <paragraph>
562 Paragraph 4.
563 """],
564 ["""\
565 Title containing *inline* ``markup``
566 ====================================
567
568 Paragraph.
569 """,
570 """\
571 <document source="test data">
572 <section names="title\\ containing\\ inline\\ markup">
573 <title>
574 Title containing \n\
575 <emphasis>
576 inline
577 \n\
578 <literal>
579 markup
580 <paragraph>
581 Paragraph.
582 """],
583 ["""\
584 1. Numbered Title
585 =================
586
587 Paragraph.
588 """,
589 """\
590 <document source="test data">
591 <section names="1.\\ numbered\\ title">
592 <title>
593 1. Numbered Title
594 <paragraph>
595 Paragraph.
596 """],
597 ["""\
598 1. Item 1.
599 2. Item 2.
600 3. Numbered Title
601 =================
602
603 Paragraph.
604 """,
605 """\
606 <document source="test data">
607 <enumerated_list enumtype="arabic" prefix="" suffix=".">
608 <list_item>
609 <paragraph>
610 Item 1.
611 <list_item>
612 <paragraph>
613 Item 2.
614 <system_message level="2" line="3" source="test data" type="WARNING">
615 <paragraph>
616 Enumerated list ends without a blank line; unexpected unindent.
617 <section names="3.\\ numbered\\ title">
618 <title>
619 3. Numbered Title
620 <paragraph>
621 Paragraph.
622 """],
623 ["""\
624 ABC
625 ===
626
627 Short title.
628 """,
629 """\
630 <document source="test data">
631 <section names="abc">
632 <title>
633 ABC
634 <paragraph>
635 Short title.
636 """],
637 ["""\
638 ABC
639 ==
640
641 Underline too short.
642 """,
643 """\
644 <document source="test data">
645 <system_message level="1" line="2" source="test data" type="INFO">
646 <paragraph>
647 Possible title underline, too short for the title.
648 Treating it as ordinary text because it's so short.
649 <paragraph>
650 ABC
651 ==
652 <paragraph>
653 Underline too short.
654 """],
655 ["""\
656 ==
657 ABC
658 ==
659
660 Over & underline too short.
661 """,
662 """\
663 <document source="test data">
664 <system_message level="1" line="1" source="test data" type="INFO">
665 <paragraph>
666 Possible incomplete section title.
667 Treating the overline as ordinary text because it's so short.
668 <paragraph>
669 ==
670 ABC
671 ==
672 <paragraph>
673 Over & underline too short.
674 """],
675 ["""\
676 ==
677 ABC
678
679 Overline too short, no underline.
680 """,
681 """\
682 <document source="test data">
683 <system_message level="1" line="1" source="test data" type="INFO">
684 <paragraph>
685 Possible incomplete section title.
686 Treating the overline as ordinary text because it's so short.
687 <paragraph>
688 ==
689 ABC
690 <paragraph>
691 Overline too short, no underline.
692 """],
693 ["""\
694 ==
695 ABC
696 """,
697 """\
698 <document source="test data">
699 <system_message level="1" line="1" source="test data" type="INFO">
700 <paragraph>
701 Possible incomplete section title.
702 Treating the overline as ordinary text because it's so short.
703 <paragraph>
704 ==
705 ABC
706 """],
707 ["""\
708 ==
709 Not a title: a definition list item.
710 """,
711 """\
712 <document source="test data">
713 <system_message level="1" line="1" source="test data" type="INFO">
714 <paragraph>
715 Possible incomplete section title.
716 Treating the overline as ordinary text because it's so short.
717 <definition_list>
718 <definition_list_item>
719 <term>
720 ==
721 <definition>
722 <paragraph>
723 Not a title: a definition list item.
724 """],
725 ["""\
726 ==
727 Not a title: a definition list item.
728 --
729 Another definition list item. It's in a different list,
730 but that's an acceptable limitation given that this will
731 probably never happen in real life.
732
733 The next line will trigger a warning:
734 ==
735 """,
736 """\
737 <document source="test data">
738 <system_message level="1" line="1" source="test data" type="INFO">
739 <paragraph>
740 Possible incomplete section title.
741 Treating the overline as ordinary text because it's so short.
742 <definition_list>
743 <definition_list_item>
744 <term>
745 ==
746 <definition>
747 <paragraph>
748 Not a title: a definition list item.
749 <system_message level="2" line="3" source="test data" type="WARNING">
750 <paragraph>
751 Definition list ends without a blank line; unexpected unindent.
752 <system_message level="1" line="3" source="test data" type="INFO">
753 <paragraph>
754 Possible incomplete section title.
755 Treating the overline as ordinary text because it's so short.
756 <definition_list>
757 <definition_list_item>
758 <term>
759 --
760 <definition>
761 <paragraph>
762 Another definition list item. It's in a different list,
763 but that's an acceptable limitation given that this will
764 probably never happen in real life.
765 <paragraph>
766 The next line will trigger a warning:
767 <system_message level="2" line="9" source="test data" type="WARNING">
768 <paragraph>
769 Definition list ends without a blank line; unexpected unindent.
770 <paragraph>
771 ==
772 """],
773 ["""\
774 Paragraph
775
776 ==
777 ABC
778 ==
779
780 Over & underline too short.
781 """,
782 """\
783 <document source="test data">
784 <paragraph>
785 Paragraph
786 <block_quote>
787 <system_message level="1" line="3" source="test data" type="INFO">
788 <paragraph>
789 Unexpected possible title overline or transition.
790 Treating it as ordinary text because it's so short.
791 <paragraph>
792 ==
793 ABC
794 ==
795 <paragraph>
796 Over & underline too short.
797 """],
798 ["""\
799 Paragraph
800
801 ABC
802 ==
803
804 Underline too short.
805 """,
806 """\
807 <document source="test data">
808 <paragraph>
809 Paragraph
810 <block_quote>
811 <paragraph>
812 ABC
813 ==
814 <paragraph>
815 Underline too short.
816 """],
817 ["""\
818 ...
819 ...
820
821 ...
822 ---
823
824 ...
825 ...
826 ...
827 """,
828 """\
829 <document source="test data">
830 <system_message level="1" line="1" source="test data" type="INFO">
831 <paragraph>
832 Possible incomplete section title.
833 Treating the overline as ordinary text because it's so short.
834 <section dupnames="..." ids="section-1">
835 <title>
836 ...
837 <system_message level="1" line="4" source="test data" type="INFO">
838 <paragraph>
839 Possible incomplete section title.
840 Treating the overline as ordinary text because it's so short.
841 <section dupnames="..." ids="section-2">
842 <title>
843 ...
844 <system_message backrefs="section-2" level="1" line="5" source="test data" type="INFO">
845 <paragraph>
846 Duplicate implicit target name: "...".
847 <system_message level="1" line="7" source="test data" type="INFO">
848 <paragraph>
849 Possible incomplete section title.
850 Treating the overline as ordinary text because it's so short.
851 <section dupnames="..." ids="section-3">
852 <title>
853 ...
854 <system_message backrefs="section-3" level="1" line="8" source="test data" type="INFO">
855 <paragraph>
856 Duplicate implicit target name: "...".
857 <paragraph>
858 ...
859 """],
860 ["""\
861 ..
862 Hi
863 ..
864
865 ...
866 Yo
867 ...
868
869 Ho
870 """,
871 """\
872 <document source="test data">
873 <comment xml:space="preserve">
874 <system_message level="2" line="2" source="test data" type="WARNING">
875 <paragraph>
876 Explicit markup ends without a blank line; unexpected unindent.
877 <section names="hi">
878 <title>
879 Hi
880 <section names="yo">
881 <title>
882 Yo
883 <paragraph>
884 Ho
885 """],
886 ["""\
887 Empty Section
888 =============
889 """,
890 """\
891 <document source="test data">
892 <section names="empty\\ section">
893 <title>
894 Empty Section
895 """],
896 ["""\
897 ===
898 One
899 ===
900
901 The bubble-up parser strategy conflicts with short titles
902 (<= 3 char-long over- & underlines).
903
904 ===
905 Two
906 ===
907
908 The parser currently contains a work-around kludge.
909 Without it, the parser ends up in an infinite loop.
910 """,
911 """\
912 <document source="test data">
913 <section names="one">
914 <title>
915 One
916 <paragraph>
917 The bubble-up parser strategy conflicts with short titles
918 (<= 3 char-long over- & underlines).
919 <section names="two">
920 <title>
921 Two
922 <paragraph>
923 The parser currently contains a work-around kludge.
924 Without it, the parser ends up in an infinite loop.
925 """],
926 ["""\
927 """,
928 """\
929 <document source="test data">
930 """],
931 ]
932
933
934 if __name__ == '__main__':
935 unittest.main()