Skip to content

Commit bab12ae

Browse files
committed
Added unit tests.
1 parent 29e5def commit bab12ae

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

tests/test_cmd2.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,9 @@ def test_multiline_complete_statement_without_terminator(multiline_app, monkeypa
18391839
assert statement.command == command
18401840
assert statement.multiline_command
18411841

1842+
pt_history = multiline_app.main_session.history.get_strings()
1843+
assert pt_history[0] == statement.raw
1844+
18421845

18431846
def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkeypatch) -> None:
18441847
read_command_mock = mock.MagicMock(name='_read_command_line', side_effect=['quotes', '" now closed;'])
@@ -1851,6 +1854,9 @@ def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkey
18511854
assert statement.multiline_command
18521855
assert statement.terminator == ';'
18531856

1857+
pt_history = multiline_app.main_session.history.get_strings()
1858+
assert pt_history[0] == statement.raw
1859+
18541860

18551861
def test_multiline_input_line_to_statement(multiline_app, monkeypatch) -> None:
18561862
# Verify _input_line_to_statement saves the fully entered input line for multiline commands
@@ -1864,28 +1870,36 @@ def test_multiline_input_line_to_statement(multiline_app, monkeypatch) -> None:
18641870
assert statement.command == 'orate'
18651871
assert statement.multiline_command
18661872

1873+
pt_history = multiline_app.main_session.history.get_strings()
1874+
assert pt_history[0] == statement.raw
1875+
18671876

18681877
def test_multiline_history_added(multiline_app, monkeypatch) -> None:
18691878
# Test that multiline commands are added to history as a single item
1879+
run_cmd(multiline_app, "history --clear")
1880+
18701881
read_command_mock = mock.MagicMock(name='_read_command_line', side_effect=['person', '\n'])
18711882
monkeypatch.setattr("cmd2.Cmd._read_command_line", read_command_mock)
18721883

1873-
multiline_app.history.clear()
1874-
18751884
# run_cmd calls onecmd_plus_hooks which triggers history addition
18761885
run_cmd(multiline_app, "orate hi")
18771886

1887+
expected = "orate hi\nperson\n\n"
18781888
assert len(multiline_app.history) == 1
1879-
assert multiline_app.history.get(1).raw == "orate hi\nperson\n\n"
1889+
assert multiline_app.history.get(1).raw == expected
1890+
1891+
pt_history = multiline_app.main_session.history.get_strings()
1892+
assert len(pt_history) == 1
1893+
assert pt_history[0] == expected
18801894

18811895

18821896
def test_multiline_history_with_quotes(multiline_app, monkeypatch) -> None:
18831897
# Test combined multiline command with quotes is added to history correctly
1898+
run_cmd(multiline_app, "history --clear")
1899+
18841900
read_command_mock = mock.MagicMock(name='_read_command_line', side_effect=[' and spaces ', ' "', ' in', 'quotes.', ';'])
18851901
monkeypatch.setattr("cmd2.Cmd._read_command_line", read_command_mock)
18861902

1887-
multiline_app.history.clear()
1888-
18891903
line = 'orate Look, "There are newlines'
18901904
run_cmd(multiline_app, line)
18911905

@@ -1899,6 +1913,10 @@ def test_multiline_history_with_quotes(multiline_app, monkeypatch) -> None:
18991913
assert history_lines[4] == 'quotes.'
19001914
assert history_lines[5] == ';'
19011915

1916+
pt_history = multiline_app.main_session.history.get_strings()
1917+
assert len(pt_history) == 1
1918+
assert pt_history[0] == history_item.raw
1919+
19021920

19031921
def test_multiline_complete_statement_eof(multiline_app, monkeypatch):
19041922
# Mock poutput to verify it's called
@@ -1920,6 +1938,9 @@ def test_multiline_complete_statement_eof(multiline_app, monkeypatch):
19201938
assert statement.args == args
19211939
assert statement.terminator == '\n'
19221940

1941+
pt_history = multiline_app.main_session.history.get_strings()
1942+
assert pt_history[0] == statement.raw
1943+
19231944
# Verify that poutput('\n') was called
19241945
poutput_mock.assert_called_once_with('\n')
19251946

0 commit comments

Comments
 (0)