Skip to content

Commit 6f947e3

Browse files
committed
[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.
1 parent 4ac8e90 commit 6f947e3

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

lldb/tools/lldb-dap/DAP.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ struct DAP {
226226
/// \param[in] default_repl_mode
227227
/// Default repl mode behavior, as configured by the binary.
228228
/// \param[in] pre_init_commands
229-
/// LLDB commands to execute as soon as the debugger instance is allocaed.
229+
/// LLDB commands to execute as soon as the debugger instance is
230+
/// allocated.
230231
/// \param[in] transport
231232
/// Transport for this debug session.
232233
DAP(Log *log, const ReplMode default_repl_mode,

lldb/unittests/DAP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_lldb_unittest(DAPTests
33
LLDBUtilsTest.cpp
44
TransportTest.cpp
55
ProtocolTypesTest.cpp
6+
DAPTest.cpp
67

78
LINK_LIBS
89
lldbDAP

lldb/unittests/DAP/DAPTest.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===-- DAPTest.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 "DAP.h"
10+
#include "Protocol/ProtocolBase.h"
11+
#include "Transport.h"
12+
#include "lldb/Host/File.h"
13+
#include "lldb/Host/Pipe.h"
14+
#include "llvm/Testing/Support/Error.h"
15+
#include "gtest/gtest.h"
16+
#include <chrono>
17+
#include <memory>
18+
#include <optional>
19+
20+
using namespace llvm;
21+
using namespace lldb;
22+
using namespace lldb_dap;
23+
using namespace lldb_dap::protocol;
24+
using lldb_private::File;
25+
using lldb_private::NativeFile;
26+
using lldb_private::Pipe;
27+
28+
class DAPTest : public testing::Test {
29+
protected:
30+
Pipe input;
31+
Pipe output;
32+
std::unique_ptr<Transport> toDAP;
33+
std::unique_ptr<Transport> fromDAP;
34+
35+
void SetUp() override {
36+
ASSERT_THAT_ERROR(input.CreateNew(false).ToError(), Succeeded());
37+
ASSERT_THAT_ERROR(output.CreateNew(false).ToError(), Succeeded());
38+
toDAP = std::make_unique<Transport>(
39+
"toDAP", nullptr,
40+
std::make_shared<NativeFile>(input.GetReadFileDescriptor(),
41+
File::eOpenOptionReadOnly,
42+
NativeFile::Unowned),
43+
std::make_shared<NativeFile>(output.GetWriteFileDescriptor(),
44+
File::eOpenOptionWriteOnly,
45+
NativeFile::Unowned));
46+
fromDAP = std::make_unique<Transport>(
47+
"fromDAP", nullptr,
48+
std::make_shared<NativeFile>(output.GetReadFileDescriptor(),
49+
File::eOpenOptionReadOnly,
50+
NativeFile::Unowned),
51+
std::make_shared<NativeFile>(input.GetWriteFileDescriptor(),
52+
File::eOpenOptionWriteOnly,
53+
NativeFile::Unowned));
54+
}
55+
};
56+
57+
TEST_F(DAPTest, SendProtocolMessages) {
58+
DAP dap{nullptr, ReplMode::Auto, {}, *toDAP};
59+
dap.Send(Event{"my-event", std::nullopt});
60+
ASSERT_THAT_EXPECTED(fromDAP->Read(std::chrono::milliseconds(1)),
61+
HasValue(testing::VariantWith<Event>(testing::FieldsAre(
62+
/*event=*/"my-event", /*body=*/std::nullopt))));
63+
}

0 commit comments

Comments
 (0)