Skip to content

Commit f4a1962

Browse files
committed
Added test for using a table as a alert message.
1 parent 45a260d commit f4a1962

1 file changed

Lines changed: 66 additions & 7 deletions

File tree

tests/test_cmd2.py

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ def test_ctrl_d_at_prompt(say_app, monkeypatch) -> None:
12581258
(None, "", False),
12591259
],
12601260
)
1261-
def test_async_alert(base_app, msg, prompt, is_stale) -> None:
1261+
def test_async_alert(base_app: cmd2.Cmd, msg: str, prompt: str, is_stale: bool) -> None:
12621262
import time
12631263

12641264
with (
@@ -1311,18 +1311,77 @@ def test_async_alert(base_app, msg, prompt, is_stale) -> None:
13111311
assert base_app.prompt == prompt
13121312

13131313

1314-
def test_add_alert(base_app) -> None:
1315-
orig_num_alerts = len(base_app._alert_queue)
1314+
def test_async_alert_rich(base_app: cmd2.Cmd) -> None:
1315+
from prompt_toolkit.formatted_text import ANSI
1316+
from rich.table import Table
1317+
1318+
table = Table("Header")
1319+
table.add_row("Value")
1320+
1321+
with (
1322+
mock.patch("cmd2.cmd2.print_formatted_text") as mock_print,
1323+
mock.patch("cmd2.cmd2.get_app") as mock_get_app,
1324+
):
1325+
mock_app = mock.MagicMock()
1326+
mock_get_app.return_value = mock_app
1327+
1328+
# Add alert with a Rich table
1329+
base_app.add_alert(msg=table, soft_wrap=False)
1330+
1331+
with create_pipe_input() as pipe_input:
1332+
base_app.main_session = PromptSession(
1333+
input=pipe_input,
1334+
output=DummyOutput(),
1335+
history=base_app.main_session.history,
1336+
completer=base_app.main_session.completer,
1337+
)
1338+
pipe_input.send_text("quit\n")
1339+
base_app._cmdloop()
1340+
1341+
# Verify that print_formatted_text was called with a formatted ANSI object
1342+
assert mock_print.called
1343+
printed_arg = mock_print.call_args[0][0]
1344+
assert isinstance(printed_arg, ANSI)
1345+
1346+
# The printed text should contain our table values
1347+
assert "Header" in printed_arg.value
1348+
assert "Value" in printed_arg.value
1349+
1350+
1351+
def test_add_alert(base_app: cmd2.Cmd) -> None:
1352+
base_app._alert_queue.clear()
13161353

13171354
# Nothing is added when both are None
13181355
base_app.add_alert(msg=None, prompt=None)
1319-
assert len(base_app._alert_queue) == orig_num_alerts
1356+
assert not base_app._alert_queue
13201357

1321-
# Now test valid alert arguments
1358+
# Test that soft_wrap defaults to True
13221359
base_app.add_alert(msg="Hello", prompt=None)
1323-
base_app.add_alert(msg="Hello", prompt="prompt> ")
1360+
last_alert = base_app._alert_queue[-1]
1361+
assert last_alert.msg == "Hello"
1362+
assert last_alert.soft_wrap is True
1363+
assert last_alert.prompt is None
1364+
1365+
# Test that soft_wrap can be set to True
1366+
base_app.add_alert(msg="Hello", soft_wrap=True, prompt=None)
1367+
last_alert = base_app._alert_queue[-1]
1368+
assert last_alert.msg == "Hello"
1369+
assert last_alert.soft_wrap is True
1370+
assert last_alert.prompt is None
1371+
1372+
# Test that soft_wrap can be set to False
1373+
base_app.add_alert(msg="Hello", soft_wrap=False, prompt=None)
1374+
last_alert = base_app._alert_queue[-1]
1375+
assert last_alert.msg == "Hello"
1376+
assert last_alert.soft_wrap is False
1377+
assert last_alert.prompt is None
1378+
1379+
# Test only prompt
13241380
base_app.add_alert(msg=None, prompt="prompt> ")
1325-
assert len(base_app._alert_queue) == orig_num_alerts + 3
1381+
last_alert = base_app._alert_queue[-1]
1382+
assert last_alert.msg is None
1383+
assert last_alert.soft_wrap is True
1384+
assert last_alert.prompt == "prompt> "
13261385

13271386

13281387
def test_visible_prompt() -> None:

0 commit comments

Comments
 (0)