Skip to content

Commit 8bec5e5

Browse files
authored
[lldb-dap] Add unit tests for protocol types (#139502)
Add unit tests for serializing and deserializing protocol types.
1 parent 608c85c commit 8bec5e5

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ llvm::json::Value toJSON(const Source &S) {
6969
return result;
7070
}
7171

72+
bool fromJSON(const llvm::json::Value &Params, ExceptionBreakpointsFilter &EBF,
73+
llvm::json::Path P) {
74+
json::ObjectMapper O(Params, P);
75+
return O && O.map("filter", EBF.filter) && O.map("label", EBF.label) &&
76+
O.mapOptional("description", EBF.description) &&
77+
O.mapOptional("default", EBF.defaultState) &&
78+
O.mapOptional("supportsCondition", EBF.supportsCondition) &&
79+
O.mapOptional("conditionDescription", EBF.conditionDescription);
80+
}
81+
7282
json::Value toJSON(const ExceptionBreakpointsFilter &EBF) {
7383
json::Object result{{"filter", EBF.filter}, {"label", EBF.label}};
7484

lldb/tools/lldb-dap/Protocol/ProtocolTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct ExceptionBreakpointsFilter {
5555
/// shown as the placeholder text for a text box and can be translated.
5656
std::optional<std::string> conditionDescription;
5757
};
58+
bool fromJSON(const llvm::json::Value &, ExceptionBreakpointsFilter &,
59+
llvm::json::Path);
5860
llvm::json::Value toJSON(const ExceptionBreakpointsFilter &);
5961

6062
enum ColumnType : unsigned {

lldb/unittests/DAP/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
add_lldb_unittest(DAPTests
22
JSONUtilsTest.cpp
33
LLDBUtilsTest.cpp
4+
ProtocolTypesTest.cpp
45

56
LINK_LIBS
67
lldbDAP
8+
LLVMTestingSupport
79
LINK_COMPONENTS
810
Support
911
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===-- ProtocolTypesTest.cpp -----------------------------------*- C++ -*-===//
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/ProtocolTypes.h"
10+
#include "llvm/Testing/Support/Error.h"
11+
#include "gtest/gtest.h"
12+
13+
using namespace lldb;
14+
using namespace lldb_dap;
15+
using namespace lldb_dap::protocol;
16+
17+
template <typename T> static llvm::Expected<T> roundtrip(const T &input) {
18+
llvm::json::Value value = toJSON(input);
19+
llvm::json::Path::Root root;
20+
T output;
21+
if (!fromJSON(value, output, root))
22+
return root.getError();
23+
return output;
24+
}
25+
26+
TEST(ProtocolTypesTest, ExceptionBreakpointsFilter) {
27+
ExceptionBreakpointsFilter filter;
28+
filter.filter = "testFilter";
29+
filter.label = "Test Filter";
30+
filter.description = "This is a test filter";
31+
filter.defaultState = true;
32+
filter.supportsCondition = true;
33+
filter.conditionDescription = "Condition for test filter";
34+
35+
llvm::Expected<ExceptionBreakpointsFilter> deserialized_filter =
36+
roundtrip(filter);
37+
ASSERT_THAT_EXPECTED(deserialized_filter, llvm::Succeeded());
38+
39+
EXPECT_EQ(filter.filter, deserialized_filter->filter);
40+
EXPECT_EQ(filter.label, deserialized_filter->label);
41+
EXPECT_EQ(filter.description, deserialized_filter->description);
42+
EXPECT_EQ(filter.defaultState, deserialized_filter->defaultState);
43+
EXPECT_EQ(filter.supportsCondition, deserialized_filter->supportsCondition);
44+
EXPECT_EQ(filter.conditionDescription,
45+
deserialized_filter->conditionDescription);
46+
}
47+
48+
TEST(ProtocolTypesTest, Source) {
49+
Source source;
50+
source.name = "testName";
51+
source.path = "/path/to/source";
52+
source.sourceReference = 12345;
53+
source.presentationHint = ePresentationHintEmphasize;
54+
55+
llvm::Expected<Source> deserialized_source = roundtrip(source);
56+
ASSERT_THAT_EXPECTED(deserialized_source, llvm::Succeeded());
57+
58+
EXPECT_EQ(source.name, deserialized_source->name);
59+
EXPECT_EQ(source.path, deserialized_source->path);
60+
EXPECT_EQ(source.sourceReference, deserialized_source->sourceReference);
61+
EXPECT_EQ(source.presentationHint, deserialized_source->presentationHint);
62+
}

0 commit comments

Comments
 (0)