Skip to content

Commit 674f703

Browse files
committed
Fix tests and simplify helper methods
1 parent 753212f commit 674f703

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

cmd2/cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,12 +3235,12 @@ def _is_tty_session(session: PromptSession[str]) -> bool:
32353235

32363236
def _add_to_pt_history(self, line: str) -> None:
32373237
"""Add a command to the prompt-toolkit UI history if it's a Cmd2History instance."""
3238-
if hasattr(self, 'main_session') and isinstance(self.main_session.history, Cmd2History):
3238+
if isinstance(self.main_session.history, Cmd2History):
32393239
self.main_session.history.add_command(line)
32403240

32413241
def _clear_pt_history(self) -> None:
32423242
"""Clear the prompt-toolkit UI history if it's a Cmd2History instance."""
3243-
if hasattr(self, 'main_session') and isinstance(self.main_session.history, Cmd2History):
3243+
if isinstance(self.main_session.history, Cmd2History):
32443244
self.main_session.history.clear()
32453245

32463246
def _read_raw_input(

tests/test_cmd2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,7 @@ def test_multiline_complete_statement_without_terminator(multiline_app, monkeypa
18351835
args = 'hello world'
18361836
line = f'{command} {args}'
18371837
statement = multiline_app._complete_statement(line)
1838+
multiline_app._add_to_pt_history(statement.raw)
18381839
assert statement == args
18391840
assert statement.command == command
18401841
assert statement.multiline_command
@@ -1849,6 +1850,7 @@ def test_multiline_complete_statement_with_unclosed_quotes(multiline_app, monkey
18491850

18501851
line = 'orate hi "partially open'
18511852
statement = multiline_app._complete_statement(line)
1853+
multiline_app._add_to_pt_history(statement.raw)
18521854
assert statement == 'hi "partially open\nquotes\n" now closed'
18531855
assert statement.command == 'orate'
18541856
assert statement.multiline_command
@@ -1865,6 +1867,7 @@ def test_multiline_input_line_to_statement(multiline_app, monkeypatch) -> None:
18651867

18661868
line = 'orate hi'
18671869
statement = multiline_app._input_line_to_statement(line)
1870+
multiline_app._add_to_pt_history(statement.raw)
18681871
assert statement.raw == 'orate hi\nperson\n\n'
18691872
assert statement == 'hi person'
18701873
assert statement.command == 'orate'
@@ -1933,6 +1936,7 @@ def test_multiline_complete_statement_eof(multiline_app, monkeypatch):
19331936
# This should call _read_command_line, get 'eof', set nextline to '\n',
19341937
# and then parse the line with the newline terminator.
19351938
statement = multiline_app._complete_statement(line)
1939+
multiline_app._add_to_pt_history(statement.raw)
19361940

19371941
assert statement.command == command
19381942
assert statement.args == args

0 commit comments

Comments
 (0)