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);
25 m_interp
= m_dbg
.GetCommandInterpreter();
29 SBCommandInterpreter m_interp
;
32 class DummyCommand
: public SBCommandPluginInterface
{
34 DummyCommand(const char *message
) : m_message(message
) {}
36 bool DoExecute(SBDebugger dbg
, char **command
,
37 SBCommandReturnObject
&result
) {
38 result
.PutCString(m_message
.c_str());
39 result
.SetStatus(eReturnStatusSuccessFinishResult
);
40 return result
.Succeeded();
44 std::string m_message
;
47 TEST_F(SBCommandInterpreterTest
, SingleWordCommand
) {
48 // We first test a command without autorepeat
49 DummyCommand
dummy("It worked");
50 m_interp
.AddCommand("dummy", &dummy
, /*help=*/nullptr);
52 SBCommandReturnObject result
;
53 m_interp
.HandleCommand("dummy", result
, /*add_to_history=*/true);
54 EXPECT_TRUE(result
.Succeeded());
55 EXPECT_STREQ(result
.GetOutput(), "It worked\n");
58 SBCommandReturnObject result
;
59 m_interp
.HandleCommand("", result
);
60 EXPECT_FALSE(result
.Succeeded());
61 EXPECT_STREQ(result
.GetError(), "error: No auto repeat.\n");
64 // Now we test a command with autorepeat
65 m_interp
.AddCommand("dummy_with_autorepeat", &dummy
, /*help=*/nullptr,
66 /*syntax=*/nullptr, /*auto_repeat_command=*/nullptr);
68 SBCommandReturnObject result
;
69 m_interp
.HandleCommand("dummy_with_autorepeat", result
,
70 /*add_to_history=*/true);
71 EXPECT_TRUE(result
.Succeeded());
72 EXPECT_STREQ(result
.GetOutput(), "It worked\n");
75 SBCommandReturnObject result
;
76 m_interp
.HandleCommand("", result
);
77 EXPECT_TRUE(result
.Succeeded());
78 EXPECT_STREQ(result
.GetOutput(), "It worked\n");
82 TEST_F(SBCommandInterpreterTest
, MultiWordCommand
) {
83 auto command
= m_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 m_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 m_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 m_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 m_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 m_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 m_interp
.HandleCommand("", result
);
135 EXPECT_TRUE(result
.Succeeded());
136 EXPECT_STREQ(result
.GetOutput(), "It worked again\n");