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