xref: /llvm-project/lldb/unittests/API/SBCommandInterpreterTest.cpp (revision 27b6a4e63afe62f6258379a61177c67a670593c6)
1 //===-- SBCommandInterpreterTest.cpp ------------------------===----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===/
8 
9 #include "gtest/gtest.h"
10 
11 #include "lldb/API/SBCommandInterpreter.h"
12 #include "lldb/API/SBCommandReturnObject.h"
13 #include "lldb/API/SBDebugger.h"
14 
15 #include <cstring>
16 #include <string>
17 
18 using namespace lldb;
19 
20 class SBCommandInterpreterTest : public testing::Test {
21 protected:
SetUp()22   void SetUp() override {
23     SBDebugger::Initialize();
24     m_dbg = SBDebugger::Create(/*source_init_files=*/false);
25   }
26 
27   SBDebugger m_dbg;
28 };
29 
30 class DummyCommand : public SBCommandPluginInterface {
31 public:
DummyCommand(const char * message)32   DummyCommand(const char *message) : m_message(message) {}
33 
DoExecute(SBDebugger dbg,char ** command,SBCommandReturnObject & result)34   bool DoExecute(SBDebugger dbg, char **command,
35                  SBCommandReturnObject &result) override {
36     result.PutCString(m_message.c_str());
37     result.SetStatus(eReturnStatusSuccessFinishResult);
38     return result.Succeeded();
39   }
40 
41 private:
42   std::string m_message;
43 };
44 
TEST_F(SBCommandInterpreterTest,SingleWordCommand)45 TEST_F(SBCommandInterpreterTest, SingleWordCommand) {
46   // We first test a command without autorepeat
47   DummyCommand dummy("It worked");
48   SBCommandInterpreter interp = m_dbg.GetCommandInterpreter();
49   interp.AddCommand("dummy", &dummy, /*help=*/nullptr);
50   {
51     SBCommandReturnObject result;
52     interp.HandleCommand("dummy", result, /*add_to_history=*/true);
53     EXPECT_TRUE(result.Succeeded());
54     EXPECT_STREQ(result.GetOutput(), "It worked\n");
55   }
56   {
57     SBCommandReturnObject result;
58     interp.HandleCommand("", result);
59     EXPECT_FALSE(result.Succeeded());
60     EXPECT_STREQ(result.GetError(), "error: No auto repeat.\n");
61   }
62 
63   // Now we test a command with autorepeat
64   interp.AddCommand("dummy_with_autorepeat", &dummy, /*help=*/nullptr,
65                     /*syntax=*/nullptr, /*auto_repeat_command=*/nullptr);
66   {
67     SBCommandReturnObject result;
68     interp.HandleCommand("dummy_with_autorepeat", result,
69                          /*add_to_history=*/true);
70     EXPECT_TRUE(result.Succeeded());
71     EXPECT_STREQ(result.GetOutput(), "It worked\n");
72   }
73   {
74     SBCommandReturnObject result;
75     interp.HandleCommand("", result);
76     EXPECT_TRUE(result.Succeeded());
77     EXPECT_STREQ(result.GetOutput(), "It worked\n");
78   }
79 }
80 
TEST_F(SBCommandInterpreterTest,MultiWordCommand)81 TEST_F(SBCommandInterpreterTest, MultiWordCommand) {
82   SBCommandInterpreter interp = m_dbg.GetCommandInterpreter();
83   auto command = interp.AddMultiwordCommand("multicommand", /*help=*/nullptr);
84   // We first test a subcommand without autorepeat
85   DummyCommand subcommand("It worked again");
86   command.AddCommand("subcommand", &subcommand, /*help=*/nullptr);
87   {
88     SBCommandReturnObject result;
89     interp.HandleCommand("multicommand subcommand", result,
90                          /*add_to_history=*/true);
91     EXPECT_TRUE(result.Succeeded());
92     EXPECT_STREQ(result.GetOutput(), "It worked again\n");
93   }
94   {
95     SBCommandReturnObject result;
96     interp.HandleCommand("", result);
97     EXPECT_FALSE(result.Succeeded());
98     EXPECT_STREQ(result.GetError(), "error: No auto repeat.\n");
99   }
100 
101   // We first test a subcommand with autorepeat
102   command.AddCommand("subcommand_with_autorepeat", &subcommand,
103                      /*help=*/nullptr, /*syntax=*/nullptr,
104                      /*auto_repeat_command=*/nullptr);
105   {
106     SBCommandReturnObject result;
107     interp.HandleCommand("multicommand subcommand_with_autorepeat", result,
108                          /*add_to_history=*/true);
109     EXPECT_TRUE(result.Succeeded());
110     EXPECT_STREQ(result.GetOutput(), "It worked again\n");
111   }
112   {
113     SBCommandReturnObject result;
114     interp.HandleCommand("", result);
115     EXPECT_TRUE(result.Succeeded());
116     EXPECT_STREQ(result.GetOutput(), "It worked again\n");
117   }
118 
119   DummyCommand subcommand2("It worked again 2");
120   // We now test a subcommand with autorepeat of the command name
121   command.AddCommand(
122       "subcommand_with_custom_autorepeat", &subcommand2, /*help=*/nullptr,
123       /*syntax=*/nullptr,
124       /*auto_repeat_command=*/"multicommand subcommand_with_autorepeat");
125   {
126     SBCommandReturnObject result;
127     interp.HandleCommand("multicommand subcommand_with_custom_autorepeat",
128                          result, /*add_to_history=*/true);
129     EXPECT_TRUE(result.Succeeded());
130     EXPECT_STREQ(result.GetOutput(), "It worked again 2\n");
131   }
132   {
133     SBCommandReturnObject result;
134     interp.HandleCommand("", result);
135     EXPECT_TRUE(result.Succeeded());
136     EXPECT_STREQ(result.GetOutput(), "It worked again\n");
137   }
138 }
139