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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
import sys
import pytest
from rich.console import Console
from rich.measure import Measurement
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"
assert result == expected
@pytest.mark.skipif(sys.platform == "win32", reason="different on Windows")
def test_render_tree_non_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", _environ={}
)
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "foo\n├── \x1b[3mbar\x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m┣━━ \x1b[0m\x1b[44m1\x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m┗━━ \x1b[0m\x1b[44m2\x1b[0m\n└── egg\n"
assert result == expected
@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", legacy_windows=True
)
console.begin_capture()
console.print(tree)
result = console.end_capture()
print(repr(result))
expected = "foo\n├── \x1b[3mbar\x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m├── \x1b[0m\x1b[44m1\x1b[0m\n\x1b[44m│ \x1b[0m\x1b[31;44m└── \x1b[0m\x1b[44m2\x1b[0m\n└── egg\n"
assert result == expected
@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")
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\n\x1b[44mbaz\x1b[0m\n\x1b[31;44m┣━━ \x1b[0m\x1b[44m1\x1b[0m\n\x1b[31;44m┗━━ \x1b[0m\x1b[44m2\x1b[0m\negg\n"
assert result == expected
@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\n\x1b[44mbaz\x1b[0m\n\x1b[31;44m├── \x1b[0m\x1b[44m1\x1b[0m\n\x1b[31;44m└── \x1b[0m\x1b[44m2\x1b[0m\negg\n"
assert result == expected
def test_tree_measure():
tree = Tree("foo")
tree.add("bar")
tree.add("mushroom risotto")
console = Console()
measurement = Measurement.get(console, console.options, tree)
assert measurement == Measurement(12, 20)
|