1 //===-- ProcessEventDataTest.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 "Plugins/Platform/MacOSX/PlatformMacOSX.h"
10 #include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/HostInfo.h"
14 #include "lldb/Interpreter/CommandInterpreter.h"
15 #include "lldb/Interpreter/CommandObject.h"
16 #include "lldb/Interpreter/CommandObjectMultiword.h"
17 #include "lldb/Interpreter/CommandReturnObject.h"
18 #include "lldb/Utility/Args.h"
19 #include "lldb/Utility/Status.h"
21 #include "gtest/gtest.h"
23 using namespace lldb_private
;
24 using namespace lldb_private::repro
;
28 class VerifyUserMultiwordCmdPathTest
: public ::testing::Test
{
29 void SetUp() override
{
30 FileSystem::Initialize();
31 HostInfo::Initialize();
32 PlatformMacOSX::Initialize();
34 void TearDown() override
{
35 PlatformMacOSX::Terminate();
36 HostInfo::Terminate();
37 FileSystem::Terminate();
42 class CommandObjectLeaf
: public CommandObjectParsed
{
44 CommandObjectLeaf(CommandInterpreter
&interpreter
)
45 : CommandObjectParsed(interpreter
, "dummy subcommand leaf",
46 "Does nothing", "dummy subcommand leaf") {
47 SetIsUserCommand(true);
51 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
52 result
.SetStatus(eReturnStatusSuccessFinishResult
);
53 result
.AppendMessage("I did nothing");
57 class CommandObjectMultiwordSubDummy
: public CommandObjectMultiword
{
59 CommandObjectMultiwordSubDummy(CommandInterpreter
&interpreter
)
60 : CommandObjectMultiword(interpreter
, "dummy subcommand", "Does nothing",
62 SetIsUserCommand(true);
63 LoadSubCommand("leaf", CommandObjectSP(new CommandObjectLeaf(interpreter
)));
66 ~CommandObjectMultiwordSubDummy() override
= default;
69 class CommandObjectMultiwordDummy
: public CommandObjectMultiword
{
71 CommandObjectMultiwordDummy(CommandInterpreter
&interpreter
)
72 : CommandObjectMultiword(interpreter
, "dummy", "Does nothing", "dummy") {
73 SetIsUserCommand(true);
76 CommandObjectSP(new CommandObjectMultiwordSubDummy(interpreter
)));
79 ~CommandObjectMultiwordDummy() override
= default;
82 // Pass in the command path to args. If success is true, we make sure the MWC
83 // returned matches the test string. If success is false, we make sure the
84 // lookup error matches test_str.
85 void RunTest(CommandInterpreter
&interp
, const char *args
, bool is_leaf
,
86 bool success
, const char *test_str
) {
87 CommandObjectMultiword
*multi_word_cmd
= nullptr;
91 interp
.VerifyUserMultiwordCmdPath(test_args
, is_leaf
, error
);
93 ASSERT_NE(multi_word_cmd
, nullptr);
94 ASSERT_TRUE(error
.Success());
95 ASSERT_STREQ(multi_word_cmd
->GetCommandName().str().c_str(), test_str
);
97 ASSERT_EQ(multi_word_cmd
, nullptr);
98 ASSERT_TRUE(error
.Fail());
99 ASSERT_STREQ(error
.AsCString(), test_str
);
103 TEST_F(VerifyUserMultiwordCmdPathTest
, TestErrors
) {
104 ArchSpec
arch("x86_64-apple-macosx-");
106 Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch
));
108 DebuggerSP debugger_sp
= Debugger::CreateInstance();
109 ASSERT_TRUE(debugger_sp
);
111 CommandInterpreter
&interp
= debugger_sp
->GetCommandInterpreter();
117 // Test that we reject non-user path components:
120 RunTest(interp
, "process launch", is_leaf
, success
,
121 "Path component: 'process' is not a user command");
123 // Test that we reject non-existent commands:
126 RunTest(interp
, "wewouldnevernameacommandthis subcommand", is_leaf
, success
,
127 "Path component: 'wewouldnevernameacommandthis' not found");
129 // Now we have to add a multiword command, and then probe it.
130 error
= interp
.AddUserCommand(
131 "dummy", CommandObjectSP(new CommandObjectMultiwordDummy(interp
)), true);
132 ASSERT_TRUE(error
.Success());
134 // Now pass the correct path, and make sure we get back the right MWC.
137 RunTest(interp
, "dummy subcommand", is_leaf
, success
, "dummy subcommand");
140 RunTest(interp
, "dummy subcommand", is_leaf
, success
, "dummy");
142 // If you tell us the last node is a leaf, we don't check that. Make sure
146 RunTest(interp
, "dummy subcommand leaf", is_leaf
, success
,
148 // But we should fail if we say the last component is a multiword:
152 RunTest(interp
, "dummy subcommand leaf", is_leaf
, success
,
153 "Path component: 'leaf' is not a container command");
155 // We should fail if we get the second path component wrong:
158 RunTest(interp
, "dummy not-subcommand", is_leaf
, success
,
159 "Path component: 'not-subcommand' not found");