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
10class TestCase(PExpectTest):
11
12    mydir = TestBase.compute_mydir(__file__)
13
14    arrow_up = "\033[A"
15    arrow_down = "\033[B"
16
17    # PExpect uses many timeouts internally and doesn't play well
18    # under ASAN on a loaded machine..
19    @skipIfAsan
20    @skipIfEditlineSupportMissing
21    @expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr48316')
22    @skipIf(oslist=["linux"], archs=["arm", "aarch64"]) # Randomly fails on buildbot
23    def test_nav_arrow_up(self):
24        """Tests that we can navigate back to the previous line with the up arrow"""
25        self.launch()
26
27        # Start multiline expression mode by just running 'expr'
28        self.child.sendline("expr")
29        self.child.expect_exact("terminate with an empty line to evaluate")
30        # Create a simple integer expression '123' and press enter.
31        self.child.send("123\n")
32        # We should see the prompt for the second line of our expression.
33        self.child.expect_exact("2: ")
34        # Go back to the first line and change 123 to 124.
35        # Then press enter twice to evaluate our expression.
36        self.child.send(self.arrow_up + "\b4\n\n")
37        # The result of our expression should be 124 (our edited expression)
38        # and not 123 (the one we initially typed).
39        self.child.expect_exact("(int) $0 = 124")
40
41        self.quit()
42
43    @skipIfAsan
44    @skipIfEditlineSupportMissing
45    @expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr48316')
46    @skipIf(oslist=["linux"], archs=["arm", "aarch64"]) # Randomly fails on buildbot
47    def test_nav_arrow_down(self):
48        """Tests that we can navigate to the next line with the down arrow"""
49        self.launch()
50
51        # Start multiline expression mode by just running 'expr'
52        self.child.sendline("expr")
53        self.child.expect_exact("terminate with an empty line to evaluate")
54        # Create a simple integer expression '111' and press enter.
55        self.child.send("111\n")
56        # We should see the prompt for the second line of our expression.
57        self.child.expect_exact("2: ")
58        # Create another simple integer expression '222'.
59        self.child.send("222")
60        # Go back to the first line and change '111' to '111+' to make
61        # an addition operation that spans two lines. We need to go up to
62        # test that we can go back down again.
63        self.child.send(self.arrow_up + "+")
64        # Go back down to our second line and change '222' to '223'
65        # so that the full expression is now '111+\n223'.
66        # Then press enter twice to evaluate the expression.
67        self.child.send(self.arrow_down + "\b3\n\n")
68        # The result of our expression '111 + 223' should be '334'.
69        # If the expression is '333' then arrow down failed to get
70        # us back to the second line.
71        self.child.expect_exact("(int) $0 = 334")
72
73        self.quit()
74
75    @skipIfAsan
76    @skipIfEditlineSupportMissing
77    @expectedFailureAll(oslist=['freebsd'], bugnumber='llvm.org/pr48316')
78    @skipIf(oslist=["linux"], archs=["arm", "aarch64"]) # Randomly fails on buildbot
79    def test_nav_arrow_up_empty_pr49845(self):
80        """Tests that navigating with the up arrow doesn't crash."""
81        self.launch()
82
83        # Create an empty history session by only entering a newline.
84        self.child.sendline("expr")
85        self.child.expect_exact("terminate with an empty line to evaluate")
86        self.child.send("\n")
87        self.expect_prompt()
88
89        # Send just the up arrow in the expression evaluator. This should bring up the previous empty expression.
90        self.child.sendline("expr")
91        self.child.expect_exact("terminate with an empty line to evaluate")
92        self.child.send(self.arrow_up)
93        self.child.expect_exact("1: ")
94        self.child.send("\n")
95        self.expect_prompt()
96
97        self.quit()
98