|
1 | 1 | import contextlib |
2 | 2 | import io |
| 3 | +import sys |
3 | 4 | import warnings |
4 | 5 | import unittest |
5 | | -from unittest.mock import patch |
| 6 | +from unittest.mock import ANY, patch |
6 | 7 | from textwrap import dedent |
7 | 8 |
|
8 | 9 | from test.support import force_not_colorized |
9 | 10 |
|
10 | 11 | from _pyrepl.console import InteractiveColoredConsole |
11 | | -from _pyrepl.simple_interact import _more_lines |
| 12 | +from _pyrepl.simple_interact import ( |
| 13 | + _more_lines, |
| 14 | + run_multiline_interactive_console, |
| 15 | +) |
12 | 16 |
|
13 | 17 | class TestSimpleInteract(unittest.TestCase): |
| 18 | + def test_prompts_are_converted_to_strings(self): |
| 19 | + console = InteractiveColoredConsole() |
| 20 | + |
| 21 | + class Prompt: |
| 22 | + def __init__(self, value): |
| 23 | + self.value = value |
| 24 | + |
| 25 | + def __str__(self): |
| 26 | + return self.value |
| 27 | + |
| 28 | + with ( |
| 29 | + patch.object(sys, "ps1", Prompt("primary"), create=True), |
| 30 | + patch.object(sys, "ps2", Prompt("secondary"), create=True), |
| 31 | + patch("_pyrepl.readline._setup"), |
| 32 | + patch( |
| 33 | + "_pyrepl.simple_interact.multiline_input", side_effect=EOFError |
| 34 | + ) as multiline_input, |
| 35 | + ): |
| 36 | + run_multiline_interactive_console(console) |
| 37 | + |
| 38 | + multiline_input.assert_called_once_with(ANY, "primary", "secondary") |
| 39 | + |
| 40 | + def test_ps1_str_error_uses_default_prompt(self): |
| 41 | + class Prompt: |
| 42 | + def __str__(self): |
| 43 | + return 1 / 0 |
| 44 | + |
| 45 | + with ( |
| 46 | + patch.object(sys, "ps1", Prompt(), create=True), |
| 47 | + patch("_pyrepl.readline._setup"), |
| 48 | + patch( |
| 49 | + "_pyrepl.simple_interact.multiline_input", side_effect=EOFError |
| 50 | + ) as multiline_input, |
| 51 | + ): |
| 52 | + run_multiline_interactive_console(InteractiveColoredConsole()) |
| 53 | + |
| 54 | + multiline_input.assert_called_once_with(ANY, ">>> ", ANY) |
| 55 | + |
14 | 56 | def test_multiple_statements(self): |
15 | 57 | namespace = {} |
16 | 58 | code = dedent("""\ |
|
0 commit comments