Debian Codegraph @

test/test_parsers/test_rst/test_enumerated_lists.py

raw symbols SCIP
1 #! /usr/bin/env python3
2
3 # $Id: test_enumerated_lists.py 9425 2023-06-30 14:56:47Z milde $
4 # Author: David Goodger <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 Tests for states.py.
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[3]))
19
20 from docutils.frontend import get_default_settings
21 from docutils.parsers.rst import Parser
22 from docutils.utils import new_document
23
24
25 class ParserTestCase(unittest.TestCase):
26 def test_parser(self):
27 parser = Parser()
28 settings = get_default_settings(Parser)
29 settings.warning_stream = ''
30 for name, cases in totest.items():
31 for casenum, (case_input, case_expected) in enumerate(cases):
32 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
33 document = new_document('test data', settings.copy())
34 parser.parse(case_input, document)
35 output = document.pformat()
36 self.assertEqual(case_expected, output)
37
38
39 totest = {}
40
41 totest['enumerated_lists'] = [
42 ["""\
43 1. Item one.
44
45 2. Item two.
46
47 3. Item three.
48 """,
49 """\
50 <document source="test data">
51 <enumerated_list enumtype="arabic" prefix="" suffix=".">
52 <list_item>
53 <paragraph>
54 Item one.
55 <list_item>
56 <paragraph>
57 Item two.
58 <list_item>
59 <paragraph>
60 Item three.
61 """],
62 ["""\
63 No blank lines betwen items:
64
65 1. Item one.
66 2. Item two.
67 3. Item three.
68 """,
69 """\
70 <document source="test data">
71 <paragraph>
72 No blank lines betwen items:
73 <enumerated_list enumtype="arabic" prefix="" suffix=".">
74 <list_item>
75 <paragraph>
76 Item one.
77 <list_item>
78 <paragraph>
79 Item two.
80 <list_item>
81 <paragraph>
82 Item three.
83 """],
84 ["""\
85 1.
86 empty item above, no blank line
87 """,
88 """\
89 <document source="test data">
90 <paragraph>
91 1.
92 empty item above, no blank line
93 """],
94 ["""\
95 Scrambled:
96
97 3. Item three.
98
99 2. Item two.
100
101 1. Item one.
102
103 3. Item three.
104 2. Item two.
105 1. Item one.
106 """,
107 """\
108 <document source="test data">
109 <paragraph>
110 Scrambled:
111 <enumerated_list enumtype="arabic" prefix="" start="3" suffix=".">
112 <list_item>
113 <paragraph>
114 Item three.
115 <system_message level="1" line="3" source="test data" type="INFO">
116 <paragraph>
117 Enumerated list start value not ordinal-1: "3" (ordinal 3)
118 <enumerated_list enumtype="arabic" prefix="" start="2" suffix=".">
119 <list_item>
120 <paragraph>
121 Item two.
122 <system_message level="1" line="5" source="test data" type="INFO">
123 <paragraph>
124 Enumerated list start value not ordinal-1: "2" (ordinal 2)
125 <enumerated_list enumtype="arabic" prefix="" suffix=".">
126 <list_item>
127 <paragraph>
128 Item one.
129 <paragraph>
130 3. Item three.
131 2. Item two.
132 1. Item one.
133 """],
134 ["""\
135 Skipping item 3:
136
137 1. Item 1.
138 2. Item 2.
139 4. Item 4.
140 """,
141 """\
142 <document source="test data">
143 <paragraph>
144 Skipping item 3:
145 <enumerated_list enumtype="arabic" prefix="" suffix=".">
146 <list_item>
147 <paragraph>
148 Item 1.
149 <system_message level="2" line="4" source="test data" type="WARNING">
150 <paragraph>
151 Enumerated list ends without a blank line; unexpected unindent.
152 <paragraph>
153 2. Item 2.
154 4. Item 4.
155 """],
156 ["""\
157 Start with non-ordinal-1:
158
159 0. Item zero.
160 1. Item one.
161 2. Item two.
162 3. Item three.
163
164 And again:
165
166 2. Item two.
167 3. Item three.
168 """,
169 """\
170 <document source="test data">
171 <paragraph>
172 Start with non-ordinal-1:
173 <enumerated_list enumtype="arabic" prefix="" start="0" suffix=".">
174 <list_item>
175 <paragraph>
176 Item zero.
177 <list_item>
178 <paragraph>
179 Item one.
180 <list_item>
181 <paragraph>
182 Item two.
183 <list_item>
184 <paragraph>
185 Item three.
186 <system_message level="1" line="3" source="test data" type="INFO">
187 <paragraph>
188 Enumerated list start value not ordinal-1: "0" (ordinal 0)
189 <paragraph>
190 And again:
191 <enumerated_list enumtype="arabic" prefix="" start="2" suffix=".">
192 <list_item>
193 <paragraph>
194 Item two.
195 <list_item>
196 <paragraph>
197 Item three.
198 <system_message level="1" line="10" source="test data" type="INFO">
199 <paragraph>
200 Enumerated list start value not ordinal-1: "2" (ordinal 2)
201 """],
202 ["""\
203 1. Item one: line 1,
204 line 2.
205 2. Item two: line 1,
206 line 2.
207 3. Item three: paragraph 1, line 1,
208 line 2.
209
210 Paragraph 2.
211 """,
212 """\
213 <document source="test data">
214 <enumerated_list enumtype="arabic" prefix="" suffix=".">
215 <list_item>
216 <paragraph>
217 Item one: line 1,
218 line 2.
219 <list_item>
220 <paragraph>
221 Item two: line 1,
222 line 2.
223 <list_item>
224 <paragraph>
225 Item three: paragraph 1, line 1,
226 line 2.
227 <paragraph>
228 Paragraph 2.
229 """],
230 ["""\
231 Different enumeration sequences:
232
233 1. Item 1.
234 2. Item 2.
235 3. Item 3.
236
237 A. Item A.
238 B. Item B.
239 C. Item C.
240
241 a. Item a.
242 b. Item b.
243 c. Item c.
244
245 I. Item I.
246 II. Item II.
247 III. Item III.
248
249 i. Item i.
250 ii. Item ii.
251 iii. Item iii.
252 """,
253 """\
254 <document source="test data">
255 <paragraph>
256 Different enumeration sequences:
257 <enumerated_list enumtype="arabic" prefix="" suffix=".">
258 <list_item>
259 <paragraph>
260 Item 1.
261 <list_item>
262 <paragraph>
263 Item 2.
264 <list_item>
265 <paragraph>
266 Item 3.
267 <enumerated_list enumtype="upperalpha" prefix="" suffix=".">
268 <list_item>
269 <paragraph>
270 Item A.
271 <list_item>
272 <paragraph>
273 Item B.
274 <list_item>
275 <paragraph>
276 Item C.
277 <enumerated_list enumtype="loweralpha" prefix="" suffix=".">
278 <list_item>
279 <paragraph>
280 Item a.
281 <list_item>
282 <paragraph>
283 Item b.
284 <list_item>
285 <paragraph>
286 Item c.
287 <enumerated_list enumtype="upperroman" prefix="" suffix=".">
288 <list_item>
289 <paragraph>
290 Item I.
291 <list_item>
292 <paragraph>
293 Item II.
294 <list_item>
295 <paragraph>
296 Item III.
297 <enumerated_list enumtype="lowerroman" prefix="" suffix=".">
298 <list_item>
299 <paragraph>
300 Item i.
301 <list_item>
302 <paragraph>
303 Item ii.
304 <list_item>
305 <paragraph>
306 Item iii.
307 """],
308 ["""\
309 Bad Roman numerals:
310
311 i. i
312
313 ii. ii
314
315 iii. iii
316
317 iiii. iiii
318 second line
319
320 (LCD) is an acronym made up of Roman numerals
321
322 (livid) is a word made up of Roman numerals
323
324 (CIVIL) is another such word
325
326 (I) I
327
328 (IVXLCDM) IVXLCDM
329 """,
330 """\
331 <document source="test data">
332 <paragraph>
333 Bad Roman numerals:
334 <enumerated_list enumtype="lowerroman" prefix="" suffix=".">
335 <list_item>
336 <paragraph>
337 i
338 <list_item>
339 <paragraph>
340 ii
341 <list_item>
342 <paragraph>
343 iii
344 <definition_list>
345 <definition_list_item>
346 <term>
347 iiii. iiii
348 <definition>
349 <paragraph>
350 second line
351 <paragraph>
352 (LCD) is an acronym made up of Roman numerals
353 <paragraph>
354 (livid) is a word made up of Roman numerals
355 <paragraph>
356 (CIVIL) is another such word
357 <enumerated_list enumtype="upperroman" prefix="(" suffix=")">
358 <list_item>
359 <paragraph>
360 I
361 <paragraph>
362 (IVXLCDM) IVXLCDM
363 """],
364 ["""\
365 Potentially ambiguous cases:
366
367 A. Item A.
368 B. Item B.
369 C. Item C.
370
371 I. Item I.
372 II. Item II.
373 III. Item III.
374
375 a. Item a.
376 b. Item b.
377 c. Item c.
378
379 i. Item i.
380 ii. Item ii.
381 iii. Item iii.
382
383 Phew! Safe!
384 """,
385 """\
386 <document source="test data">
387 <paragraph>
388 Potentially ambiguous cases:
389 <enumerated_list enumtype="upperalpha" prefix="" suffix=".">
390 <list_item>
391 <paragraph>
392 Item A.
393 <list_item>
394 <paragraph>
395 Item B.
396 <list_item>
397 <paragraph>
398 Item C.
399 <enumerated_list enumtype="upperroman" prefix="" suffix=".">
400 <list_item>
401 <paragraph>
402 Item I.
403 <list_item>
404 <paragraph>
405 Item II.
406 <list_item>
407 <paragraph>
408 Item III.
409 <enumerated_list enumtype="loweralpha" prefix="" suffix=".">
410 <list_item>
411 <paragraph>
412 Item a.
413 <list_item>
414 <paragraph>
415 Item b.
416 <list_item>
417 <paragraph>
418 Item c.
419 <enumerated_list enumtype="lowerroman" prefix="" suffix=".">
420 <list_item>
421 <paragraph>
422 Item i.
423 <list_item>
424 <paragraph>
425 Item ii.
426 <list_item>
427 <paragraph>
428 Item iii.
429 <paragraph>
430 Phew! Safe!
431 """],
432 ["""\
433 Definitely ambiguous:
434
435 A. Item A.
436 B. Item B.
437 C. Item C.
438 D. Item D.
439 E. Item E.
440 F. Item F.
441 G. Item G.
442 H. Item H.
443 I. Item I.
444 II. Item II.
445 III. Item III.
446
447 a. Item a.
448 b. Item b.
449 c. Item c.
450 d. Item d.
451 e. Item e.
452 f. Item f.
453 g. Item g.
454 h. Item h.
455 i. Item i.
456 ii. Item ii.
457 iii. Item iii.
458 """,
459 """\
460 <document source="test data">
461 <paragraph>
462 Definitely ambiguous:
463 <enumerated_list enumtype="upperalpha" prefix="" suffix=".">
464 <list_item>
465 <paragraph>
466 Item A.
467 <list_item>
468 <paragraph>
469 Item B.
470 <list_item>
471 <paragraph>
472 Item C.
473 <list_item>
474 <paragraph>
475 Item D.
476 <list_item>
477 <paragraph>
478 Item E.
479 <list_item>
480 <paragraph>
481 Item F.
482 <list_item>
483 <paragraph>
484 Item G.
485 <list_item>
486 <paragraph>
487 Item H.
488 <system_message level="2" line="11" source="test data" type="WARNING">
489 <paragraph>
490 Enumerated list ends without a blank line; unexpected unindent.
491 <enumerated_list enumtype="upperroman" prefix="" suffix=".">
492 <list_item>
493 <paragraph>
494 Item I.
495 <list_item>
496 <paragraph>
497 Item II.
498 <list_item>
499 <paragraph>
500 Item III.
501 <enumerated_list enumtype="loweralpha" prefix="" suffix=".">
502 <list_item>
503 <paragraph>
504 Item a.
505 <list_item>
506 <paragraph>
507 Item b.
508 <list_item>
509 <paragraph>
510 Item c.
511 <list_item>
512 <paragraph>
513 Item d.
514 <list_item>
515 <paragraph>
516 Item e.
517 <list_item>
518 <paragraph>
519 Item f.
520 <list_item>
521 <paragraph>
522 Item g.
523 <list_item>
524 <paragraph>
525 Item h.
526 <system_message level="2" line="23" source="test data" type="WARNING">
527 <paragraph>
528 Enumerated list ends without a blank line; unexpected unindent.
529 <enumerated_list enumtype="lowerroman" prefix="" suffix=".">
530 <list_item>
531 <paragraph>
532 Item i.
533 <list_item>
534 <paragraph>
535 Item ii.
536 <list_item>
537 <paragraph>
538 Item iii.
539 """],
540 ["""\
541 Different enumeration formats:
542
543 1. Item 1.
544 2. Item 2.
545 3. Item 3.
546
547 1) Item 1).
548 2) Item 2).
549 3) Item 3).
550
551 (1) Item (1).
552 (2) Item (2).
553 (3) Item (3).
554 """,
555 """\
556 <document source="test data">
557 <paragraph>
558 Different enumeration formats:
559 <enumerated_list enumtype="arabic" prefix="" suffix=".">
560 <list_item>
561 <paragraph>
562 Item 1.
563 <list_item>
564 <paragraph>
565 Item 2.
566 <list_item>
567 <paragraph>
568 Item 3.
569 <enumerated_list enumtype="arabic" prefix="" suffix=")">
570 <list_item>
571 <paragraph>
572 Item 1).
573 <list_item>
574 <paragraph>
575 Item 2).
576 <list_item>
577 <paragraph>
578 Item 3).
579 <enumerated_list enumtype="arabic" prefix="(" suffix=")">
580 <list_item>
581 <paragraph>
582 Item (1).
583 <list_item>
584 <paragraph>
585 Item (2).
586 <list_item>
587 <paragraph>
588 Item (3).
589 """],
590 ["""\
591 Nested enumerated lists:
592
593 1. Item 1.
594
595 A) Item A).
596 B) Item B).
597 C) Item C).
598
599 2. Item 2.
600
601 (a) Item (a).
602
603 I) Item I).
604 II) Item II).
605 III) Item III).
606
607 (b) Item (b).
608
609 (c) Item (c).
610
611 (i) Item (i).
612 (ii) Item (ii).
613 (iii) Item (iii).
614
615 3. Item 3.
616 """,
617 """\
618 <document source="test data">
619 <paragraph>
620 Nested enumerated lists:
621 <enumerated_list enumtype="arabic" prefix="" suffix=".">
622 <list_item>
623 <paragraph>
624 Item 1.
625 <enumerated_list enumtype="upperalpha" prefix="" suffix=")">
626 <list_item>
627 <paragraph>
628 Item A).
629 <list_item>
630 <paragraph>
631 Item B).
632 <list_item>
633 <paragraph>
634 Item C).
635 <list_item>
636 <paragraph>
637 Item 2.
638 <enumerated_list enumtype="loweralpha" prefix="(" suffix=")">
639 <list_item>
640 <paragraph>
641 Item (a).
642 <enumerated_list enumtype="upperroman" prefix="" suffix=")">
643 <list_item>
644 <paragraph>
645 Item I).
646 <list_item>
647 <paragraph>
648 Item II).
649 <list_item>
650 <paragraph>
651 Item III).
652 <list_item>
653 <paragraph>
654 Item (b).
655 <list_item>
656 <paragraph>
657 Item (c).
658 <enumerated_list enumtype="lowerroman" prefix="(" suffix=")">
659 <list_item>
660 <paragraph>
661 Item (i).
662 <list_item>
663 <paragraph>
664 Item (ii).
665 <list_item>
666 <paragraph>
667 Item (iii).
668 <list_item>
669 <paragraph>
670 Item 3.
671 """],
672 ["""\
673 A. Einstein was a great influence on
674 B. Physicist, who was a colleague of
675 C. Chemist. They all worked in
676 Princeton, NJ.
677
678 Using a non-breaking space as a workaround:
679
680 A.\u00a0Einstein was a great influence on
681 B. Physicist, who was a colleague of
682 C. Chemist. They all worked in
683 Princeton, NJ.
684 """,
685 """\
686 <document source="test data">
687 <enumerated_list enumtype="upperalpha" prefix="" suffix=".">
688 <list_item>
689 <paragraph>
690 Einstein was a great influence on
691 <list_item>
692 <paragraph>
693 Physicist, who was a colleague of
694 <system_message level="2" line="3" source="test data" type="WARNING">
695 <paragraph>
696 Enumerated list ends without a blank line; unexpected unindent.
697 <paragraph>
698 C. Chemist. They all worked in
699 Princeton, NJ.
700 <paragraph>
701 Using a non-breaking space as a workaround:
702 <paragraph>
703 A.\xa0Einstein was a great influence on
704 B. Physicist, who was a colleague of
705 C. Chemist. They all worked in
706 Princeton, NJ.
707 """],
708 ["""\
709 1. Item one: line 1,
710 line 2.
711 2. Item two: line 1,
712 line 2.
713 3. Item three: paragraph 1, line 1,
714 line 2.
715
716 Paragraph 2.
717 """,
718 """\
719 <document source="test data">
720 <enumerated_list enumtype="arabic" prefix="" suffix=".">
721 <list_item>
722 <paragraph>
723 Item one: line 1,
724 line 2.
725 <list_item>
726 <paragraph>
727 Item two: line 1,
728 <system_message level="2" line="4" source="test data" type="WARNING">
729 <paragraph>
730 Enumerated list ends without a blank line; unexpected unindent.
731 <block_quote>
732 <paragraph>
733 line 2.
734 <system_message level="2" line="5" source="test data" type="WARNING">
735 <paragraph>
736 Block quote ends without a blank line; unexpected unindent.
737 <enumerated_list enumtype="arabic" prefix="" start="3" suffix=".">
738 <list_item>
739 <paragraph>
740 Item three: paragraph 1, line 1,
741 <system_message level="1" line="5" source="test data" type="INFO">
742 <paragraph>
743 Enumerated list start value not ordinal-1: "3" (ordinal 3)
744 <system_message level="2" line="6" source="test data" type="WARNING">
745 <paragraph>
746 Enumerated list ends without a blank line; unexpected unindent.
747 <block_quote>
748 <paragraph>
749 line 2.
750 <block_quote>
751 <paragraph>
752 Paragraph 2.
753 """],
754 ["""\
755 1. Item one.
756
757 #. Item two.
758
759 #. Item three.
760 """,
761 """\
762 <document source="test data">
763 <enumerated_list enumtype="arabic" prefix="" suffix=".">
764 <list_item>
765 <paragraph>
766 Item one.
767 <list_item>
768 <paragraph>
769 Item two.
770 <list_item>
771 <paragraph>
772 Item three.
773 """],
774 ["""\
775 a. Item one.
776 #. Item two.
777 #. Item three.
778 """,
779 """\
780 <document source="test data">
781 <enumerated_list enumtype="loweralpha" prefix="" suffix=".">
782 <list_item>
783 <paragraph>
784 Item one.
785 <list_item>
786 <paragraph>
787 Item two.
788 <list_item>
789 <paragraph>
790 Item three.
791 """],
792 ["""\
793 i. Item one.
794 ii. Item two.
795 #. Item three.
796 """,
797 """\
798 <document source="test data">
799 <enumerated_list enumtype="lowerroman" prefix="" suffix=".">
800 <list_item>
801 <paragraph>
802 Item one.
803 <list_item>
804 <paragraph>
805 Item two.
806 <list_item>
807 <paragraph>
808 Item three.
809 """],
810 ["""\
811 #. Item one.
812 #. Item two.
813 #. Item three.
814 """,
815 """\
816 <document source="test data">
817 <enumerated_list enumtype="arabic" prefix="" suffix=".">
818 <list_item>
819 <paragraph>
820 Item one.
821 <list_item>
822 <paragraph>
823 Item two.
824 <list_item>
825 <paragraph>
826 Item three.
827 """],
828 ["""\
829 1. Item one.
830 #. Item two.
831 3. Item three.
832 """,
833 """\
834 <document source="test data">
835 <enumerated_list enumtype="arabic" prefix="" suffix=".">
836 <list_item>
837 <paragraph>
838 Item one.
839 <system_message level="2" line="2" source="test data" type="WARNING">
840 <paragraph>
841 Enumerated list ends without a blank line; unexpected unindent.
842 <paragraph>
843 #. Item two.
844 3. Item three.
845 """],
846 ["""\
847 z.
848 x
849 """,
850 """\
851 <document source="test data">
852 <paragraph>
853 z.
854 x
855 """],
856 ["""\
857 3-space indent, with a trailing space:
858
859 1. \n\
860 foo
861
862 3-space indent, no trailing space:
863
864 1.
865 foo
866
867 2-space indent:
868
869 1.
870 foo
871
872 1-space indent:
873
874 1.
875 foo
876
877 0-space indent, not a list item:
878
879 1.
880 foo
881
882 No item content:
883
884 1.
885 """,
886 """\
887 <document source="test data">
888 <paragraph>
889 3-space indent, with a trailing space:
890 <enumerated_list enumtype="arabic" prefix="" suffix=".">
891 <list_item>
892 <paragraph>
893 foo
894 <paragraph>
895 3-space indent, no trailing space:
896 <enumerated_list enumtype="arabic" prefix="" suffix=".">
897 <list_item>
898 <paragraph>
899 foo
900 <paragraph>
901 2-space indent:
902 <enumerated_list enumtype="arabic" prefix="" suffix=".">
903 <list_item>
904 <paragraph>
905 foo
906 <paragraph>
907 1-space indent:
908 <enumerated_list enumtype="arabic" prefix="" suffix=".">
909 <list_item>
910 <paragraph>
911 foo
912 <paragraph>
913 0-space indent, not a list item:
914 <paragraph>
915 1.
916 foo
917 <paragraph>
918 No item content:
919 <enumerated_list enumtype="arabic" prefix="" suffix=".">
920 <list_item>
921 """],
922 ]
923
924
925 if __name__ == '__main__':
926 unittest.main()