summaryrefslogtreecommitdiff
path: root/docutils/test/test_parsers/test_rst/test_SimpleTableParser.py
blob: a19e496af4aff7d9de6689b1b517c878a400d9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#! /usr/bin/env python3
# $Id$
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.

"""
Tests for states.py.
"""

from pathlib import Path
import sys
import unittest

if __name__ == '__main__':
    # prepend the "docutils root" to the Python library path
    # so we import the local `docutils` package.
    sys.path.insert(0, str(Path(__file__).resolve().parents[3]))

from docutils.parsers.rst import tableparser
from docutils.statemachine import StringList, string2lines


class SimpleTableParserTestCase(unittest.TestCase):
    def test_parse(self):
        parser = tableparser.SimpleTableParser()
        for name, cases in totest.items():
            for casenum, (case_input, case_expected) in enumerate(cases):
                lines_input = StringList(string2lines(case_input), 'test data')
                with self.subTest(id=f'totest[{name!r}][{casenum}]'):
                    try:
                        output = parser.parse(lines_input)
                    except Exception as details:
                        output = f'{details.__class__.__name__}: {details}'
                    self.assertEqual(output, case_expected)


totest = {}

totest['simple_tables'] = [
["""\
============  ============
A table with  two columns.
============  ============
""",
([12, 12],
 [],
 [[[0, 0, 1, ['A table with']],
   [0, 0, 1, ['two columns.']]]])],
["""\
============  ===============
A tāble w̅ith  comb̲ining chars
============  ===============
""",
([12, 15],
 [],
 [[[0, 0, 1, ['A ta\u0304ble w\u0305ith']],
   [0, 0, 1, ['comb\u0332ining chars']]]])],
["""\
============  ============
A table with  two columns
and           two rows.
============  ============
""",
([12, 12],
 [],
 [[[0, 0, 1, ['A table with']],
   [0, 0, 1, ['two columns']]],
  [[0, 0, 2, ['and']],
   [0, 0, 2, ['two rows.']]]])],
["""\
======================================
The last row might stick into the margin
second row.
======================================
""",
([40],
 [],
 [[[0, 0, 1, ['The last row might stick into the margin']]],
  [[0, 0, 2, ['second row.']]]])],
["""\
==========  ===========
A table with four rows,
-----------------------
and two     columns.
First and   last rows
contain column spans.
=======================
""",
([10, 11],
 [],
 [[[0, 1, 1, ['A table with four rows,']]],
  [[0, 0, 3, ['and two']],
   [0, 0, 3, ['columns.']]],
  [[0, 0, 4, ['First and']],
   [0, 0, 4, ['last rows']]],
  [[0, 1, 5, ['contain column spans.']]]])],
["""\
=======  =====  ======
A bad table     cell 2
cell 3          cell 4
============  ======
""",
'TableMarkupError: Text in column margin in table line 2.'],
["""\
======  =====  ======
row     one
Another bad    table
======  =====  ======
""",
'TableMarkupError: Text in column margin in table line 3.'],
["""\
===========  ================
A table with two header rows,
-----------------------------
the first    with a span.
===========  ================
Two body     rows,
the second with a span.
=============================
""",
([11, 16],
 [[[0, 1, 1, ['A table with two header rows,']]],
  [[0, 0, 3, ['the first']],
   [0, 0, 3, ['with a span.']]]],
 [[[0, 0, 5, ['Two body']],
   [0, 0, 5, ['rows,']]],
  [[0, 1, 6, ['the second with a span.']]]])],
["""\
============  =============
A table with  two head/body
============  =============
row           separators.
============  =============
That's bad.
============  =============
""",
'TableMarkupError: Multiple head/body row separators '
'(table lines 3 and 5); only one allowed.'],
["""\
============  ============
============  ============
""",
([12, 12],
 [],
 [[[0, 0, 1, []],
   [0, 0, 1, []]]])],
# ["""\
# ==============  ==========
# Table with row  separators
# ==============  ==========
#                 and blank
# --------------  ----------
#                 entries
# --------------  ----------
#                 in first
# --------------  ----------
#                 columns.
# ==============  ==========
# """,
# '']
]


if __name__ == '__main__':
    unittest.main()