| 1 |
import sys |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from rich.console import Console |
| 6 |
from rich.layout import Layout, NoSplitter |
| 7 |
from rich.panel import Panel |
| 8 |
|
| 9 |
|
| 10 |
def test_no_layout(): |
| 11 |
layout = Layout() |
| 12 |
with pytest.raises(NoSplitter): |
| 13 |
layout.split(Layout(), Layout(), splitter="nope") |
| 14 |
|
| 15 |
|
| 16 |
def test_add_split(): |
| 17 |
layout = Layout() |
| 18 |
layout.split(Layout(), Layout()) |
| 19 |
assert len(layout.children) == 2 |
| 20 |
layout.add_split(Layout(name="foo")) |
| 21 |
assert len(layout.children) == 3 |
| 22 |
assert layout.children[2].name == "foo" |
| 23 |
|
| 24 |
|
| 25 |
def test_unsplit(): |
| 26 |
layout = Layout() |
| 27 |
layout.split(Layout(), Layout()) |
| 28 |
assert len(layout.children) == 2 |
| 29 |
|
| 30 |
layout.unsplit() |
| 31 |
assert len(layout.children) == 0 |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 35 |
def test_render(): |
| 36 |
layout = Layout(name="root") |
| 37 |
repr(layout) |
| 38 |
|
| 39 |
layout.split_column(Layout(name="top"), Layout(name="bottom")) |
| 40 |
top = layout["top"] |
| 41 |
top.update(Panel("foo")) |
| 42 |
|
| 43 |
print(type(top._renderable)) |
| 44 |
assert isinstance(top.renderable, Panel) |
| 45 |
layout["bottom"].split_row(Layout(name="left"), Layout(name="right")) |
| 46 |
|
| 47 |
assert layout["root"].name == "root" |
| 48 |
assert layout["left"].name == "left" |
| 49 |
|
| 50 |
assert isinstance(layout.map, dict) |
| 51 |
|
| 52 |
with pytest.raises(KeyError): |
| 53 |
top["asdasd"] |
| 54 |
|
| 55 |
layout["left"].update("foobar") |
| 56 |
print(layout["left"].children) |
| 57 |
|
| 58 |
console = Console(width=60, color_system=None) |
| 59 |
|
| 60 |
with console.capture() as capture: |
| 61 |
console.print(layout, height=10) |
| 62 |
|
| 63 |
result = capture.get() |
| 64 |
print(repr(result)) |
| 65 |
expected = "╭──────────────────────────────────────────────────────────╮\n│ foo │\n│ │\n│ │\n╰──────────────────────────────────────────────────────────╯\nfoobar ╭───── 'right' (30 x 5) ─────╮\n │ │\n │ Layout(name='right') │\n │ │\n ╰────────────────────────────╯\n" |
| 66 |
|
| 67 |
assert result == expected |
| 68 |
|
| 69 |
|
| 70 |
def test_tree(): |
| 71 |
layout = Layout(name="root") |
| 72 |
layout.split(Layout("foo", size=2), Layout("bar", name="bar")) |
| 73 |
layout["bar"].split_row(Layout(), Layout()) |
| 74 |
|
| 75 |
console = Console(width=60, color_system=None) |
| 76 |
|
| 77 |
with console.capture() as capture: |
| 78 |
console.print(layout.tree, height=10) |
| 79 |
result = capture.get() |
| 80 |
print(repr(result)) |
| 81 |
expected = "⬍ Layout(name='root')\n├── ⬍ Layout(size=2)\n└── ⬌ Layout(name='bar')\n ├── ⬍ Layout()\n └── ⬍ Layout()\n" |
| 82 |
print(result, "\n", expected) |
| 83 |
assert result == expected |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows") |
| 87 |
def test_refresh_screen(): |
| 88 |
layout = Layout() |
| 89 |
layout.split_row(Layout(name="foo"), Layout(name="bar")) |
| 90 |
console = Console(force_terminal=True, width=20, height=5, _environ={}) |
| 91 |
with console.capture(): |
| 92 |
console.print(layout) |
| 93 |
with console.screen(): |
| 94 |
with console.capture() as capture: |
| 95 |
layout.refresh_screen(console, "foo") |
| 96 |
result = capture.get() |
| 97 |
print() |
| 98 |
print(repr(result)) |
| 99 |
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" |
| 100 |
assert result == expected |