xref: /llvm-project/lldb/test/API/repl/clang/TestClangREPL.py (revision cd344a4c20e295d49f8163ec9a0656c1061a6e42)
1c2232997SRaphael Isemannfrom lldbsuite.test.decorators import *
2c2232997SRaphael Isemannfrom lldbsuite.test.lldbpexpect import PExpectTest
3*cd344a4cSWalter Erquinigofrom lldbsuite.test.lldbtest import *
4c2232997SRaphael Isemann
5c2232997SRaphael Isemann
62238dcc3SJonas Devlieghereclass TestCase(PExpectTest):
7c2232997SRaphael Isemann    def expect_repl(self, expr, substrs=[]):
8c2232997SRaphael Isemann        """Evaluates the expression in the REPL and verifies that the list
9c2232997SRaphael Isemann        of substrs is in the REPL output."""
10c2232997SRaphael Isemann        # Only single line expressions supported.
11c2232997SRaphael Isemann        self.assertNotIn("\n", expr)
12c2232997SRaphael Isemann        self.child.send(expr + "\n")
13c2232997SRaphael Isemann        for substr in substrs:
14c2232997SRaphael Isemann            self.child.expect_exact(substr)
15c2232997SRaphael Isemann        # Look for the start of the next REPL input line.
16c2232997SRaphael Isemann        self.current_repl_line_number += 1
17c2232997SRaphael Isemann        self.child.expect_exact(str(self.current_repl_line_number) + ">")
18c2232997SRaphael Isemann
19*cd344a4cSWalter Erquinigo    def start_repl(self):
20c2232997SRaphael Isemann        self.build()
21c2232997SRaphael Isemann        self.current_repl_line_number = 1
22c2232997SRaphael Isemann
23c2232997SRaphael Isemann        self.launch(executable=self.getBuildArtifact("a.out"), dimensions=(100, 500))
24c2232997SRaphael Isemann        # Try launching the REPL before we have a running target.
252238dcc3SJonas Devlieghere        self.expect(
262238dcc3SJonas Devlieghere            "expression --repl -l c --",
272238dcc3SJonas Devlieghere            substrs=["REPL requires a running target process."],
282238dcc3SJonas Devlieghere        )
29c2232997SRaphael Isemann
30c2232997SRaphael Isemann        self.expect("b main", substrs=["Breakpoint 1", "address ="])
31c2232997SRaphael Isemann        self.expect("run", substrs=["stop reason = breakpoint 1"])
32c2232997SRaphael Isemann
33c2232997SRaphael Isemann        # Start the REPL.
34c2232997SRaphael Isemann        self.child.send("expression --repl -l c --\n")
35c2232997SRaphael Isemann        self.child.expect_exact("1>")
36c2232997SRaphael Isemann
37*cd344a4cSWalter Erquinigo    # PExpect uses many timeouts internally and doesn't play well
38*cd344a4cSWalter Erquinigo    # under ASAN on a loaded machine..
39*cd344a4cSWalter Erquinigo    @skipIfAsan
40*cd344a4cSWalter Erquinigo    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
41*cd344a4cSWalter Erquinigo    @skipIfEditlineSupportMissing
42*cd344a4cSWalter Erquinigo    def test_basic_completion(self):
43*cd344a4cSWalter Erquinigo        """Test that we can complete a simple multiline expression"""
44*cd344a4cSWalter Erquinigo        self.start_repl()
45c2232997SRaphael Isemann        # Try evaluating a simple expression.
46c2232997SRaphael Isemann        self.expect_repl("3 + 3", substrs=["(int) $0 = 6"])
47c2232997SRaphael Isemann
48c2232997SRaphael Isemann        # Try declaring a persistent variable.
492238dcc3SJonas Devlieghere        self.expect_repl(
502238dcc3SJonas Devlieghere            "long $persistent = 7; 5",
512238dcc3SJonas Devlieghere            substrs=["(int) $1 = 5", "(long) $persistent = 7"],
522238dcc3SJonas Devlieghere        )
53c2232997SRaphael Isemann
54c2232997SRaphael Isemann        # Try using the persistent variable from before.
552238dcc3SJonas Devlieghere        self.expect_repl("$persistent + 10", substrs=["(long) $2 = 17"])
56c2232997SRaphael Isemann
57c2232997SRaphael Isemann        self.quit()
58*cd344a4cSWalter Erquinigo
59*cd344a4cSWalter Erquinigo    # PExpect uses many timeouts internally and doesn't play well
60*cd344a4cSWalter Erquinigo    # under ASAN on a loaded machine..
61*cd344a4cSWalter Erquinigo    @skipIfAsan
62*cd344a4cSWalter Erquinigo    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
63*cd344a4cSWalter Erquinigo    @skipIfEditlineSupportMissing
64*cd344a4cSWalter Erquinigo    def test_completion_with_space_only_line(self):
65*cd344a4cSWalter Erquinigo        """Test that we don't crash when completing lines with spaces only"""
66*cd344a4cSWalter Erquinigo        self.start_repl()
67*cd344a4cSWalter Erquinigo
68*cd344a4cSWalter Erquinigo        self.child.send("   ")
69*cd344a4cSWalter Erquinigo        self.child.send("\t")
70*cd344a4cSWalter Erquinigo        self.expect_repl("3 + 3", substrs=["(int) $0 = 6"])
71