gh-121499: Fix multi-line history rendering in the REPL (#121531)

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
This commit is contained in:
Pablo Galindo Salgado 2024-07-13 12:54:10 +02:00 committed by GitHub
parent e745996b2d
commit 4b9e10d0ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 62 additions and 0 deletions

View file

@ -21,6 +21,7 @@ from .support import (
more_lines,
multiline_input,
code_to_events,
clean_screen
)
from _pyrepl.console import Event
from _pyrepl.readline import ReadlineAlikeReader, ReadlineConfig
@ -483,6 +484,7 @@ class TestPyReplOutput(TestCase):
console = FakeConsole(events)
config = ReadlineConfig(readline_completer=None)
reader = ReadlineAlikeReader(console=console, config=config)
reader.can_colorize = False
return reader
def test_basic(self):
@ -490,6 +492,7 @@ class TestPyReplOutput(TestCase):
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
def test_multiline_edit(self):
events = itertools.chain(
@ -519,8 +522,10 @@ class TestPyReplOutput(TestCase):
output = multiline_input(reader)
self.assertEqual(output, "def f():\n ...\n ")
self.assertEqual(clean_screen(reader.screen), "def f():\n ...")
output = multiline_input(reader)
self.assertEqual(output, "def g():\n pass\n ")
self.assertEqual(clean_screen(reader.screen), "def g():\n pass")
def test_history_navigation_with_up_arrow(self):
events = itertools.chain(
@ -539,12 +544,40 @@ class TestPyReplOutput(TestCase):
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
output = multiline_input(reader)
self.assertEqual(output, "2+2")
self.assertEqual(clean_screen(reader.screen), "2+2")
output = multiline_input(reader)
self.assertEqual(output, "2+2")
self.assertEqual(clean_screen(reader.screen), "2+2")
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
def test_history_with_multiline_entries(self):
code = "def foo():\nx = 1\ny = 2\nz = 3\n\ndef bar():\nreturn 42\n\n"
events = list(itertools.chain(
code_to_events(code),
[
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
Event(evt="key", data="\n", raw=bytearray(b"\n")),
]
))
reader = self.prepare_reader(events)
output = multiline_input(reader)
output = multiline_input(reader)
output = multiline_input(reader)
self.assertEqual(
clean_screen(reader.screen),
'def foo():\n x = 1\n y = 2\n z = 3'
)
self.assertEqual(output, "def foo():\n x = 1\n y = 2\n z = 3\n ")
def test_history_navigation_with_down_arrow(self):
events = itertools.chain(
@ -562,6 +595,7 @@ class TestPyReplOutput(TestCase):
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
def test_history_search(self):
events = itertools.chain(
@ -578,18 +612,23 @@ class TestPyReplOutput(TestCase):
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
output = multiline_input(reader)
self.assertEqual(output, "2+2")
self.assertEqual(clean_screen(reader.screen), "2+2")
output = multiline_input(reader)
self.assertEqual(output, "3+3")
self.assertEqual(clean_screen(reader.screen), "3+3")
output = multiline_input(reader)
self.assertEqual(output, "1+1")
self.assertEqual(clean_screen(reader.screen), "1+1")
def test_control_character(self):
events = code_to_events("c\x1d\n")
reader = self.prepare_reader(events)
output = multiline_input(reader)
self.assertEqual(output, "c\x1d")
self.assertEqual(clean_screen(reader.screen), "c")
class TestPyReplCompleter(TestCase):