-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathtest_rich_utils.py
More file actions
277 lines (238 loc) · 9 KB
/
test_rich_utils.py
File metadata and controls
277 lines (238 loc) · 9 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""Unit testing for cmd2/rich_utils.py module"""
import pytest
import rich.box
from rich.console import Console
from rich.segment import Segment
from rich.style import Style
from rich.table import Table
from rich.text import Text
from cmd2 import (
Cmd2Style,
Color,
)
from cmd2 import rich_utils as ru
def test_cmd2_base_console() -> None:
# Test the keyword arguments which are not allowed.
with pytest.raises(TypeError) as excinfo:
ru.Cmd2BaseConsole(force_terminal=True)
assert 'force_terminal' in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
ru.Cmd2BaseConsole(force_interactive=True)
assert 'force_interactive' in str(excinfo.value)
with pytest.raises(TypeError) as excinfo:
ru.Cmd2BaseConsole(theme=None)
assert 'theme' in str(excinfo.value)
def test_indented_text() -> None:
console = Console(width=20)
# With an indention of 10, text will be evenly split across two lines.
text = "A" * 20
level = 10
indented_text = ru.indent(text, level)
with console.capture() as capture:
console.print(indented_text)
result = capture.get().splitlines()
padding = " " * level
expected_line = padding + ("A" * 10)
assert result[0] == expected_line
assert result[1] == expected_line
def test_indented_table() -> None:
console = Console()
level = 2
table = Table("Column", box=rich.box.ASCII)
table.add_row("Some Data")
indented_table = ru.indent(table, level)
with console.capture() as capture:
console.print(indented_table)
result = capture.get().splitlines()
padding = " " * level
assert result[0].startswith(padding + "+-----------+")
assert result[1].startswith(padding + "| Column |")
assert result[2].startswith(padding + "|-----------|")
assert result[3].startswith(padding + "| Some Data |")
assert result[4].startswith(padding + "+-----------+")
@pytest.mark.parametrize(
('rich_text', 'string'),
[
(Text("Hello"), "Hello"),
(Text("Hello\n"), "Hello\n"),
(Text("Hello", style="blue"), "\x1b[34mHello\x1b[0m"),
],
)
def test_rich_text_to_string(rich_text: Text, string: str) -> None:
assert ru.rich_text_to_string(rich_text) == string
def test_set_theme() -> None:
# Save a cmd2, rich-argparse, and rich-specific style.
cmd2_style_key = Cmd2Style.ERROR
argparse_style_key = "argparse.args"
rich_style_key = "inspect.attr"
orig_cmd2_style = ru.APP_THEME.styles[cmd2_style_key]
orig_argparse_style = ru.APP_THEME.styles[argparse_style_key]
orig_rich_style = ru.APP_THEME.styles[rich_style_key]
# Overwrite these styles by setting a new theme.
theme = {
cmd2_style_key: Style(color=Color.CYAN),
argparse_style_key: Style(color=Color.AQUAMARINE3, underline=True),
rich_style_key: Style(color=Color.DARK_GOLDENROD, bold=True),
}
ru.set_theme(theme)
# Verify theme styles have changed to our custom values.
assert ru.APP_THEME.styles[cmd2_style_key] != orig_cmd2_style
assert ru.APP_THEME.styles[cmd2_style_key] == theme[cmd2_style_key]
assert ru.APP_THEME.styles[argparse_style_key] != orig_argparse_style
assert ru.APP_THEME.styles[argparse_style_key] == theme[argparse_style_key]
assert ru.APP_THEME.styles[rich_style_key] != orig_rich_style
assert ru.APP_THEME.styles[rich_style_key] == theme[rich_style_key]
def test_from_ansi_wrapper() -> None:
# Check if we are still patching Text.from_ansi(). If this check fails, then Rich
# has fixed the bug. Therefore, we can remove this test function and ru._from_ansi_wrapper.
assert Text.from_ansi.__func__ is ru._from_ansi_wrapper.__func__ # type: ignore[attr-defined]
# Line breaks recognized by str.splitlines().
# Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
line_breaks = {
"\n", # Line Feed
"\r", # Carriage Return
"\r\n", # Carriage Return + Line Feed
"\v", # Vertical Tab
"\f", # Form Feed
"\x1c", # File Separator
"\x1d", # Group Separator
"\x1e", # Record Separator
"\x85", # Next Line (NEL)
"\u2028", # Line Separator
"\u2029", # Paragraph Separator
}
# Test all line breaks
for lb in line_breaks:
input_string = f"Text{lb}"
expected_output = input_string.replace(lb, "\n")
assert Text.from_ansi(input_string).plain == expected_output
# Test string without trailing line break
input_string = "No trailing\nline break"
assert Text.from_ansi(input_string).plain == input_string
# Test empty string
input_string = ""
assert Text.from_ansi(input_string).plain == input_string
@pytest.mark.parametrize(
# Print with style and verify that everything but newline characters have style.
('objects', 'expected', 'sep', 'end'),
[
# Print nothing
((), "\n", " ", "\n"),
# Empty string
(("",), "\n", " ", "\n"),
# Multple empty strings
(("", ""), '\x1b[34;47m \x1b[0m\n', " ", "\n"),
# Basic string
(
("str_1",),
"\x1b[34;47mstr_1\x1b[0m\n",
" ",
"\n",
),
# String which ends with newline
(
("str_1\n",),
"\x1b[34;47mstr_1\x1b[0m\n\n",
" ",
"\n",
),
# String which ends with multiple newlines
(
("str_1\n\n",),
"\x1b[34;47mstr_1\x1b[0m\n\n\n",
" ",
"\n",
),
# Mutiple lines
(
("str_1\nstr_2",),
"\x1b[34;47mstr_1\x1b[0m\n\x1b[34;47mstr_2\x1b[0m\n",
" ",
"\n",
),
# Multiple strings
(
("str_1", "str_2"),
"\x1b[34;47mstr_1 str_2\x1b[0m\n",
" ",
"\n",
),
# Multiple strings with newline between them.
(
("str_1\n", "str_2"),
"\x1b[34;47mstr_1\x1b[0m\n\x1b[34;47m str_2\x1b[0m\n",
" ",
"\n",
),
# Multiple strings and non-space value for sep
(
("str_1", "str_2"),
"\x1b[34;47mstr_1(sep)str_2\x1b[0m\n",
"(sep)",
"\n",
),
# Multiple strings and sep is a newline
(
("str_1", "str_2"),
"\x1b[34;47mstr_1\x1b[0m\n\x1b[34;47mstr_2\x1b[0m\n",
"\n",
"\n",
),
# Multiple strings and sep has newlines
(
("str_1", "str_2"),
"\x1b[34;47mstr_1(sep1)\x1b[0m\n\x1b[34;47m(sep2)str_2\x1b[0m\n",
"(sep1)\n(sep2)",
"\n",
),
# Non-newline value for end.
(
("str_1", "str_2"),
"\x1b[34;47mstr_1(sep1)\x1b[0m\n\x1b[34;47m(sep2)str_2\x1b[0m\x1b[34;47m(end)\x1b[0m",
"(sep1)\n(sep2)",
"(end)",
),
# end has newlines.
(
("str_1", "str_2"),
"\x1b[34;47mstr_1(sep1)\x1b[0m\n\x1b[34;47m(sep2)str_2\x1b[0m\x1b[34;47m(end1)\x1b[0m\n\x1b[34;47m(end2)\x1b[0m",
"(sep1)\n(sep2)",
"(end1)\n(end2)",
),
# Empty sep and end values
(
("str_1", "str_2"),
"\x1b[34;47mstr_1str_2\x1b[0m",
"",
"",
),
],
)
def test_apply_style_wrapper(objects: tuple[str], expected: str, sep: str, end: str) -> None:
# Check if we are still patching Segment.apply_style(). If this check fails, then Rich
# has fixed the bug. Therefore, we can remove this test function and ru._apply_style_wrapper.
assert Segment.apply_style.__func__ is ru._apply_style_wrapper.__func__ # type: ignore[attr-defined]
console = Console(force_terminal=True)
try:
# Since our patch was meant to fix behavior seen when soft wrapping,
# we will first test in that condition.
with console.capture() as capture:
console.print(*objects, sep=sep, end=end, style="blue on white", soft_wrap=True)
result = capture.get()
assert result == expected
# Now print with soft wrapping disabled. Since none of our input strings are long enough
# to auto wrap, the results should be the same as our soft-wrapping output.
with console.capture() as capture:
console.print(*objects, sep=sep, end=end, style="blue on white", soft_wrap=False)
result = capture.get()
assert result == expected
# Now remove our patch and disable soft wrapping. This will prove that our patch produces
# the same result as unpatched Rich
Segment.apply_style = ru._orig_segment_apply_style # type: ignore[assignment]
with console.capture() as capture:
console.print(*objects, sep=sep, end=end, style="blue on white", soft_wrap=False)
result = capture.get()
assert result == expected
finally:
# Restore the patch
Segment.apply_style = ru._apply_style_wrapper # type: ignore[assignment]