-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MarzellT <tobias.marzell@pionix.de>
- Loading branch information
Showing
11 changed files
with
181 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,85 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
|
||
#include "../main/RegisteredCommand.hpp" | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <catch2/matchers/catch_matchers.hpp> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace module::main; | ||
|
||
SCENARIO("Commands can be registered", "[RegisteredCommand]") { | ||
GIVEN("A command with 0 arguments") { | ||
const auto commandName = std::string{"testCommand0"}; | ||
const auto argumentCount = 0; | ||
const auto testCommand0Function = [](const std::vector<std::string>& arguments) { return arguments.empty(); }; | ||
|
||
WHEN("The command is registered") { | ||
RegisteredCommandBase::registerCommand(commandName, testCommand0Function, argumentCount); | ||
|
||
THEN("The command can be retrieved") { | ||
const auto* registeredCommand = RegisteredCommandBase::getRegisteredCommand(commandName); | ||
REQUIRE(registeredCommand != nullptr); | ||
THEN("The command can be executed") { | ||
REQUIRE((*registeredCommand)({}) == true); | ||
} | ||
THEN("The command throws when the number of arguments is invalid") { | ||
REQUIRE((*registeredCommand)({}) == true); | ||
REQUIRE_THROWS_WITH((*registeredCommand)({"arg1"}), "Invalid number of arguments"); | ||
REQUIRE_THROWS_WITH((*registeredCommand)({"arg1", "arg2"}), "Invalid number of arguments"); | ||
REQUIRE_THROWS_WITH((*registeredCommand)({"arg1", "arg2", "arg3"}), "Invalid number of arguments"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
GIVEN("A command with 1 argument") { | ||
const auto commandName = std::string{"testCommand1"}; | ||
const auto argumentCount = 1; | ||
const auto testCommand1Function = [](const std::vector<std::string>& arguments) { | ||
return arguments.size() == 1; | ||
}; | ||
|
||
WHEN("The command is registered") { | ||
RegisteredCommandBase::registerCommand(commandName, testCommand1Function, argumentCount); | ||
|
||
THEN("The command can be retrieved") { | ||
const auto* registeredCommand = RegisteredCommandBase::getRegisteredCommand(commandName); | ||
REQUIRE(registeredCommand != nullptr); | ||
THEN("The command can be executed") { | ||
REQUIRE((*registeredCommand)({"arg1"}) == true); | ||
} | ||
THEN("The command throws when the number of arguments is invalid") { | ||
REQUIRE_THROWS_WITH((*registeredCommand)({}), "Invalid number of arguments"); | ||
REQUIRE_THROWS_WITH((*registeredCommand)({"arg1", "arg2"}), "Invalid number of arguments"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
GIVEN("A command with 2 arguments") { | ||
const auto commandName = std::string{"testCommand2"}; | ||
const auto argumentCount = 2; | ||
const auto testCommand2Function = [](const std::vector<std::string>& arguments) { | ||
return arguments.size() == 2; | ||
}; | ||
|
||
WHEN("The command is registered") { | ||
RegisteredCommandBase::registerCommand(commandName, testCommand2Function, argumentCount); | ||
|
||
THEN("The command can be retrieved") { | ||
const auto* registeredCommand = RegisteredCommandBase::getRegisteredCommand(commandName); | ||
REQUIRE(registeredCommand != nullptr); | ||
THEN("The command can be executed") { | ||
REQUIRE((*registeredCommand)({"arg1", "arg2"}) == true); | ||
} | ||
THEN("The command throws when the number of arguments is invalid") { | ||
REQUIRE_THROWS_WITH((*registeredCommand)({}), "Invalid number of arguments"); | ||
REQUIRE_THROWS_WITH((*registeredCommand)({"arg1"}), "Invalid number of arguments"); | ||
REQUIRE_THROWS_WITH((*registeredCommand)({"arg1", "arg2", "arg3"}), "Invalid number of arguments"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Pionix GmbH and Contributors to EVerest | ||
|
||
#include "../main/SimCommand.hpp" | ||
#include "../main/RegisteredCommand.hpp" | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <catch2/matchers/catch_matchers.hpp> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace module::main; | ||
|
||
SCENARIO("SimCommands can be created", "[SimCommand]") { | ||
GIVEN("A command with 0 arguments called testCommand is registered") { | ||
const auto commandName = std::string{"testCommand"}; | ||
const auto argumentCount = 0; | ||
const auto testCommandFunction = [](const std::vector<std::string>& arguments) { return arguments.empty(); }; | ||
RegisteredCommandBase::registerCommand(commandName, testCommandFunction, argumentCount); | ||
|
||
WHEN("The SimCommand is created") { | ||
const auto simCommand = SimCommand{commandName, {}}; | ||
|
||
THEN("The command can be executed") { | ||
REQUIRE(simCommand.execute() == true); | ||
} | ||
} | ||
|
||
WHEN("The SimCommand is created with the wrong number of arguments") { | ||
const auto simCommand = SimCommand{commandName, {"arg1"}}; | ||
|
||
THEN("The command throws") { | ||
REQUIRE_THROWS_WITH(simCommand.execute(), "Invalid number of arguments"); | ||
} | ||
} | ||
} | ||
|
||
WHEN("The SimCommand is created with the wrong commandName") { | ||
THEN("The command throws") { | ||
REQUIRE_THROWS_WITH(SimCommand("wrongCommand", {}), "Command not found"); | ||
} | ||
} | ||
} |