diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2024-05-21 19:16:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-21 23:16:56 +0000 |
commit | a3e4fec8734a304d654e4ae24a4aa2f41a7b0640 (patch) | |
tree | a152e54e9dad1a39e40b2bfd5f2b167576dda446 /Lib/test/test_pyrepl | |
parent | 9fa206aaeccc979a4bd03852ba38c045294a3d6f (diff) | |
download | cpython-a3e4fec8734a304d654e4ae24a4aa2f41a7b0640.tar.gz cpython-a3e4fec8734a304d654e4ae24a4aa2f41a7b0640.zip |
gh-118893: Evaluate all statements in the new REPL separately (#119318)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/test/test_pyrepl')
-rw-r--r-- | Lib/test/test_pyrepl/test_interact.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/Lib/test/test_pyrepl/test_interact.py b/Lib/test/test_pyrepl/test_interact.py new file mode 100644 index 00000000000..b3dc07c063f --- /dev/null +++ b/Lib/test/test_pyrepl/test_interact.py @@ -0,0 +1,92 @@ +import contextlib +import io +import unittest +from unittest.mock import patch +from textwrap import dedent + +from test.support import force_not_colorized + +from _pyrepl.simple_interact import InteractiveColoredConsole + + +class TestSimpleInteract(unittest.TestCase): + def test_multiple_statements(self): + namespace = {} + code = dedent("""\ + class A: + def foo(self): + + + pass + + class B: + def bar(self): + pass + + a = 1 + a + """) + console = InteractiveColoredConsole(namespace, filename="<stdin>") + with ( + patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror, + patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource, + ): + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] + self.assertFalse(more) + showsyntaxerror.assert_not_called() + + + def test_multiple_statements_output(self): + namespace = {} + code = dedent("""\ + b = 1 + b + a = 1 + a + """) + console = InteractiveColoredConsole(namespace, filename="<stdin>") + f = io.StringIO() + with contextlib.redirect_stdout(f): + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] + self.assertFalse(more) + self.assertEqual(f.getvalue(), "1\n") + + def test_empty(self): + namespace = {} + code = "" + console = InteractiveColoredConsole(namespace, filename="<stdin>") + f = io.StringIO() + with contextlib.redirect_stdout(f): + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] + self.assertFalse(more) + self.assertEqual(f.getvalue(), "") + + def test_runsource_compiles_and_runs_code(self): + console = InteractiveColoredConsole() + source = "print('Hello, world!')" + with patch.object(console, "runcode") as mock_runcode: + console.runsource(source) + mock_runcode.assert_called_once() + + def test_runsource_returns_false_for_successful_compilation(self): + console = InteractiveColoredConsole() + source = "print('Hello, world!')" + result = console.runsource(source) + self.assertFalse(result) + + @force_not_colorized + def test_runsource_returns_false_for_failed_compilation(self): + console = InteractiveColoredConsole() + source = "print('Hello, world!'" + f = io.StringIO() + with contextlib.redirect_stderr(f): + result = console.runsource(source) + self.assertFalse(result) + self.assertIn('SyntaxError', f.getvalue()) + + def test_runsource_shows_syntax_error_for_failed_compilation(self): + console = InteractiveColoredConsole() + source = "print('Hello, world!'" + with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror: + console.runsource(source) + mock_showsyntaxerror.assert_called_once() |