1616 register_pt_mapping ,
1717 register_synchronized_prefix ,
1818 set_theme ,
19+ unregister_pt_mapping ,
20+ unregister_synchronized_prefix ,
1921)
2022
2123
@@ -96,8 +98,83 @@ def test_pt_theme_is_none() -> None:
9698 assert get_pt_theme () is not None
9799
98100
101+ def test_register_pt_mapping () -> None :
102+ """Test style to UI mapping."""
103+ style_name = "my_custom_scrollbar"
104+ ui_name = "scrollbar"
105+
106+ register_pt_mapping (style_name , ui_name )
107+
108+ set_theme ({style_name : Style (color = Color .BLUE )})
109+
110+ pt_theme = get_pt_theme ()
111+
112+ # Check that both the main style name and the UI name are mapped
113+ attrs_main = pt_theme .get_attrs_for_style_str (f"class:{ style_name } " )
114+ attrs_ui = pt_theme .get_attrs_for_style_str (f"class:{ ui_name } " )
115+
116+ assert attrs_main .color == "ansiblue"
117+ assert attrs_ui .color == "ansiblue"
118+
119+
120+ def test_register_pt_mapping_redundant () -> None :
121+ """Test that redundant mappings are filtered out."""
122+ from cmd2 import theme
123+
124+ style_name = "redundant"
125+ register_pt_mapping (style_name , [style_name , "other" ])
126+
127+ assert style_name not in theme ._PT_UI_MAP [style_name ]
128+ assert "other" in theme ._PT_UI_MAP [style_name ]
129+
130+
131+ def test_unregister_pt_mapping () -> None :
132+ """Test unmapping styles from UI components."""
133+ from prompt_toolkit .styles import DEFAULT_ATTRS
134+
135+ style_name = "custom_scroll"
136+ ui_names = ["scroll1" , "scroll2" ]
137+
138+ register_pt_mapping (style_name , ui_names )
139+ set_theme ({style_name : Style (color = Color .RED )})
140+
141+ pt_theme = get_pt_theme ()
142+ assert pt_theme .get_attrs_for_style_str ("class:scroll1" ).color == "ansired"
143+ assert pt_theme .get_attrs_for_style_str ("class:scroll2" ).color == "ansired"
144+
145+ # Unregister one UI component
146+ unregister_pt_mapping (style_name , "scroll1" )
147+ pt_theme = get_pt_theme ()
148+ assert pt_theme .get_attrs_for_style_str ("class:scroll1" ) == DEFAULT_ATTRS
149+ assert pt_theme .get_attrs_for_style_str ("class:scroll2" ).color == "ansired"
150+
151+ # Unregister the entire style mapping
152+ unregister_pt_mapping (style_name )
153+ pt_theme = get_pt_theme ()
154+ assert pt_theme .get_attrs_for_style_str ("class:scroll2" ) == DEFAULT_ATTRS
155+
156+
157+ def test_unregister_pt_mapping_nonexistent () -> None :
158+ """Test unregistering a mapping that doesn't exist."""
159+ unregister_pt_mapping ("nonexistent_style" )
160+
161+
162+ def test_unregister_pt_mapping_cleans_up_key () -> None :
163+ """Test that unregistering the last UI component removes the style key."""
164+ style_name = "cleanup_style"
165+ ui_name = "cleanup_ui"
166+ register_pt_mapping (style_name , ui_name )
167+
168+ from cmd2 import theme
169+
170+ assert style_name in theme ._PT_UI_MAP
171+
172+ unregister_pt_mapping (style_name , ui_name )
173+ assert style_name not in theme ._PT_UI_MAP
174+
175+
99176def test_register_synchronized_prefix () -> None :
100- """Test registering a custom synchronization prefix."""
177+ """Test registering a custom synchronized prefix."""
101178 from prompt_toolkit .styles import DEFAULT_ATTRS
102179
103180 prefix = "myapp."
@@ -124,31 +201,25 @@ def test_register_synchronized_prefix_empty() -> None:
124201 register_synchronized_prefix ("" )
125202
126203
127- def test_register_pt_mapping () -> None :
128- """Test style to UI mapping."""
129- style_name = "my_custom_scrollbar"
130- ui_name = "scrollbar"
131-
132- register_pt_mapping (style_name , ui_name )
204+ def test_unregister_synchronized_prefix () -> None :
205+ """Test unregistering a custom synchronized prefix."""
206+ from prompt_toolkit .styles import DEFAULT_ATTRS
133207
134- set_theme ({style_name : Style (color = Color .BLUE )})
208+ prefix = "unregister."
209+ style_name = f"{ prefix } prompt"
210+ set_theme ({style_name : Style (color = Color .GREEN )})
135211
212+ # Register the prefix and make sure the style has been synced to the pt theme
213+ register_synchronized_prefix (prefix )
136214 pt_theme = get_pt_theme ()
215+ assert pt_theme .get_attrs_for_style_str (f"class:{ style_name } " ).color == "ansigreen"
137216
138- # Check that both the main style name and the UI name are mapped
139- attrs_main = pt_theme .get_attrs_for_style_str (f"class:{ style_name } " )
140- attrs_ui = pt_theme .get_attrs_for_style_str (f"class:{ ui_name } " )
141-
142- assert attrs_main .color == "ansiblue"
143- assert attrs_ui .color == "ansiblue"
144-
217+ # Unregister the prefix and make sure the style is no longer synced
218+ unregister_synchronized_prefix (prefix )
219+ new_pt_theme = get_pt_theme ()
220+ assert new_pt_theme .get_attrs_for_style_str (f"class:{ style_name } " ) == DEFAULT_ATTRS
145221
146- def test_register_pt_mapping_redundant () -> None :
147- """Test that redundant mappings are filtered out."""
148- from cmd2 import theme
149222
150- style_name = "redundant"
151- register_pt_mapping (style_name , [style_name , "other" ])
152-
153- assert style_name not in theme ._PT_UI_MAP [style_name ]
154- assert "other" in theme ._PT_UI_MAP [style_name ]
223+ def test_unregister_synchronized_prefix_nonexistent () -> None :
224+ """Test unregistering a prefix that doesn't exist."""
225+ unregister_synchronized_prefix ("nonexistent_prefix." )
0 commit comments