Asib27's picture
try 1
065fee7 verified
raw
history blame
4.06 kB
import sys
import pytest
from rich.console import Console
from rich.layout import Layout, NoSplitter
from rich.panel import Panel
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
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_render():
layout = Layout(name="root")
repr(layout)
layout.split_column(Layout(name="top"), Layout(name="bottom"))
top = layout["top"]
top.update(Panel("foo"))
print(type(top._renderable))
assert isinstance(top.renderable, Panel)
layout["bottom"].split_row(Layout(name="left"), Layout(name="right"))
assert layout["root"].name == "root"
assert layout["left"].name == "left"
assert isinstance(layout.map, dict)
with pytest.raises(KeyError):
top["asdasd"]
layout["left"].update("foobar")
print(layout["left"].children)
console = Console(width=60, color_system=None)
with console.capture() as capture:
console.print(layout, height=10)
result = capture.get()
print(repr(result))
expected = "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ\nโ”‚ foo โ”‚\nโ”‚ โ”‚\nโ”‚ โ”‚\nโ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\nfoobar โ•ญโ”€โ”€โ”€โ”€โ”€ 'right' (30 x 5) โ”€โ”€โ”€โ”€โ”€โ•ฎ\n โ”‚ โ”‚\n โ”‚ Layout(name='right') โ”‚\n โ”‚ โ”‚\n โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ\n"
assert result == expected
def test_tree():
layout = Layout(name="root")
layout.split(Layout("foo", size=2), Layout("bar", name="bar"))
layout["bar"].split_row(Layout(), Layout())
console = Console(width=60, color_system=None)
with console.capture() as capture:
console.print(layout.tree, height=10)
result = capture.get()
print(repr(result))
expected = "โฌ Layout(name='root')\nโ”œโ”€โ”€ โฌ Layout(size=2)\nโ””โ”€โ”€ โฌŒ Layout(name='bar')\n โ”œโ”€โ”€ โฌ Layout()\n โ””โ”€โ”€ โฌ Layout()\n"
print(result, "\n", expected)
assert result == expected
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
def test_refresh_screen():
layout = Layout()
layout.split_row(Layout(name="foo"), Layout(name="bar"))
console = Console(force_terminal=True, width=20, height=5, _environ={})
with console.capture():
console.print(layout)
with console.screen():
with console.capture() as capture:
layout.refresh_screen(console, "foo")
result = capture.get()
print()
print(repr(result))
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"
assert result == expected