1 #! /usr/bin/env python3
4 # Author: David Goodger <goodger@python.org>
5 # Copyright: This module has been placed in the public domain.
13 from pathlib
import Path
17 if __name__
== '__main__':
18 # prepend the "docutils root" to the Python library path
19 # so we import the local `docutils` package.
20 sys
.path
.insert(0, str(Path(__file__
).resolve().parents
[3]))
22 from docutils
.frontend
import get_default_settings
23 from docutils
.parsers
.rst
import Parser
24 from docutils
.utils
import new_document
26 # TEST_ROOT is ./test/ from the docutils root
27 TEST_ROOT
= os
.path
.abspath(os
.path
.join(__file__
, '..', '..', '..'))
30 class ParserTestCase(unittest
.TestCase
):
31 def test_parser(self
):
33 settings
= get_default_settings(Parser
)
34 settings
.warning_stream
= ''
35 settings
.halt_level
= 5
36 for name
, 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 output
= document
.pformat()
42 self
.assertEqual(case_expected
, output
)
45 mydir
= os
.path
.join(TEST_ROOT
, 'test_parsers/test_rst')
46 include2
= os
.path
.join(mydir
, 'test_directives/include2.txt')
50 totest
['grid_tables'] = [
52 +-------------------------------------+
53 | A table with one cell and one line. |
54 +-------------------------------------+
57 <document source="test data">
60 <colspec colwidth="37">
65 A table with one cell and one line.
68 +-----------------------+
69 | A table with one cell |
71 +-----------------------+
74 <document source="test data">
77 <colspec colwidth="23">
86 +-----------------------+
87 | A malformed table. |
88 +-----------------------+
91 <document source="test data">
92 <system_message level="3" line="1" source="test data" type="ERROR">
95 <literal_block xml:space="preserve">
96 +-----------------------+
97 | A malformed table. |
98 +-----------------------+
101 +------------------------+
102 | A well-formed | table. |
103 +------------------------+
105 +------------------------+
106 | This +----------+ too! |
107 +------------------------+
110 <document source="test data">
113 <colspec colwidth="24">
118 A well-formed | table.
121 <colspec colwidth="24">
126 This +----------+ too!
129 +--------------+--------------+
130 | A table with | two columns. |
131 +--------------+--------------+
134 <document source="test data">
137 <colspec colwidth="14">
138 <colspec colwidth="14">
156 <document source="test data">
159 <colspec colwidth="14">
171 +--------------+-------------+
172 | A table with | two columns |
173 +--------------+-------------+
175 +--------------+-------------+
178 <document source="test data">
181 <colspec colwidth="14">
182 <colspec colwidth="13">
200 +--------------+---------------+
201 | A table with | two columns, |
202 +--------------+---------------+
203 | two rows, and a column span. |
204 +------------------------------+
207 <document source="test data">
210 <colspec colwidth="14">
211 <colspec colwidth="15">
223 two rows, and a column span.
226 +--------------------------+
227 | A table with three rows, |
228 +------------+-------------+
229 | and two | columns. |
230 +------------+-------------+
231 | First and last rows |
232 | contains column spans. |
233 +--------------------------+
236 <document source="test data">
239 <colspec colwidth="12">
240 <colspec colwidth="13">
245 A table with three rows,
257 contains column spans.
260 +--------------+--------------+
261 | A table with | two columns, |
262 +--------------+ and a row |
263 | two rows, | span. |
264 +--------------+--------------+
267 <document source="test data">
270 <colspec colwidth="14">
271 <colspec colwidth="14">
288 +------------+-------------+---------------+
289 | A table | two rows in | and row spans |
290 | with three +-------------+ to left and |
291 | columns, | the middle, | right. |
292 +------------+-------------+---------------+
295 <document source="test data">
298 <colspec colwidth="12">
299 <colspec colwidth="13">
300 <colspec colwidth="15">
322 Complex spanning pattern (no edge knows all rows/cols):
324 +-----------+-------------------------+
325 | W/NW cell | N/NE cell |
326 | +-------------+-----------+
327 | | Middle cell | E/SE cell |
328 +-----------+-------------+ |
330 +-------------------------+-----------+
333 <document source="test data">
335 Complex spanning pattern (no edge knows all rows/cols):
338 <colspec colwidth="11">
339 <colspec colwidth="13">
340 <colspec colwidth="11">
362 +------------------------+------------+----------+----------+
363 | Header row, column 1 | Header 2 | Header 3 | Header 4 |
364 +========================+============+==========+==========+
365 | body row 1, column 1 | column 2 | column 3 | column 4 |
366 +------------------------+------------+----------+----------+
367 | body row 2 | Cells may span columns. |
368 +------------------------+------------+---------------------+
369 | body row 3 | Cells may | - Table cells |
370 +------------------------+ span rows. | - contain |
371 | body row 4 | | - body elements. |
372 +------------------------+------------+---------------------+
375 <document source="test data">
378 <colspec colwidth="24">
379 <colspec colwidth="12">
380 <colspec colwidth="10">
381 <colspec colwidth="10">
416 Cells may span columns.
425 <entry morecols="1" morerows="1">
426 <bullet_list bullet="-">
442 +-----------------+--------+
443 | A simple table | cell 2 |
444 +-----------------+--------+
446 +-----------------+--------+
447 No blank line after table.
450 <document source="test data">
453 <colspec colwidth="17">
454 <colspec colwidth="8">
470 <system_message level="2" line="6" source="test data" type="WARNING">
472 Blank line required after table.
474 No blank line after table.
477 +-----------------+--------+
478 | A simple table | cell 2 |
479 +-----------------+--------+
481 +-----------------+--------+
482 Unexpected indent and no blank line after table.
485 <document source="test data">
488 <colspec colwidth="17">
489 <colspec colwidth="8">
505 <system_message level="3" line="6" source="test data" type="ERROR">
507 Unexpected indentation.
508 <system_message level="2" line="6" source="test data" type="WARNING">
510 Blank line required after table.
513 Unexpected indent and no blank line after table.
516 +--------------+-------------+
519 | Cells must be rectangles. |
520 +----------------------------+
523 <document source="test data">
524 <system_message level="3" line="1" source="test data" type="ERROR">
527 Malformed table; parse incomplete.
528 <literal_block xml:space="preserve">
529 +--------------+-------------+
532 | Cells must be rectangles. |
533 +----------------------------+
536 +------------------------------+
537 | This table contains another. |
539 | +-------------------------+ |
540 | | A table within a table. | |
541 | +-------------------------+ |
542 +------------------------------+
545 <document source="test data">
548 <colspec colwidth="30">
553 This table contains another.
556 <colspec colwidth="25">
561 A table within a table.
564 +------------------+--------+
566 +------------------+--------+
567 | with empty cells | |
568 +------------------+--------+
571 <document source="test data">
574 <colspec colwidth="18">
575 <colspec colwidth="8">
589 +------------------------------------------------------------------------------+
592 +------------------------------------------------------------------------------+
593 | (The first cell of this table may expand |
594 | to accommodate long filesystem paths.) |
595 +------------------------------------------------------------------------------+
596 """) % ('\n'.join('| %-70s |' % include2
[part
* 70 : (part
+ 1) * 70] # noqa: E203
597 for part
in range(len(include2
) // 70 + 1))),
599 <document source="test data">
602 <colspec colwidth="78">
607 Here are some paragraphs
608 that can appear at any level.
610 This file (include2.txt) is used by
617 (The first cell of this table may expand
618 to accommodate long filesystem paths.)
623 +------------------------------------------------------------------------------+
626 +------------------------------------------------------------------------------+
628 Something afterwards.
631 """) % ('\n'.join('| %-70s |' % include2
[part
* 70 : (part
+ 1) * 70] # noqa: E203
632 for part
in range(len(include2
) // 70 + 1))),
634 <document source="test data">
639 <colspec colwidth="78">
644 Here are some paragraphs
645 that can appear at any level.
647 This file (include2.txt) is used by
652 Something afterwards.
658 totest
['simple_tables'] = [
660 ============ ============
661 A table with two columns.
662 ============ ============
667 <document source="test data">
670 <colspec colwidth="12">
671 <colspec colwidth="12">
684 ============ ============
685 A table with two columns
687 ============ ============
690 <document source="test data">
693 <colspec colwidth="12">
694 <colspec colwidth="12">
712 ============ ==============
713 A table with two columns,
714 two rows, and a column span.
715 ============================
718 <document source="test data">
721 <colspec colwidth="12">
722 <colspec colwidth="14">
734 two rows, and a column span.
737 == =========== ===========
738 1 A table with three rows,
739 -- ------------------------
741 3 First and third rows
742 contain column spans.
744 This row is a multi-line row, and overflows to the right.
745 -- ------------------------
747 == =========== ===========
750 <document source="test data">
753 <colspec colwidth="2">
754 <colspec colwidth="11">
755 <colspec colwidth="44">
763 A table with three rows,
781 contain column spans.
783 This row is a multi-line row, and overflows to the right.
796 ======= ========= ========
797 A table with three columns.
798 ================== ========
801 <document source="test data">
804 <colspec colwidth="7">
805 <colspec colwidth="9">
806 <colspec colwidth="8">
817 ============== ======
818 A simple table this text extends to the right
819 cell 3 as does this text
820 ============== ======
823 <document source="test data">
826 <colspec colwidth="14">
827 <colspec colwidth="30">
835 this text extends to the right
845 ============== ======
846 A simple table this text extends to the right
847 continuation of cell 2
848 ============== ======
851 <document source="test data">
854 <colspec colwidth="14">
855 <colspec colwidth="30">
863 this text extends to the right
864 continuation of cell 2
867 ============== ======
872 <document source="test data">
873 <system_message level="3" line="1" source="test data" type="ERROR">
876 No bottom table border found.
877 <literal_block xml:space="preserve">
878 ============== ======
883 ============== ======
884 A simple table cell 2
886 ============== ======
887 No blank line after table.
890 <document source="test data">
891 <system_message level="3" line="1" source="test data" type="ERROR">
894 No bottom table border found or no blank line after table bottom.
895 <literal_block xml:space="preserve">
896 ============== ======
897 A simple table cell 2
899 ============== ======
900 <system_message level="2" line="5" source="test data" type="WARNING">
902 Blank line required after table.
904 No blank line after table.
907 ============== ======
908 A simple table cell 2
909 ============== ======
911 ============== ======
912 No blank line after table.
915 <document source="test data">
918 <colspec colwidth="14">
919 <colspec colwidth="6">
936 <system_message level="2" line="6" source="test data" type="WARNING">
938 Blank line required after table.
940 No blank line after table.
943 ============== ======
944 A simple table cell 2
946 ============== ======
947 Unexpected indent and no blank line after table.
950 <document source="test data">
951 <system_message level="3" line="1" source="test data" type="ERROR">
954 No bottom table border found or no blank line after table bottom.
955 <literal_block xml:space="preserve">
956 ============== ======
957 A simple table cell 2
959 ============== ======
960 <system_message level="2" line="5" source="test data" type="WARNING">
962 Blank line required after table.
965 Unexpected indent and no blank line after table.
968 ============== ======
971 ============ ========
974 <document source="test data">
975 <system_message level="3" line="4" source="test data" type="ERROR">
978 Column span alignment problem in table line 4.
979 <literal_block xml:space="preserve">
980 ============== ======
983 ============ ========
992 <document source="test data">
993 <system_message level="3" line="2" source="test data" type="ERROR">
996 Text in column margin in table line 2.
997 <literal_block xml:space="preserve">
1004 == ============================
1005 1 This table contains another.
1006 2 ======= ====== ========
1007 A table within a table.
1008 ======= ====== ========
1010 The outer table does have to
1011 have at least two columns
1013 == ============================
1016 <document source="test data">
1019 <colspec colwidth="2">
1020 <colspec colwidth="28">
1028 This table contains another.
1036 <colspec colwidth="7">
1037 <colspec colwidth="6">
1038 <colspec colwidth="8">
1051 The outer table does have to
1052 have at least two columns
1056 ================ ======
1059 ================ ======
1062 <document source="test data">
1065 <colspec colwidth="16">
1066 <colspec colwidth="6">
1080 ============== ========
1082 ============== ========
1085 ============== ========
1088 <document source="test data">
1091 <colspec colwidth="14">
1092 <colspec colwidth="8">
1111 ============== ======
1112 A simple table this text extends to the right
1113 cell 3 the bottom border below is too long
1114 ============== ========
1117 <document source="test data">
1118 <system_message level="3" line="1" source="test data" type="ERROR">
1121 Bottom/header table border does not match top border.
1122 <literal_block xml:space="preserve">
1123 ============== ======
1124 A simple table this text extends to the right
1125 cell 3 the bottom border below is too long
1126 ============== ========
1129 ============ =================
1130 A table with row separators.
1131 ------------ -----------------
1134 ------------ -----------------
1136 Blank lines before and after.
1138 ------------ -----------------
1141 ============ =================
1144 <document source="test data">
1147 <colspec colwidth="12">
1148 <colspec colwidth="17">
1180 ============ ====================
1181 A table with many row separators.
1182 ------------ --------------------
1183 ------------ --------------------
1185 ------------ --------------------
1186 ============ ====================
1189 <document source="test data">
1192 <colspec colwidth="12">
1193 <colspec colwidth="20">
1201 many row separators.
1213 == =========== ===========
1214 1 Span columns 2 & 3
1215 -- ------------------------
1216 2 Span columns 2 & 3
1217 ------------------------
1219 == =========== ===========
1221 == =========== ===========
1222 1 Span cols 1&2 but not 3
1223 --------------- -----------
1224 2 Span cols 1&2 but not 3
1227 == =========== ===========
1229 == =========== ===========
1230 1 Not a span Not a span
1231 ----------- -----------
1233 == =========== ===========
1236 <document source="test data">
1237 <system_message level="3" line="4" source="test data" type="ERROR">
1240 Text in column margin in table line 4.
1241 <literal_block xml:space="preserve">
1242 == =========== ===========
1243 1 Span columns 2 & 3
1244 -- ------------------------
1245 2 Span columns 2 & 3
1246 ------------------------
1248 == =========== ===========
1249 <system_message level="3" line="13" source="test data" type="ERROR">
1252 Column span incomplete in table line 5.
1253 <literal_block xml:space="preserve">
1254 == =========== ===========
1255 1 Span cols 1&2 but not 3
1256 --------------- -----------
1257 2 Span cols 1&2 but not 3
1260 == =========== ===========
1263 <colspec colwidth="2">
1264 <colspec colwidth="11">
1265 <colspec colwidth="11">
1272 <system_message level="4" line="19" source="test data" type="SEVERE">
1274 Unexpected section title.
1275 <literal_block xml:space="preserve">
1279 <system_message level="4" line="19" source="test data" type="SEVERE">
1281 Unexpected section title.
1282 <literal_block xml:space="preserve">
1293 ========= =====================================================================
1294 Inclusion .. include::
1296 Note The first row of this table may expand
1297 to accommodate long filesystem paths.
1298 ========= =====================================================================
1299 """ % ('\n'.join(' %-65s' % include2
[part
* 65 : (part
+ 1) * 65] # noqa: E203
1300 for part
in range(len(include2
) // 65 + 1))),
1302 <document source="test data">
1305 <colspec colwidth="9">
1306 <colspec colwidth="69">
1314 Here are some paragraphs
1315 that can appear at any level.
1317 This file (include2.txt) is used by
1327 The first row of this table may expand
1328 to accommodate long filesystem paths.
1333 if __name__
== '__main__':