Skip to content

Commit

Permalink
#410: add unit test for ICommandSystem::addWithCheck()
Browse files Browse the repository at this point in the history
Confirm that the canExecute() method returns the correct value based on
the check function.
  • Loading branch information
Matthew Mott committed Jun 29, 2022
1 parent ca72700 commit b21d09a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion include/icommandsystem.h
Expand Up @@ -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;

/**
Expand Down
21 changes: 20 additions & 1 deletion test/CommandSystem.cpp
Expand Up @@ -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"));
Expand All @@ -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));
}

}

0 comments on commit b21d09a

Please sign in to comment.