1"""
2Tests autosuggestion using pexpect.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test.lldbpexpect import PExpectTest
9
10def cursor_horizontal_abs(s):
11    return "\x1b[" + str(len(s) + 1) + "G"
12
13
14
15class TestCase(PExpectTest):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    # PExpect uses many timeouts internally and doesn't play well
20    # under ASAN on a loaded machine..
21    @skipIfAsan
22    @skipIfEditlineSupportMissing
23    def test_autosuggestion_add_spaces(self):
24        self.launch(extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"])
25
26        # Common input codes and escape sequences.
27        faint_color = "\x1b[2m"
28        reset = "\x1b[0m"
29
30        # Check if spaces are added to hide the previous gray characters.
31        self.expect("help frame var")
32        self.expect("help frame info")
33        self.child.send("help frame v")
34        self.child.expect_exact(cursor_horizontal_abs("(lldb) help frame ") + "v" + faint_color + "ar" + reset + " ")
35
36        self.quit()
37
38    @skipIfAsan
39    @skipIfEditlineSupportMissing
40    def test_autosuggestion(self):
41        self.launch(extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"])
42
43        # Common input codes and escape sequences.
44        ctrl_f = "\x06"
45        faint_color = "\x1b[2m"
46        reset = "\x1b[0m"
47        delete = chr(127)
48
49        frame_output_needle = "Syntax: frame <subcommand>"
50        # Run 'help frame' once to put it into the command history.
51        self.expect("help frame", substrs=[frame_output_needle])
52
53        # Check that LLDB shows the autosuggestion in gray behind the text.
54        self.child.send("hel")
55        self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + faint_color + "p frame" + reset)
56
57        # Apply the autosuggestion and press enter. This should print the
58        # 'help frame' output if everything went correctly.
59        self.child.send(ctrl_f + "\n")
60        self.child.expect_exact(frame_output_needle)
61
62        # Check that pressing Ctrl+F directly after Ctrl+F again does nothing.
63        self.child.send("hel" + ctrl_f + ctrl_f + "\n")
64        self.child.expect_exact(frame_output_needle)
65
66        # Try autosuggestion using tab and ^f.
67        # \t makes "help" and ^f makes "help frame". If everything went
68        # correct we should see the 'help frame' output again.
69        self.child.send("hel\t" + ctrl_f + "\n")
70        self.child.expect_exact(frame_output_needle)
71
72        # Check that autosuggestion works after delete.
73        self.child.send("a1234" + 5 * delete + "hel" + ctrl_f + "\n")
74        self.child.expect_exact(frame_output_needle)
75
76        # Check that autosuggestion works after delete.
77        self.child.send("help x" + delete + ctrl_f + "\n")
78        self.child.expect_exact(frame_output_needle)
79
80        # Check that autosuggestion complete to the most recent one.
81        self.child.send("help frame variable\n")
82        self.child.send("help fr")
83        self.child.expect_exact(faint_color + "ame variable" + reset)
84        self.child.send("\n")
85
86        # Try another command.
87        apropos_output_needle = "Syntax: apropos <search-word>"
88        # Run 'help frame' once to put it into the command history.
89        self.expect("help apropos", substrs=[apropos_output_needle])
90
91        # Check that 'hel' should have an autosuggestion for 'help apropos' now.
92        self.child.send("hel")
93        self.child.expect_exact(cursor_horizontal_abs("(lldb) he") + "l" + faint_color + "p apropos" + reset)
94
95        # Run the command and expect the 'help apropos' output.
96        self.child.send(ctrl_f + "\n")
97        self.child.expect_exact(apropos_output_needle)
98
99        # Check that pressing Ctrl+F in an empty prompt does nothing.
100        breakpoint_output_needle = "Syntax: breakpoint <subcommand>"
101        self.child.send(ctrl_f + "help breakpoint" +"\n")
102        self.child.expect_exact(breakpoint_output_needle)
103
104
105        self.quit()
106