| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 138 |
|
| 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 |
|
| 145 |
|
| 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 |
|
| 154 |
|
| 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 |
|
| 163 |
|
| 164 |
mixed = "\x1b[31mRed Text\x1b[0m\x1b[?25l" |
| 165 |
|
| 166 |
assert su.strip_style(mixed) == "Red Text\x1b[?25l" |
| 167 |
|
| 168 |
|
| 169 |
def test_str_width() -> None: |
| 170 |
|
| 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 |
|
| 253 |
assert su.common_prefix([]) == "" |
| 254 |
|
| 255 |
|
| 256 |
assert su.common_prefix(["abc"]) == "abc" |
| 257 |
|
| 258 |
|
| 259 |
assert su.common_prefix(["abcdef", "abcde", "abcd"]) == "abcd" |
| 260 |
|
| 261 |
|
| 262 |
assert su.common_prefix(["abc", "def"]) == "" |
| 263 |
|
| 264 |
|
| 265 |
assert su.common_prefix(["apple", "app"]) == "app" |
| 266 |
|
| 267 |
|
| 268 |
assert su.common_prefix(["test", "test"]) == "test" |
| 269 |
|
| 270 |
|
| 271 |
assert su.common_prefix(["Apple", "apple"]) == "" |
| 272 |
|
| 273 |
|
| 274 |
assert su.common_prefix(["abc", ""]) == "" |