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    mydir = TestBase.compute_mydir(__file__)
14
15    @no_debug_info_test
16    def test_command_abbreviations_and_aliases(self):
17        command_interpreter = self.dbg.GetCommandInterpreter()
18        self.assertTrue(command_interpreter, VALID_COMMAND_INTERPRETER)
19        result = lldb.SBCommandReturnObject()
20
21        # Check that abbreviations are expanded to the full command.
22        command_interpreter.ResolveCommand("ap script", result)
23        self.assertTrue(result.Succeeded())
24        self.assertEqual("apropos script", result.GetOutput())
25
26        command_interpreter.ResolveCommand("h", result)
27        self.assertTrue(result.Succeeded())
28        self.assertEqual("help", result.GetOutput())
29
30        # Check resolution of abbreviations for multi-word commands.
31        command_interpreter.ResolveCommand("lo li", result)
32        self.assertTrue(result.Succeeded())
33        self.assertEqual("log list", result.GetOutput())
34
35        command_interpreter.ResolveCommand("br s", result)
36        self.assertTrue(result.Succeeded())
37        self.assertEqual("breakpoint set", result.GetOutput())
38
39        # Try an ambiguous abbreviation.
40        # "pl" could be "platform" or "plugin".
41        command_interpreter.ResolveCommand("pl", result)
42        self.assertFalse(result.Succeeded())
43        self.assertTrue(result.GetError().startswith("Ambiguous command"))
44
45        # Make sure an unabbreviated command is not mangled.
46        command_interpreter.ResolveCommand(
47            "breakpoint set --name main --line 123", result)
48        self.assertTrue(result.Succeeded())
49        self.assertEqual(
50            "breakpoint set --name main --line 123",
51            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",
73            result.GetOutput())
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(
79            "breakpoint set -n main -l 123",
80            result.GetOutput().strip())
81
82        # And again, without enough parameters.
83        command_interpreter.ResolveCommand("xyzzy main", result)
84        self.assertFalse(result.Succeeded())
85
86        # Check a command that wants the raw input.
87        command_interpreter.ResolveCommand(
88            r'''sc print("\n\n\tHello!\n")''', result)
89        self.assertTrue(result.Succeeded())
90        self.assertEqual(
91            r'''script print("\n\n\tHello!\n")''',
92            result.GetOutput())
93
94        # Prompt changing stuff should be tested, but this doesn't seem like the
95        # right test to do it in.  It has nothing to do with aliases or abbreviations.
96        #self.runCmd("com sou ./change_prompt.lldb")
97        # self.expect("settings show prompt",
98        #            startstr = 'prompt (string) = "[with-three-trailing-spaces]   "')
99        #self.runCmd("settings clear prompt")
100        # self.expect("settings show prompt",
101        #            startstr = 'prompt (string) = "(lldb) "')
102        #self.runCmd("se se prompt 'Sycamore> '")
103        # self.expect("se sh prompt",
104        #            startstr = 'prompt (string) = "Sycamore> "')
105        #self.runCmd("se cl prompt")
106        # self.expect("set sh prompt",
107        #            startstr = 'prompt (string) = "(lldb) "')
108