|
1 | 1 | import itertools
|
2 | 2 | import os
|
3 | 3 | import rlcompleter
|
4 |
| -import sys |
5 | 4 | import tempfile
|
6 | 5 | import unittest
|
7 | 6 | from code import InteractiveConsole
|
8 | 7 | from functools import partial
|
9 | 8 | from unittest import TestCase
|
10 | 9 | from unittest.mock import MagicMock, patch
|
| 10 | +from textwrap import dedent |
| 11 | +import contextlib |
| 12 | +import io |
11 | 13 |
|
12 | 14 | from test.support import requires
|
13 | 15 | from test.support.import_helper import import_module
|
@@ -1002,5 +1004,62 @@ def test_up_arrow_after_ctrl_r(self):
|
1002 | 1004 | self.assert_screen_equals(reader, "")
|
1003 | 1005 |
|
1004 | 1006 |
|
| 1007 | +class TestSimpleInteract(unittest.TestCase): |
| 1008 | + def test_multiple_statements(self): |
| 1009 | + namespace = {} |
| 1010 | + code = dedent("""\ |
| 1011 | + class A: |
| 1012 | + def foo(self): |
| 1013 | +
|
| 1014 | +
|
| 1015 | + pass |
| 1016 | +
|
| 1017 | + class B: |
| 1018 | + def bar(self): |
| 1019 | + pass |
| 1020 | +
|
| 1021 | + a = 1 |
| 1022 | + a |
| 1023 | + """) |
| 1024 | + console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 1025 | + with ( |
| 1026 | + patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror, |
| 1027 | + patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource, |
| 1028 | + ): |
| 1029 | + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 1030 | + self.assertFalse(more) |
| 1031 | + showsyntaxerror.assert_not_called() |
| 1032 | + |
| 1033 | + |
| 1034 | + def test_multiple_statements_output(self): |
| 1035 | + namespace = {} |
| 1036 | + code = dedent("""\ |
| 1037 | + b = 1 |
| 1038 | + b |
| 1039 | + a = 1 |
| 1040 | + a |
| 1041 | + """) |
| 1042 | + console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 1043 | + f = io.StringIO() |
| 1044 | + with contextlib.redirect_stdout(f): |
| 1045 | + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 1046 | + self.assertFalse(more) |
| 1047 | + self.assertEqual(f.getvalue(), "1\n") |
| 1048 | + |
| 1049 | + def test_empty(self): |
| 1050 | + namespace = {} |
| 1051 | + code = "" |
| 1052 | + console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 1053 | + f = io.StringIO() |
| 1054 | + with contextlib.redirect_stdout(f): |
| 1055 | + more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 1056 | + self.assertFalse(more) |
| 1057 | + self.assertEqual(f.getvalue(), "") |
| 1058 | + |
| 1059 | + |
| 1060 | +if __name__ == '__main__': |
| 1061 | + unittest.main() |
| 1062 | + |
| 1063 | + |
1005 | 1064 | if __name__ == '__main__':
|
1006 | 1065 | unittest.main()
|
0 commit comments