Skip to content

Commit 9dbb7fa

Browse files
committed
test(cmd2): add coverage for EOF during multiline command input
Added a unit test 'test_multiline_complete_statement_eof' to cover the logic in '_complete_statement' where an EOF is encountered during multiline command input. The test verifies that 'eof' is correctly handled by converting it to a newline terminator and printing it.
1 parent 93be6d2 commit 9dbb7fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/test_cmd2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,31 @@ def test_multiline_history_with_quotes(multiline_app, monkeypatch) -> None:
18331833
assert history_lines[5] == ';'
18341834

18351835

1836+
def test_multiline_complete_statement_eof(multiline_app, monkeypatch):
1837+
# Mock poutput to verify it's called
1838+
poutput_mock = mock.MagicMock(name='poutput')
1839+
monkeypatch.setattr(multiline_app, 'poutput', poutput_mock)
1840+
1841+
# Mock out the read_input call so we return EOFError
1842+
read_input_mock = mock.MagicMock(name='read_input', side_effect=EOFError)
1843+
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
1844+
1845+
command = 'orate'
1846+
args = 'hello world'
1847+
line = f'{command} {args}'
1848+
1849+
# This should call _read_command_line, get 'eof', set nextline to '\n',
1850+
# and then parse the line with the newline terminator.
1851+
statement = multiline_app._complete_statement(line)
1852+
1853+
assert statement.command == command
1854+
assert statement.args == args
1855+
assert statement.terminator == '\n'
1856+
1857+
# Verify that poutput('\n') was called
1858+
poutput_mock.assert_called_once_with('\n')
1859+
1860+
18361861
class CommandResultApp(cmd2.Cmd):
18371862
def __init__(self, *args, **kwargs) -> None:
18381863
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)