Debian Codegraph @

test/test_parsers/test_rst/test_SimpleTableParser.py

raw symbols SCIP
1 #! /usr/bin/env python3
2 # $Id: test_SimpleTableParser.py 9425 2023-06-30 14:56:47Z 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 SimpleTableParserTestCase(unittest.TestCase):
24 def test_parse(self):
25 parser = tableparser.SimpleTableParser()
26 for name, cases in totest.items():
27 for casenum, (case_input, case_expected) in enumerate(cases):
28 lines_input = StringList(string2lines(case_input), 'test data')
29 with self.subTest(id=f'totest[{name!r}][{casenum}]'):
30 try:
31 output = parser.parse(lines_input)
32 except Exception as details:
33 output = f'{details.__class__.__name__}: {details}'
34 self.assertEqual(case_expected, output)
35
36
37 totest = {}
38
39 totest['simple_tables'] = [
40 ["""\
41 ============ ============
42 A table with two columns.
43 ============ ============
44 """,
45 ([12, 12],
46 [],
47 [[[0, 0, 1, ['A table with']],
48 [0, 0, 1, ['two columns.']]]])],
49 ["""\
50 ============ ===============
51 A tāble w̅ith comb̲ining chars
52 ============ ===============
53 """,
54 ([12, 15],
55 [],
56 [[[0, 0, 1, ['A ta\u0304ble w\u0305ith']],
57 [0, 0, 1, ['comb\u0332ining chars']]]])],
58 ["""\
59 ============ ============
60 A table with two columns
61 and two rows.
62 ============ ============
63 """,
64 ([12, 12],
65 [],
66 [[[0, 0, 1, ['A table with']],
67 [0, 0, 1, ['two columns']]],
68 [[0, 0, 2, ['and']],
69 [0, 0, 2, ['two rows.']]]])],
70 ["""\
71 ======================================
72 The last row might stick into the margin
73 second row.
74 ======================================
75 """,
76 ([40],
77 [],
78 [[[0, 0, 1, ['The last row might stick into the margin']]],
79 [[0, 0, 2, ['second row.']]]])],
80 ["""\
81 ========== ===========
82 A table with four rows,
83 -----------------------
84 and two columns.
85 First and last rows
86 contain column spans.
87 =======================
88 """,
89 ([10, 11],
90 [],
91 [[[0, 1, 1, ['A table with four rows,']]],
92 [[0, 0, 3, ['and two']],
93 [0, 0, 3, ['columns.']]],
94 [[0, 0, 4, ['First and']],
95 [0, 0, 4, ['last rows']]],
96 [[0, 1, 5, ['contain column spans.']]]])],
97 ["""\
98 ======= ===== ======
99 A bad table cell 2
100 cell 3 cell 4
101 ============ ======
102 """,
103 'TableMarkupError: Text in column margin in table line 2.'],
104 ["""\
105 ====== ===== ======
106 row one
107 Another bad table
108 ====== ===== ======
109 """,
110 'TableMarkupError: Text in column margin in table line 3.'],
111 ["""\
112 =========== ================
113 A table with two header rows,
114 -----------------------------
115 the first with a span.
116 =========== ================
117 Two body rows,
118 the second with a span.
119 =============================
120 """,
121 ([11, 16],
122 [[[0, 1, 1, ['A table with two header rows,']]],
123 [[0, 0, 3, ['the first']],
124 [0, 0, 3, ['with a span.']]]],
125 [[[0, 0, 5, ['Two body']],
126 [0, 0, 5, ['rows,']]],
127 [[0, 1, 6, ['the second with a span.']]]])],
128 ["""\
129 ============ =============
130 A table with two head/body
131 ============ =============
132 row separators.
133 ============ =============
134 That's bad.
135 ============ =============
136 """,
137 'TableMarkupError: Multiple head/body row separators '
138 '(table lines 3 and 5); only one allowed.'],
139 ["""\
140 ============ ============
141 ============ ============
142 """,
143 ([12, 12],
144 [],
145 [[[0, 0, 1, []],
146 [0, 0, 1, []]]])],
147 # ["""\
148 # ============== ==========
149 # Table with row separators
150 # ============== ==========
151 # and blank
152 # -------------- ----------
153 # entries
154 # -------------- ----------
155 # in first
156 # -------------- ----------
157 # columns.
158 # ============== ==========
159 # """,
160 # '']
161 ]
162
163
164 if __name__ == '__main__':
165 unittest.main()