Skip to content

Commit b810c4d

Browse files
committed
Added tests.
1 parent 7133c8d commit b810c4d

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

tests/test_cmd2.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
12061267
class ShellApp(cmd2.Cmd):
12071268
def __init__(self, *args, **kwargs) -> None:
12081269
super().__init__(*args, **kwargs)

tests/test_pt_utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
stylize,
1919
utils,
2020
)
21+
from cmd2 import rich_utils as ru
22+
from cmd2 import string_utils as su
2123
from cmd2.history import HistoryItem
2224
from cmd2.parsing import Statement
25+
from cmd2.pt_utils import pt_filter_style
26+
27+
from .conftest import with_ansi_style
2328

2429

2530
# Mock for cmd2.Cmd
@@ -47,6 +52,48 @@ def mock_cmd_app() -> MockCmd:
4752
return MockCmd()
4853

4954

55+
@with_ansi_style(ru.AllowStyle.ALWAYS)
56+
def test_pt_filter_style_always() -> None:
57+
"""This should preserve all styles and return ANSI."""
58+
unstyled = "unstyled"
59+
result = pt_filter_style(unstyled)
60+
assert isinstance(result, ANSI)
61+
assert result.value == unstyled
62+
63+
styled = stylize("styled", Cmd2Style.COMMAND_LINE)
64+
result = pt_filter_style(styled)
65+
assert isinstance(result, ANSI)
66+
assert result.value == styled
67+
68+
69+
@with_ansi_style(ru.AllowStyle.TERMINAL)
70+
def test_pt_filter_style_terminal() -> None:
71+
"""This should preserve all styles and return ANSI."""
72+
unstyled = "unstyled"
73+
result = pt_filter_style(unstyled)
74+
assert isinstance(result, ANSI)
75+
assert result.value == unstyled
76+
77+
styled = stylize("styled", Cmd2Style.COMMAND_LINE)
78+
result = pt_filter_style(styled)
79+
assert isinstance(result, ANSI)
80+
assert result.value == styled
81+
82+
83+
@with_ansi_style(ru.AllowStyle.NEVER)
84+
def test_pt_filter_style_never() -> None:
85+
"""This should strip all styles and return str."""
86+
unstyled = "unstyled"
87+
result = pt_filter_style(unstyled)
88+
assert isinstance(result, str)
89+
assert result == unstyled
90+
91+
styled = stylize("styled", Cmd2Style.COMMAND_LINE)
92+
result = pt_filter_style(styled)
93+
assert isinstance(result, str)
94+
assert result == su.strip_style(styled)
95+
96+
5097
class TestCmd2Lexer:
5198
def test_lex_document_command(self, mock_cmd_app):
5299
"""Test lexing a command name."""

0 commit comments

Comments
 (0)