xref: /llvm-project/lldb/test/API/terminal/TestEditline.py (revision 2841cdbfda92b3fb5fbec726f3376b198106d496)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest that the lldb editline handling is configured correctly.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprechtimport lldb
699451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
999451b44SJordan Rupprechtfrom lldbsuite.test.lldbpexpect import PExpectTest
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprechtclass EditlineTest(PExpectTest):
1399451b44SJordan Rupprecht    @skipIfAsan
1499451b44SJordan Rupprecht    @skipIfEditlineSupportMissing
15f2128abeSMuhammad Omair Javaid    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])
1699451b44SJordan Rupprecht    def test_left_right_arrow(self):
1799451b44SJordan Rupprecht        """Test that ctrl+left/right arrow navigates words correctly.
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht        Note: just sending escape characters to pexpect and checking the buffer
2099451b44SJordan Rupprecht        doesn't work well, so we run real commands. We want to type
2199451b44SJordan Rupprecht        "help command" while exercising word-navigation, so type it as below,
2299451b44SJordan Rupprecht        where [] indicates cursor position.
2399451b44SJordan Rupprecht
2499451b44SJordan Rupprecht        1. Send "el rint"  -> "el rint[]"
2599451b44SJordan Rupprecht        2. Ctrl+left once  -> "el []rint"
2699451b44SJordan Rupprecht        3. Send "p"        -> "el p[]rint"
2799451b44SJordan Rupprecht        4. Ctrl+left twice -> "[]el print"
2899451b44SJordan Rupprecht        5. Send "h"        -> "h[]el print"
2999451b44SJordan Rupprecht        6. Ctrl+right      -> "hel[] print"
3099451b44SJordan Rupprecht        7. Send "p"        -> "help print"
3199451b44SJordan Rupprecht        """
3299451b44SJordan Rupprecht        self.launch()
3399451b44SJordan Rupprecht
3499451b44SJordan Rupprecht        escape_pairs = [
3599451b44SJordan Rupprecht            ("\x1b[1;5D", "\x1b[1;5C"),
3699451b44SJordan Rupprecht            ("\x1b[5D", "\x1b[5C"),
3799451b44SJordan Rupprecht            ("\x1b\x1b[D", "\x1b\x1b[C"),
3899451b44SJordan Rupprecht        ]
392238dcc3SJonas Devlieghere        for l_escape, r_escape in escape_pairs:
402238dcc3SJonas Devlieghere            self.expect(
412238dcc3SJonas Devlieghere                "el rint{L}p{L}{L}h{R}p".format(L=l_escape, R=r_escape),
422238dcc3SJonas Devlieghere                substrs=["Syntax: print"],
432238dcc3SJonas Devlieghere            )
4499451b44SJordan Rupprecht
4599451b44SJordan Rupprecht        self.quit()
46850e90c4SJonas Devlieghere
47850e90c4SJonas Devlieghere    @skipIfAsan
48850e90c4SJonas Devlieghere    @skipIfEditlineSupportMissing
49111bc6f1SJonas Devlieghere    @skipIfEditlineWideCharSupportMissing
50850e90c4SJonas Devlieghere    def test_prompt_unicode(self):
51850e90c4SJonas Devlieghere        """Test that we can use Unicode in the LLDB prompt."""
52850e90c4SJonas Devlieghere        self.launch(use_colors=True, encoding="utf-8")
53850e90c4SJonas Devlieghere        self.child.send('settings set prompt "�� "\n')
54850e90c4SJonas Devlieghere        # Check that the cursor is at position 4 ([4G)
55850e90c4SJonas Devlieghere        # Prompt: �� _
56850e90c4SJonas Devlieghere        # Column: 1..4
57850e90c4SJonas Devlieghere        self.child.expect(re.escape("�� \x1b[0m\x1b[4G"))
58645a3855SJonas Devlieghere
59645a3855SJonas Devlieghere    @skipIfAsan
60645a3855SJonas Devlieghere    @skipIfEditlineSupportMissing
61645a3855SJonas Devlieghere    def test_prompt_color(self):
62645a3855SJonas Devlieghere        """Test that we can change the prompt color with prompt-ansi-prefix."""
63645a3855SJonas Devlieghere        self.launch(use_colors=True)
64645a3855SJonas Devlieghere        self.child.send('settings set prompt-ansi-prefix "${ansi.fg.red}"\n')
65645a3855SJonas Devlieghere        # Make sure this change is reflected immediately. Check that the color
66645a3855SJonas Devlieghere        # is set (31) and the cursor position (8) is correct.
67645a3855SJonas Devlieghere        # Prompt: (lldb) _
68645a3855SJonas Devlieghere        # Column: 1....6.8
69645a3855SJonas Devlieghere        self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8G"))
70645a3855SJonas Devlieghere
71645a3855SJonas Devlieghere    @skipIfAsan
72645a3855SJonas Devlieghere    @skipIfEditlineSupportMissing
73*2841cdbfSJonas Devlieghere    def test_prompt_format_color(self):
74*2841cdbfSJonas Devlieghere        """Test that we can change the prompt color with a format string."""
75*2841cdbfSJonas Devlieghere        self.launch(use_colors=True)
76*2841cdbfSJonas Devlieghere        # Clear the prefix and suffix setting to simplify the output.
77*2841cdbfSJonas Devlieghere        self.expect('settings set prompt-ansi-prefix ""')
78*2841cdbfSJonas Devlieghere        self.expect('settings set prompt-ansi-suffix ""')
79*2841cdbfSJonas Devlieghere        self.expect('settings set prompt "${ansi.fg.red}(lldb) ${ansi.normal}"')
80*2841cdbfSJonas Devlieghere        self.child.send("foo")
81*2841cdbfSJonas Devlieghere        # Make sure this change is reflected immediately. Check that the color
82*2841cdbfSJonas Devlieghere        # is set (31) and the cursor position (8) is correct.
83*2841cdbfSJonas Devlieghere        # Prompt: (lldb) _
84*2841cdbfSJonas Devlieghere        # Column: 1....6.8
85*2841cdbfSJonas Devlieghere        self.child.expect(re.escape("\x1b[31m(lldb) \x1b[0m\x1b[8Gfoo"))
86*2841cdbfSJonas Devlieghere
87*2841cdbfSJonas Devlieghere    @skipIfAsan
88*2841cdbfSJonas Devlieghere    @skipIfEditlineSupportMissing
89645a3855SJonas Devlieghere    def test_prompt_no_color(self):
90645a3855SJonas Devlieghere        """Test that prompt-ansi-prefix doesn't color the prompt when colors are off."""
91645a3855SJonas Devlieghere        self.launch(use_colors=False)
92645a3855SJonas Devlieghere        self.child.send('settings set prompt-ansi-prefix "${ansi.fg.red}"\n')
93645a3855SJonas Devlieghere        # Send foo so we can match the newline before the prompt and the foo
94645a3855SJonas Devlieghere        # after the prompt.
95645a3855SJonas Devlieghere        self.child.send("foo")
96645a3855SJonas Devlieghere        # Check that there are no escape codes.
97645a3855SJonas Devlieghere        self.child.expect(re.escape("\n(lldb) foo"))
98