Debian Codegraph @

tests/test_table.py

raw
1 # encoding=utf-8
2
3 import io
4 from textwrap import dedent
5
6 import pytest
7
8 from rich import box, errors
9 from rich.console import Console
10 from rich.measure import Measurement
11 from rich.style import Style
12 from rich.table import Column, Table
13 from rich.text import Text
14
15
16 def render_tables():
17 console = Console(
18 width=60,
19 force_terminal=True,
20 file=io.StringIO(),
21 legacy_windows=False,
22 color_system=None,
23 _environ={},
24 )
25
26 table = Table(title="test table", caption="table caption", expand=False)
27 table.add_column("foo", footer=Text("total"), no_wrap=True, overflow="ellipsis")
28 table.add_column("bar", justify="center")
29 table.add_column("baz", justify="right")
30
31 table.add_row("Averlongwordgoeshere", "banana pancakes", None)
32
33 assert Measurement.get(console, console.options, table) == Measurement(41, 48)
34 table.expand = True
35 assert Measurement.get(console, console.options, table) == Measurement(41, 48)
36
37 for width in range(10, 60, 5):
38 console.print(table, width=width)
39
40 table.expand = False
41 console.print(table, justify="left")
42 console.print(table, justify="center")
43 console.print(table, justify="right")
44
45 assert table.row_count == 1
46
47 table.row_styles = ["red", "yellow"]
48 table.add_row("Coffee")
49 table.add_row("Coffee", "Chocolate", None, "cinnamon")
50
51 assert table.row_count == 3
52
53 console.print(table)
54
55 table.show_lines = True
56 console.print(table)
57
58 table.show_footer = True
59 console.print(table)
60
61 table.show_edge = False
62
63 console.print(table)
64
65 table.padding = 1
66 console.print(table)
67
68 table.width = 20
69 assert Measurement.get(console, console.options, table) == Measurement(20, 20)
70 table.expand = False
71 assert Measurement.get(console, console.options, table) == Measurement(20, 20)
72 table.expand = True
73 console.print(table)
74
75 table.columns[0].no_wrap = True
76 table.columns[1].no_wrap = True
77 table.columns[2].no_wrap = True
78
79 console.print(table)
80
81 table.padding = 0
82 table.width = 60
83 table.leading = 1
84 console.print(table)
85
86 return console.file.getvalue()
87
88
89 def test_render_table():
90 expected = "test table\n┏━━━━━━┳┳┓\n┃ foo ┃┃┃\n┡━━━━━━╇╇┩\n│ Ave… │││\n└──────┴┴┘\n table \n caption \n test table \n┏━━━━━━━━━━━┳┳┓\n┃ foo ┃┃┃\n┡━━━━━━━━━━━╇╇┩\n│ Averlong… │││\n└───────────┴┴┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━┳┳┓\n┃ foo ┃┃┃\n┡━━━━━━━━━━━━━━━━╇╇┩\n│ Averlongwordg… │││\n└────────────────┴┴┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━┳┳┓\n┃ foo ┃┃┃\n┡━━━━━━━━━━━━━━━━━━━━━╇╇┩\n│ Averlongwordgoeshe… │││\n└─────────────────────┴┴┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━┳━━┓\n┃ foo ┃ ┃ ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━╇━━┩\n│ Averlongwordgoeshere │ │ │\n└──────────────────────┴──┴──┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━┓\n┃ foo ┃ bar ┃ b… ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━┩\n│ Averlongwordgoeshere │ ba… │ │\n│ │ pa… │ │\n└──────────────────────┴─────┴────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana │ │\n│ │ pancak… │ │\n└──────────────────────┴─────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana │ │\n│ │ pancakes │ │\n└──────────────────────┴──────────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana pancakes │ │\n└───────────────────────┴──────────────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana pancakes │ │\n└──────────────────────────┴────────────────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┓ \n┃ foo ┃ bar ┃ baz ┃ \n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━┩ \n│ Averlongwordgoeshere │ banana pancakes │ │ \n└──────────────────────┴─────────────────┴─────┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┓ \n ┃ foo ┃ bar ┃ baz ┃ \n ┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━┩ \n │ Averlongwordgoeshere │ banana pancakes │ │ \n └──────────────────────┴─────────────────┴─────┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┓\n ┃ foo ┃ bar ┃ baz ┃\n ┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━┩\n │ Averlongwordgoeshere │ banana pancakes │ │\n └──────────────────────┴─────────────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━┓\n┃ foo ┃ bar ┃ baz ┃ ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━━━━━━━┩\n│ Averlongwordgoeshere │ banana pancakes │ │ │\n│ Coffee │ │ │ │\n│ Coffee │ Chocolate │ │ cinnamon │\n└──────────────────────┴─────────────────┴─────┴──────────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━┓\n┃ foo ┃ bar ┃ baz ┃ ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━━━━━━━┩\n│ Averlongwordgoeshere │ banana pancakes │ │ │\n├──────────────────────┼─────────────────┼─────┼──────────┤\n│ Coffee │ │ │ │\n├──────────────────────┼─────────────────┼─────┼──────────┤\n│ Coffee │ Chocolate │ │ cinnamon │\n└──────────────────────┴─────────────────┴─────┴──────────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━┓\n┃ foo ┃ bar ┃ baz ┃ ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━━━━━━━┩\n│ Averlongwordgoeshere │ banana pancakes │ │ │\n├──────────────────────┼─────────────────┼─────┼──────────┤\n│ Coffee │ │ │ │\n├──────────────────────┼─────────────────┼─────┼──────────┤\n│ Coffee │ Chocolate │ │ cinnamon │\n├──────────────────────┼─────────────────┼─────┼──────────┤\n│ total │ │ │ │\n└──────────────────────┴─────────────────┴─────┴──────────┘\n table caption \n test table \n foo ┃ bar ┃ baz ┃ \n━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━━━━━━━\n Averlongwordgoeshere │ banana pancakes │ │ \n──────────────────────┼─────────────────┼─────┼──────────\n Coffee │ │ │ \n──────────────────────┼─────────────────┼─────┼──────────\n Coffee │ Chocolate │ │ cinnamon \n──────────────────────┼─────────────────┼─────┼──────────\n total │ │ │ \n table caption \n test table \n ┃ ┃ ┃ \n foo ┃ bar ┃ baz ┃ \n ┃ ┃ ┃ \n━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━━━━━━━\n │ │ │ \n Averlongwordgoeshere │ banana pancakes │ │ \n │ │ │ \n──────────────────────┼─────────────────┼─────┼──────────\n │ │ │ \n Coffee │ │ │ \n │ │ │ \n──────────────────────┼─────────────────┼─────┼──────────\n │ │ │ \n Coffee │ Chocolate │ │ cinnamon \n │ │ │ \n──────────────────────┼─────────────────┼─────┼──────────\n │ │ │ \n total │ │ │ \n │ │ │ \n table caption \n test table \n ┃┃┃\n foo ┃┃┃\n ┃┃┃\n━━━━━━━━━━━━━━━━━╇╇╇\n │││\n Averlongwordgo… │││\n │││\n─────────────────┼┼┼\n │││\n Coffee │││\n │││\n─────────────────┼┼┼\n │││\n Coffee │││\n │││\n─────────────────┼┼┼\n │││\n total │││\n │││\n table caption \n test table \n ┃ ┃┃\n foo ┃ bar ┃┃\n ┃ ┃┃\n━━━━━━━━━━╇━━━━━━━━━╇╇\n │ ││\n Averlon… │ banana… ││\n │ ││\n──────────┼─────────┼┼\n │ ││\n Coffee │ ││\n │ ││\n──────────┼─────────┼┼\n │ ││\n Coffee │ Chocol… ││\n │ ││\n──────────┼─────────┼┼\n │ ││\n total │ ││\n │ ││\n table caption \n test table \nfoo ┃ bar ┃ baz┃ \n━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━╇━━━━━━━━━\nAverlongwordgoeshere │ banana pancakes │ │ \n │ │ │ \nCoffee │ │ │ \n │ │ │ \nCoffee │ Chocolate │ │cinnamon \n─────────────────────────┼───────────────────┼────┼─────────\ntotal │ │ │ \n table caption \n"
91 result = render_tables()
92 print(repr(result))
93 assert result == expected
94
95
96 def test_not_renderable():
97 class Foo:
98 pass
99
100 table = Table()
101 with pytest.raises(errors.NotRenderableError):
102 table.add_row(Foo())
103
104
105 def test_init_append_column():
106 header_names = ["header1", "header2", "header3"]
107 test_columns = [
108 Column(_index=index, header=header) for index, header in enumerate(header_names)
109 ]
110
111 # Test appending of strings for header names
112 assert Table(*header_names).columns == test_columns
113 # Test directly passing a Table Column objects
114 assert Table(*test_columns).columns == test_columns
115
116
117 def test_rich_measure():
118 console = Console()
119 assert Table("test_header", width=-1).__rich_measure__(
120 console, console.options
121 ) == Measurement(0, 0)
122 # Check __rich_measure__() for a positive width passed as an argument
123 assert Table("test_header", width=None).__rich_measure__(
124 console, console.options.update_width(10)
125 ) == Measurement(10, 10)
126
127
128 def test_min_width():
129 table = Table("foo", min_width=30)
130 table.add_row("bar")
131 console = Console()
132 assert table.__rich_measure__(
133 console, console.options.update_width(100)
134 ) == Measurement(30, 30)
135 console = Console(color_system=None)
136 console.begin_capture()
137 console.print(table)
138 output = console.end_capture()
139 print(output)
140 assert all(len(line) == 30 for line in output.splitlines())
141
142
143 def test_no_columns():
144 console = Console(color_system=None)
145 console.begin_capture()
146 console.print(Table())
147 output = console.end_capture()
148 print(repr(output))
149 assert output == "\n"
150
151
152 def test_get_row_style():
153 console = Console()
154 table = Table()
155 table.add_row("foo")
156 table.add_row("bar", style="on red")
157 assert table.get_row_style(console, 0) == Style.parse("")
158 assert table.get_row_style(console, 1) == Style.parse("on red")
159
160
161 def test_vertical_align_top():
162 console = Console(_environ={})
163
164 def make_table(vertical_align):
165 table = Table(show_header=False, box=box.SQUARE)
166 table.add_column(vertical=vertical_align)
167 table.add_row("foo", "\n".join(["bar"] * 5))
168
169 return table
170
171 with console.capture() as capture:
172 console.print(make_table("top"))
173 console.print()
174 console.print(make_table("middle"))
175 console.print()
176 console.print(make_table("bottom"))
177 console.print()
178 result = capture.get()
179 print(repr(result))
180 expected = "┌─────┬─────┐\n│ foo │ bar │\n│ │ bar │\n│ │ bar │\n│ │ bar │\n│ │ bar │\n└─────┴─────┘\n\n┌─────┬─────┐\n│ │ bar │\n│ │ bar │\n│ foo │ bar │\n│ │ bar │\n│ │ bar │\n└─────┴─────┘\n\n┌─────┬─────┐\n│ │ bar │\n│ │ bar │\n│ │ bar │\n│ │ bar │\n│ foo │ bar │\n└─────┴─────┘\n\n"
181 assert result == expected
182
183
184 @pytest.mark.parametrize(
185 "box,result",
186 [
187 (None, " 1 2 \n 3 4 \n"),
188 (box.HEAVY_HEAD, "┌───┬───┐\n│ 1 │ 2 │\n│ 3 │ 4 │\n└───┴───┘\n"),
189 (box.SQUARE_DOUBLE_HEAD, "┌───┬───┐\n│ 1 │ 2 │\n│ 3 │ 4 │\n└───┴───┘\n"),
190 (box.MINIMAL_DOUBLE_HEAD, " ╷ \n 1 │ 2 \n 3 │ 4 \n ╵ \n"),
191 (box.MINIMAL_HEAVY_HEAD, " ╷ \n 1 │ 2 \n 3 │ 4 \n ╵ \n"),
192 (box.ASCII_DOUBLE_HEAD, "+---+---+\n| 1 | 2 |\n| 3 | 4 |\n+---+---+\n"),
193 ],
194 )
195 def test_table_show_header_false_substitution(box, result):
196 """When the box style is one with a custom header edge, it should be substituted for
197 the equivalent box that does not have a custom header when show_header=False"""
198 table = Table(show_header=False, box=box)
199 table.add_column()
200 table.add_column()
201
202 table.add_row("1", "2")
203 table.add_row("3", "4")
204
205 console = Console(record=True)
206 console.print(table)
207 output = console.export_text()
208
209 assert output == result
210
211
212 def test_section():
213 table = Table("foo")
214 table.add_section() # Null-op
215 table.add_row("row1")
216 table.add_row("row2", end_section=True)
217 table.add_row("row3")
218 table.add_row("row4")
219 table.add_section()
220 table.add_row("row5")
221 table.add_section() # Null-op
222
223 console = Console(
224 width=80,
225 force_terminal=True,
226 color_system="truecolor",
227 legacy_windows=False,
228 record=True,
229 )
230 console.print(table)
231 output = console.export_text()
232 print(repr(output))
233 expected = "┏━━━━━━┓\n┃ foo ┃\n┡━━━━━━┩\n│ row1 │\n│ row2 │\n├──────┤\n│ row3 │\n│ row4 │\n├──────┤\n│ row5 │\n└──────┘\n"
234
235 assert output == expected
236
237
238 @pytest.mark.parametrize(
239 "show_header,show_footer,expected",
240 [
241 (
242 False,
243 False,
244 dedent(
245 """
246 abbbbbcbbbbbbbbbcbbbbcbbbbbd
247 1Dec 2Skywalker2275M2375M 3
248 4May 5Solo 5275M5393M 6
249 ijjjjjkjjjjjjjjjkjjjjkjjjjjl
250 7Dec 8Last Jedi8262M81333M9
251 qrrrrrsrrrrrrrrrsrrrrsrrrrrt
252 """
253 ).lstrip(),
254 ),
255 (
256 True,
257 False,
258 dedent(
259 """
260 abbbbbcbbbbbbbbbcbbbbcbbbbbd
261 1Month2Nickname 2Cost2Gross3
262 efffffgfffffffffgffffgfffffh
263 4Dec 5Skywalker5275M5375M 6
264 4May 5Solo 5275M5393M 6
265 ijjjjjkjjjjjjjjjkjjjjkjjjjjl
266 7Dec 8Last Jedi8262M81333M9
267 qrrrrrsrrrrrrrrrsrrrrsrrrrrt
268 """
269 ).lstrip(),
270 ),
271 (
272 False,
273 True,
274 dedent(
275 """
276 abbbbbcbbbbbbbbbcbbbbcbbbbbd
277 1Dec 2Skywalker2275M2375M 3
278 4May 5Solo 5275M5393M 6
279 ijjjjjkjjjjjjjjjkjjjjkjjjjjl
280 4Dec 5Last Jedi5262M51333M6
281 mnnnnnonnnnnnnnnonnnnonnnnnp
282 7MONTH8NICKNAME 8COST8GROSS9
283 qrrrrrsrrrrrrrrrsrrrrsrrrrrt
284 """
285 ).lstrip(),
286 ),
287 (
288 True,
289 True,
290 dedent(
291 """
292 abbbbbcbbbbbbbbbcbbbbcbbbbbd
293 1Month2Nickname 2Cost2Gross3
294 efffffgfffffffffgffffgfffffh
295 4Dec 5Skywalker5275M5375M 6
296 4May 5Solo 5275M5393M 6
297 ijjjjjkjjjjjjjjjkjjjjkjjjjjl
298 4Dec 5Last Jedi5262M51333M6
299 mnnnnnonnnnnnnnnonnnnonnnnnp
300 7MONTH8NICKNAME 8COST8GROSS9
301 qrrrrrsrrrrrrrrrsrrrrsrrrrrt
302 """
303 ).lstrip(),
304 ),
305 ],
306 )
307 def test_placement_table_box_elements(show_header, show_footer, expected):
308 """Ensure box drawing characters correctly positioned."""
309
310 table = Table(
311 box=box.ASCII, show_header=show_header, show_footer=show_footer, padding=0
312 )
313
314 # content rows indicated by numerals, pure dividers by letters
315 table.box.__dict__.update(
316 top_left="a",
317 top="b",
318 top_divider="c",
319 top_right="d",
320 head_left="1",
321 head_vertical="2",
322 head_right="3",
323 head_row_left="e",
324 head_row_horizontal="f",
325 head_row_cross="g",
326 head_row_right="h",
327 mid_left="4",
328 mid_vertical="5",
329 mid_right="6",
330 row_left="i",
331 row_horizontal="j",
332 row_cross="k",
333 row_right="l",
334 foot_left="7",
335 foot_vertical="8",
336 foot_right="9",
337 foot_row_left="m",
338 foot_row_horizontal="n",
339 foot_row_cross="o",
340 foot_row_right="p",
341 bottom_left="q",
342 bottom="r",
343 bottom_divider="s",
344 bottom_right="t",
345 )
346
347 # add content - note headers title case, footers upper case
348 table.add_column("Month", "MONTH", width=5)
349 table.add_column("Nickname", "NICKNAME", width=9)
350 table.add_column("Cost", "COST", width=4)
351 table.add_column("Gross", "GROSS", width=5)
352
353 table.add_row("Dec", "Skywalker", "275M", "375M")
354 table.add_row("May", "Solo", "275M", "393M")
355 table.add_section()
356 table.add_row("Dec", "Last Jedi", "262M", "1333M")
357
358 console = Console(record=True, width=28)
359 console.print(table)
360 output = console.export_text()
361 print(repr(output))
362
363 assert output == expected
364
365
366 def test_columns_highlight_added_by_add_row() -> None:
367 """Regression test for https://github.com/Textualize/rich/issues/3517"""
368 table = Table(show_header=False, highlight=True)
369 table.add_row("1", repr("FOO"))
370
371 assert table.columns[0].highlight == table.highlight
372 assert table.columns[1].highlight == table.highlight
373
374 console = Console(record=True)
375 console.print(table)
376 output = console.export_text(styles=True)
377 print(repr(output))
378
379 expected = (
380 "┌───┬───────┐\n│ \x1b[1;36m1\x1b[0m │ \x1b[32m'FOO'\x1b[0m │\n└───┴───────┘\n"
381 )
382 assert output == expected
383
384
385 def test_padding_width():
386 """Regression test for https://github.com/Textualize/rich/issues/3871
387
388 Padding should be even between cells/
389
390 """
391
392 table = Table.grid(padding=(0, 1))
393
394 for _ in range(3):
395 table.add_column(width=3)
396
397 for row in range(1):
398 table.add_row(*["a" * 3 for _ in range(3)])
399
400 console = Console(record=True)
401 console.print(table)
402 output = console.export_text(styles=True)
403
404 print(repr(output))
405 expected = "aaa aaa aaa\n"
406 assert output == expected
407
408
409 if __name__ == "__main__":
410 render = render_tables()
411 print(render)
412 print(repr(render))