Skip to content

Commit 5be1072

Browse files
committed
Updated tests.
1 parent 070ad52 commit 5be1072

File tree

5 files changed

+26
-65
lines changed

5 files changed

+26
-65
lines changed

examples/async_printing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AlerterApp(cmd2.Cmd):
3232

3333
def __init__(self) -> None:
3434
"""Initializer."""
35-
super().__init__()
35+
super().__init__(multiline_commands=["help"])
3636

3737
self.prompt = "(APR)> "
3838

examples/hello_cmd2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
include_ipy=True, # Enable support for interactive Python shell via py command
1414
include_py=True, # Enable support for interactive IPython shell via ipy command
1515
persistent_history_file='cmd2_history.dat', # Persist history between runs
16+
multiline_commands=["help"],
1617
)
1718
app.self_in_py = True # Enable access to "self" within the py command
1819
app.debug = True # Show traceback if/when an exception occurs

tests/test_cmd2.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,23 +1208,19 @@ def test_ctrl_d_at_prompt(say_app, monkeypatch) -> None:
12081208
reason="Don't have a real Windows console with how we are currently running tests in GitHub Actions",
12091209
)
12101210
@pytest.mark.parametrize(
1211-
('msg', 'prompt', 'is_stale', 'at_continuation_prompt'),
1211+
('msg', 'prompt', 'is_stale'),
12121212
[
1213-
("msg_text", None, False, False),
1214-
("msg_text", "new_prompt> ", False, False),
1215-
("msg_text", "new_prompt> ", False, True),
1216-
("msg_text", "new_prompt> ", True, False),
1217-
("msg_text", "new_prompt> ", True, True),
1218-
(None, "new_prompt> ", False, False),
1219-
(None, "new_prompt> ", False, True),
1220-
(None, "new_prompt> ", True, False),
1221-
(None, "new_prompt> ", True, True),
1213+
("msg_text", None, False),
1214+
("msg_text", "new_prompt> ", False),
1215+
("msg_text", "new_prompt> ", True),
1216+
(None, "new_prompt> ", False),
1217+
(None, "new_prompt> ", True),
12221218
# Blank prompt is acceptable
1223-
("msg_text", "", False, False),
1224-
(None, "", False, False),
1219+
("msg_text", "", False),
1220+
(None, "", False),
12251221
],
12261222
)
1227-
def test_async_alert(base_app, msg, prompt, is_stale, at_continuation_prompt) -> None:
1223+
def test_async_alert(base_app, msg, prompt, is_stale) -> None:
12281224
import time
12291225

