aboutsummaryrefslogtreecommitdiff
path: root/lldb/test/API/terminal/TestPythonInterpreterEcho.py
blob: 758a4f9cede5a67db237824471790980e9422d25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Test that typing python expression in the terminal is echoed back to stdout.
"""

from lldbsuite.test.decorators import skipIfAsan
from lldbsuite.test.lldbpexpect import PExpectTest


@skipIfAsan
class PythonInterpreterEchoTest(PExpectTest):
    PYTHON_PROMPT = ">>> "

    def verify_command_echo(
        self, command: str, expected_output: str = "", is_regex: bool = False
    ):
        assert self.child != None
        child = self.child
        self.assertIsNotNone(self.child, "expected a running lldb process.")

        child.sendline(command)

        # Build pattern list: match whichever comes first (output or prompt).
        # This prevents waiting for a timeout if there's no match.
        pattern = []
        match_expected = expected_output and len(expected_output) > 0

        if match_expected:
            pattern.append(expected_output)
        pattern.append(self.PYTHON_PROMPT)

        expect_func = child.expect if is_regex else child.expect_exact
        match_idx = expect_func(pattern)
        if match_expected:
            self.assertEqual(
                match_idx, 0, "Expected output `{expected_output}` in stdout."
            )

        self.assertIsNotNone(self.child.before, "Expected output before prompt")
        self.assertIsInstance(self.child.before, bytes)
        echoed_text: str = self.child.before.decode("ascii").strip()
        self.assertEqual(
            command, echoed_text, f"Command '{command}' should be echoed to stdout."
        )

        if match_expected:
            child.expect_exact(self.PYTHON_PROMPT)

    def test_python_interpreter_echo(self):
        """Test that that the user typed commands is echoed to stdout"""

        self.launch(use_colors=False, dimensions=(100, 100))

        # Enter the python interpreter.
        self.verify_command_echo(
            "script --language python --", expected_output="Python.*\\.", is_regex=True
        )
        self.child_in_script_interpreter = True

        self.verify_command_echo("val = 300")
        self.verify_command_echo(
            "print('result =', 300)", expected_output="result = 300"
        )