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