|
| 1 | +import contextlib |
| 2 | +import io |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | +from textwrap import dedent |
| 6 | + |
| 7 | +from test.support import force_not_colorized |
| 8 | + |
| 9 | +from _pyrepl.simple_interact import InteractiveColoredConsole |
| 10 | + |
| 11 | + |
| 12 | +class TestSimpleInteract(unittest.TestCase): |
| 13 | + def test_multiple_statements(self): |
| 14 | + namespace = {} |
| 15 | + code = dedent("""\ |
| 16 | + class A: |
| 17 | + def foo(self): |
| 18 | +
|
| 19 | +
|
| 20 | + pass |
| 21 | +
|
| 22 | + class B: |
| 23 | + def bar(self): |
| 24 | + pass |
| 25 | +
|
| 26 | + a = 1 |
| 27 | + a |
| 28 | + """) |
| 29 | + console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 30 | + with ( |
| 31 | + patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror, |
| 32 | + patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource, |
| 33 | + ): |
| 34 | + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 35 | + self.assertFalse(more) |
| 36 | + showsyntaxerror.assert_not_called() |
| 37 | + |
| 38 | + |
| 39 | + def test_multiple_statements_output(self): |
| 40 | + namespace = {} |
| 41 | + code = dedent("""\ |
| 42 | + b = 1 |
| 43 | + b |
| 44 | + a = 1 |
| 45 | + a |
| 46 | + """) |
| 47 | + console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 48 | + f = io.StringIO() |
| 49 | + with contextlib.redirect_stdout(f): |
| 50 | + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 51 | + self.assertFalse(more) |
| 52 | + self.assertEqual(f.getvalue(), "1\n") |
| 53 | + |
| 54 | + def test_empty(self): |
| 55 | + namespace = {} |
| 56 | + code = "" |
| 57 | + console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 58 | + f = io.StringIO() |
| 59 | + with contextlib.redirect_stdout(f): |
| 60 | + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 61 | + self.assertFalse(more) |
| 62 | + self.assertEqual(f.getvalue(), "") |
| 63 | + |
| 64 | + def test_runsource_compiles_and_runs_code(self): |
| 65 | + console = InteractiveColoredConsole() |
| 66 | + source = "print('Hello, world!')" |
| 67 | + with patch.object(console, "runcode") as mock_runcode: |
| 68 | + console.runsource(source) |
| 69 | + mock_runcode.assert_called_once() |
| 70 | + |
| 71 | + def test_runsource_returns_false_for_successful_compilation(self): |
| 72 | + console = InteractiveColoredConsole() |
| 73 | + source = "print('Hello, world!')" |
| 74 | + result = console.runsource(source) |
| 75 | + self.assertFalse(result) |
| 76 | + |
| 77 | + @force_not_colorized |
| 78 | + def test_runsource_returns_false_for_failed_compilation(self): |
| 79 | + console = InteractiveColoredConsole() |
| 80 | + source = "print('Hello, world!'" |
| 81 | + f = io.StringIO() |
| 82 | + with contextlib.redirect_stderr(f): |
| 83 | + result = console.runsource(source) |
| 84 | + self.assertFalse(result) |
| 85 | + self.assertIn('SyntaxError', f.getvalue()) |
| 86 | + |
| 87 | + def test_runsource_shows_syntax_error_for_failed_compilation(self): |
| 88 | + console = InteractiveColoredConsole() |
| 89 | + source = "print('Hello, world!'" |
| 90 | + with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror: |
| 91 | + console.runsource(source) |
| 92 | + mock_showsyntaxerror.assert_called_once() |
0 commit comments