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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "DAP.h" |
10 | #include "Protocol/ProtocolBase.h" |
11 | #include "Transport.h" |
12 | #include "lldb/Host/Pipe.h" |
13 | #include "llvm/ADT/StringRef.h" |
14 | #include "gmock/gmock.h" |
15 | #include "gtest/gtest.h" |
16 | |
17 | namespace lldb_dap_tests { |
18 | |
19 | /// A base class for tests that need a pair of pipes for communication. |
20 | class PipeBase : public testing::Test { |
21 | protected: |
22 | lldb_private::Pipe input; |
23 | lldb_private::Pipe output; |
24 | |
25 | void SetUp() override; |
26 | }; |
27 | |
28 | /// A base class for tests that need transport configured for communicating DAP |
29 | /// messages. |
30 | class TransportBase : public PipeBase { |
31 | protected: |
32 | std::unique_ptr<lldb_dap::Transport> to_dap; |
33 | std::unique_ptr<lldb_dap::Transport> from_dap; |
34 | |
35 | void SetUp() override; |
36 | }; |
37 | |
38 | /// Matches an "output" event. |
39 | inline auto OutputMatcher(const llvm::StringRef output, |
40 | const llvm::StringRef category = "console" ) { |
41 | return testing::VariantWith<lldb_dap::protocol::Event>(matcher: testing::FieldsAre( |
42 | /*event=*/matchers: "output" , /*body=*/matchers: testing::Optional<llvm::json::Value>( |
43 | value_matcher: llvm::json::Object{{.K: "category" , .V: category}, {.K: "output" , .V: output}}))); |
44 | } |
45 | |
46 | /// A base class for tests that interact with a `lldb_dap::DAP` instance. |
47 | class DAPTestBase : public TransportBase { |
48 | protected: |
49 | std::unique_ptr<lldb_dap::DAP> dap; |
50 | std::optional<llvm::sys::fs::TempFile> core; |
51 | std::optional<llvm::sys::fs::TempFile> binary; |
52 | |
53 | static constexpr llvm::StringLiteral k_linux_binary = "linux-x86_64.out.yaml" ; |
54 | static constexpr llvm::StringLiteral k_linux_core = "linux-x86_64.core.yaml" ; |
55 | |
56 | static void SetUpTestSuite(); |
57 | static void TeatUpTestSuite(); |
58 | void SetUp() override; |
59 | void TearDown() override; |
60 | |
61 | bool GetDebuggerSupportsTarget(llvm::StringRef platform); |
62 | void CreateDebugger(); |
63 | void LoadCore(); |
64 | |
65 | /// Closes the DAP output pipe and returns the remaining protocol messages in |
66 | /// the buffer. |
67 | std::vector<lldb_dap::protocol::Message> DrainOutput(); |
68 | }; |
69 | |
70 | } // namespace lldb_dap_tests |
71 | |