-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mdx_spantables.py
240 lines (200 loc) · 6.92 KB
/
test_mdx_spantables.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""
Tests for SpanTables Extension
==============================
These tests are used to check the functionality of the spantables extension,
allowing me to more easily upgrade to a later version of the tables extension
it was based on without accidentally breaking functionality.
Run them like this: python -m unittest discover
Copyright 2018 [Maurice van der Pot](griffon26@kfk4ever.com)
License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
"""
import unittest
from markdown.util import etree
import mdx_spantables
class FakeMarkdown():
def __init__(self):
self.tab_length = None
class FakeParser():
def __init__(self):
self.markdown = FakeMarkdown()
class TestSpanTableProcessor(unittest.TestCase):
def setUp(self):
self.proc = mdx_spantables.SpanTableProcessor(FakeParser())
def cell_to_text(self, cell):
colspan_str = cell.get('colspan')
colspan = int(colspan_str) if colspan_str else 1
rowspan_str = cell.get('rowspan')
rowspan = int(rowspan_str) if rowspan_str else 1
output = '%dx%d' % (colspan, rowspan)
return output
def element_to_table(self, element):
output = ''
children = list(element)
self.assertEqual(len(children), 1)
table = children[0]
self.assertEqual(table.tag, 'table')
table_children = list(table)
self.assertEqual(len(table_children), 2)
table_head, table_body = table_children
self.assertEqual(table_head.tag, 'thead')
self.assertEqual(table_body.tag, 'tbody')
for tr in table_head:
if len(tr) > 0:
output += ' '.join('%s' % self.cell_to_text(th) for th in tr)
output += '\n'
for tr in table_body:
if len(tr) > 0:
output += ' '.join('%s' % self.cell_to_text(td) for td in tr)
output += '\n'
return output
def test_test(self):
self.assertTrue(self.proc.test(None, '| bla | nogwat |\n'
'|-----|--------|\n'))
def test_run_table_without_col_or_rowspan(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 |\n'
'|---------|---------|\n'
'| content1| content2|'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'1x1 1x1\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_single_rowspan_in_first_column(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 |\n'
'|---------|---------|\n'
'| content1| content2|\n'
'|_ | content2|'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'1x2 1x1\n'
'1x1\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_single_rowspan_in_later_column(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 |\n'
'|---------|---------|\n'
'| content1| content2|\n'
'| content2|_ |'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'1x1 1x2\n'
'1x1\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_single_rowspan_at_last_character(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 |\n'
'|---------|---------|\n'
'| content1| content2|\n'
'| content2| _|'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'1x1 1x2\n'
'1x1\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_single_colspan_in_first_column(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 |\n'
'|---------|---------|\n'
'| content1 ||'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'2x1\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_single_colspan_in_later_column(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 | header3 |\n'
'|---------|---------|---------|\n'
'| content1| content2 ||'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1 1x1\n'
'1x1 2x1\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_combined_row_and_colspan(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| header1 | header2 |\n'
'|---------|---------|\n'
'| content1 ||\n'
'|_ ||'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'2x2\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_rowspan_stops_at_first_nonempty_cell(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| head |\n'
'| ------------ |\n'
'| regular cell |\n'
'| span 2 rows |\n'
'|_ |'
])
actual = self.element_to_table(parent)
expected = (
'1x1\n'
'1x1\n'
'1x2\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_rowspan_limited_by_end_of_previous_rowspan(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| head |\n'
'| ----------- |\n'
'| span 2 rows |\n'
'|_ |\n'
'| |\n'
'|_ |'
])
actual = self.element_to_table(parent)
expected = (
'1x1\n'
'1x2\n'
'1x2\n'
)
self.assertMultiLineEqual(actual, expected)
def test_run_table_with_rowspan_limited_by_row_with_other_colspan(self):
parent = etree.Element('parent')
self.proc.run(parent, [
'| | |\n'
'| ----------- | ---------- |\n'
'| not included in rowspan ||\n'
'| span 2 rows | |\n'
'|_ | |'
])
actual = self.element_to_table(parent)
expected = (
'1x1 1x1\n'
'2x1\n'
'1x2 1x1\n'
'1x1\n'
)
self.assertMultiLineEqual(actual, expected)