| 1 | //===-- SBCommandInterpreterTest.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 | // Use the umbrella header for -Wdocumentation. |
| 10 | #include "lldb/API/LLDB.h" |
| 11 | |
| 12 | #include "TestingSupport/SubsystemRAII.h" |
| 13 | #include "lldb/API/SBDebugger.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | #include <cstring> |
| 16 | #include <string> |
| 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
| 21 | class SBCommandInterpreterTest : public testing::Test { |
| 22 | protected: |
| 23 | void SetUp() override { |
| 24 | debugger = SBDebugger::Create(/*source_init_files=*/false); |
| 25 | } |
| 26 | |
| 27 | void TearDown() override { SBDebugger::Destroy(debugger); } |
| 28 | |
| 29 | SubsystemRAII<lldb::SBDebugger> subsystems; |
| 30 | SBDebugger debugger; |
| 31 | }; |
| 32 | |
| 33 | class DummyCommand : public SBCommandPluginInterface { |
| 34 | public: |
| 35 | DummyCommand(const char *message) : m_message(message) {} |
| 36 | |
| 37 | bool DoExecute(SBDebugger dbg, char **command, |
| 38 | SBCommandReturnObject &result) override { |
| 39 | result.PutCString(string: m_message.c_str()); |
| 40 | result.SetStatus(eReturnStatusSuccessFinishResult); |
| 41 | return result.Succeeded(); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | std::string m_message; |
| 46 | }; |
| 47 | |
| 48 | TEST_F(SBCommandInterpreterTest, SingleWordCommand) { |
| 49 | // We first test a command without autorepeat |
| 50 | DummyCommand dummy("It worked" ); |
| 51 | SBCommandInterpreter interp = debugger.GetCommandInterpreter(); |
| 52 | interp.AddCommand(name: "dummy" , impl: &dummy, /*help=*/nullptr); |
| 53 | { |
| 54 | SBCommandReturnObject result; |
| 55 | interp.HandleCommand(command_line: "dummy" , result, /*add_to_history=*/true); |
| 56 | EXPECT_TRUE(result.Succeeded()); |
| 57 | EXPECT_STREQ(result.GetOutput(), "It worked\n" ); |
| 58 | } |
| 59 | { |
| 60 | SBCommandReturnObject result; |
| 61 | interp.HandleCommand(command_line: "" , result); |
| 62 | EXPECT_FALSE(result.Succeeded()); |
| 63 | EXPECT_STREQ(result.GetError(), "error: No auto repeat.\n" ); |
| 64 | } |
| 65 | |
| 66 | // Now we test a command with autorepeat |
| 67 | interp.AddCommand(name: "dummy_with_autorepeat" , impl: &dummy, /*help=*/nullptr, |
| 68 | /*syntax=*/nullptr, /*auto_repeat_command=*/nullptr); |
| 69 | { |
| 70 | SBCommandReturnObject result; |
| 71 | interp.HandleCommand(command_line: "dummy_with_autorepeat" , result, |
| 72 | /*add_to_history=*/true); |
| 73 | EXPECT_TRUE(result.Succeeded()); |
| 74 | EXPECT_STREQ(result.GetOutput(), "It worked\n" ); |
| 75 | } |
| 76 | { |
| 77 | SBCommandReturnObject result; |
| 78 | interp.HandleCommand(command_line: "" , result); |
| 79 | EXPECT_TRUE(result.Succeeded()); |
| 80 | EXPECT_STREQ(result.GetOutput(), "It worked\n" ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | TEST_F(SBCommandInterpreterTest, MultiWordCommand) { |
| 85 | SBCommandInterpreter interp = debugger.GetCommandInterpreter(); |
| 86 | auto command = interp.AddMultiwordCommand(name: "multicommand" , /*help=*/nullptr); |
| 87 | // We first test a subcommand without autorepeat |
| 88 | DummyCommand subcommand("It worked again" ); |
| 89 | command.AddCommand(name: "subcommand" , impl: &subcommand, /*help=*/nullptr); |
| 90 | { |
| 91 | SBCommandReturnObject result; |
| 92 | interp.HandleCommand(command_line: "multicommand subcommand" , result, |
| 93 | /*add_to_history=*/true); |
| 94 | EXPECT_TRUE(result.Succeeded()); |
| 95 | EXPECT_STREQ(result.GetOutput(), "It worked again\n" ); |
| 96 | } |
| 97 | { |
| 98 | SBCommandReturnObject result; |
| 99 | interp.HandleCommand(command_line: "" , result); |
| 100 | EXPECT_FALSE(result.Succeeded()); |
| 101 | EXPECT_STREQ(result.GetError(), "error: No auto repeat.\n" ); |
| 102 | } |
| 103 | |
| 104 | // We first test a subcommand with autorepeat |
| 105 | command.AddCommand(name: "subcommand_with_autorepeat" , impl: &subcommand, |
| 106 | /*help=*/nullptr, /*syntax=*/nullptr, |
| 107 | /*auto_repeat_command=*/nullptr); |
| 108 | { |
| 109 | SBCommandReturnObject result; |
| 110 | interp.HandleCommand(command_line: "multicommand subcommand_with_autorepeat" , result, |
| 111 | /*add_to_history=*/true); |
| 112 | EXPECT_TRUE(result.Succeeded()); |
| 113 | EXPECT_STREQ(result.GetOutput(), "It worked again\n" ); |
| 114 | } |
| 115 | { |
| 116 | SBCommandReturnObject result; |
| 117 | interp.HandleCommand(command_line: "" , result); |
| 118 | EXPECT_TRUE(result.Succeeded()); |
| 119 | EXPECT_STREQ(result.GetOutput(), "It worked again\n" ); |
| 120 | } |
| 121 | |
| 122 | DummyCommand subcommand2("It worked again 2" ); |
| 123 | // We now test a subcommand with autorepeat of the command name |
| 124 | command.AddCommand( |
| 125 | name: "subcommand_with_custom_autorepeat" , impl: &subcommand2, /*help=*/nullptr, |
| 126 | /*syntax=*/nullptr, |
| 127 | /*auto_repeat_command=*/"multicommand subcommand_with_autorepeat" ); |
| 128 | { |
| 129 | SBCommandReturnObject result; |
| 130 | interp.HandleCommand(command_line: "multicommand subcommand_with_custom_autorepeat" , |
| 131 | result, /*add_to_history=*/true); |
| 132 | EXPECT_TRUE(result.Succeeded()); |
| 133 | EXPECT_STREQ(result.GetOutput(), "It worked again 2\n" ); |
| 134 | } |
| 135 | { |
| 136 | SBCommandReturnObject result; |
| 137 | interp.HandleCommand(command_line: "" , result); |
| 138 | EXPECT_TRUE(result.Succeeded()); |
| 139 | EXPECT_STREQ(result.GetOutput(), "It worked again\n" ); |
| 140 | } |
| 141 | } |
| 142 | |