https://github.com/ashgti updated https://github.com/llvm/llvm-project/pull/139937
>From 6f947e38ad4f744754cf13c1094c4e5e3fd249b6 Mon Sep 17 00:00:00 2001 From: John Harrison <harj...@google.com> Date: Wed, 14 May 2025 10:31:40 -0700 Subject: [PATCH 1/3] [lldb-dap] Setup DAP for unit testing. This is a very simple case that currently only validates we can create a DAP instance and send a message over the transport layer. More in-depth tests will require additional helpers and possibly refactors of DAP to make it more testable, however this is some ground work to have basic support for unit tests. --- lldb/tools/lldb-dap/DAP.h | 3 +- lldb/unittests/DAP/CMakeLists.txt | 1 + lldb/unittests/DAP/DAPTest.cpp | 63 +++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 lldb/unittests/DAP/DAPTest.cpp diff --git a/lldb/tools/lldb-dap/DAP.h b/lldb/tools/lldb-dap/DAP.h index c2e4c2dea582e..2ff66d1cd0182 100644 --- a/lldb/tools/lldb-dap/DAP.h +++ b/lldb/tools/lldb-dap/DAP.h @@ -226,7 +226,8 @@ struct DAP { /// \param[in] default_repl_mode /// Default repl mode behavior, as configured by the binary. /// \param[in] pre_init_commands - /// LLDB commands to execute as soon as the debugger instance is allocaed. + /// LLDB commands to execute as soon as the debugger instance is + /// allocated. /// \param[in] transport /// Transport for this debug session. DAP(Log *log, const ReplMode default_repl_mode, diff --git a/lldb/unittests/DAP/CMakeLists.txt b/lldb/unittests/DAP/CMakeLists.txt index 110733e93b192..6074e9b872c49 100644 --- a/lldb/unittests/DAP/CMakeLists.txt +++ b/lldb/unittests/DAP/CMakeLists.txt @@ -3,6 +3,7 @@ add_lldb_unittest(DAPTests LLDBUtilsTest.cpp TransportTest.cpp ProtocolTypesTest.cpp + DAPTest.cpp LINK_LIBS lldbDAP diff --git a/lldb/unittests/DAP/DAPTest.cpp b/lldb/unittests/DAP/DAPTest.cpp new file mode 100644 index 0000000000000..9d2a9b944678e --- /dev/null +++ b/lldb/unittests/DAP/DAPTest.cpp @@ -0,0 +1,63 @@ +//===-- DAPTest.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 "DAP.h" +#include "Protocol/ProtocolBase.h" +#include "Transport.h" +#include "lldb/Host/File.h" +#include "lldb/Host/Pipe.h" +#include "llvm/Testing/Support/Error.h" +#include "gtest/gtest.h" +#include <chrono> +#include <memory> +#include <optional> + +using namespace llvm; +using namespace lldb; +using namespace lldb_dap; +using namespace lldb_dap::protocol; +using lldb_private::File; +using lldb_private::NativeFile; +using lldb_private::Pipe; + +class DAPTest : public testing::Test { +protected: + Pipe input; + Pipe output; + std::unique_ptr<Transport> toDAP; + std::unique_ptr<Transport> fromDAP; + + void SetUp() override { + ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), Succeeded()); + ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), Succeeded()); + toDAP = std::make_unique<Transport>( + "toDAP", nullptr, + std::make_shared<NativeFile>(input.GetReadFileDescriptor(), + File::eOpenOptionReadOnly, + NativeFile::Unowned), + std::make_shared<NativeFile>(output.GetWriteFileDescriptor(), + File::eOpenOptionWriteOnly, + NativeFile::Unowned)); + fromDAP = std::make_unique<Transport>( + "fromDAP", nullptr, + std::make_shared<NativeFile>(output.GetReadFileDescriptor(), + File::eOpenOptionReadOnly, + NativeFile::Unowned), + std::make_shared<NativeFile>(input.GetWriteFileDescriptor(), + File::eOpenOptionWriteOnly, + NativeFile::Unowned)); + } +}; + +TEST_F(DAPTest, SendProtocolMessages) { + DAP dap{nullptr, ReplMode::Auto, {}, *toDAP}; + dap.Send(Event{"my-event", std::nullopt}); + ASSERT_THAT_EXPECTED(fromDAP->Read(std::chrono::milliseconds(1)), + HasValue(testing::VariantWith<Event>(testing::FieldsAre( + /*event=*/"my-event", /*body=*/std::nullopt)))); +} >From 8bc4880a338dd0d0f2daa64b77c8bdbf567270b2 Mon Sep 17 00:00:00 2001 From: John Harrison <harj...@google.com> Date: Wed, 14 May 2025 10:46:51 -0700 Subject: [PATCH 2/3] Adjusting the naming of variables. --- lldb/unittests/DAP/DAPTest.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lldb/unittests/DAP/DAPTest.cpp b/lldb/unittests/DAP/DAPTest.cpp index 9d2a9b944678e..8e8c2ea3ccbb0 100644 --- a/lldb/unittests/DAP/DAPTest.cpp +++ b/lldb/unittests/DAP/DAPTest.cpp @@ -29,22 +29,22 @@ class DAPTest : public testing::Test { protected: Pipe input; Pipe output; - std::unique_ptr<Transport> toDAP; - std::unique_ptr<Transport> fromDAP; + std::unique_ptr<Transport> to_dap; + std::unique_ptr<Transport> from_dap; void SetUp() override { ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), Succeeded()); ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), Succeeded()); - toDAP = std::make_unique<Transport>( - "toDAP", nullptr, + to_dap = std::make_unique<Transport>( + "to_dap", nullptr, std::make_shared<NativeFile>(input.GetReadFileDescriptor(), File::eOpenOptionReadOnly, NativeFile::Unowned), std::make_shared<NativeFile>(output.GetWriteFileDescriptor(), File::eOpenOptionWriteOnly, NativeFile::Unowned)); - fromDAP = std::make_unique<Transport>( - "fromDAP", nullptr, + from_dap = std::make_unique<Transport>( + "from_dap", nullptr, std::make_shared<NativeFile>(output.GetReadFileDescriptor(), File::eOpenOptionReadOnly, NativeFile::Unowned), @@ -55,9 +55,9 @@ class DAPTest : public testing::Test { }; TEST_F(DAPTest, SendProtocolMessages) { - DAP dap{nullptr, ReplMode::Auto, {}, *toDAP}; + DAP dap{nullptr, ReplMode::Auto, {}, *to_dap}; dap.Send(Event{"my-event", std::nullopt}); - ASSERT_THAT_EXPECTED(fromDAP->Read(std::chrono::milliseconds(1)), + ASSERT_THAT_EXPECTED(from_dap->Read(std::chrono::milliseconds(1)), HasValue(testing::VariantWith<Event>(testing::FieldsAre( /*event=*/"my-event", /*body=*/std::nullopt)))); } >From 41f8f41a6cae98c6e0fe703f5b34f9f3f4acba9a Mon Sep 17 00:00:00 2001 From: John Harrison <harj...@google.com> Date: Wed, 14 May 2025 10:51:12 -0700 Subject: [PATCH 3/3] Adding inline comments to improve readability. --- lldb/unittests/DAP/DAPTest.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lldb/unittests/DAP/DAPTest.cpp b/lldb/unittests/DAP/DAPTest.cpp index 8e8c2ea3ccbb0..e23fb32eebea9 100644 --- a/lldb/unittests/DAP/DAPTest.cpp +++ b/lldb/unittests/DAP/DAPTest.cpp @@ -55,8 +55,9 @@ class DAPTest : public testing::Test { }; TEST_F(DAPTest, SendProtocolMessages) { - DAP dap{nullptr, ReplMode::Auto, {}, *to_dap}; - dap.Send(Event{"my-event", std::nullopt}); + DAP dap{/*log=*/nullptr, /*default_repl_mode=*/ReplMode::Auto, + /*pre_init_commands=*/{}, /*transport=*/*to_dap}; + dap.Send(Event{/*event=*/"my-event", /*body=*/std::nullopt}); ASSERT_THAT_EXPECTED(from_dap->Read(std::chrono::milliseconds(1)), HasValue(testing::VariantWith<Event>(testing::FieldsAre( /*event=*/"my-event", /*body=*/std::nullopt)))); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits