Skip to content

Commit

Permalink
#401: add unit test for a command sequence
Browse files Browse the repository at this point in the history
Confirm that the execute() method can run a sequence of commands
separated by semicolons.
  • Loading branch information
Matthew Mott committed Jun 29, 2022
1 parent b21d09a commit b7d87a4
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions test/CommandSystem.cpp
Expand Up @@ -13,21 +13,47 @@ TEST_F(CommandSystemTest, GetCommandSystem)

TEST_F(CommandSystemTest, AddAndRunCommand)
{
const char* COMMAND_NAME = "testRunCount";
int runCount = 0;

// Add a command which just logs the number of times it is called
ASSERT_FALSE(GlobalCommandSystem().commandExists("testRunCount"));
GlobalCommandSystem().addCommand("testRunCount",
[&](const cmd::ArgumentList&) { ++runCount; });
EXPECT_TRUE(GlobalCommandSystem().commandExists("testRunCount"));
ASSERT_FALSE(GlobalCommandSystem().commandExists(COMMAND_NAME));
GlobalCommandSystem().addCommand(COMMAND_NAME, [&](const cmd::ArgumentList&) { ++runCount; });
EXPECT_TRUE(GlobalCommandSystem().commandExists(COMMAND_NAME));

// Ensure that the call happens when we run the command
GlobalCommandSystem().executeCommand("testRunCount");
GlobalCommandSystem().executeCommand(COMMAND_NAME);
EXPECT_EQ(runCount, 1);
GlobalCommandSystem().executeCommand("testRunCount");
GlobalCommandSystem().executeCommand(COMMAND_NAME);
EXPECT_EQ(runCount, 2);
}

TEST_F(CommandSystemTest, RunCommandSequence)
{
const char* FIRST_COMMAND = "firstRunCountCommand";
int firstRunCount = 0;
const char* SECOND_COMMAND = "secondRunCountCommand";
int secondRunCount = 0;

// Register a command for each run count
ASSERT_FALSE(GlobalCommandSystem().commandExists(FIRST_COMMAND));
ASSERT_FALSE(GlobalCommandSystem().commandExists(SECOND_COMMAND));
GlobalCommandSystem().addCommand(FIRST_COMMAND,
[&](const cmd::ArgumentList&) { ++firstRunCount; });
GlobalCommandSystem().addCommand(SECOND_COMMAND,
[&](const cmd::ArgumentList&) { ++secondRunCount; });

// Run a semicolon-separated sequence of both commands
GlobalCommandSystem().execute("firstRunCountCommand; secondRunCountCommand");
EXPECT_EQ(firstRunCount, 1);
EXPECT_EQ(secondRunCount, 1);
GlobalCommandSystem().execute(" secondRunCountCommand ; firstRunCountCommand ");
EXPECT_EQ(firstRunCount, 2);
EXPECT_EQ(secondRunCount, 2);
GlobalCommandSystem().execute("secondRunCountCommand ;secondRunCountCommand");
EXPECT_EQ(secondRunCount, 4);
}

TEST_F(CommandSystemTest, AddCheckedCommand)
{
const char* COMMAND_NAME = "testCheckedCommand";
Expand Down

0 comments on commit b7d87a4

Please sign in to comment.