Debian Codegraph @

tests/test_tree.py

raw
1 import sys
2
3 import pytest
4
5 from rich.console import Console
6 from rich.measure import Measurement
7 from rich.tree import Tree
8
9
10 def test_render_single_node():
11 tree = Tree("foo")
12 console = Console(color_system=None, width=20)
13 console.begin_capture()
14 console.print(tree)
15 assert console.end_capture() == "foo\n"
16
17
18 def test_render_single_branch():
19 tree = Tree("foo")
20 tree.add("bar")
21 console = Console(color_system=None, width=20)
22 console.begin_capture()
23 console.print(tree)
24 result = console.end_capture()
25 print(repr(result))
26 expected = "foo\n└── bar\n"
27 assert result == expected
28
29
30 def test_render_double_branch():
31 tree = Tree("foo")
32 tree.add("bar")
33 tree.add("baz")
34 console = Console(color_system=None, width=20)
35 console.begin_capture()
36 console.print(tree)
37 result = console.end_capture()
38 print(repr(result))
39 expected = "foo\n├── bar\n└── baz\n"
40 assert result == expected
41
42
43 def test_render_ascii():
44 tree = Tree("foo")
45 tree.add("bar")
46 tree.add("baz")
47
48 class AsciiConsole(Console):
49 @property
50 def encoding(self):
51 return "ascii"
52
53 console = AsciiConsole(color_system=None, width=20)
54 console.begin_capture()
55 console.print(tree)
56 result = console.end_capture()
57 expected = "foo\n+-- bar\n`-- baz\n"
58 assert result == expected
59
60
61 @pytest.mark.skipif(sys.platform == "win32", reason="different on Windows")
62 def test_render_tree_non_win32():
63 tree = Tree("foo")
64 tree.add("bar", style="italic")
65 baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
66 baz_tree.add("1")
67 baz_tree.add("2")
68 tree.add("egg")
69
70 console = Console(
71 width=20, force_terminal=True, color_system="standard", _environ={}
72 )
73 console.begin_capture()
74 console.print(tree)
75 result = console.end_capture()
76 print(repr(result))
77 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"
78 assert result == expected
79
80
81 @pytest.mark.skipif(sys.platform != "win32", reason="Windows specific")
82 def test_render_tree_win32():
83 tree = Tree("foo")
84 tree.add("bar", style="italic")
85 baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
86 baz_tree.add("1")
87 baz_tree.add("2")
88 tree.add("egg")
89
90 console = Console(
91 width=20, force_terminal=True, color_system="standard", legacy_windows=True
92 )
93 console.begin_capture()
94 console.print(tree)
95 result = console.end_capture()
96 print(repr(result))
97 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"
98 assert result == expected
99
100
101 @pytest.mark.skipif(sys.platform == "win32", reason="different on Windows")
102 def test_render_tree_hide_root_non_win32():
103 tree = Tree("foo", hide_root=True)
104 tree.add("bar", style="italic")
105 baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
106 baz_tree.add("1")
107 baz_tree.add("2")
108 tree.add("egg")
109
110 console = Console(
111 width=20, force_terminal=True, color_system="standard", _environ={}
112 )
113 console.begin_capture()
114 console.print(tree)
115 result = console.end_capture()
116 print(repr(result))
117 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"
118 assert result == expected
119
120
121 @pytest.mark.skipif(sys.platform != "win32", reason="Windows specific")
122 def test_render_tree_hide_root_win32():
123 tree = Tree("foo", hide_root=True)
124 tree.add("bar", style="italic")
125 baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
126 baz_tree.add("1")
127 baz_tree.add("2")
128 tree.add("egg")
129
130 console = Console(width=20, force_terminal=True, color_system="standard")
131 console.begin_capture()
132 console.print(tree)
133 result = console.end_capture()
134 print(repr(result))
135 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"
136 assert result == expected
137
138
139 def test_tree_measure():
140 tree = Tree("foo")
141 tree.add("bar")
142 tree.add("mushroom risotto")
143 console = Console()
144 measurement = Measurement.get(console, console.options, tree)
145 assert measurement == Measurement(12, 20)