12301226
with (
@@ -1246,8 +1242,6 @@ def test_async_alert(base_app, msg, prompt, is_stale, at_continuation_prompt) ->
12461242
# In the future
12471243
alert.timestamp = time.monotonic() + 99999999
12481244

1249-
base_app._at_continuation_prompt = at_continuation_prompt
1250-
12511245
with create_pipe_input() as pipe_input:
12521246
base_app.main_session = PromptSession(
12531247
input=pipe_input,
@@ -1266,7 +1260,7 @@ def test_async_alert(base_app, msg, prompt, is_stale, at_continuation_prompt) ->
12661260

12671261
# If there's only a prompt update, we expect invalidate() only if not continuation/stale
12681262
elif prompt is not None:
1269-
if is_stale or at_continuation_prompt:
1263+
if is_stale:
12701264
mock_app.invalidate.assert_not_called()
12711265
else:
12721266
mock_app.invalidate.assert_called_once()
@@ -1819,9 +1813,6 @@ def test_multiline_complete_statement_without_terminator(multiline_app, monkeypa
18191813
assert statement.command == command
18201814
assert statement.multiline_command
18211815

1822-
pt_history = multiline_app.main_session.history.get_strings()
1823-
assert pt_history[0] == statement.raw
1824-
18251816

18261817
def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkeypatch) -> None:
18271818
read_command_mock = mock.MagicMock(name='_read_command_line', side_effect=['quotes', '" now closed;'])
@@ -1834,9 +1825,6 @@ def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkey
18341825
assert statement.multiline_command
18351826
assert statement.terminator == ';'
18361827

1837-
pt_history = multiline_app.main_session.history.get_strings()
1838-
assert pt_history[0] == statement.raw
1839-
18401828

18411829
def test_multiline_input_line_to_statement(multiline_app, monkeypatch) -> None:
18421830
# Verify _input_line_to_statement saves the fully entered input line for multiline commands
@@ -1850,9 +1838,6 @@ def test_multiline_input_line_to_statement(multiline_app, monkeypatch) -> None:
18501838
assert statement.command == 'orate'
18511839
assert statement.multiline_command
18521840

1853-
pt_history = multiline_app.main_session.history.get_strings()
1854-
assert pt_history[0] == statement.raw
1855-
18561841

18571842
def test_multiline_history_added(multiline_app, monkeypatch) -> None:
18581843
# Test that multiline commands are added to history as a single item
@@ -1864,13 +1849,8 @@ def test_multiline_history_added(multiline_app, monkeypatch) -> None:
18641849
# run_cmd calls onecmd_plus_hooks which triggers history addition
18651850
run_cmd(multiline_app, "orate hi")
18661851

1867-
expected = "orate hi\nperson\n\n"
18681852
assert len(multiline_app.history) == 1
1869-
assert multiline_app.history.get(1).raw == expected
1870-
1871-
pt_history = multiline_app.main_session.history.get_strings()
1872-
assert len(pt_history) == 1
1873-
assert pt_history[0] == expected
1853+
assert multiline_app.history.get(1).raw == "orate hi\nperson\n\n"
18741854

18751855

18761856
def test_multiline_history_with_quotes(multiline_app, monkeypatch) -> None:
@@ -1893,10 +1873,6 @@ def test_multiline_history_with_quotes(multiline_app, monkeypatch) -> None:
18931873
assert history_lines[4] == 'quotes.'
18941874
assert history_lines[5] == ';'
18951875

1896-
pt_history = multiline_app.main_session.history.get_strings()
1897-
assert len(pt_history) == 1
1898-
assert pt_history[0] == history_item.raw
1899-
19001876

19011877
def test_multiline_complete_statement_eof(multiline_app, monkeypatch):
19021878
# Mock poutput to verify it's called
@@ -1918,9 +1894,6 @@ def test_multiline_complete_statement_eof(multiline_app, monkeypatch):
19181894
assert statement.args == args
19191895
assert statement.terminator == '\n'
19201896

1921-
pt_history = multiline_app.main_session.history.get_strings()
1922-
assert pt_history[0] == statement.raw
1923-
19241897
# Verify that poutput('\n') was called
19251898
poutput_mock.assert_called_once_with('\n')
19261899

tests/test_completion.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,12 +830,8 @@ def test_complete_multiline_on_single_line(cmd2_app) -> None:
830830

831831

832832
def test_complete_multiline_on_multiple_lines(cmd2_app) -> None:
833-
# Set the same variables _complete_statement() sets when a user is entering data at a continuation prompt
834-
cmd2_app._at_continuation_prompt = True
835-
cmd2_app._multiline_in_progress = "test_multiline\n"
836-
837833
text = 'Ba'
838-
line = f'{text}'
834+
line = f'test_multiline\n{text}'
839835
endidx = len(line)
840836
begidx = endidx - len(text)
841837

tests/test_pt_utils.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -538,44 +538,35 @@ def test_get_strings(self):
538538
assert history.get_strings() == expected
539539

540540
def test_append_string(self):
541-
"""Test that append_string() does nothing."""
541+
"""Test that append_string() adds data."""
542542
history = pt_utils.Cmd2History()
543543
assert history._loaded
544544
assert not history._loaded_strings
545545

546546
history.append_string("new command")
547-
assert not history._loaded_strings
548-
549-
def test_store_string(self):
550-
"""Test that store_string() does nothing."""
551-
history = pt_utils.Cmd2History()
552-
assert history._loaded
553-
assert not history._loaded_strings
554-
555-
history.store_string("new command")
556-
assert not history._loaded_strings
557-
558-
def test_add_command(self):
559-
"""Test that add_command() adds data."""
560-
history = pt_utils.Cmd2History()
561-
assert history._loaded
562-
assert not history._loaded_strings
563-
564-
history.add_command("new command")
565547
assert len(history._loaded_strings) == 1
566548
assert history._loaded_strings[0] == "new command"
567549

568550
# Show that consecutive duplicates are filtered
569-
history.add_command("new command")
551+
history.append_string("new command")
570552
assert len(history._loaded_strings) == 1
571553
assert history._loaded_strings[0] == "new command"
572554

573555
# Show that new items are placed at the front
574-
history.add_command("even newer command")
556+
history.append_string("even newer command")
575557
assert len(history._loaded_strings) == 2
576558
assert history._loaded_strings[0] == "even newer command"
577559
assert history._loaded_strings[1] == "new command"
578560

561+
def test_store_string(self):
562+
"""Test that store_string() does nothing."""
563+
history = pt_utils.Cmd2History()
564+
assert history._loaded
565+
assert not history._loaded_strings
566+
567+
history.store_string("new command")
568+
assert not history._loaded_strings
569+
579570
def test_clear(self):
580571
history_strings = ["cmd1", "cmd2"]
581572
history = pt_utils.Cmd2History(history_strings)

0 commit comments

Comments
 (0)