test/cli_argument_parser: More thorough unknwon flag test, add unknown unpositional argument test

BodgeMaster-unfinished
BodgeMaster 2022-07-18 21:26:37 +02:00
parent a3a9078965
commit 09e2030a55
1 changed files with 52 additions and 5 deletions

View File

@ -183,11 +183,7 @@ int main(int argc, char* argv[]) {
delete[] emptyTestParameterList; delete[] emptyTestParameterList;
std::cout << "Passed empty input test." << std::endl; std::cout << "Passed empty input test." << std::endl;
//TODO add tests for invalid input // unknown flag ####################################################
// test cases for invalid input:
// - unknown flags, arguments (mixed with other input and standalone)
// - too few, too many positional arguments (mixed with other input and standalone)
// - check the produced error messages
std::vector<CLI::Flag> unknownFlagTestFlags; std::vector<CLI::Flag> unknownFlagTestFlags;
unknownFlagTestFlags.push_back(CLI::Flag('0', "zero", "test flag")); unknownFlagTestFlags.push_back(CLI::Flag('0', "zero", "test flag"));
@ -215,6 +211,9 @@ int main(int argc, char* argv[]) {
ASSERT(unknownFlagStandaloneTestParser.getFlag('1').isError); ASSERT(unknownFlagStandaloneTestParser.getFlag('1').isError);
ASSERT(unknownFlagStandaloneTestParser.getFlag('1').errorCode == ErrorCodes::NOT_PRESENT); ASSERT(unknownFlagStandaloneTestParser.getFlag('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(!unknownFlagStandaloneTestParser.getFlag('1').value); ASSERT(!unknownFlagStandaloneTestParser.getFlag('1').value);
ASSERT(unknownFlagStandaloneTestParser.getFlag(std::string("four")).isError);
ASSERT(unknownFlagStandaloneTestParser.getFlag(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(!unknownFlagStandaloneTestParser.getFlag(std::string("four")).value);
int unknownFlagMixedTestParameterCount = 7; int unknownFlagMixedTestParameterCount = 7;
@ -222,9 +221,57 @@ int main(int argc, char* argv[]) {
ASSERT(unknownFlagMixedTestParser.getFlag('1').isError); ASSERT(unknownFlagMixedTestParser.getFlag('1').isError);
ASSERT(unknownFlagMixedTestParser.getFlag('1').errorCode == ErrorCodes::WRONG_USAGE); ASSERT(unknownFlagMixedTestParser.getFlag('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownFlagMixedTestParser.getFlag('1').value); ASSERT(unknownFlagMixedTestParser.getFlag('1').value);
ASSERT(unknownFlagMixedTestParser.getFlag(std::string("four")).isError);
ASSERT(unknownFlagMixedTestParser.getFlag(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownFlagMixedTestParser.getFlag(std::string("four")).value);
delete[] unknownFlagTestParameterList; delete[] unknownFlagTestParameterList;
std::cout << "Passed unknown flag test." << std::endl; std::cout << "Passed unknown flag test." << std::endl;
// unknown unpositional argument ###################################
std::vector<CLI::Flag> unknownArgumentTestFlags;
std::vector<CLI::UnpositionalArgument> unknownArgumentTestUnpositionalArguments;
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('0', "zero", "zero", "test flag"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('1', "one", "one", "test flag"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('2', "two", "two", "test flag"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('3', "three","three","test flag"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('4', "four", "four", "test flag"));
unknownArgumentTestUnpositionalArguments.push_back(CLI::UnpositionalArgument('5', "five", "five", "test flag"));
std::vector<CLI::PositionalArgument> unknownArgumentTestPositionalArguments;
const char** unknownArgumentTestParameterList = new const char*[3];
unknownArgumentTestParameterList[0] = "test";
unknownArgumentTestParameterList[1] = "-a123";
unknownArgumentTestParameterList[2] = "-1a";
unknownArgumentTestParameterList[3] = "-2b";
unknownArgumentTestParameterList[4] = "-3c";
unknownArgumentTestParameterList[5] = "-4d";
unknownArgumentTestParameterList[6] = "-5e";
int unknownArgumentStandaloneTestParameterCount = 2;
CLI::ArgumentsParser unknownArgumentStandaloneTestParser = CLI::ArgumentsParser(unknownArgumentStandaloneTestParameterCount, unknownArgumentTestParameterList, unknownArgumentTestFlags, unknownArgumentTestUnpositionalArguments, unknownArgumentTestPositionalArguments);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument('1').isError);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument('1').value == "");
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT);
ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == "");
int unknownArgumentMixedTestParameterCount = 7;
CLI::ArgumentsParser unknownArgumentMixedTestParser = CLI::ArgumentsParser(unknownArgumentMixedTestParameterCount, unknownArgumentTestParameterList, unknownArgumentTestFlags, unknownArgumentTestUnpositionalArguments, unknownArgumentTestPositionalArguments);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument('1').isError);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument('1').errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument('1').value == "a");
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).isError);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE);
ASSERT(unknownArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).value == "d");
delete[] unknownArgumentTestParameterList;
std::cout << "Passed unknown unpositional argument test." << std::endl;
return 0; return 0;
} }