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(self):
80        """
81        Tests that navigating with the up arrow doesn't crash and skips
82        empty history entries.
83        """
84        self.launch()
85
86        # Create a real history entry '456' and then follow up with an
87        # empty entry (that shouldn't be saved).
88        self.child.sendline("expr")
89        self.child.expect_exact("terminate with an empty line to evaluate")
90        self.child.send("456\n\n")
91        self.expect_prompt()
92
93        self.child.sendline("expr")
94        self.child.expect_exact("terminate with an empty line to evaluate")
95        self.child.send("\n")
96        self.expect_prompt()
97
98        # The up arrow should recall the actual history entry and not the
99        # the empty entry (as that one shouldn't have been saved).
100        self.child.sendline("expr")
101        self.child.expect_exact("terminate with an empty line to evaluate")
102        self.child.send(self.arrow_up)
103        self.child.expect_exact("456")
104        self.child.send("\n\n")
105        self.expect_prompt()
106
107        self.quit()
108