Debian Codegraph @

tests/test_string_utils.py

raw symbols SCIP
1 """Unit testing for cmd2/string_utils.py module"""
2
3 from rich.style import Style
4
5 from cmd2 import Color
6 from cmd2 import rich_utils as ru
7 from cmd2 import string_utils as su
8
9 HELLO_WORLD = "Hello, world!"
10
11
12 def test_align_blank() -> None:
13 text = ""
14 character = "-"
15 width = 5
16 aligned = su.align(text, "left", width=width, character=character)
17 assert aligned == character * width
18
19
20 def test_align_wider_than_width() -> None:
21 text = "long text field"
22 character = "-"
23 width = 8
24 aligned = su.align(text, "left", width=width, character=character)
25 assert aligned == text[:width]
26
27
28 def test_align_term_width() -> None:
29 text = "foo"
30 character = " "
31
32 term_width = ru.console_width()
33 expected_padding = (term_width - su.str_width(text)) * character
34
35 aligned = su.align(text, "left", character=character)
36 assert aligned == text + expected_padding
37
38
39 def test_align_left() -> None:
40 text = "foo"
41 character = "-"
42 width = 5
43 aligned = su.align_left(text, width=width, character=character)
44 assert aligned == text + character * 2
45
46
47 def test_align_left_wide_text() -> None:
48 text = "苹"
49 character = "-"
50 width = 4
51 aligned = su.align_left(text, width=width, character=character)
52 assert aligned == text + character * 2
53
54
55 def test_align_left_with_style() -> None:
56 character = "-"
57
58 styled_text = su.stylize("table", style=Color.BRIGHT_BLUE)
59 width = 8
60
61 aligned = su.align_left(styled_text, width=width, character=character)
62 assert aligned == styled_text + character * 3
63
64
65 def test_align_center() -> None:
66 text = "foo"
67 character = "-"
68 width = 5
69 aligned = su.align_center(text, width=width, character=character)
70 assert aligned == character + text + character
71
72
73 def test_align_center_wide_text() -> None:
74 text = "苹"
75 character = "-"
76 width = 4
77 aligned = su.align_center(text, width=width, character=character)
78 assert aligned == character + text + character
79
80
81 def test_align_center_with_style() -> None:
82 character = "-"
83
84 styled_text = su.stylize("table", style=Color.BRIGHT_BLUE)
85 width = 8
86
87 aligned = su.align_center(styled_text, width=width, character=character)
88 assert aligned == character + styled_text + character * 2
89
90
91 def test_align_right() -> None:
92 text = "foo"
93 character = "-"
94 width = 5
95 aligned = su.align_right(text, width=width, character=character)
96 assert aligned == character * 2 + text
97
98
99 def test_align_right_wide_text() -> None:
100 text = "苹"
101 character = "-"
102 width = 4
103 aligned = su.align_right(text, width=width, character=character)
104 assert aligned == character * 2 + text
105
106
107 def test_align_right_with_style() -> None:
108 character = "-"
109
110 styled_text = su.stylize("table", style=Color.BRIGHT_BLUE)
111 width = 8
112
113 aligned = su.align_right(styled_text, width=width, character=character)
114 assert aligned == character * 3 + styled_text
115
116
117 def test_stylize() -> None:
118 # Test string with no existing style
119 style = Style(color=Color.GREEN, bgcolor=Color.BLUE, bold=True, underline=True)
120 styled_str = su.stylize(HELLO_WORLD, style=style)
121 assert styled_str == "\x1b[1;4;32;44mHello, world!\x1b[0m"
122
123 # Add style to already-styled string
124 updated_style = Style.combine([style, Style(strike=True)])
125 restyled_string = su.stylize(styled_str, style=updated_style)
126 assert restyled_string == "\x1b[1;4;9;32;44mHello, world!\x1b[0m"
127
128
129 def test_strip_basic_styles() -> None:
130 # Test bold, colors, and resets
131 assert su.strip_style("\x1b[1mBold\x1b[0m") == "Bold"
132 assert su.strip_style("\x1b[31mRed\x1b[0m") == "Red"
133 assert su.strip_style("\x1b[4;32mUnderline Green\x1b[0m") == "Underline Green"
134
135
136 def test_strip_complex_colors() -> None:
137 # Test 256-color and RGB (TrueColor) sequences
138 # These use semicolons as separators and end in 'm'
139 assert su.strip_style("\x1b[38;5;208mOrange\x1b[0m") == "Orange"
140 assert su.strip_style("\x1b[38;2;255;87;51mCustom RGB\x1b[0m") == "Custom RGB"
141
142
143 def test_preserve_non_style_csi() -> None:
144 # Test that cursor movements and other CSI codes are not stripped
145 # Cursor Up (\x1b[A) and Cursor Position (\x1b[10;5H)
146 cursor_up = "\x1b[A"
147 cursor_pos = "\x1b[10;5H"
148 assert su.strip_style(cursor_up) == cursor_up
149 assert su.strip_style(cursor_pos) == cursor_pos
150
151
152 def test_preserve_private_modes() -> None:
153 # Test that DEC Private Modes (containing '?') are not stripped
154 # Hide cursor (\x1b[?25l) and Show cursor (\x1b[?25h)
155 hide_cursor = "\x1b[?25l"
156 show_cursor = "\x1b[?25h"
157 assert su.strip_style(hide_cursor) == hide_cursor
158 assert su.strip_style(show_cursor) == show_cursor
159
160
161 def test_mixed_content() -> None:
162 # Test a string that has both style and control sequences
163 # Red text + Hide Cursor
164 mixed = "\x1b[31mRed Text\x1b[0m\x1b[?25l"
165 # Only the red style should be removed
166 assert su.strip_style(mixed) == "Red Text\x1b[?25l"
167
168
169 def test_str_width() -> None:
170 # Include a full-width character
171 base_str = HELLO_WORLD + "深"
172 styled_str = su.stylize(base_str, style=Color.GREEN)
173 expected_width = len(HELLO_WORLD) + 2
174 assert su.str_width(base_str) == su.str_width(styled_str) == expected_width
175
176
177 def test_is_quoted_short() -> None:
178 my_str = ""
179 assert not su.is_quoted(my_str)
180 your_str = '"'
181 assert not su.is_quoted(your_str)
182
183
184 def test_is_quoted_yes() -> None:
185 my_str = '"This is a test"'
186 assert su.is_quoted(my_str)
187 your_str = "'of the emergengy broadcast system'"
188 assert su.is_quoted(your_str)
189
190
191 def test_is_quoted_no() -> None:
192 my_str = '"This is a test'
193 assert not su.is_quoted(my_str)
194 your_str = "of the emergengy broadcast system'"
195 assert not su.is_quoted(your_str)
196 simple_str = "hello world"
197 assert not su.is_quoted(simple_str)
198
199
200 def test_quote() -> None:
201 my_str = "Hello World"
202 assert su.quote(my_str) == '"' + my_str + '"'
203
204 my_str = "'Hello World'"
205 assert su.quote(my_str) == '"' + my_str + '"'
206
207 my_str = '"Hello World"'
208 assert su.quote(my_str) == "'" + my_str + "'"
209
210
211 def test_quote_if_needed_yes() -> None:
212 my_str = "Hello World"
213 assert su.quote_if_needed(my_str) == '"' + my_str + '"'
214 your_str = '"foo" bar'
215 assert su.quote_if_needed(your_str) == "'" + your_str + "'"
216
217
218 def test_quote_if_needed_no() -> None:
219 my_str = "HelloWorld"
220 assert su.quote_if_needed(my_str) == my_str
221 your_str = "'Hello World'"
222 assert su.quote_if_needed(your_str) == your_str
223
224
225 def test_strip_quotes_no_quotes() -> None:
226 base_str = HELLO_WORLD
227 stripped = su.strip_quotes(base_str)
228 assert base_str == stripped
229
230
231 def test_strip_quotes_with_quotes() -> None:
232 base_str = '"' + HELLO_WORLD + '"'
233 stripped = su.strip_quotes(base_str)
234 assert stripped == HELLO_WORLD
235
236
237 def test_unicode_normalization() -> None:
238 s1 = "café"
239 s2 = "cafe\u0301"
240 assert s1 != s2
241 assert su.norm_fold(s1) == su.norm_fold(s2)
242
243
244 def test_unicode_casefold() -> None:
245 micro = "µ"
246 micro_cf = micro.casefold()
247 assert micro != micro_cf
248 assert su.norm_fold(micro) == su.norm_fold(micro_cf)
249
250
251 def test_common_prefix() -> None:
252 # Empty list
253 assert su.common_prefix([]) == ""
254
255 # Single item
256 assert su.common_prefix(["abc"]) == "abc"
257
258 # Common prefix exists
259 assert su.common_prefix(["abcdef", "abcde", "abcd"]) == "abcd"
260
261 # No common prefix
262 assert su.common_prefix(["abc", "def"]) == ""
263
264 # One is a prefix of another
265 assert su.common_prefix(["apple", "app"]) == "app"
266
267 # Identical strings
268 assert su.common_prefix(["test", "test"]) == "test"
269
270 # Case sensitivity (matches os.path.commonprefix behavior)
271 assert su.common_prefix(["Apple", "apple"]) == ""
272
273 # Empty string in list
274 assert su.common_prefix(["abc", ""]) == ""