1 | //===-- DisconnectTest.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 "Handler/RequestHandler.h" |
11 | #include "Protocol/ProtocolBase.h" |
12 | #include "TestBase.h" |
13 | #include "lldb/API/SBDefines.h" |
14 | #include "lldb/lldb-enumerations.h" |
15 | #include "llvm/Testing/Support/Error.h" |
16 | #include "gmock/gmock.h" |
17 | #include "gtest/gtest.h" |
18 | #include <memory> |
19 | #include <optional> |
20 | |
21 | using namespace llvm; |
22 | using namespace lldb; |
23 | using namespace lldb_dap; |
24 | using namespace lldb_dap_tests; |
25 | using namespace lldb_dap::protocol; |
26 | |
27 | class DisconnectRequestHandlerTest : public DAPTestBase {}; |
28 | |
29 | TEST_F(DisconnectRequestHandlerTest, DisconnectTriggersTerminated) { |
30 | DisconnectRequestHandler handler(*dap); |
31 | EXPECT_FALSE(dap->disconnecting); |
32 | ASSERT_THAT_ERROR(handler.Run(std::nullopt), Succeeded()); |
33 | EXPECT_TRUE(dap->disconnecting); |
34 | std::vector<Message> messages = DrainOutput(); |
35 | EXPECT_THAT(messages, |
36 | testing::Contains(testing::VariantWith<Event>(testing::FieldsAre( |
37 | /*event=*/"terminated" , /*body=*/testing::_)))); |
38 | } |
39 | |
40 | TEST_F(DisconnectRequestHandlerTest, DisconnectTriggersTerminateCommands) { |
41 | CreateDebugger(); |
42 | |
43 | if (!GetDebuggerSupportsTarget(platform: "X86" )) |
44 | GTEST_SKIP() << "Unsupported platform" ; |
45 | |
46 | LoadCore(); |
47 | |
48 | DisconnectRequestHandler handler(*dap); |
49 | |
50 | EXPECT_FALSE(dap->disconnecting); |
51 | dap->configuration.terminateCommands = {"?script print(1)" , |
52 | "script print(2)" }; |
53 | EXPECT_EQ(dap->target.GetProcess().GetState(), lldb::eStateStopped); |
54 | ASSERT_THAT_ERROR(handler.Run(std::nullopt), Succeeded()); |
55 | EXPECT_TRUE(dap->disconnecting); |
56 | std::vector<Message> messages = DrainOutput(); |
57 | EXPECT_THAT(messages, testing::ElementsAre( |
58 | OutputMatcher("Running terminateCommands:\n" ), |
59 | OutputMatcher("(lldb) script print(2)\n" ), |
60 | OutputMatcher("2\n" ), |
61 | testing::VariantWith<Event>(testing::FieldsAre( |
62 | /*event=*/"terminated" , /*body=*/testing::_)))); |
63 | } |
64 | |