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
|
# coding=utf-8
# flake8: noqa E302
"""
Unit testing for cmd2/ansi.py module
"""
import pytest
from cmd2 import (
ansi,
)
HELLO_WORLD = 'Hello, world!'
def test_strip_style():
base_str = HELLO_WORLD
ansi_str = ansi.style(base_str, fg=ansi.Fg.GREEN)
assert base_str != ansi_str
assert base_str == ansi.strip_style(ansi_str)
def test_style_aware_wcswidth():
base_str = HELLO_WORLD
ansi_str = ansi.style(base_str, fg=ansi.Fg.GREEN)
assert ansi.style_aware_wcswidth(HELLO_WORLD) == ansi.style_aware_wcswidth(ansi_str)
assert ansi.style_aware_wcswidth('i have a tab\t') == -1
assert ansi.style_aware_wcswidth('i have a newline\n') == -1
def test_widest_line():
text = ansi.style('i have\n3 lines\nThis is the longest one', fg=ansi.Fg.GREEN)
assert ansi.widest_line(text) == ansi.style_aware_wcswidth("This is the longest one")
text = "I'm just one line"
assert ansi.widest_line(text) == ansi.style_aware_wcswidth(text)
assert ansi.widest_line('i have a tab\t') == -1
def test_style_none():
base_str = HELLO_WORLD
ansi_str = base_str
assert ansi.style(base_str) == ansi_str
@pytest.mark.parametrize('fg_color', [ansi.Fg.BLUE, ansi.EightBitFg.AQUAMARINE_1A, ansi.RgbFg(0, 2, 4)])
def test_style_fg(fg_color):
base_str = HELLO_WORLD
ansi_str = fg_color + base_str + ansi.Fg.RESET
assert ansi.style(base_str, fg=fg_color) == ansi_str
@pytest.mark.parametrize('bg_color', [ansi.Bg.BLUE, ansi.EightBitBg.AQUAMARINE_1A, ansi.RgbBg(0, 2, 4)])
def test_style_bg(bg_color):
base_str = HELLO_WORLD
ansi_str = bg_color + base_str + ansi.Bg.RESET
assert ansi.style(base_str, bg=bg_color) == ansi_str
# noinspection PyTypeChecker
def test_style_invalid_types():
# Use a BgColor with fg
with pytest.raises(TypeError):
ansi.style('test', fg=ansi.Bg.BLUE)
# Use a FgColor with bg
with pytest.raises(TypeError):
ansi.style('test', bg=ansi.Fg.BLUE)
def test_style_bold():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.INTENSITY_BOLD + base_str + ansi.TextStyle.INTENSITY_NORMAL
assert ansi.style(base_str, bold=True) == ansi_str
def test_style_dim():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.INTENSITY_DIM + base_str + ansi.TextStyle.INTENSITY_NORMAL
assert ansi.style(base_str, dim=True) == ansi_str
def test_style_italic():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.ITALIC_ENABLE + base_str + ansi.TextStyle.ITALIC_DISABLE
assert ansi.style(base_str, italic=True) == ansi_str
def test_style_overline():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.OVERLINE_ENABLE + base_str + ansi.TextStyle.OVERLINE_DISABLE
assert ansi.style(base_str, overline=True) == ansi_str
def test_style_strikethrough():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.STRIKETHROUGH_ENABLE + base_str + ansi.TextStyle.STRIKETHROUGH_DISABLE
assert ansi.style(base_str, strikethrough=True) == ansi_str
def test_style_underline():
base_str = HELLO_WORLD
ansi_str = ansi.TextStyle.UNDERLINE_ENABLE + base_str + ansi.TextStyle.UNDERLINE_DISABLE
assert ansi.style(base_str, underline=True) == ansi_str
def test_style_multi():
base_str = HELLO_WORLD
fg_color = ansi.Fg.LIGHT_BLUE
bg_color = ansi.Bg.LIGHT_GRAY
ansi_str = (
fg_color
+ bg_color
+ ansi.TextStyle.INTENSITY_BOLD
+ ansi.TextStyle.INTENSITY_DIM
+ ansi.TextStyle.ITALIC_ENABLE
+ ansi.TextStyle.OVERLINE_ENABLE
+ ansi.TextStyle.STRIKETHROUGH_ENABLE
+ ansi.TextStyle.UNDERLINE_ENABLE
+ base_str
+ ansi.Fg.RESET
+ ansi.Bg.RESET
+ ansi.TextStyle.INTENSITY_NORMAL
+ ansi.TextStyle.INTENSITY_NORMAL
+ ansi.TextStyle.ITALIC_DISABLE
+ ansi.TextStyle.OVERLINE_DISABLE
+ ansi.TextStyle.STRIKETHROUGH_DISABLE
+ ansi.TextStyle.UNDERLINE_DISABLE
)
assert (
ansi.style(
base_str,
fg=fg_color,
bg=bg_color,
bold=True,
dim=True,
italic=True,
overline=True,
strikethrough=True,
underline=True,
)
== ansi_str
)
def test_set_title():
title = HELLO_WORLD
assert ansi.set_title(title) == ansi.OSC + '2;' + title + ansi.BEL
@pytest.mark.parametrize(
'cols, prompt, line, cursor, msg, expected',
[
(
127,
'(Cmd) ',
'help his',
12,
ansi.style('Hello World!', fg=ansi.Fg.MAGENTA),
'\x1b[2K\r\x1b[35mHello World!\x1b[39m',
),
(127, '\n(Cmd) ', 'help ', 5, 'foo', '\x1b[2K\x1b[1A\x1b[2K\rfoo'),
(
10,
'(Cmd) ',
'help history of the american republic',
4,
'boo',
'\x1b[3B\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\rboo',
),
],
)
def test_async_alert_str(cols, prompt, line, cursor, msg, expected):
alert_str = ansi.async_alert_str(terminal_columns=cols, prompt=prompt, line=line, cursor_offset=cursor, alert_msg=msg)
assert alert_str == expected
def test_clear_screen():
clear_type = 2
assert ansi.clear_screen(clear_type) == f"{ansi.CSI}{clear_type}J"
clear_type = -1
with pytest.raises(ValueError):
ansi.clear_screen(clear_type)
clear_type = 4
with pytest.raises(ValueError):
ansi.clear_screen(clear_type)
def test_clear_line():
clear_type = 2
assert ansi.clear_line(clear_type) == f"{ansi.CSI}{clear_type}K"
clear_type = -1
with pytest.raises(ValueError):
ansi.clear_line(clear_type)
clear_type = 3
with pytest.raises(ValueError):
ansi.clear_line(clear_type)
def test_cursor():
count = 1
assert ansi.Cursor.UP(count) == f"{ansi.CSI}{count}A"
assert ansi.Cursor.DOWN(count) == f"{ansi.CSI}{count}B"
assert ansi.Cursor.FORWARD(count) == f"{ansi.CSI}{count}C"
assert ansi.Cursor.BACK(count) == f"{ansi.CSI}{count}D"
x = 4
y = 5
assert ansi.Cursor.SET_POS(x, y) == f"{ansi.CSI}{y};{x}H"
@pytest.mark.parametrize(
'ansi_sequence',
[
ansi.fg.green,
ansi.bg.blue,
ansi.Fg.MAGENTA,
ansi.Bg.LIGHT_GRAY,
ansi.EightBitBg.CHARTREUSE_2A,
ansi.EightBitBg.MEDIUM_PURPLE,
ansi.RgbFg(0, 5, 22),
ansi.RgbBg(100, 150, 222),
ansi.TextStyle.OVERLINE_ENABLE,
],
)
def test_sequence_str_building(ansi_sequence):
"""This tests __add__(), __radd__(), and __str__() methods for AnsiSequences"""
assert ansi_sequence + ansi_sequence == str(ansi_sequence) + str(ansi_sequence)
@pytest.mark.parametrize(
'r, g, b, valid',
[
(0, 0, 0, True),
(255, 255, 255, True),
(-1, 0, 0, False),
(256, 255, 255, False),
(0, -1, 0, False),
(255, 256, 255, False),
(0, 0, -1, False),
(255, 255, 256, False),
],
)
def test_rgb_bounds(r, g, b, valid):
if valid:
ansi.RgbFg(r, g, b)
ansi.RgbBg(r, g, b)
else:
with pytest.raises(ValueError):
ansi.RgbFg(r, g, b)
with pytest.raises(ValueError):
ansi.RgbBg(r, g, b)
|