| 1 |
"""Unit testing for cmd2/theme.py module""" |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
import pytest |
| 6 |
from rich.style import Style |
| 7 |
|
| 8 |
from cmd2 import ( |
| 9 |
Cmd2Style, |
| 10 |
Color, |
| 11 |
) |
| 12 |
from cmd2 import rich_utils as ru |
| 13 |
from cmd2.rich_utils import Cmd2HelpFormatter |
| 14 |
from cmd2.theme import ( |
| 15 |
get_pt_theme, |
| 16 |
get_theme, |
| 17 |
register_pt_mapping, |
| 18 |
register_synchronized_prefix, |
| 19 |
register_synchronized_style, |
| 20 |
reset_theme, |
| 21 |
unregister_pt_mapping, |
| 22 |
unregister_synchronized_prefix, |
| 23 |
unregister_synchronized_style, |
| 24 |
update_theme, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def test_reset_theme() -> None: |
| 29 |
|
| 30 |
orig_cmd2_styles = dict(get_theme().styles) |
| 31 |
orig_rich_argparse_styles = Cmd2HelpFormatter.styles.copy() |
| 32 |
orig_pt_styles = get_pt_theme().style_rules |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
cmd2_style_key = Cmd2Style.ERROR |
| 37 |
argparse_style_key = "argparse.args" |
| 38 |
rich_style_key = "inspect.attr" |
| 39 |
pt_mapped_key = Cmd2Style.COMPLETION_MENU |
| 40 |
|
| 41 |
new_styles = { |
| 42 |
cmd2_style_key: Style(color=Color.CYAN), |
| 43 |
argparse_style_key: Style(color=Color.AQUAMARINE3, underline=True), |
| 44 |
rich_style_key: Style(color=Color.DARK_GOLDENROD, bold=True), |
| 45 |
pt_mapped_key: Style(color=Color.BLUE), |
| 46 |
} |
| 47 |
update_theme(new_styles) |
| 48 |
|
| 49 |
|
| 50 |
assert orig_cmd2_styles != get_theme().styles |
| 51 |
assert orig_rich_argparse_styles != Cmd2HelpFormatter.styles |
| 52 |
assert orig_pt_styles != get_pt_theme().style_rules |
| 53 |
|
| 54 |
|
| 55 |
reset_theme() |
| 56 |
|
| 57 |
assert orig_cmd2_styles == get_theme().styles |
| 58 |
assert orig_rich_argparse_styles == Cmd2HelpFormatter.styles |
| 59 |
assert orig_pt_styles == get_pt_theme().style_rules |
| 60 |
|
| 61 |
|
| 62 |
def test_update_theme() -> None: |
| 63 |
|
| 64 |
|
| 65 |
cmd2_style_key = Cmd2Style.ERROR |
| 66 |
argparse_style_key = "argparse.args" |
| 67 |
rich_style_key = "inspect.attr" |
| 68 |
pt_mapped_key = Cmd2Style.COMPLETION_MENU |
| 69 |
|
| 70 |
theme = get_theme() |
| 71 |
pt_theme = get_pt_theme() |
| 72 |
|
| 73 |
|
| 74 |
orig_cmd2_style = theme.styles[cmd2_style_key] |
| 75 |
orig_argparse_style = theme.styles[argparse_style_key] |
| 76 |
orig_rich_argparse_style = ru.Cmd2HelpFormatter.styles[argparse_style_key] |
| 77 |
orig_rich_style = theme.styles[rich_style_key] |
| 78 |
orig_completion_menu = theme.styles[pt_mapped_key] |
| 79 |
orig_pt_completion_menu = pt_theme.get_attrs_for_style_str("class:completion-menu") |
| 80 |
|
| 81 |
|
| 82 |
new_styles = { |
| 83 |
cmd2_style_key: Style(color=Color.CYAN), |
| 84 |
argparse_style_key: Style(color=Color.AQUAMARINE3, underline=True), |
| 85 |
rich_style_key: Style(color=Color.DARK_GOLDENROD, bold=True), |
| 86 |
pt_mapped_key: Style(color=Color.BLUE), |
| 87 |
} |
| 88 |
update_theme(new_styles) |
| 89 |
|
| 90 |
|
| 91 |
assert theme.styles[cmd2_style_key] != orig_cmd2_style |
| 92 |
assert theme.styles[cmd2_style_key] == new_styles[cmd2_style_key] |
| 93 |
|
| 94 |
assert theme.styles[argparse_style_key] != orig_argparse_style |
| 95 |
assert theme.styles[argparse_style_key] == new_styles[argparse_style_key] |
| 96 |
assert ru.Cmd2HelpFormatter.styles[argparse_style_key] != orig_rich_argparse_style |
| 97 |
assert ru.Cmd2HelpFormatter.styles[argparse_style_key] == new_styles[argparse_style_key] |
| 98 |
|
| 99 |
assert theme.styles[rich_style_key] != orig_rich_style |
| 100 |
assert theme.styles[rich_style_key] == new_styles[rich_style_key] |
| 101 |
|
| 102 |
assert theme.styles[pt_mapped_key] != orig_completion_menu |
| 103 |
assert theme.styles[pt_mapped_key] == new_styles[pt_mapped_key] |
| 104 |
|
| 105 |
|
| 106 |
new_pt_theme = get_pt_theme() |
| 107 |
new_pt_completion_menu = new_pt_theme.get_attrs_for_style_str("class:completion-menu") |
| 108 |
assert orig_pt_completion_menu != new_pt_completion_menu |
| 109 |
|
| 110 |
for field, value in new_pt_completion_menu._asdict().items(): |
| 111 |
if field == "color": |
| 112 |
expected: Any = "ansiblue" |
| 113 |
elif field == "bgcolor": |
| 114 |
expected = "default" |
| 115 |
else: |
| 116 |
expected = False |
| 117 |
|
| 118 |
assert expected == value |
| 119 |
|
| 120 |
|
| 121 |
update_theme({"custom_style": "bold red"}) |
| 122 |
update_theme({"another_style": "blue"}) |
| 123 |
assert theme.styles["custom_style"] == Style(color=Color.RED, bold=True) |
| 124 |
assert theme.styles["another_style"] == Style(color="blue") |
| 125 |
|
| 126 |
|
| 127 |
def test_theme_is_none() -> None: |
| 128 |
"""Test that get_theme() creates the theme when it's None.""" |
| 129 |
from cmd2 import theme |
| 130 |
|
| 131 |
theme._THEME = None |
| 132 |
|
| 133 |
assert get_theme() is not None |
| 134 |
|
| 135 |
|
| 136 |
def test_pt_theme_is_none() -> None: |
| 137 |
"""Test that get_pt_theme() creates the pt theme when it's None.""" |
| 138 |
from cmd2 import theme |
| 139 |
|
| 140 |
theme._PT_THEME = None |
| 141 |
|
| 142 |
assert get_pt_theme() is not None |
| 143 |
|
| 144 |
|
| 145 |
def test_sync_all_theme_is_none() -> None: |
| 146 |
"""Test that calling _sync_all() when _THEME is None is a no-op.""" |
| 147 |
from cmd2 import theme |
| 148 |
|
| 149 |
theme._THEME = None |
| 150 |
theme._PT_THEME = None |
| 151 |
|
| 152 |
theme._sync_all() |
| 153 |
|
| 154 |
assert theme._THEME is None |
| 155 |
assert theme._PT_THEME is None |
| 156 |
|
| 157 |
|
| 158 |
def test_sync_pt_theme_theme_is_none() -> None: |
| 159 |
"""Test that calling _sync_pt_theme() when _THEME is None is a no-op.""" |
| 160 |
from cmd2 import theme |
| 161 |
|
| 162 |
theme._THEME = None |
| 163 |
theme._PT_THEME = None |
| 164 |
|
| 165 |
theme._sync_pt_theme() |
| 166 |
|
| 167 |
assert theme._THEME is None |
| 168 |
assert theme._PT_THEME is None |
| 169 |
|
| 170 |
|
| 171 |
def test_register_pt_mapping() -> None: |
| 172 |
"""Test style registration with UI mapping.""" |
| 173 |
style_name = "my_custom_scrollbar" |
| 174 |
ui_name = "scrollbar" |
| 175 |
|
| 176 |
register_pt_mapping(style_name, ui_name) |
| 177 |
|
| 178 |
update_theme({style_name: Style(color=Color.BLUE)}) |
| 179 |
|
| 180 |
pt_theme = get_pt_theme() |
| 181 |
|
| 182 |
|
| 183 |
attrs_main = pt_theme.get_attrs_for_style_str(f"class:{style_name}") |
| 184 |
attrs_ui = pt_theme.get_attrs_for_style_str(f"class:{ui_name}") |
| 185 |
|
| 186 |
assert attrs_main.color == "ansiblue" |
| 187 |
assert attrs_ui.color == "ansiblue" |
| 188 |
|
| 189 |
|
| 190 |
def test_register_pt_mapping_redundant() -> None: |
| 191 |
"""Test that redundant mappings are filtered out.""" |
| 192 |
from cmd2 import theme |
| 193 |
|
| 194 |
style_name = "redundant" |
| 195 |
register_pt_mapping(style_name, [style_name, "other"]) |
| 196 |
|
| 197 |
assert style_name not in theme._PT_UI_MAP[style_name] |
| 198 |
assert "other" in theme._PT_UI_MAP[style_name] |
| 199 |
|
| 200 |
|
| 201 |
def test_register_pt_mapping_existing_style() -> None: |
| 202 |
"""Test calling register_pt_mapping with an existing style name.""" |
| 203 |
style_name = "existing_style" |
| 204 |
ui_name = "ui_component" |
| 205 |
|
| 206 |
|
| 207 |
register_pt_mapping(style_name, ui_name) |
| 208 |
|
| 209 |
|
| 210 |
register_pt_mapping(style_name, ui_name) |
| 211 |
|
| 212 |
|
| 213 |
from cmd2 import theme |
| 214 |
|
| 215 |
assert style_name in theme._PT_UI_MAP |
| 216 |
assert ui_name in theme._PT_UI_MAP[style_name] |
| 217 |
|
| 218 |
|
| 219 |
def test_unregister_pt_mapping() -> None: |
| 220 |
"""Test unregistering UI mappings from styles.""" |
| 221 |
from prompt_toolkit.styles import DEFAULT_ATTRS |
| 222 |
|
| 223 |
style_name = "custom_scroll" |
| 224 |
ui_names = ["scroll1", "scroll2"] |
| 225 |
|
| 226 |
register_pt_mapping(style_name, ui_names) |
| 227 |
update_theme({style_name: Style(color=Color.RED)}) |
| 228 |
|
| 229 |
pt_theme = get_pt_theme() |
| 230 |
assert pt_theme.get_attrs_for_style_str("class:scroll1").color == "ansired" |
| 231 |
assert pt_theme.get_attrs_for_style_str("class:scroll2").color == "ansired" |
| 232 |
|
| 233 |
|
| 234 |
unregister_pt_mapping(style_name, "scroll1") |
| 235 |
pt_theme = get_pt_theme() |
| 236 |
assert pt_theme.get_attrs_for_style_str("class:scroll1") == DEFAULT_ATTRS |
| 237 |
assert pt_theme.get_attrs_for_style_str("class:scroll2").color == "ansired" |
| 238 |
|
| 239 |
|
| 240 |
unregister_pt_mapping(style_name, "scroll2") |
| 241 |
pt_theme = get_pt_theme() |
| 242 |
assert pt_theme.get_attrs_for_style_str("class:scroll2") == DEFAULT_ATTRS |
| 243 |
|
| 244 |
|
| 245 |
def test_unregister_pt_mapping_nonexistent() -> None: |
| 246 |
"""Test unregistering a style that doesn't exist.""" |
| 247 |
unregister_pt_mapping("nonexistent_style", "some_ui") |
| 248 |
|
| 249 |
|
| 250 |
def test_unregister_pt_mapping_preserves_key() -> None: |
| 251 |
"""Test that unregistering UI components preserves the style key for synchronization.""" |
| 252 |
style_name = "preserved_style" |
| 253 |
ui_name = "some_ui" |
| 254 |
register_pt_mapping(style_name, ui_name) |
| 255 |
|
| 256 |
from cmd2 import theme |
| 257 |
|
| 258 |
assert style_name in theme._PT_UI_MAP |
| 259 |
assert ui_name in theme._PT_UI_MAP[style_name] |
| 260 |
|
| 261 |
|
| 262 |
unregister_pt_mapping(style_name, ui_name) |
| 263 |
|
| 264 |
|
| 265 |
assert style_name in theme._PT_UI_MAP |
| 266 |
assert not theme._PT_UI_MAP[style_name] |
| 267 |
|
| 268 |
|
| 269 |
def test_register_synchronized_style() -> None: |
| 270 |
"""Test that simple registration (no UI mapping) synchronizes to PT.""" |
| 271 |
style_name = "simple_style" |
| 272 |
register_synchronized_style(style_name) |
| 273 |
|
| 274 |
update_theme({style_name: Style(color=Color.RED)}) |
| 275 |
|
| 276 |
|
| 277 |
pt_theme = get_pt_theme() |
| 278 |
attrs = pt_theme.get_attrs_for_style_str(f"class:{style_name}") |
| 279 |
assert attrs.color == "ansired" |
| 280 |
|
| 281 |
|
| 282 |
def test_unregister_synchronized_style() -> None: |
| 283 |
"""Test that unregistering removes the style entirely.""" |
| 284 |
style_name = "removal_style" |
| 285 |
ui_name = "removal_ui" |
| 286 |
register_pt_mapping(style_name, ui_name) |
| 287 |
|
| 288 |
from cmd2 import theme |
| 289 |
|
| 290 |
assert style_name in theme._PT_UI_MAP |
| 291 |
unregister_synchronized_style(style_name) |
| 292 |
assert style_name not in theme._PT_UI_MAP |
| 293 |
|
| 294 |
|
| 295 |
def test_register_synchronized_prefix() -> None: |
| 296 |
"""Test registering a custom synchronized prefix.""" |
| 297 |
from prompt_toolkit.styles import DEFAULT_ATTRS |
| 298 |
|
| 299 |
prefix = "myapp." |
| 300 |
style_name = f"{prefix}prompt" |
| 301 |
update_theme({style_name: Style(color=Color.GREEN)}) |
| 302 |
|
| 303 |
|
| 304 |
rich_theme = get_theme() |
| 305 |
orig_pt_theme = get_pt_theme() |
| 306 |
|
| 307 |
assert style_name in rich_theme.styles |
| 308 |
assert orig_pt_theme.get_attrs_for_style_str(f"class:{style_name}") == DEFAULT_ATTRS |
| 309 |
|
| 310 |
|
| 311 |
register_synchronized_prefix(prefix) |
| 312 |
new_pt_theme = get_pt_theme() |
| 313 |
style_attrs = new_pt_theme.get_attrs_for_style_str(f"class:{style_name}") |
| 314 |
assert style_attrs.color == "ansigreen" |
| 315 |
|
| 316 |
|
| 317 |
def test_register_synchronized_prefix_empty() -> None: |
| 318 |
"""Test that an empty prefix raises a ValueError.""" |
| 319 |
with pytest.raises(ValueError, match="Prefix cannot be empty"): |
| 320 |
register_synchronized_prefix("") |
| 321 |
|
| 322 |
|
| 323 |
def test_unregister_synchronized_prefix() -> None: |
| 324 |
"""Test unregistering a custom synchronized prefix.""" |
| 325 |
from prompt_toolkit.styles import DEFAULT_ATTRS |
| 326 |
|
| 327 |
prefix = "unregister." |
| 328 |
style_name = f"{prefix}prompt" |
| 329 |
update_theme({style_name: Style(color=Color.GREEN)}) |
| 330 |
|
| 331 |
|
| 332 |
register_synchronized_prefix(prefix) |
| 333 |
pt_theme = get_pt_theme() |
| 334 |
assert pt_theme.get_attrs_for_style_str(f"class:{style_name}").color == "ansigreen" |
| 335 |
|
| 336 |
|
| 337 |
unregister_synchronized_prefix(prefix) |
| 338 |
new_pt_theme = get_pt_theme() |
| 339 |
assert new_pt_theme.get_attrs_for_style_str(f"class:{style_name}") == DEFAULT_ATTRS |
| 340 |
|
| 341 |
|
| 342 |
def test_unregister_synchronized_prefix_nonexistent() -> None: |
| 343 |
"""Test unregistering a prefix that doesn't exist.""" |
| 344 |
unregister_synchronized_prefix("nonexistent_prefix.") |