Skip to content

Commit 32e9a56

Browse files
committed
[lldb-dap] Add the new test type.
1 parent c879be8 commit 32e9a56

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed

lldb/tools/lldb-dap/Protocol/ProtocolRequests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,22 @@ llvm::json::Value toJSON(const ModuleSymbolsResponseBody &DGMSR) {
625625
return result;
626626
}
627627

628+
bool fromJSON(const json::Value &Params, ExceptionInfoArguments &Args,
629+
json::Path Path) {
630+
json::ObjectMapper O(Params, Path);
631+
return O && O.map("threadId", Args.threadId);
632+
}
633+
634+
json::Value toJSON(const ExceptionInfoResponseBody &ERB) {
635+
json::Object result{{"exceptionId", ERB.exceptionId},
636+
{"breakMode", ERB.breakMode}};
637+
638+
if (!ERB.description.empty())
639+
result.insert({"description", ERB.description.c_str()});
640+
if (ERB.details.has_value())
641+
result.insert({"details", *ERB.details});
642+
643+
return result;
644+
}
645+
628646
} // namespace lldb_dap::protocol

lldb/unittests/DAP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_lldb_unittest(DAPTests
77
Handler/ContinueTest.cpp
88
JSONUtilsTest.cpp
99
LLDBUtilsTest.cpp
10+
ProtocolRequestsTest.cpp
1011
ProtocolTypesTest.cpp
1112
ProtocolUtilsTest.cpp
1213
TestBase.cpp
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===-- ProtocolRequestsTest.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 "Protocol/ProtocolRequests.h"
10+
#include "Protocol/ProtocolTypes.h"
11+
#include "TestingSupport/TestUtilities.h"
12+
#include "llvm/Testing/Support/Error.h"
13+
#include <gtest/gtest.h>
14+
15+
using namespace llvm;
16+
using namespace lldb_dap::protocol;
17+
using lldb_private::pprint;
18+
using llvm::json::parse;
19+
20+
TEST(ProtocolRequestsTest, ExceptionInfoArguments) {
21+
llvm::Expected<ExceptionInfoArguments> expected =
22+
parse<ExceptionInfoArguments>(R"({
23+
"threadId": 3434
24+
})");
25+
ASSERT_THAT_EXPECTED(expected, llvm::Succeeded());
26+
EXPECT_EQ(expected->threadId, 3434U);
27+
28+
// Check required keys;
29+
EXPECT_THAT_EXPECTED(parse<ExceptionInfoArguments>(R"({})"),
30+
FailedWithMessage("missing value at (root).threadId"));
31+
32+
EXPECT_THAT_EXPECTED(parse<ExceptionInfoArguments>(R"({"id": 10})"),
33+
FailedWithMessage("missing value at (root).threadId"));
34+
}
35+
36+
TEST(ProtocolRequestsTest, ExceptionInfoResponseBody) {
37+
ExceptionInfoResponseBody body;
38+
body.exceptionId = "signal";
39+
body.breakMode = eExceptionBreakModeAlways;
40+
41+
// Check required keys.
42+
Expected<json::Value> expected = parse(
43+
R"({
44+
"exceptionId": "signal",
45+
"breakMode": "always"
46+
})");
47+
48+
ASSERT_THAT_EXPECTED(expected, llvm::Succeeded());
49+
EXPECT_EQ(pprint(*expected), pprint(body));
50+
51+
// Check optional keys.
52+
body.description = "SIGNAL SIGWINCH";
53+
body.breakMode = eExceptionBreakModeNever;
54+
body.details = ExceptionDetails{};
55+
body.details->message = "some message";
56+
57+
Expected<json::Value> expected_opt = parse(
58+
R"({
59+
"exceptionId": "signal",
60+
"description": "SIGNAL SIGWINCH",
61+
"breakMode": "never",
62+
"details": {
63+
"message": "some message"
64+
}
65+
})");
66+
67+
ASSERT_THAT_EXPECTED(expected_opt, llvm::Succeeded());
68+
EXPECT_EQ(pprint(*expected_opt), pprint(body));
69+
}

