Skip to content

Commit 19f8a4c

Browse files
committed
Polish commands and cover them with tests
1 parent c4a120b commit 19f8a4c

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

Lib/_pyrepl/commands.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,13 @@ def do(self) -> None:
469469

470470
class invalid_key(Command):
471471
def do(self) -> None:
472-
pending = self.reader.console.getpending()
473-
s = "".join(self.event) + pending.data
474-
self.reader.error("`%r' not bound" % s)
472+
self.reader.console.getpending()
473+
self.reader.error("no command is bound to this key")
475474

476475

477476
class invalid_command(Command):
478477
def do(self) -> None:
479-
s = self.event_name
480-
self.reader.error("command `%s' not known" % s)
478+
self.reader.error(f"command {self.event_name!r} is not implemented")
481479

482480

483481
class show_history(Command):

Lib/test/test_pyrepl/test_reader.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,47 @@ def test_setpos_from_xy_for_non_printing_char(self):
456456
reader.setpos_from_xy(8, 0)
457457
self.assertEqual(reader.pos, 7)
458458

459+
460+
@force_not_colorized_test_class
461+
class TestErrorMessages(TestCase):
462+
def _test_error_message(
463+
self,
464+
keys: str,
465+
expected: str, *,
466+
command: str | None = None,
467+
):
468+
console = prepare_console(code_to_events(keys))
469+
reader = prepare_reader(console)
470+
if command is not None:
471+
reader.bind(keys, command)
472+
473+
while not reader.msg:
474+
reader.handle1()
475+
476+
self.assertEqual(reader.msg, f"! {expected}")
477+
self.assertEqual(reader.screen[-1], f"! {expected}")
478+
console.beep.assert_called_once()
479+
480+
def test_invalid_key_message(self):
481+
self._test_error_message("\x1bo", "no command is bound to this key")
482+
483+
def test_unimplemented_command_message(self):
484+
self._test_error_message(
485+
"x",
486+
"command 'missing-command' is not implemented",
487+
command="missing-command",
488+
)
489+
490+
def test_nothing_to_yank_message(self):
491+
self._test_error_message("\x19", "nothing to yank")
492+
493+
def test_start_of_history_message(self):
494+
self._test_error_message("\x10", "start of history list")
495+
496+
def test_no_completion_matches_message(self):
497+
self._test_error_message("does_not_exist\t", "no matches")
498+
499+
459500
@force_colorized_test_class
460501
class TestReaderInColor(ScreenEqualMixin, TestCase):
461502
def test_syntax_highlighting_basic(self):

0 commit comments

Comments
 (0)