rich/tests/test_table.py

99 lines
18 KiB
Python
Raw Normal View History

2020-06-06 14:34:59 +00:00
# encoding=utf-8
import io
2020-06-28 10:10:59 +00:00
import pytest
2020-06-06 14:34:59 +00:00
2020-06-28 10:10:59 +00:00
from rich import errors
2020-06-06 14:34:59 +00:00
from rich.console import Console
2020-06-28 10:10:59 +00:00
from rich.measure import Measurement
2020-06-06 14:34:59 +00:00
from rich.table import Table
2020-06-23 10:40:31 +00:00
from rich.text import Text
2020-06-06 14:34:59 +00:00
def render_tables():
2020-06-23 10:40:31 +00:00
console = Console(
width=60,
force_terminal=True,
file=io.StringIO(),
legacy_windows=False,
2020-06-28 10:10:59 +00:00
color_system=None,
2020-06-23 10:40:31 +00:00
)
2020-06-06 14:34:59 +00:00
2020-06-23 10:40:31 +00:00
table = Table(title="test table", caption="table caption", expand=True)
table.add_column("foo", footer=Text("total"), no_wrap=True, overflow="ellipsis")
2020-06-06 14:34:59 +00:00
table.add_column("bar", justify="center")
table.add_column("baz", justify="right")
table.add_row("Averlongwordgoeshere", "banana pancakes", None)
2020-06-28 10:10:59 +00:00
assert Measurement.get(console, table, 80) == Measurement(41, 48)
2020-06-06 14:34:59 +00:00
for width in range(10, 60, 5):
console.print(table, width=width)
2020-06-23 10:40:31 +00:00
table.expand = False
console.print(table, justify="left")
console.print(table, justify="center")
console.print(table, justify="right")
assert table.row_count == 1
table.row_styles = ["red", "yellow"]
2020-06-28 10:10:59 +00:00
table.add_row("Coffee")
table.add_row("Coffee", "Chocolate", None, "cinnamon")
2020-06-23 10:40:31 +00:00
2020-06-28 10:10:59 +00:00
assert table.row_count == 3
2020-06-23 10:40:31 +00:00
console.print(table)
table.show_lines = True
console.print(table)
table.show_footer = True
console.print(table)
table.show_edge = False
console.print(table)
table.padding = 1
console.print(table)
table.width = 20
2020-06-30 20:40:11 +00:00
assert Measurement.get(console, table, 80) == Measurement(20, 20)
2020-06-23 10:40:31 +00:00
console.print(table)
2020-07-12 15:17:36 +00:00
table.columns[0].no_wrap = True
table.columns[1].no_wrap = True
table.columns[2].no_wrap = True
console.print(table)
table.padding = 0
table.width = 60
table.leading = 1
console.print(table)
2020-06-06 14:34:59 +00:00
return console.file.getvalue()
def test_render_table():
2020-09-07 15:44:31 +00:00
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
2020-06-06 14:34:59 +00:00
assert render_tables() == expected
2020-06-28 10:10:59 +00:00
def test_not_renderable():
class Foo:
pass
table = Table()
with pytest.raises(errors.NotRenderableError):
table.add_row(Foo())
2020-06-06 14:34:59 +00:00
if __name__ == "__main__":
render = render_tables()
print(render)
print(repr(render))