lldb/unittests/DAP/ProtocolTypesTest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,3 +1126,51 @@ TEST(ProtocolTypesTest, DataBreakpointInfoArguments) {
11261126
EXPECT_THAT_EXPECTED(parse<DataBreakpointInfoArguments>(R"({"name":"data"})"),
11271127
llvm::Succeeded());
11281128
}
1129+
1130+
TEST(ProtocolTypesTest, ExceptionBreakMode) {
1131+
const std::vector<std::pair<ExceptionBreakMode, llvm::StringRef>> test_cases =
1132+
{{ExceptionBreakMode::eExceptionBreakModeAlways, "always"},
1133+
{ExceptionBreakMode::eExceptionBreakModeNever, "never"},
1134+
{ExceptionBreakMode::eExceptionBreakModeUnhandled, "unhandled"},
1135+
{ExceptionBreakMode::eExceptionBreakModeUserUnhandled, "userUnhandled"}};
1136+
1137+
for (const auto [value, expected] : test_cases) {
1138+
json::Value const serialized = toJSON(value);
1139+
ASSERT_EQ(serialized.kind(), llvm::json::Value::Kind::String);
1140+
EXPECT_EQ(serialized.getAsString(), expected);
1141+
1142+
}
1143+
}
1144+
1145+
TEST(ProtocolTypesTest, ExceptionDetails) {
1146+
ExceptionDetails details;
1147+
1148+
// Check required keys.
1149+
Expected<json::Value> expected = parse(R"({})");
1150+
ASSERT_THAT_EXPECTED(expected, llvm::Succeeded());
1151+
EXPECT_EQ(pp(*expected), pp(details));
1152+
1153+
// Check optional keys.
1154+
details.message = "SIGABRT exception";
1155+
details.typeName = "signal";
1156+
details.fullTypeName = "SIGABRT";
1157+
details.evaluateName = "process handle SIGABRT";
1158+
details.stackTrace = "some stacktrace";
1159+
ExceptionDetails inner_details;
1160+
inner_details.message = "inner message";
1161+
details.innerException = {std::move(inner_details)};
1162+
1163+
Expected<json::Value> expected_opt = parse(R"({
1164+
"message": "SIGABRT exception",
1165+
"typeName": "signal",
1166+
"fullTypeName": "SIGABRT",
1167+
"evaluateName": "process handle SIGABRT",
1168+
"stackTrace": "some stacktrace",
1169+
"innerException": [{
1170+
"message": "inner message"
1171+
}]
1172+
})");
1173+
1174+
ASSERT_THAT_EXPECTED(expected_opt, llvm::Succeeded());
1175+
EXPECT_EQ(pp(*expected_opt), pp(details));
1176+
}

lldb/unittests/TestingSupport/TestUtilities.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ using namespace lldb_private;
2020
extern const char *TestMainArgv0;
2121

2222
std::once_flag TestUtilities::g_debugger_initialize_flag;
23+
24+
std::string lldb_private::pprint(const llvm::json::Value &value) {
25+
return llvm::formatv("{0:2}", value).str();
26+
}
27+
2328
std::string lldb_private::GetInputFilePath(const llvm::Twine &name) {
2429
llvm::SmallString<128> result = llvm::sys::path::parent_path(TestMainArgv0);
2530
llvm::sys::fs::make_absolute(result);

lldb/unittests/TestingSupport/TestUtilities.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
}
3131

3232
namespace lldb_private {
33+
34+
/// Returns a pretty printed json string of a `llvm::json::Value`.
35+
std::string pprint(const llvm::json::Value &E);
36+
3337
std::string GetInputFilePath(const llvm::Twine &name);
3438

3539
class TestUtilities {

0 commit comments

Comments
 (0)