1 //===-- SBCommandInterpreterTest.cpp ------------------------===----------===//
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
7 //===----------------------------------------------------------------------===/
9 #include "gtest/gtest.h"
11 #include "lldb/API/SBCommandInterpreter.h"
12 #include "lldb/API/SBCommandReturnObject.h"
13 #include "lldb/API/SBDebugger.h"
20 class SBCommandInterpreterTest
: public testing::Test
{
22 void SetUp() override
{
23 SBDebugger::Initialize();
24 m_dbg
= SBDebugger::Create(/*source_init_files=*/false);
30 class DummyCommand
: public SBCommandPluginInterface
{
32 DummyCommand(const char *message
) : m_message(message
) {}
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();
42 std::string m_message
;
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);
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");
57 SBCommandReturnObject result
;
58 interp
.HandleCommand("", result
);
59 EXPECT_FALSE(result
.Succeeded());
60 EXPECT_STREQ(result
.GetError(), "error: No auto repeat.\n");
63 // Now we test a command with autorepeat
64 interp
.AddCommand("dummy_with_autorepeat", &dummy
, /*help=*/nullptr,
65 /*syntax=*/nullptr, /*auto_repeat_command=*/nullptr);
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");
74 SBCommandReturnObject result
;
75 interp
.HandleCommand("", result
);
76 EXPECT_TRUE(result
.Succeeded());
77 EXPECT_STREQ(result
.GetOutput(), "It worked\n");
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);
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");
95 SBCommandReturnObject result
;
96 interp
.HandleCommand("", result
);
97 EXPECT_FALSE(result
.Succeeded());
98 EXPECT_STREQ(result
.GetError(), "error: No auto repeat.\n");
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);
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");
113 SBCommandReturnObject result
;
114 interp
.HandleCommand("", result
);
115 EXPECT_TRUE(result
.Succeeded());
116 EXPECT_STREQ(result
.GetOutput(), "It worked again\n");
119 DummyCommand
subcommand2("It worked again 2");
120 // We now test a subcommand with autorepeat of the command name
122 "subcommand_with_custom_autorepeat", &subcommand2
, /*help=*/nullptr,
124 /*auto_repeat_command=*/"multicommand subcommand_with_autorepeat");
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");
133 SBCommandReturnObject result
;
134 interp
.HandleCommand("", result
);
135 EXPECT_TRUE(result
.Succeeded());
136 EXPECT_STREQ(result
.GetOutput(), "It worked again\n");