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
18using namespace lldb;
19
20class SBCommandInterpreterTest : public testing::Test {
21protected:
22 void SetUp() override {
23 SBDebugger::Initialize();
24 m_dbg = SBDebugger::Create(/*source_init_files=*/false);
25 }
26
27 SBDebugger m_dbg;
28};
29
30class DummyCommand : public SBCommandPluginInterface {
31public:
32 DummyCommand(const char *message) : m_message(message) {}
33
34 bool DoExecute(SBDebugger dbg, char **command,
35 SBCommandReturnObject &result) override {
36 result.PutCString(string: m_message.c_str());
37 result.SetStatus(eReturnStatusSuccessFinishResult);
38 return result.Succeeded();
39 }
40
41private:
42 std::string m_message;
43};
44
45TEST_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(name: "dummy", impl: &dummy, /*help=*/nullptr);
50 {
51 SBCommandReturnObject result;
52 interp.HandleCommand(command_line: "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(command_line: "", 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(name: "dummy_with_autorepeat", impl: &dummy, /*help=*/nullptr,
65 /*syntax=*/nullptr, /*auto_repeat_command=*/nullptr);
66 {
67 SBCommandReturnObject result;
68 interp.HandleCommand(command_line: "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(command_line: "", result);
76 EXPECT_TRUE(result.Succeeded());
77 EXPECT_STREQ(result.GetOutput(), "It worked\n");
78 }
79}
80
81TEST_F(SBCommandInterpreterTest, MultiWordCommand) {
82 SBCommandInterpreter interp = m_dbg.GetCommandInterpreter();
83 auto command = interp.AddMultiwordCommand(name: "multicommand", /*help=*/nullptr);
84 // We first test a subcommand without autorepeat
85 DummyCommand subcommand("It worked again");
86 command.AddCommand(name: "subcommand", impl: &subcommand, /*help=*/nullptr);
87 {
88 SBCommandReturnObject result;
89 interp.HandleCommand(command_line: "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(command_line: "", 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(name: "subcommand_with_autorepeat", impl: &subcommand,
103 /*help=*/nullptr, /*syntax=*/nullptr,
104 /*auto_repeat_command=*/nullptr);
105 {
106 SBCommandReturnObject result;
107 interp.HandleCommand(command_line: "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(command_line: "", 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 name: "subcommand_with_custom_autorepeat", impl: &subcommand2, /*help=*/nullptr,
123 /*syntax=*/nullptr,
124 /*auto_repeat_command=*/"multicommand subcommand_with_autorepeat");
125 {
126 SBCommandReturnObject result;
127 interp.HandleCommand(command_line: "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(command_line: "", result);
135 EXPECT_TRUE(result.Succeeded());
136 EXPECT_STREQ(result.GetOutput(), "It worked again\n");
137 }
138}
139

source code of lldb/unittests/API/SBCommandInterpreterTest.cpp