xref: /llvm-project/lldb/test/API/commands/expression/multiline-navigation/TestMultilineNavigation.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Tests navigating in the multiline expression editor.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test.lldbpexpect import PExpectTest
9
10
11class TestCase(PExpectTest):
12    arrow_up = "\033[A"
13    arrow_down = "\033[B"
14
15    # PExpect uses many timeouts internally and doesn't play well
16    # under ASAN on a loaded machine..
17    @skipIfAsan
18    @skipIfEditlineSupportMissing
19    @expectedFailureAll(oslist=["freebsd"], bugnumber="llvm.org/pr48316")
20    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
21    def test_nav_arrow_up(self):
22        """Tests that we can navigate back to the previous line with the up arrow"""
23        self.launch()
24
25        # Start multiline expression mode by just running 'expr'
26        self.child.sendline("expr")
27        self.child.expect_exact("terminate with an empty line to evaluate")
28        # Create a simple integer expression '123' and press enter.
29        self.child.send("123\n")
30        # We should see the prompt for the second line of our expression.
31        self.child.expect_exact("2: ")
32        # Go back to the first line and change 123 to 124.
33        # Then press enter twice to evaluate our expression.
34        self.child.send(self.arrow_up + "\b4\n\n")
35        # The result of our expression should be 124 (our edited expression)
36        # and not 123 (the one we initially typed).
37        self.child.expect_exact("(int) $0 = 124")
38
39        self.quit()
40
41    @skipIfAsan
42    @skipIfEditlineSupportMissing
43    @expectedFailureAll(oslist=["freebsd"], bugnumber="llvm.org/pr48316")
44    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
45    def test_nav_arrow_down(self):
46        """Tests that we can navigate to the next line with the down arrow"""
47        self.launch()
48
49        # Start multiline expression mode by just running 'expr'
50        self.child.sendline("expr")
51        self.child.expect_exact("terminate with an empty line to evaluate")
52        # Create a simple integer expression '111' and press enter.
53        self.child.send("111\n")
54        # We should see the prompt for the second line of our expression.
55        self.child.expect_exact("2: ")
56        # Create another simple integer expression '222'.
57        self.child.send("222")
58        # Go back to the first line and change '111' to '111+' to make
59        # an addition operation that spans two lines. We need to go up to
60        # test that we can go back down again.
61        self.child.send(self.arrow_up + "+")
62        # Go back down to our second line and change '222' to '223'
63        # so that the full expression is now '111+\n223'.
64        # Then press enter twice to evaluate the expression.
65        self.child.send(self.arrow_down + "\b3\n\n")
66        # The result of our expression '111 + 223' should be '334'.
67        # If the expression is '333' then arrow down failed to get
68        # us back to the second line.
69        self.child.expect_exact("(int) $0 = 334")
70
71        self.quit()
72
73    @skipIfAsan
74    @skipIfEditlineSupportMissing
75    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
76    def test_nav_arrow_up_empty(self):
77        """
78        Tests that navigating with the up arrow doesn't crash and skips
79        empty history entries.
80        """
81        self.launch()
82
83        # Create a real history entry '456' and then follow up with an
84        # empty entry (that shouldn't be saved).
85        self.child.sendline("expr")
86        self.child.expect_exact("terminate with an empty line to evaluate")
87        self.child.send("456\n\n")
88        self.expect_prompt()
89
90        self.child.sendline("expr")
91        self.child.expect_exact("terminate with an empty line to evaluate")
92        self.child.send("\n")
93        self.expect_prompt()
94
95        # The up arrow should recall the actual history entry and not the
96        # the empty entry (as that one shouldn't have been saved).
97        self.child.sendline("expr")
98        self.child.expect_exact("terminate with an empty line to evaluate")
99        self.child.send(self.arrow_up)
100        self.child.expect_exact("456")
101        self.child.send("\n\n")
102        self.expect_prompt()
103
104        self.quit()
105