forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCommandPaths.cpp
160 lines (136 loc) · 5.32 KB
/
TestCommandPaths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//===-- ProcessEventDataTest.cpp ------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
#include "Plugins/Platform/MacOSX/PlatformRemoteMacOSX.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandObject.h"
#include "lldb/Interpreter/CommandObjectMultiword.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Utility/Args.h"
#include "lldb/Utility/Status.h"
#include "gtest/gtest.h"
using namespace lldb_private;
using namespace lldb_private::repro;
using namespace lldb;
namespace {
class VerifyUserMultiwordCmdPathTest : public ::testing::Test {
void SetUp() override {
FileSystem::Initialize();
HostInfo::Initialize();
PlatformMacOSX::Initialize();
}
void TearDown() override {
PlatformMacOSX::Terminate();
HostInfo::Terminate();
FileSystem::Terminate();
}
};
} // namespace
class CommandObjectLeaf : public CommandObjectParsed {
public:
CommandObjectLeaf(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "dummy subcommand leaf",
"Does nothing", "dummy subcommand leaf") {
SetIsUserCommand(true);
}
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
result.SetStatus(eReturnStatusSuccessFinishResult);
result.AppendMessage("I did nothing");
}
};
class CommandObjectMultiwordSubDummy : public CommandObjectMultiword {
public:
CommandObjectMultiwordSubDummy(CommandInterpreter &interpreter)
: CommandObjectMultiword(interpreter, "dummy subcommand", "Does nothing",
"dummy subcommand") {
SetIsUserCommand(true);
LoadSubCommand("leaf", CommandObjectSP(new CommandObjectLeaf(interpreter)));
}
~CommandObjectMultiwordSubDummy() override = default;
};
class CommandObjectMultiwordDummy : public CommandObjectMultiword {
public:
CommandObjectMultiwordDummy(CommandInterpreter &interpreter)
: CommandObjectMultiword(interpreter, "dummy", "Does nothing", "dummy") {
SetIsUserCommand(true);
LoadSubCommand(
"subcommand",
CommandObjectSP(new CommandObjectMultiwordSubDummy(interpreter)));
}
~CommandObjectMultiwordDummy() override = default;
};
// Pass in the command path to args. If success is true, we make sure the MWC
// returned matches the test string. If success is false, we make sure the
// lookup error matches test_str.
void RunTest(CommandInterpreter &interp, const char *args, bool is_leaf,
bool success, const char *test_str) {
CommandObjectMultiword *multi_word_cmd = nullptr;
Args test_args(args);
Status error;
multi_word_cmd =
interp.VerifyUserMultiwordCmdPath(test_args, is_leaf, error);
if (success) {
ASSERT_NE(multi_word_cmd, nullptr);
ASSERT_TRUE(error.Success());
ASSERT_STREQ(multi_word_cmd->GetCommandName().str().c_str(), test_str);
} else {
ASSERT_EQ(multi_word_cmd, nullptr);
ASSERT_TRUE(error.Fail());
ASSERT_STREQ(error.AsCString(), test_str);
}
}
TEST_F(VerifyUserMultiwordCmdPathTest, TestErrors) {
ArchSpec arch("x86_64-apple-macosx-");
Platform::SetHostPlatform(PlatformRemoteMacOSX::CreateInstance(true, &arch));
DebuggerSP debugger_sp = Debugger::CreateInstance();
ASSERT_TRUE(debugger_sp);
CommandInterpreter &interp = debugger_sp->GetCommandInterpreter();
Status error;
bool success;
bool is_leaf;
// Test that we reject non-user path components:
success = false;
is_leaf = true;
RunTest(interp, "process launch", is_leaf, success,
"Path component: 'process' is not a user command");
// Test that we reject non-existent commands:
is_leaf = true;
success = false;
RunTest(interp, "wewouldnevernameacommandthis subcommand", is_leaf, success,
"Path component: 'wewouldnevernameacommandthis' not found");
// Now we have to add a multiword command, and then probe it.
error = interp.AddUserCommand(
"dummy", CommandObjectSP(new CommandObjectMultiwordDummy(interp)), true);
ASSERT_TRUE(error.Success());
// Now pass the correct path, and make sure we get back the right MWC.
is_leaf = false;
success = true;
RunTest(interp, "dummy subcommand", is_leaf, success, "dummy subcommand");
is_leaf = true;
RunTest(interp, "dummy subcommand", is_leaf, success, "dummy");
// If you tell us the last node is a leaf, we don't check that. Make sure
// that is true:
is_leaf = true;
success = true;
RunTest(interp, "dummy subcommand leaf", is_leaf, success,
"dummy subcommand");
// But we should fail if we say the last component is a multiword:
is_leaf = false;
success = false;
RunTest(interp, "dummy subcommand leaf", is_leaf, success,
"Path component: 'leaf' is not a container command");
// We should fail if we get the second path component wrong:
is_leaf = false;
success = false;
RunTest(interp, "dummy not-subcommand", is_leaf, success,
"Path component: 'not-subcommand' not found");
}