Debian Codegraph @

test/test_parsers/test_rst/test_TableParser.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_TableParser.py 10251 2025-09-22 21:00:13Z 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.parsers.rst import tableparser
20 from docutils.statemachine import StringList, string2lines
21
22
23 class GridTableParserTestCase(unittest.TestCase):
24 def test_parse_table(self):
25 parser = tableparser.GridTableParser()
26 for cases in totest.values():
27 for case in cases:
28 case_input, case_table, _case_expected = case
29 lines_input = StringList(string2lines(case_input), 'test data')
30 parser.setup(lines_input)
31 try:
32 parser.find_head_body_sep()
33 parser.parse_table()
34 output = parser.cells
35 except Exception as details:
36 output = f'{details.__class__.__name__}: {details}'
37 self.assertEqual(output, case_table)
38
39 def test_parse(self):
40 parser = tableparser.GridTableParser()
41 for name, cases in totest.items():
42 for casenum, case in enumerate(cases):
43 case_input, _case_table, case_expected = case
44 lines_input = StringList(string2lines(case_input), 'test data')
45 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
46 try:
47 output = parser.parse(lines_input)
48 except Exception as details:
49 output = f'{details.__class__.__name__}: {details}'
50 self.assertEqual(case_expected, output)
51
52
53 totest = {}
54
55 totest['grid_tables'] = [
56 ["""\
57 +-------------------------------------+
58 | A table with one cell and one line. |
59 +-------------------------------------+
60 """,
61 [(0, 0, 2, 38, ['A table with one cell and one line.'])],
62 ([37],
63 [],
64 [[(0, 0, 1, ['A table with one cell and one line.'])]])],
65 ["""\
66 +--------------+--------------+
67 | A table with | two columns. |
68 +--------------+--------------+
69 """,
70 [(0, 0, 2, 15, ['A table with']),
71 (0, 15, 2, 30, ['two columns.'])],
72 ([14, 14],
73 [],
74 [[(0, 0, 1, ['A table with']),
75 (0, 0, 1, ['two columns.'])]])],
76 # Combining chars in table cells
77 ["""\
78 +--------------+------------------+
79 | A tāble w̅ith | comb̲ining chars. |
80 +--------------+------------------+
81 """,
82 [(0, 0, 2, 15, ['A tāble w̅ith']),
83 (0, 15, 2, 34, ['comb̲ining chars.'])],
84 ([14, 18],
85 [],
86 [[(0, 0, 1, ['A tāble w̅ith']),
87 (0, 0, 1, ['comb̲ining chars.'])]])],
88 ["""\
89 +--------------+-------------+
90 | A tāble w̅ith | two columns |
91 +--------------+-------------+
92 | and | two rows. |
93 +--------------+-------------+
94 """,
95 [(0, 0, 2, 15, ['A tāble w̅ith']),
96 (0, 15, 2, 29, ['two columns']),
97 (2, 0, 4, 15, ['and']),
98 (2, 15, 4, 29, ['two rows.'])],
99 ([14, 13],
100 [],
101 [[(0, 0, 1, ['A tāble w̅ith']),
102 (0, 0, 1, ['two columns'])],
103 [(0, 0, 3, ['and']),
104 (0, 0, 3, ['two rows.'])]])],
105 ["""\
106 +--------------------------+
107 | A table with three rows, |
108 +------------+-------------+
109 | and two | columns. |
110 +------------+-------------+
111 | First and last rows |
112 | contain column spans. |
113 +--------------------------+
114 """,
115 [(0, 0, 2, 27, ['A table with three rows,']),
116 (2, 0, 4, 13, ['and two']),
117 (2, 13, 4, 27, ['columns.']),
118 (4, 0, 7, 27, ['First and last rows', 'contain column spans.'])],
119 ([12, 13],
120 [],
121 [[(0, 1, 1, ['A table with three rows,']),
122 None],
123 [(0, 0, 3, ['and two']),
124 (0, 0, 3, ['columns.'])],
125 [(0, 1, 5, ['First and last rows', 'contain column spans.']),
126 None]])],
127 ["""\
128 +------------+-------------+---------------+
129 | A tāble | two rows in | and row spans |
130 | with t̲h̲r̲e̲e̲ +-------------+ to left and |
131 | columns, | the middle, | right. |
132 +------------+-------------+---------------+
133 """,
134 [(0, 0, 4, 13, ['A tāble', 'with t̲h̲r̲e̲e̲', 'columns,']),
135 (0, 13, 2, 27, ['two rows in']),
136 (0, 27, 4, 43, ['and row spans', 'to left and', 'right.']),
137 (2, 13, 4, 27, ['the middle,'])],
138 ([12, 13, 15],
139 [],
140 [[(1, 0, 1, ['A tāble', 'with t̲h̲r̲e̲e̲', 'columns,']),
141 (0, 0, 1, ['two rows in']),
142 (1, 0, 1, ['and row spans', 'to left and', 'right.'])],
143 [None,
144 (0, 0, 3, ['the middle,']),
145 None]])],
146 ["""\
147 +------------+-------------+---------------+
148 | A table | | two rows in | and funny |
149 | with 3 +--+-------------+-+ stuff. |
150 | columns, | the middle, | | |
151 +------------+-------------+---------------+
152 """,
153 [(0, 0, 4, 13, ['A table |', 'with 3 +--', 'columns,']),
154 (0, 13, 2, 27, ['two rows in']),
155 (0, 27, 4, 43, [' and funny', '-+ stuff.', ' |']),
156 (2, 13, 4, 27, ['the middle,'])],
157 ([12, 13, 15],
158 [],
159 [[(1, 0, 1, ['A table |', 'with 3 +--', 'columns,']),
160 (0, 0, 1, ['two rows in']),
161 (1, 0, 1, [' and funny', '-+ stuff.', ' |'])],
162 [None,
163 (0, 0, 3, ['the middle,']),
164 None]])],
165 ["""\
166 +-----------+-------------------------+
167 | W/NW cell | N/NE cell |
168 | +-------------+-----------+
169 | | Middle cell | E/SE cell |
170 +-----------+-------------+ |
171 | S/SE cell | |
172 +-------------------------+-----------+
173 """,
174 [(0, 0, 4, 12, ['W/NW cell', '', '']),
175 (0, 12, 2, 38, ['N/NE cell']),
176 (2, 12, 4, 26, ['Middle cell']),
177 (2, 26, 6, 38, ['E/SE cell', '', '']),
178 (4, 0, 6, 26, ['S/SE cell'])],
179 ([11, 13, 11],
180 [],
181 [[(1, 0, 1, ['W/NW cell', '', '']),
182 (0, 1, 1, ['N/NE cell']),
183 None],
184 [None,
185 (0, 0, 3, ['Middle cell']),
186 (1, 0, 3, ['E/SE cell', '', ''])],
187 [(0, 1, 5, ['S/SE cell']),
188 None,
189 None]])],
190 ["""\
191 +--------------+-------------+
192 | A bad table. | |
193 +--------------+ |
194 | Cells must be rectangles. |
195 +----------------------------+
196 """,
197 'TableMarkupError: Malformed table; parse incomplete.',
198 'TableMarkupError: Malformed table; parse incomplete.'],
199 ["""\
200 +-------------------------------+
201 | A table with two header rows, |
202 +------------+------------------+
203 | the first | with a span. |
204 +============+==================+
205 | Two body | rows, |
206 +------------+------------------+
207 | the second with a span. |
208 +-------------------------------+
209 """,
210 [(0, 0, 2, 32, ['A table with two header rows,']),
211 (2, 0, 4, 13, ['the first']),
212 (2, 13, 4, 32, ['with a span.']),
213 (4, 0, 6, 13, ['Two body']),
214 (4, 13, 6, 32, ['rows,']),
215 (6, 0, 8, 32, ['the second with a span.'])],
216 ([12, 18],
217 [[(0, 1, 1, ['A table with two header rows,']),
218 None],
219 [(0, 0, 3, ['the first']),
220 (0, 0, 3, ['with a span.'])]],
221 [[(0, 0, 5, ['Two body']),
222 (0, 0, 5, ['rows,'])],
223 [(0, 1, 7, ['the second with a span.']),
224 None]])],
225 ["""\
226 +-------------------------------+
227 | A table with two head/body |
228 +=============+=================+
229 | row | separators. |
230 +=============+=================+
231 | That's bad. | |
232 +-------------+-----------------+
233 """,
234 'TableMarkupError: Multiple head/body row separators '
235 '(table lines 3 and 5); only one allowed.',
236 'TableMarkupError: Multiple head/body row separators '
237 '(table lines 3 and 5); only one allowed.'],
238 ["""\
239 +-------------------------------------+
240 | |
241 +-------------------------------------+
242 """,
243 [(0, 0, 2, 38, [''])],
244 ([37],
245 [],
246 [[(0, 0, 1, [''])]])],
247 ]
248
249
250 if __name__ == '__main__':
251 unittest.main()