@@ -1203,6 +1203,67 @@ def test_ctrl_d_at_prompt(say_app, monkeypatch) -> None:
12031203 assert out == 'hello\n \n '
12041204
12051205
1206+ @pytest .mark .parametrize (
1207+ ('msg' , 'prompt' , 'is_stale' ),
1208+ [
1209+ ("msg_text" , None , False ),
1210+ (None , "new_prompt> " , False ),
1211+ ("msg_text" , "new_prompt> " , True ),
1212+ # Blank prompt is acceptable
1213+ ("msg_text" , "" , False ),
1214+ ],
1215+ )
1216+ def test_async_alert (base_app , msg , prompt , is_stale ) -> None :
1217+ import time
1218+
1219+ with mock .patch ('cmd2.cmd2.print_formatted_text' ) as mock_print :
1220+ base_app .add_alert (msg = msg , prompt = prompt )
1221+ alert = base_app ._alert_queue .get ()
1222+
1223+ # Stale means alert was created before the current prompt.
1224+ if is_stale :
1225+ # In the past
1226+ alert .timestamp = 0.0
1227+ else :
1228+ # In the future
1229+ alert .timestamp = time .monotonic () + 99999999
1230+
1231+ base_app ._alert_queue .put (alert )
1232+
1233+ with create_pipe_input () as pipe_input :
1234+ base_app .session = PromptSession (
1235+ input = pipe_input ,
1236+ output = DummyOutput (),
1237+ history = base_app .session .history ,
1238+ completer = base_app .session .completer ,
1239+ )
1240+ pipe_input .send_text ("quit\n " )
1241+
1242+ base_app ._cmdloop ()
1243+
1244+ if msg :
1245+ assert msg in str (mock_print .call_args_list [0 ])
1246+ if prompt is not None :
1247+ if is_stale :
1248+ assert base_app .prompt != prompt
1249+ else :
1250+ assert base_app .prompt == prompt
1251+
1252+
1253+ def test_add_alert (base_app ) -> None :
1254+ orig_num_alerts = base_app ._alert_queue .qsize ()
1255+
1256+ # Nothing is added when both are None
1257+ base_app .add_alert (msg = None , prompt = None )
1258+ assert base_app ._alert_queue .qsize () == orig_num_alerts
1259+
1260+ # Now test valid alert arguments
1261+ base_app .add_alert (msg = "Hello" , prompt = None )
1262+ base_app .add_alert (msg = "Hello" , prompt = "prompt> " )
1263+ base_app .add_alert (msg = None , prompt = "prompt> " )
1264+ assert base_app ._alert_queue .qsize () == orig_num_alerts + 3
1265+
1266+
12061267class ShellApp (cmd2 .Cmd ):
12071268 def __init__ (self , * args , ** kwargs ) -> None :
12081269 super ().__init__ (* args , ** kwargs )
0 commit comments