diff options
Diffstat (limited to 'lldb/unittests/DAP/TestBase.h')
-rw-r--r-- | lldb/unittests/DAP/TestBase.h | 123 |
1 files changed, 52 insertions, 71 deletions
diff --git a/lldb/unittests/DAP/TestBase.h b/lldb/unittests/DAP/TestBase.h index c19eead..c32f3a7 100644 --- a/lldb/unittests/DAP/TestBase.h +++ b/lldb/unittests/DAP/TestBase.h @@ -7,73 +7,48 @@ //===----------------------------------------------------------------------===// #include "DAP.h" +#include "DAPLog.h" #include "Protocol/ProtocolBase.h" #include "TestingSupport/Host/JSONTransportTestUtilities.h" #include "TestingSupport/SubsystemRAII.h" +#include "Transport.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/MainLoop.h" #include "lldb/Host/MainLoopBase.h" -#include "lldb/lldb-forward.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/JSON.h" -#include "llvm/Testing/Support/Error.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include <memory> +#include <optional> + +/// Helpers for gtest printing. +namespace lldb_dap::protocol { + +inline void PrintTo(const Request &req, std::ostream *os) { + *os << llvm::formatv("{0}", toJSON(req)).str(); +} + +inline void PrintTo(const Response &resp, std::ostream *os) { + *os << llvm::formatv("{0}", toJSON(resp)).str(); +} + +inline void PrintTo(const Event &evt, std::ostream *os) { + *os << llvm::formatv("{0}", toJSON(evt)).str(); +} + +inline void PrintTo(const Message &message, std::ostream *os) { + return std::visit([os](auto &&message) { return PrintTo(message, os); }, + message); +} + +} // namespace lldb_dap::protocol namespace lldb_dap_tests { -class TestTransport final - : public lldb_private::Transport<lldb_dap::protocol::Request, - lldb_dap::protocol::Response, - lldb_dap::protocol::Event> { -public: - using Message = lldb_private::Transport<lldb_dap::protocol::Request, - lldb_dap::protocol::Response, - lldb_dap::protocol::Event>::Message; - - TestTransport(lldb_private::MainLoop &loop, MessageHandler &handler) - : m_loop(loop), m_handler(handler) {} - - llvm::Error Send(const lldb_dap::protocol::Event &e) override { - m_loop.AddPendingCallback([this, e](lldb_private::MainLoopBase &) { - this->m_handler.Received(e); - }); - return llvm::Error::success(); - } - - llvm::Error Send(const lldb_dap::protocol::Request &r) override { - m_loop.AddPendingCallback([this, r](lldb_private::MainLoopBase &) { - this->m_handler.Received(r); - }); - return llvm::Error::success(); - } - - llvm::Error Send(const lldb_dap::protocol::Response &r) override { - m_loop.AddPendingCallback([this, r](lldb_private::MainLoopBase &) { - this->m_handler.Received(r); - }); - return llvm::Error::success(); - } - - llvm::Expected<lldb_private::MainLoop::ReadHandleUP> - RegisterMessageHandler(lldb_private::MainLoop &loop, - MessageHandler &handler) override; - - void Log(llvm::StringRef message) override { - log_messages.emplace_back(message); - } - - std::vector<std::string> log_messages; - -private: - lldb_private::MainLoop &m_loop; - MessageHandler &m_handler; - lldb::FileSP m_dummy_file; -}; +using TestDAPTransport = TestTransport<lldb_dap::ProtocolDescriptor>; /// A base class for tests that need transport configured for communicating DAP /// messages. @@ -82,22 +57,36 @@ protected: lldb_private::SubsystemRAII<lldb_private::FileSystem, lldb_private::HostInfo> subsystems; lldb_private::MainLoop loop; - std::unique_ptr<TestTransport> transport; - MockMessageHandler<lldb_dap::protocol::Request, lldb_dap::protocol::Response, - lldb_dap::protocol::Event> - client; - - void SetUp() override { - transport = std::make_unique<TestTransport>(loop, client); - } + lldb_private::MainLoop::ReadHandleUP handles[2]; + + std::unique_ptr<lldb_dap::Log> log; + + std::unique_ptr<TestDAPTransport> to_client; + MockMessageHandler<lldb_dap::ProtocolDescriptor> client; + + std::unique_ptr<TestDAPTransport> to_server; + std::unique_ptr<lldb_dap::DAP> dap; + + void SetUp() override; + + void Run(); }; /// A matcher for a DAP event. -template <typename M1, typename M2> +template <typename EventMatcher, typename BodyMatcher> inline testing::Matcher<const lldb_dap::protocol::Event &> -IsEvent(const M1 &m1, const M2 &m2) { - return testing::AllOf(testing::Field(&lldb_dap::protocol::Event::event, m1), - testing::Field(&lldb_dap::protocol::Event::body, m2)); +IsEvent(const EventMatcher &event_matcher, const BodyMatcher &body_matcher) { + return testing::AllOf( + testing::Field(&lldb_dap::protocol::Event::event, event_matcher), + testing::Field(&lldb_dap::protocol::Event::body, body_matcher)); +} + +template <typename EventMatcher> +inline testing::Matcher<const lldb_dap::protocol::Event &> +IsEvent(const EventMatcher &event_matcher) { + return testing::AllOf( + testing::Field(&lldb_dap::protocol::Event::event, event_matcher), + testing::Field(&lldb_dap::protocol::Event::body, std::nullopt)); } /// Matches an "output" event. @@ -110,8 +99,6 @@ inline auto Output(llvm::StringRef o, llvm::StringRef cat = "console") { /// A base class for tests that interact with a `lldb_dap::DAP` instance. class DAPTestBase : public TransportBase { protected: - std::unique_ptr<lldb_dap::Log> log; - std::unique_ptr<lldb_dap::DAP> dap; std::optional<llvm::sys::fs::TempFile> core; std::optional<llvm::sys::fs::TempFile> binary; @@ -126,12 +113,6 @@ protected: bool GetDebuggerSupportsTarget(llvm::StringRef platform); void CreateDebugger(); void LoadCore(); - - void RunOnce() { - loop.AddPendingCallback( - [](lldb_private::MainLoopBase &loop) { loop.RequestTermination(); }); - ASSERT_THAT_ERROR(dap->Loop(), llvm::Succeeded()); - } }; } // namespace lldb_dap_tests |