-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_smoothtables.py
396 lines (372 loc) · 20.3 KB
/
test_smoothtables.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""Tests for the smoothtables module."""
import sys
import smoothtables as st
def _validate_output(expected_output, capfd):
"""Validate the captured output against the expected output."""
captured = capfd.readouterr()
assert captured.out == expected_output
def test_table_1(capfd):
"""Test a table with a fixed width."""
expected_output = (
'My Test Table\n'
'┌───────────────┬───┬──────────────────────────────────────┐\n'
'│ Column 1 │ C │ Column 3 │\n'
'╞═══════════════╪═══╪══════════════════════════════════════╡\n'
'│ First value 1 │ Y │ Third value 1 │\n'
'├───────────────┼───┼──────────────────────────────────────┤\n'
'│ │ │ │\n'
'├───────────────┼───┼──────────────────────────────────────┤\n'
'│ First value 2 │ N │ Third value 2 │\n'
'├───────────────┼───┼──────────────────────────────────────┤\n'
'│ │ │ │\n'
'├───────────────┼───┼──────────────────────────────────────┤\n'
'│ First value 3 │ │ │\n'
'└───────────────┴───┴──────────────────────────────────────┘\n')
columns = [
st.Column('Column 1'),
st.Column('Column 2', width=1, h_alignment=st.Alignment.CENTER),
st.Column('Column 3'),
]
data = [
('First value 1', 'Y', 'Third value 1'),
(None, None, ' '),
('First value 2', 'N', 'Third value 2'),
(None, None, None),
('First value 3', None, None),
]
table = st.Table(columns, title='My Test Table', max_width=60)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_table_2(capfd):
"""Test a table without a fixed width."""
expected_output = (
'My Test Table\n'
'┌───────────────┬────┬───────────────┐\n'
'│ Column 1 │ Co │ Column 3 │\n'
'╞═══════════════╪════╪═══════════════╡\n'
'│ First value 1 │ Y │ Third value 1 │\n'
'│ │ │ │\n'
'│ First value 2 │ N │ Third value 2 │\n'
'│ │ │ │\n'
'│ First value 3 │ │ │\n'
'└───────────────┴────┴───────────────┘\n')
columns = [
st.Column('Column 1'),
st.Column('Column 2', width=2, h_alignment=st.Alignment.CENTER),
st.Column('Column 3'),
]
data = [
('First value 1', 'Y', 'Third value 1'),
(None, None, ' '),
('First value 2', 'N', 'Third value 2'),
(None, None, None),
('First value 3', None, None),
]
table = st.Table(columns, title='My Test Table', use_row_separators=False)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_table_3(capfd):
"""Test a table where no columns have a fixed width."""
expected_output = (
'My Test Table\n'
'┌───────────────┬──────────┬───────────────┐\n'
'│ Column 1 │ Column 2 │ Column 3 │\n'
'╞═══════════════╪══════════╪═══════════════╡\n'
'│ First value 1 │ Y │ Third value 1 │\n'
'│ │ │ │\n'
'│ First value 2 │ N │ Third value 2 │\n'
'│ │ │ │\n'
'│ First value 3 │ │ │\n'
'└───────────────┴──────────┴───────────────┘\n')
columns = [
st.Column('Column 1'),
st.Column('Column 2', h_alignment=st.Alignment.CENTER),
st.Column('Column 3'),
]
data = [
['First value 1', 'Y', 'Third value 1'],
(None, None, ' '),
('First value 2', 'N', 'Third value 2'),
[None, None, None],
['First value 3', None, None],
]
table = st.Table(columns, title='My Test Table', use_row_separators=False)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_table_4(capfd):
"""Test a table where data rows have too many/too few values."""
expected_output = (
'┌───────────────┬──────────┬───────────────┐\n'
'│ Column 1 │ Column 2 │ Column 3 │\n'
'╞═══════════════╪══════════╪═══════════════╡\n'
'│ First value 1 │ Y │ Third value 1 │\n'
'│ │ │ │\n'
'│ First value 2 │ N │ Third value 2 │\n'
'│ │ │ │\n'
'│ First value 3 │ │ │\n'
'└───────────────┴──────────┴───────────────┘\n')
columns = [
st.Column('Column 1'),
st.Column('Column 2', h_alignment=st.Alignment.CENTER),
st.Column('Column 3'),
]
data = [
['First value 1', 'Y', 'Third value 1'],
(None,),
('First value 2', 'N', 'Third value 2', 'What is this?!'),
[],
['First value 3', None],
]
table = st.Table(columns, use_row_separators=False)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_table_5(capfd):
"""Test a table with complex rendering."""
expected_output = (
'┌─────┬───────────────────────────────────────────────┬───────┐\n'
'│ # │ Lorem Ipsum │ Huh? │\n'
'╞═════╪═══════════════════════════════════════════════╪═══════╡\n'
'│ │ Lorem ipsum dolor sit amet, consectetur │ │\n'
'│ │ adipiscing elit, sed do eiusmod tempor │ │\n'
'│ 1 │ incididunt ut labore et dolore magna aliqua. │ Start │\n'
'│ │ Vel pharetra vel turpis nunc eget lorem dolor │ │\n'
'│ │ sed viverra. │ │\n'
'├─────┼───────────────────────────────────────────────┼───────┤\n'
'│ │ In pellentesque massa placerat duis ultricies │ │\n'
'│ │ lacus sed turpis tincidunt. Ultricies │ │\n'
'│ 2 │ tristique nulla aliquet enim tortor at auctor │ ? │\n'
'│ │ urna. Morbi tristique senectus et netus et │ │\n'
'│ │ malesuada. Eget lorem dolor sed viverra. │ │\n'
'├─────┼───────────────────────────────────────────────┼───────┤\n'
'│ 3 │ Amet est placerat in egestas erat. │ ?!! │\n'
'├─────┼───────────────────────────────────────────────┼───────┤\n'
'│ │ Integer feugiat scelerisque varius morbi │ │\n'
'│ 999 │ enim. Risus nullam eget felis eget nunc │ End │\n'
'│ │ lobortis. At elementum eu facilisis sed odio │ │\n'
'│ │ morbi quis. Ut aliquam purus sit amet luctus. │ │\n'
'└─────┴───────────────────────────────────────────────┴───────┘\n')
columns = [
st.Column('#', h_alignment=st.Alignment.RIGHT,
v_alignment=st.Alignment.MIDDLE),
st.Column('Lorem Ipsum', width=45),
st.Column('Huh?', h_alignment=st.Alignment.CENTER,
v_alignment=st.Alignment.MIDDLE),
]
data = [
(
1,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '
'eiusmod tempor incididunt ut labore et dolore magna aliqua. Vel '
'pharetra vel turpis nunc eget lorem dolor sed viverra.',
'Start'
),
(
2,
'In pellentesque massa placerat duis ultricies lacus sed turpis '
'tincidunt. Ultricies tristique nulla aliquet enim tortor at '
'auctor urna. Morbi tristique senectus et netus et malesuada. '
'Eget lorem dolor sed viverra.',
'?'
),
(
3,
'Amet est placerat in egestas erat.',
'?!!'
),
(
999,
'Integer feugiat scelerisque varius morbi enim. Risus nullam eget '
'felis eget nunc lobortis. At elementum eu facilisis sed odio '
'morbi quis. Ut aliquam purus sit amet luctus.',
'End'
),
]
table = st.Table(columns)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_table_6(capfd):
"""Test a table with many columns."""
expected_output = (
'┌───────────┬───────────┬───────────┬──────────┬───────┐\n'
'│ Column 1 │ Column 2 │ Column 3 │ Column 4 │ Colum │\n'
'╞═══════════╪═══════════╪═══════════╪══════════╪═══════╡\n'
'│ Value 1.1 │ Value 1.2 │ Value 1.3 │ Yes │ True │\n'
'│ Value 2.1 │ Value 2.2 │ Value 2.3 │ No │ True │\n'
'│ Value 3.1 │ Value 3.2 │ Value 3.3 │ Yes │ False │\n'
'│ Value 4.1 │ Value 4.2 │ Value 4.3 │ No │ False │\n'
'│ Value 5.1 │ Value 5.2 │ Value 5.3 │ Maybe │ True? │\n'
'└───────────┴───────────┴───────────┴──────────┴───────┘\n')
columns = [
st.Column('Column 1'),
st.Column('Column 2'),
st.Column('Column 3'),
st.Column('Column 4', h_alignment=st.Alignment.CENTER),
st.Column('Column 5', fit_heading=False),
]
data = [
('Value 1.1', 'Value 1.2', 'Value 1.3', 'Yes', True),
('Value 2.1', 'Value 2.2', 'Value 2.3', 'No', True),
('Value 3.1', 'Value 3.2', 'Value 3.3', 'Yes', False),
('Value 4.1', 'Value 4.2', 'Value 4.3', 'No', False),
('Value 5.1', 'Value 5.2', 'Value 5.3', 'Maybe', 'True?'),
]
table = st.Table(columns, use_row_separators=False)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_table_7(capfd):
"""Test a table with many columns and no headings."""
expected_output = (
'┌───────────┬───────────┬───────────┬───────┬───────┐\n'
'│ Value 1.1 │ Value 1.2 │ Value 1.3 │ Yes │ True │\n'
'│ Value 2.1 │ Value 2.2 │ Value 2.3 │ No │ True │\n'
'│ Value 3.1 │ Value 3.2 │ Value 3.3 │ Yes │ False │\n'
'│ Value 4.1 │ Value 4.2 │ Value 4.3 │ No │ False │\n'
'│ Value 5.1 │ Value 5.2 │ Value 5.3 │ Maybe │ True? │\n'
'└───────────┴───────────┴───────────┴───────┴───────┘\n')
columns = [
st.Column('Column 1'),
st.Column('Column 2'),
st.Column('Column 3'),
st.Column('Column 4', h_alignment=st.Alignment.CENTER),
st.Column('Column 5', fit_heading=False),
]
data = [
('Value 1.1', 'Value 1.2', 'Value 1.3', 'Yes', True),
('Value 2.1', 'Value 2.2', 'Value 2.3', 'No', True),
('Value 3.1', 'Value 3.2', 'Value 3.3', 'Yes', False),
('Value 4.1', 'Value 4.2', 'Value 4.3', 'No', False),
('Value 5.1', 'Value 5.2', 'Value 5.3', 'Maybe', 'True?'),
]
table = st.Table(columns, include_headings=False, use_row_separators=False)
table.data = data
table.print_table(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_panel_1(capfd):
"""Test a panel."""
expected_output = (
' ╭──────────────────╮\n'
'Attribute 1: │ Value 1 │\n'
' ├──────────────────┤\n'
'Attribute 2: │ Longer value 2 │\n'
' │ on multiple rows │\n'
' ├──────────────────┤\n'
'Attribute 3: │ │\n'
' ├──────────────────┤\n'
' │ Value 4 │\n'
' ╰──────────────────╯\n')
data = [
('Attribute 1', 'Value 1'),
('Attribute 2', ['Longer value 2', 'on multiple rows']),
('Attribute 3', None),
(None, 'Value 4'),
]
panel = st.Panel(data)
panel.print_panel(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_panel_2(capfd):
"""Test a panel with word wrapping."""
expected_output = (
' ╭────────╮\n'
'Attribute 1: │ Value │\n'
' │ 1 │\n'
' ├────────┤\n'
'Attribute 2: │ Longer │\n'
' │ value │\n'
' │ 2 │\n'
' │ on mul │\n'
' │ tiple │\n'
' │ rows │\n'
' ├────────┤\n'
'Attribute 3: │ │\n'
' ├────────┤\n'
' │ Value │\n'
' │ 4 │\n'
' ╰────────╯\n')
data = [
('Attribute 1', 'Value 1'),
('Attribute 2', ['Longer value 2', 'on multiple rows']),
('Attribute 3', None),
(None, 'Value 4'),
]
panel = st.Panel(data, value_width=6)
panel.print_panel(file=sys.stdout)
_validate_output(expected_output, capfd)
def test_panel_3(capfd):
"""Test a panel with real data."""
expected_output = (
' ╭────────────────────────────────╮\n'
' Album Title: │ Moment │\n'
' ├────────────────────────────────┤\n'
' Artist: │ Dark Tranquillity │\n'
' ├────────────────────────────────┤\n'
' Release Date: │ 20 November 2020 │\n'
' ├────────────────────────────────┤\n'
' Label: │ Century Media │\n'
' ├────────────────────────────────┤\n'
' Track Listing: │ 1. Phantom Days │\n'
' │ 2. Transient │\n'
' │ 3. Identical to None │\n'
' │ 4. The Dark Unbroken │\n'
' │ 5. Remain in the Unknown │\n'
' │ 6. Standstill │\n'
' │ 7. Ego Deception │\n'
' │ 8. A Drawn Out Exit │\n'
' │ 9. Eyes of the World │\n'
' │ 10. Failstate │\n'
' │ 11. Empires Lost to Time │\n'
' │ 12. In Truth Divided │\n'
' │ 13. Silence as a Force │\n'
' │ 14. Time in Relativity │\n'
' ├────────────────────────────────┤\n'
'Peak Chart Positions: │ SWE: 14 │\n'
' │ AUT: 24 │\n'
' │ FIN: 9 │\n'
' │ FRA: 181 │\n'
' │ GER: 17 │\n'
' │ SWI: 16 │\n'
' ╰────────────────────────────────╯\n')
data = [
('Album Title', 'Moment'),
('Artist', 'Dark Tranquillity'),
('Release Date', '20 November 2020'),
('Label', 'Century Media'),
(
'Track Listing',
[
' 1. Phantom Days',
' 2. Transient',
' 3. Identical to None',
' 4. The Dark Unbroken',
' 5. Remain in the Unknown',
' 6. Standstill',
' 7. Ego Deception',
' 8. A Drawn Out Exit',
' 9. Eyes of the World',
'10. Failstate',
'11. Empires Lost to Time',
'12. In Truth Divided',
'13. Silence as a Force',
'14. Time in Relativity',
]
),
(
'Peak Chart Positions',
[
'SWE: 14',
'AUT: 24',
'FIN: 9',
'FRA: 181',
'GER: 17',
'SWI: 16',
]
)
]
panel = st.Panel(data, value_width=30)
panel.print_panel(file=sys.stdout)
_validate_output(expected_output, capfd)