rich/tests/test_layout.py

101 lines
4.0 KiB
Python
Raw Normal View History

2021-02-13 19:14:56 +00:00
import sys
2022-02-26 20:25:42 +00:00
2021-02-13 17:50:43 +00:00
import pytest
from rich.console import Console
2021-03-19 21:37:47 +00:00
from rich.layout import Layout, NoSplitter
2021-02-13 17:50:43 +00:00
from rich.panel import Panel
2021-03-19 21:37:47 +00:00
def test_no_layout():
layout = Layout()
with pytest.raises(NoSplitter):
layout.split(Layout(), Layout(), splitter="nope")
def test_add_split():
layout = Layout()
layout.split(Layout(), Layout())
assert len(layout.children) == 2
layout.add_split(Layout(name="foo"))
assert len(layout.children) == 3
assert layout.children[2].name == "foo"
def test_unsplit():
layout = Layout()
layout.split(Layout(), Layout())
assert len(layout.children) == 2
layout.unsplit()
assert len(layout.children) == 0
2021-02-13 19:14:56 +00:00
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
2021-02-13 17:50:43 +00:00
def test_render():
layout = Layout(name="root")
repr(layout)
2021-02-14 20:56:30 +00:00
2021-03-19 21:37:47 +00:00
layout.split_column(Layout(name="top"), Layout(name="bottom"))
2021-02-14 20:56:30 +00:00
top = layout["top"]
2021-02-13 17:50:43 +00:00
top.update(Panel("foo"))
2021-02-14 20:56:30 +00:00
2021-02-13 17:50:43 +00:00
print(type(top._renderable))
assert isinstance(top.renderable, Panel)
2021-03-19 21:37:47 +00:00
layout["bottom"].split_row(Layout(name="left"), Layout(name="right"))
2021-02-13 17:50:43 +00:00
assert layout["root"].name == "root"
assert layout["left"].name == "left"
2021-06-17 21:31:08 +00:00
assert isinstance(layout.map, dict)
2021-02-13 17:50:43 +00:00
with pytest.raises(KeyError):
top["asdasd"]
layout["left"].update("foobar")
2021-02-13 18:17:14 +00:00
print(layout["left"].children)
2021-02-13 17:50:43 +00:00
console = Console(width=60, color_system=None)
with console.capture() as capture:
console.print(layout, height=10)
result = capture.get()
2021-03-19 21:37:47 +00:00
print(repr(result))
expected = "╭──────────────────────────────────────────────────────────╮\n│ foo │\n│ │\n│ │\n╰──────────────────────────────────────────────────────────╯\nfoobar ╭───── 'right' (30 x 5) ─────╮\n │ │\n │ Layout(name='right') │\n │ │\n ╰────────────────────────────╯\n"
2021-06-09 14:39:58 +00:00
2021-02-13 17:50:43 +00:00
assert result == expected
def test_tree():
layout = Layout(name="root")
2021-03-19 21:37:47 +00:00
layout.split(Layout("foo", size=2), Layout("bar", name="bar"))
layout["bar"].split_row(Layout(), Layout())
2021-02-13 17:50:43 +00:00
console = Console(width=60, color_system=None)
with console.capture() as capture:
console.print(layout.tree, height=10)
result = capture.get()
print(repr(result))
2022-02-26 20:25:42 +00:00
expected = "⬍ Layout(name='root')\n├── ⬍ Layout(size=2)\n└── ⬌ Layout(name='bar')\n ├── ⬍ Layout()\n └── ⬍ Layout()\n"
2021-06-09 14:39:58 +00:00
print(result, "\n", expected)
2021-02-13 18:25:47 +00:00
assert result == expected
2021-03-25 21:08:36 +00:00
2021-03-25 21:18:04 +00:00
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
2021-03-25 21:08:36 +00:00
def test_refresh_screen():
layout = Layout()
layout.split_row(Layout(name="foo"), Layout(name="bar"))
2022-01-05 14:40:36 +00:00
console = Console(force_terminal=True, width=20, height=5, _environ={})
2021-04-04 13:43:23 +00:00
with console.capture():
console.print(layout)
2021-03-25 21:08:36 +00:00
with console.screen():
with console.capture() as capture:
layout.refresh_screen(console, "foo")
result = capture.get()
2021-05-06 14:13:40 +00:00
print()
2021-03-25 21:08:36 +00:00
print(repr(result))
2021-05-06 14:13:40 +00:00
expected = "\x1b[1;1H\x1b[34m╭─\x1b[0m\x1b[34m \x1b[0m\x1b[32m'foo'\x1b[0m\x1b[34m─╮\x1b[0m\x1b[2;1H\x1b[34m│\x1b[0m \x1b[1;35mLayout\x1b[0m \x1b[34m│\x1b[0m\x1b[3;1H\x1b[34m│\x1b[0m \x1b[1m(\x1b[0m \x1b[34m│\x1b[0m\x1b[4;1H\x1b[34m│\x1b[0m \x1b[33mna\x1b[0m \x1b[34m│\x1b[0m\x1b[5;1H\x1b[34m╰────────╯\x1b[0m"
2021-03-25 21:08:36 +00:00
assert result == expected