diff --git a/include/icommandsystem.h b/include/icommandsystem.h index ac96cb56f5..c8605fde61 100644 --- a/include/icommandsystem.h +++ b/include/icommandsystem.h @@ -285,7 +285,14 @@ class ICommandSystem: public RegisterableModule /// Returns true if the named command exists virtual bool commandExists(const std::string& name) = 0; - /// Return true if the named command is currently able to execute + /** + * @brief Check if the named command is currently runnable. + * + * This is just a signal to the UI that a command should be disabled; the + * command system does NOT guarantee that a command for which canExecute() + * returns false won't actually be invoked by a subsequent call to + * executeCommand(). + */ virtual bool canExecute(const std::string& name) const = 0; /** diff --git a/test/CommandSystem.cpp b/test/CommandSystem.cpp index ff11d36c77..a11418f4b4 100644 --- a/test/CommandSystem.cpp +++ b/test/CommandSystem.cpp @@ -16,7 +16,7 @@ TEST_F(CommandSystemTest, AddAndRunCommand) int runCount = 0; // Add a command which just logs the number of times it is called - EXPECT_FALSE(GlobalCommandSystem().commandExists("testRunCount")); + ASSERT_FALSE(GlobalCommandSystem().commandExists("testRunCount")); GlobalCommandSystem().addCommand("testRunCount", [&](const cmd::ArgumentList&) { ++runCount; }); EXPECT_TRUE(GlobalCommandSystem().commandExists("testRunCount")); @@ -28,4 +28,23 @@ TEST_F(CommandSystemTest, AddAndRunCommand) EXPECT_EQ(runCount, 2); } +TEST_F(CommandSystemTest, AddCheckedCommand) +{ + const char* COMMAND_NAME = "testCheckedCommand"; + bool commandEnabled = false; + + // Add a command which is conditionally enabled based on our variable flag + ASSERT_FALSE(GlobalCommandSystem().commandExists(COMMAND_NAME)); + GlobalCommandSystem().addWithCheck( + COMMAND_NAME, [](const cmd::ArgumentList&) {}, [&]() { return commandEnabled; } + ); + + // The flag should control the executability of the command + EXPECT_FALSE(GlobalCommandSystem().canExecute(COMMAND_NAME)); + commandEnabled = true; + EXPECT_TRUE(GlobalCommandSystem().canExecute(COMMAND_NAME)); + commandEnabled = false; + EXPECT_FALSE(GlobalCommandSystem().canExecute(COMMAND_NAME)); +} + } \ No newline at end of file