rich/tests/test_tree.py

144 lines
5.2 KiB
Python
Raw Normal View History

2021-01-20 17:48:41 +00:00
import sys
import pytest
2021-01-09 14:10:07 +00:00
from rich.console import Console
2021-01-10 19:26:36 +00:00
from rich.measure import Measurement
2021-01-09 14:10:07 +00:00
from rich.tree import Tree
def test_render_single_node():
tree = Tree("foo")
console = Console(color_system=None, width=20)
console.begin_capture()
console.print(tree)
assert console.end_capture() == "foo \n"
def test_render_single_branch():
tree = Tree("foo")
tree.add("bar")
console = Console(color_system=None, width=20)
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "foo \n└── bar \n"
assert result == expected
def test_render_double_branch():
tree = Tree("foo")
tree.add("bar")
tree.add("baz")
console = Console(color_system=None, width=20)
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "foo \n├── bar \n└── baz \n"
assert result == expected
def test_render_ascii():
tree = Tree("foo")
tree.add("bar")
tree.add("baz")
class AsciiConsole(Console):
@property
def encoding(self):
return "ascii"
console = AsciiConsole(color_system=None, width=20)
console.begin_capture()
console.print(tree)
result = console.end_capture()
expected = "foo \n+-- bar \n`-- baz \n"
2021-01-09 14:15:36 +00:00
assert result == expected
2021-01-20 17:48:41 +00:00
@pytest.mark.skipif(sys.platform == "win32", reason="different on Windows")
2021-07-05 14:29:26 +00:00
def test_render_tree_non_win32():
2021-01-09 14:15:36 +00:00
tree = Tree("foo")
tree.add("bar", style="italic")
baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
baz_tree.add("1")
baz_tree.add("2")
tree.add("egg")
2022-01-05 14:40:36 +00:00
console = Console(
width=20, force_terminal=True, color_system="standard", _environ={}
)
2021-01-09 14:15:36 +00:00
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
2021-01-20 17:36:20 +00:00
expected = "foo \n├── \x1b[3mbar\x1b[0m\x1b[3m \x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m┣━━ \x1b[0m\x1b[44m1\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m┗━━ \x1b[0m\x1b[44m2\x1b[0m\x1b[44m \x1b[0m\n└── egg \n"
2021-01-09 14:15:36 +00:00
assert result == expected
2021-01-10 19:26:36 +00:00
2021-07-05 14:29:26 +00:00
@pytest.mark.skipif(sys.platform != "win32", reason="Windows specific")
def test_render_tree_win32():
tree = Tree("foo")
tree.add("bar", style="italic")
baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
baz_tree.add("1")
baz_tree.add("2")
tree.add("egg")
console = Console(width=20, force_terminal=True, color_system="standard")
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "foo \n├── \x1b[3mbar\x1b[0m\x1b[3m \x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m├── \x1b[0m\x1b[44m1\x1b[0m\x1b[44m \x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m└── \x1b[0m\x1b[44m2\x1b[0m\x1b[44m \x1b[0m\n└── egg \n"
assert result == expected
2021-10-15 13:54:03 +00:00
@pytest.mark.skipif(sys.platform == "win32", reason="different on Windows")
def test_render_tree_hide_root_non_win32():
tree = Tree("foo", hide_root=True)
tree.add("bar", style="italic")
baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
baz_tree.add("1")
baz_tree.add("2")
tree.add("egg")
2022-01-05 14:40:36 +00:00
console = Console(
width=20, force_terminal=True, color_system="standard", _environ={}
)
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "\x1b[3mbar\x1b[0m\x1b[3m \x1b[0m\n\x1b[44mbaz\x1b[0m\x1b[44m \x1b[0m\n\x1b[31;44m┣━━ \x1b[0m\x1b[44m1\x1b[0m\x1b[44m \x1b[0m\n\x1b[31;44m┗━━ \x1b[0m\x1b[44m2\x1b[0m\x1b[44m \x1b[0m\negg \n"
assert result == expected
2021-10-15 13:54:03 +00:00
@pytest.mark.skipif(sys.platform != "win32", reason="Windows specific")
def test_render_tree_hide_root_win32():
tree = Tree("foo", hide_root=True)
tree.add("bar", style="italic")
baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
baz_tree.add("1")
baz_tree.add("2")
tree.add("egg")
console = Console(width=20, force_terminal=True, color_system="standard")
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "\x1b[3mbar\x1b[0m\x1b[3m \x1b[0m\n\x1b[44mbaz\x1b[0m\x1b[44m \x1b[0m\n\x1b[31;44m├── \x1b[0m\x1b[44m1\x1b[0m\x1b[44m \x1b[0m\n\x1b[31;44m└── \x1b[0m\x1b[44m2\x1b[0m\x1b[44m \x1b[0m\negg \n"
assert result == expected
2021-07-05 14:29:26 +00:00
2021-10-15 13:54:03 +00:00
2021-01-10 19:26:36 +00:00
def test_tree_measure():
tree = Tree("foo")
tree.add("bar")
tree.add("musroom risotto")
console = Console()
2021-03-25 21:08:36 +00:00
measurement = Measurement.get(console, console.options, tree)
2021-01-10 19:26:36 +00:00
assert measurement == Measurement(11, 19)