Skip to content

Commit 0b5e443

Browse files
committed
Add a unit test for _get_pt_style method
1 parent 1a73c22 commit 0b5e443

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tests/test_cmd2.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,48 @@ def do_orate(self, opts, arg) -> None:
18891889
self.stdout.write(arg + "\n")
18901890

18911891

1892+
def test_get_pt_style_caching(base_app) -> None:
1893+
# Get the initial style (populates the cache)
1894+
style1 = base_app._get_pt_style()
1895+
1896+
# Getting it again should return the exact same object from the cache
1897+
style2 = base_app._get_pt_style()
1898+
assert style1 is style2
1899+
1900+
# Change the theme which should invalidate the cache
1901+
from rich.style import Style
1902+
1903+
import cmd2.rich_utils as ru
1904+
from cmd2.styles import Cmd2Style
1905+
1906+
# Save the original theme to restore later
1907+
orig_theme = ru.get_theme()
1908+
1909+
try:
1910+
ru.set_theme({Cmd2Style.COMPLETION_MENU_ITEM: Style(color="red")})
1911+
1912+
# Getting the style now should return a new object
1913+
style3 = base_app._get_pt_style()
1914+
assert style3 is not style1
1915+
1916+
# Getting it again should return the new cached object
1917+
style4 = base_app._get_pt_style()
1918+
assert style4 is style3
1919+
1920+
# Verify the style reflects the change
1921+
# In prompt_toolkit 3, styles are accessed differently
1922+
attrs = style3.class_names_and_attrs
1923+
found = False
1924+
for classes, attr in attrs:
1925+
if "completion-menu.completion.current" in classes and attr.color in ("800000", "darkred", "ff0000", "#800000"):
1926+
found = True
1927+
break
1928+
assert found, "Color change not found in cached style"
1929+
1930+
finally:
1931+
ru._APP_THEME = orig_theme
1932+
1933+
18921934
@pytest.fixture
18931935
def multiline_app():
18941936
return MultilineApp()

0 commit comments

Comments
 (0)