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    ANSI_FAINT = "\x1b[2m"
18    ANSI_RESET = "\x1b[0m"
19    ANSI_RED = "\x1b[31m"
20    ANSI_CYAN = "\x1b[36m"
21
22    # PExpect uses many timeouts internally and doesn't play well
23    # under ASAN on a loaded machine..
24    @skipIfAsan
25    @skipIfEditlineSupportMissing
26    def test_autosuggestion_add_spaces(self):
27        self.launch(use_colors=True,
28                    extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"])
29
30
31        # Check if spaces are added to hide the previous gray characters.
32        self.expect("help frame var")
33        self.expect("help frame info")
34        self.child.send("help frame v")
35        self.child.expect_exact(cursor_horizontal_abs("(lldb) help frame ") + "v" + self.ANSI_FAINT + "ar" + self.ANSI_RESET + " ")
36
37        self.quit()
38
39    @skipIfAsan
40    @skipIfEditlineSupportMissing
41    def test_autosuggestion(self):
42        self.launch(use_colors=True,
43                    extra_args=["-o", "settings set show-autosuggestion true", "-o", "settings set use-color true"])
44
45        # Common input codes.
46        ctrl_f = "\x06"
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" + self.ANSI_FAINT + "p frame" + self.ANSI_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(self.ANSI_FAINT + "ame variable" + self.ANSI_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" + self.ANSI_FAINT + "p apropos" + self.ANSI_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
107    @skipIfAsan
108    @skipIfEditlineSupportMissing
109    def test_autosuggestion_custom_ansi_prefix_suffix(self):
110        self.launch(use_colors=True,
111                    extra_args=["-o", "settings set show-autosuggestion true",
112                                "-o", "settings set use-color true",
113                                "-o", "settings set show-autosuggestion-ansi-prefix ${ansi.fg.red}",
114                                "-o", "setting set show-autosuggestion-ansi-suffix ${ansi.fg.cyan}"])
115
116        self.child.send("help frame variable\n")
117        self.child.send("help fr")
118        self.child.expect_exact(self.ANSI_RED + "ame variable" + self.ANSI_CYAN)
119        self.child.send("\n")
120
121        self.quit()
122