brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.7 KiB · c32f3a7 Raw
119 lines · c
1//===-- TestBase.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "DAP.h"10#include "DAPLog.h"11#include "Protocol/ProtocolBase.h"12#include "TestingSupport/Host/JSONTransportTestUtilities.h"13#include "TestingSupport/SubsystemRAII.h"14#include "Transport.h"15#include "lldb/Host/FileSystem.h"16#include "lldb/Host/HostInfo.h"17#include "lldb/Host/MainLoop.h"18#include "lldb/Host/MainLoopBase.h"19#include "llvm/ADT/StringRef.h"20#include "llvm/Support/FileSystem.h"21#include "llvm/Support/JSON.h"22#include "gmock/gmock.h"23#include "gtest/gtest.h"24#include <memory>25#include <optional>26 27/// Helpers for gtest printing.28namespace lldb_dap::protocol {29 30inline void PrintTo(const Request &req, std::ostream *os) {31  *os << llvm::formatv("{0}", toJSON(req)).str();32}33 34inline void PrintTo(const Response &resp, std::ostream *os) {35  *os << llvm::formatv("{0}", toJSON(resp)).str();36}37 38inline void PrintTo(const Event &evt, std::ostream *os) {39  *os << llvm::formatv("{0}", toJSON(evt)).str();40}41 42inline void PrintTo(const Message &message, std::ostream *os) {43  return std::visit([os](auto &&message) { return PrintTo(message, os); },44                    message);45}46 47} // namespace lldb_dap::protocol48 49namespace lldb_dap_tests {50 51using TestDAPTransport = TestTransport<lldb_dap::ProtocolDescriptor>;52 53/// A base class for tests that need transport configured for communicating DAP54/// messages.55class TransportBase : public testing::Test {56protected:57  lldb_private::SubsystemRAII<lldb_private::FileSystem, lldb_private::HostInfo>58      subsystems;59  lldb_private::MainLoop loop;60  lldb_private::MainLoop::ReadHandleUP handles[2];61 62  std::unique_ptr<lldb_dap::Log> log;63 64  std::unique_ptr<TestDAPTransport> to_client;65  MockMessageHandler<lldb_dap::ProtocolDescriptor> client;66 67  std::unique_ptr<TestDAPTransport> to_server;68  std::unique_ptr<lldb_dap::DAP> dap;69 70  void SetUp() override;71 72  void Run();73};74 75/// A matcher for a DAP event.76template <typename EventMatcher, typename BodyMatcher>77inline testing::Matcher<const lldb_dap::protocol::Event &>78IsEvent(const EventMatcher &event_matcher, const BodyMatcher &body_matcher) {79  return testing::AllOf(80      testing::Field(&lldb_dap::protocol::Event::event, event_matcher),81      testing::Field(&lldb_dap::protocol::Event::body, body_matcher));82}83 84template <typename EventMatcher>85inline testing::Matcher<const lldb_dap::protocol::Event &>86IsEvent(const EventMatcher &event_matcher) {87  return testing::AllOf(88      testing::Field(&lldb_dap::protocol::Event::event, event_matcher),89      testing::Field(&lldb_dap::protocol::Event::body, std::nullopt));90}91 92/// Matches an "output" event.93inline auto Output(llvm::StringRef o, llvm::StringRef cat = "console") {94  return IsEvent("output",95                 testing::Optional(llvm::json::Value(96                     llvm::json::Object{{"category", cat}, {"output", o}})));97}98 99/// A base class for tests that interact with a `lldb_dap::DAP` instance.100class DAPTestBase : public TransportBase {101protected:102  std::optional<llvm::sys::fs::TempFile> core;103  std::optional<llvm::sys::fs::TempFile> binary;104 105  static constexpr llvm::StringLiteral k_linux_binary = "linux-x86_64.out.yaml";106  static constexpr llvm::StringLiteral k_linux_core = "linux-x86_64.core.yaml";107 108  static void SetUpTestSuite();109  static void TeatUpTestSuite();110  void SetUp() override;111  void TearDown() override;112 113  bool GetDebuggerSupportsTarget(llvm::StringRef platform);114  void CreateDebugger();115  void LoadCore();116};117 118} // namespace lldb_dap_tests119