xref: /llvm-project/lldb/test/API/functionalities/abbreviation/TestAbbreviations.py (revision 60e3a81c4299baf80e7b80db9cb8368223ee9546)
1"""
2Test some lldb command abbreviations and aliases for proper resolution.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class AbbreviationsTestCase(TestBase):
12    @no_debug_info_test
13    def test_command_abbreviations_and_aliases(self):
14        command_interpreter = self.dbg.GetCommandInterpreter()
15        self.assertTrue(command_interpreter, VALID_COMMAND_INTERPRETER)
16        result = lldb.SBCommandReturnObject()
17
18        # Check that abbreviations are expanded to the full command.
19        command_interpreter.ResolveCommand("ap script", result)
20        self.assertTrue(result.Succeeded())
21        self.assertEqual("apropos script", result.GetOutput())
22
23        command_interpreter.ResolveCommand("e", result)
24        self.assertTrue(result.Succeeded())
25        self.assertEqual("expression", result.GetOutput())
26
27        command_interpreter.ResolveCommand("h", result)
28        self.assertTrue(result.Succeeded())
29        self.assertEqual("help", result.GetOutput())
30
31        # Check resolution of abbreviations for multi-word commands.
32        command_interpreter.ResolveCommand("lo li", result)
33        self.assertTrue(result.Succeeded())
34        self.assertEqual("log list", result.GetOutput())
35
36        command_interpreter.ResolveCommand("br s", result)
37        self.assertTrue(result.Succeeded())
38        self.assertEqual("breakpoint set", result.GetOutput())
39
40        # Try an ambiguous abbreviation.
41        # "pl" could be "platform" or "plugin".
42        command_interpreter.ResolveCommand("pl", result)
43        self.assertFalse(result.Succeeded())
44        self.assertTrue(result.GetError().startswith("Ambiguous command"))
45
46        # Make sure an unabbreviated command is not mangled.
47        command_interpreter.ResolveCommand(
48            "breakpoint set --name main --line 123", result
49        )
50        self.assertTrue(result.Succeeded())
51        self.assertEqual("breakpoint set --name main --line 123", result.GetOutput())
52
53        # Create some aliases.
54        self.runCmd("com a alias com al")
55        self.runCmd("alias gurp help")
56
57        # Check that an alias is replaced with the actual command
58        command_interpreter.ResolveCommand("gurp target create", result)
59        self.assertTrue(result.Succeeded())
60        self.assertEqual("help target create", result.GetOutput())
61
62        # Delete the alias and make sure it no longer has an effect.
63        self.runCmd("com u gurp")
64        command_interpreter.ResolveCommand("gurp", result)
65        self.assertFalse(result.Succeeded())
66
67        # Check aliases with text replacement.
68        self.runCmd("alias pltty process launch -s -o %1 -e %1")
69        command_interpreter.ResolveCommand("pltty /dev/tty0", result)
70        self.assertTrue(result.Succeeded())
71        self.assertEqual(
72            "process launch -s -o /dev/tty0 -e /dev/tty0", result.GetOutput()
73        )
74
75        self.runCmd("alias xyzzy breakpoint set -n %1 -l %2")
76        command_interpreter.ResolveCommand("xyzzy main 123", result)
77        self.assertTrue(result.Succeeded())
78        self.assertEqual("breakpoint set -n main -l 123", result.GetOutput().strip())
79
80        # And again, without enough parameters.
81        command_interpreter.ResolveCommand("xyzzy main", result)
82        self.assertFalse(result.Succeeded())
83
84        # Check a command that wants the raw input.
85        command_interpreter.ResolveCommand(r"""sc print("\n\n\tHello!\n")""", result)
86        self.assertTrue(result.Succeeded())
87        self.assertEqual(
88            r"""scripting run print("\n\n\tHello!\n")""", result.GetOutput()
89        )
90
91        command_interpreter.ResolveCommand("script 1+1", result)
92        self.assertTrue(result.Succeeded())
93        self.assertEqual("scripting run 1+1", result.GetOutput())
94
95        # Prompt changing stuff should be tested, but this doesn't seem like the
96        # right test to do it in.  It has nothing to do with aliases or abbreviations.
97        # self.runCmd("com sou ./change_prompt.lldb")
98        # self.expect("settings show prompt",
99        #            startstr = 'prompt (string) = "[with-three-trailing-spaces]   "')
100        # self.runCmd("settings clear prompt")
101        # self.expect("settings show prompt",
102        #            startstr = 'prompt (string) = "(lldb) "')
103        # self.runCmd("se se prompt 'Sycamore> '")
104        # self.expect("se sh prompt",
105        #            startstr = 'prompt (string) = "Sycamore> "')
106        # self.runCmd("se cl prompt")
107        # self.expect("set sh prompt",
108        #            startstr = 'prompt (string) = "(lldb) "')
109