File size: 4,055 Bytes
065fee7 |
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 |
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
|