Author: Jonas Devlieghere
Date: 2025-05-12T13:23:50-07:00
New Revision: 8bec5e5b88de6cfa7cd979b73eafdc2ba69ee053

URL: 
https://github.com/llvm/llvm-project/commit/8bec5e5b88de6cfa7cd979b73eafdc2ba69ee053
DIFF: 
https://github.com/llvm/llvm-project/commit/8bec5e5b88de6cfa7cd979b73eafdc2ba69ee053.diff

LOG: [lldb-dap] Add unit tests for protocol types (#139502)

Add unit tests for serializing and deserializing protocol types.

Added: 
    lldb/unittests/DAP/ProtocolTypesTest.cpp

Modified: 
    lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp
    lldb/tools/lldb-dap/Protocol/ProtocolTypes.h
    lldb/unittests/DAP/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp 
b/lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp
index 2b7419916268b..c9cab350f9f12 100644
--- a/lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp
+++ b/lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp
@@ -69,6 +69,16 @@ llvm::json::Value toJSON(const Source &S) {
   return result;
 }
 
+bool fromJSON(const llvm::json::Value &Params, ExceptionBreakpointsFilter &EBF,
+              llvm::json::Path P) {
+  json::ObjectMapper O(Params, P);
+  return O && O.map("filter", EBF.filter) && O.map("label", EBF.label) &&
+         O.mapOptional("description", EBF.description) &&
+         O.mapOptional("default", EBF.defaultState) &&
+         O.mapOptional("supportsCondition", EBF.supportsCondition) &&
+         O.mapOptional("conditionDescription", EBF.conditionDescription);
+}
+
 json::Value toJSON(const ExceptionBreakpointsFilter &EBF) {
   json::Object result{{"filter", EBF.filter}, {"label", EBF.label}};
 

diff  --git a/lldb/tools/lldb-dap/Protocol/ProtocolTypes.h 
b/lldb/tools/lldb-dap/Protocol/ProtocolTypes.h
index 1f0cb1e0b2d41..d1e86b0897675 100644
--- a/lldb/tools/lldb-dap/Protocol/ProtocolTypes.h
+++ b/lldb/tools/lldb-dap/Protocol/ProtocolTypes.h
@@ -55,6 +55,8 @@ struct ExceptionBreakpointsFilter {
   /// shown as the placeholder text for a text box and can be translated.
   std::optional<std::string> conditionDescription;
 };
+bool fromJSON(const llvm::json::Value &, ExceptionBreakpointsFilter &,
+              llvm::json::Path);
 llvm::json::Value toJSON(const ExceptionBreakpointsFilter &);
 
 enum ColumnType : unsigned {

diff  --git a/lldb/unittests/DAP/CMakeLists.txt 
b/lldb/unittests/DAP/CMakeLists.txt
index 4bbb552be9f34..8b240654046e2 100644
--- a/lldb/unittests/DAP/CMakeLists.txt
+++ b/lldb/unittests/DAP/CMakeLists.txt
@@ -1,9 +1,11 @@
 add_lldb_unittest(DAPTests
   JSONUtilsTest.cpp
   LLDBUtilsTest.cpp
+  ProtocolTypesTest.cpp
 
   LINK_LIBS
     lldbDAP
+    LLVMTestingSupport
   LINK_COMPONENTS
     Support
   )

diff  --git a/lldb/unittests/DAP/ProtocolTypesTest.cpp 
b/lldb/unittests/DAP/ProtocolTypesTest.cpp
new file mode 100644
index 0000000000000..fa46816ca4a10
--- /dev/null
+++ b/lldb/unittests/DAP/ProtocolTypesTest.cpp
@@ -0,0 +1,62 @@
+//===-- ProtocolTypesTest.cpp -----------------------------------*- C++ 
-*-===//
+//
+// 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 "Protocol/ProtocolTypes.h"
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+
+using namespace lldb;
+using namespace lldb_dap;
+using namespace lldb_dap::protocol;
+
+template <typename T> static llvm::Expected<T> roundtrip(const T &input) {
+  llvm::json::Value value = toJSON(input);
+  llvm::json::Path::Root root;
+  T output;
+  if (!fromJSON(value, output, root))
+    return root.getError();
+  return output;
+}
+
+TEST(ProtocolTypesTest, ExceptionBreakpointsFilter) {
+  ExceptionBreakpointsFilter filter;
+  filter.filter = "testFilter";
+  filter.label = "Test Filter";
+  filter.description = "This is a test filter";
+  filter.defaultState = true;
+  filter.supportsCondition = true;
+  filter.conditionDescription = "Condition for test filter";
+
+  llvm::Expected<ExceptionBreakpointsFilter> deserialized_filter =
+      roundtrip(filter);
+  ASSERT_THAT_EXPECTED(deserialized_filter, llvm::Succeeded());
+
+  EXPECT_EQ(filter.filter, deserialized_filter->filter);
+  EXPECT_EQ(filter.label, deserialized_filter->label);
+  EXPECT_EQ(filter.description, deserialized_filter->description);
+  EXPECT_EQ(filter.defaultState, deserialized_filter->defaultState);
+  EXPECT_EQ(filter.supportsCondition, deserialized_filter->supportsCondition);
+  EXPECT_EQ(filter.conditionDescription,
+            deserialized_filter->conditionDescription);
+}
+
+TEST(ProtocolTypesTest, Source) {
+  Source source;
+  source.name = "testName";
+  source.path = "/path/to/source";
+  source.sourceReference = 12345;
+  source.presentationHint = ePresentationHintEmphasize;
+
+  llvm::Expected<Source> deserialized_source = roundtrip(source);
+  ASSERT_THAT_EXPECTED(deserialized_source, llvm::Succeeded());
+
+  EXPECT_EQ(source.name, deserialized_source->name);
+  EXPECT_EQ(source.path, deserialized_source->path);
+  EXPECT_EQ(source.sourceReference, deserialized_source->sourceReference);
+  EXPECT_EQ(source.presentationHint, deserialized_source->presentationHint);
+}


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to