From 6baff11ebdcdfd3663bcea4da7c51b01f18eaf35 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Thu, 21 Jul 2022 09:41:04 +0200 Subject: [PATCH] test/cli_argument_parser: implement tests for generated error messages --- src/test/cli_argument_parser.cpp | 50 +++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/test/cli_argument_parser.cpp b/src/test/cli_argument_parser.cpp index 769d6a5..5979778 100644 --- a/src/test/cli_argument_parser.cpp +++ b/src/test/cli_argument_parser.cpp @@ -148,6 +148,7 @@ int main(int argc, char* argv[]) { ASSERT(!validTestParameterParser.getUnpositionalArgument('z').isError); ASSERT(validTestParameterParser.getUnpositionalArgument('z').errorCode == ErrorCodes::NOT_PRESENT); ASSERT(validTestParameterParser.getUnpositionalArgument('z').value == std::string("")); + ASSERT(validTestParameterParser.wrongUsageMessages.size() == 0); delete[] validTestParameterList; std::cout << "Passed valid input test." << std::endl; @@ -170,6 +171,7 @@ int main(int argc, char* argv[]) { CLI::ArgumentsParser validEmptyTestParameterParser = CLI::ArgumentsParser(emptyTestParameterCount, emptyTestParameterList, emptyTestFlags, emptyTestUnpositionalArguments, emptyTestPositionalArguments); ASSERT(!validEmptyTestParameterParser.wrongUsage); ASSERT(validEmptyTestParameterParser.programName == std::string("test")); + ASSERT(validEmptyTestParameterParser.wrongUsageMessages.size() == 0); //invalid emptyTestPositionalArguments.push_back(CLI::PositionalArgument("argument", "positional argument")); @@ -179,6 +181,8 @@ int main(int argc, char* argv[]) { ASSERT(invalidEmptyTestParameterParser.programName == std::string("test")); ASSERT(invalidEmptyTestParameterParser.getPositionalArgument(0).isError); ASSERT(invalidEmptyTestParameterParser.getPositionalArgument(0).errorCode == ErrorCodes::NOT_PRESENT); + ASSERT(invalidEmptyTestParameterParser.wrongUsageMessages.size() == 1); + ASSERT(invalidEmptyTestParameterParser.wrongUsageMessages.at(0) == "Too few positional arguments! Missing: argument"); delete[] emptyTestParameterList; std::cout << "Passed empty input test." << std::endl; @@ -214,6 +218,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownFlagStandaloneTestParser.getFlag(std::string("four")).isError); ASSERT(unknownFlagStandaloneTestParser.getFlag(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(!unknownFlagStandaloneTestParser.getFlag(std::string("four")).value); + ASSERT(unknownFlagStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownFlagStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a"); int unknownFlagMixedTestParameterCount = 7; @@ -224,6 +230,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownFlagMixedTestParser.getFlag(std::string("four")).isError); ASSERT(unknownFlagMixedTestParser.getFlag(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE); ASSERT(unknownFlagMixedTestParser.getFlag(std::string("four")).value); + ASSERT(unknownFlagMixedTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownFlagMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a"); delete[] unknownFlagTestParameterList; @@ -256,6 +264,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownLongFlagStandaloneTestParser.getFlag(std::string("four")).isError); ASSERT(unknownLongFlagStandaloneTestParser.getFlag(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(!unknownLongFlagStandaloneTestParser.getFlag(std::string("four")).value); + ASSERT(unknownLongFlagStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownLongFlagStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag: --a"); int unknownLongFlagMixedTestParameterCount = 7; @@ -266,6 +276,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownLongFlagMixedTestParser.getFlag(std::string("four")).isError); ASSERT(unknownLongFlagMixedTestParser.getFlag(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE); ASSERT(unknownLongFlagMixedTestParser.getFlag(std::string("four")).value); + ASSERT(unknownLongFlagMixedTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownLongFlagMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag: --a"); delete[] unknownLongFlagTestParameterList; std::cout << "Passed unknown flag test." << std::endl; @@ -301,6 +313,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError); ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(unknownArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == ""); + ASSERT(unknownArgumentStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a123"); int unknownArgumentMixedTestParameterCount = 7; @@ -311,6 +325,8 @@ int main(int argc, char* argv[]) { 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"); + ASSERT(unknownArgumentMixedTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownArgumentMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument or flag(s): a123"); delete[] unknownArgumentTestParameterList; @@ -343,6 +359,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError); ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(unknownLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == ""); + ASSERT(unknownLongArgumentStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownLongArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Unknown argument (or it's a flag that doesn't take a value): --a=123"); int unknownLongArgumentMixedTestParameterCount = 7; @@ -353,6 +371,8 @@ int main(int argc, char* argv[]) { ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).isError); ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::WRONG_USAGE); ASSERT(unknownLongArgumentMixedTestParser.getUnpositionalArgument(std::string("four")).value == "d"); + ASSERT(unknownLongArgumentMixedTestParser.wrongUsageMessages.size() == 1); + ASSERT(unknownLongArgumentMixedTestParser.wrongUsageMessages.at(0) == "Unknown argument (or it's a flag that doesn't take a value): --a=123"); delete[] unknownLongArgumentTestParameterList; std::cout << "Passed unknown unpositional argument test." << std::endl; @@ -374,7 +394,7 @@ int main(int argc, char* argv[]) { const char** incompleteArgumentTestParameterList = new const char*[6]; incompleteArgumentTestParameterList[0] = "test"; incompleteArgumentTestParameterList[1] = "-1"; - incompleteArgumentTestParameterList[2] = "-2b"; + incompleteArgumentTestParameterList[2] = "--two="; // value "" incompleteArgumentTestParameterList[3] = "-3c"; incompleteArgumentTestParameterList[4] = "-4"; incompleteArgumentTestParameterList[5] = "-5e"; @@ -387,6 +407,8 @@ int main(int argc, char* argv[]) { ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError); ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(incompleteArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == ""); + ASSERT(incompleteArgumentStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(incompleteArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one"); int incompleteArgumentMixedTestParameterCount = 6; @@ -400,6 +422,9 @@ int main(int argc, char* argv[]) { ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('5').isError); ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('5').errorCode == ErrorCodes::WRONG_USAGE); ASSERT(incompleteArgumentMixedTestParser.getUnpositionalArgument('5').value == "e"); + ASSERT(incompleteArgumentMixedTestParser.wrongUsageMessages.size() == 2); + ASSERT(incompleteArgumentMixedTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one"); + ASSERT(incompleteArgumentMixedTestParser.wrongUsageMessages.at(1) == "Argument expects value but has none: four"); delete[] incompleteArgumentTestParameterList; @@ -433,8 +458,11 @@ int main(int argc, char* argv[]) { ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).isError); ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(incompleteLongArgumentStandaloneTestParser.getUnpositionalArgument(std::string("four")).value == ""); + ASSERT(incompleteLongArgumentStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(incompleteLongArgumentStandaloneTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one"); int incompleteLongArgumentMixedTestParameterCount = 8; + incompleteLongArgumentTestPositionalArguments.push_back(CLI::PositionalArgument("arg", "argument")); CLI::ArgumentsParser incompleteLongArgumentMixedTestParser = CLI::ArgumentsParser(incompleteLongArgumentMixedTestParameterCount, incompleteLongArgumentTestParameterList, incompleteLongArgumentTestFlags, incompleteLongArgumentTestUnpositionalArguments, incompleteLongArgumentTestPositionalArguments); ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('1').isError); @@ -449,6 +477,9 @@ int main(int argc, char* argv[]) { ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('5').isError); ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('5').errorCode == ErrorCodes::WRONG_USAGE); ASSERT(incompleteLongArgumentMixedTestParser.getUnpositionalArgument('5').value == "e"); + ASSERT(incompleteLongArgumentMixedTestParser.wrongUsageMessages.size() == 2); + ASSERT(incompleteLongArgumentMixedTestParser.wrongUsageMessages.at(0) == "Argument expects value but has none: one"); + ASSERT(incompleteLongArgumentMixedTestParser.wrongUsageMessages.at(1) == "Argument expects value but has none: four"); delete[] incompleteLongArgumentTestParameterList; std::cout << "Passed incomplete unpositional argument test." << std::endl; @@ -468,6 +499,7 @@ int main(int argc, char* argv[]) { tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument1", "test argument")); tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument2", "test argument")); tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument3", "test argument")); + tooFewArgumentsTestPositionalArguments.push_back(CLI::PositionalArgument("argument4", "test argument")); const char** tooFewArgumentsTestParameterList = new const char*[10]; tooFewArgumentsTestParameterList[0] = "test"; @@ -499,6 +531,11 @@ int main(int argc, char* argv[]) { ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('d').isError); ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::NOT_PRESENT); ASSERT(tooFewArgumentsStandaloneTestParser.getUnpositionalArgument('d').value == ""); + ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.size() == 4); + ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(0) == "Too few positional arguments! Missing: argument1"); + ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(1) == "Too few positional arguments! Missing: argument2"); + ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(2) == "Too few positional arguments! Missing: argument3"); + ASSERT(tooFewArgumentsStandaloneTestParser.wrongUsageMessages.at(3) == "Too few positional arguments! Missing: argument4"); int tooFewArgumentsMixedTestParameterCount = 10; CLI::ArgumentsParser tooFewArgumentsMixedTestParser = CLI::ArgumentsParser(tooFewArgumentsMixedTestParameterCount, tooFewArgumentsTestParameterList, tooFewArgumentsTestFlags, tooFewArgumentsTestUnpositionalArguments, tooFewArgumentsTestPositionalArguments); @@ -515,6 +552,9 @@ int main(int argc, char* argv[]) { ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(3).isError); ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(3).errorCode == ErrorCodes::NOT_PRESENT); ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(3).value == ""); + ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(4).isError); + ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(4).errorCode == ErrorCodes::NOT_PRESENT); + ASSERT(tooFewArgumentsMixedTestParser.getPositionalArgument(4).value == ""); ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('b').isError); ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('b').errorCode == ErrorCodes::WRONG_USAGE); ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('b').value == "1"); @@ -524,6 +564,9 @@ int main(int argc, char* argv[]) { ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('d').isError); ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::WRONG_USAGE); ASSERT(tooFewArgumentsMixedTestParser.getUnpositionalArgument('d').value == "ddd"); + ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.size() == 2); + ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.at(0) == "Too few positional arguments! Missing: argument3"); + ASSERT(tooFewArgumentsMixedTestParser.wrongUsageMessages.at(1) == "Too few positional arguments! Missing: argument4"); delete[] tooFewArgumentsTestParameterList; std::cout << "Passed too few positional arguments test." << std::endl; @@ -568,6 +611,8 @@ int main(int argc, char* argv[]) { ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('d').isError); ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::NOT_PRESENT); ASSERT(tooManyArgumentsStandaloneTestParser.getUnpositionalArgument('d').value == ""); + ASSERT(tooManyArgumentsStandaloneTestParser.wrongUsageMessages.size() == 1); + ASSERT(tooManyArgumentsStandaloneTestParser.wrongUsageMessages.at(0) == "Too many positional arguments! Unexpected encounter of: positional_arg_1"); int tooManyArgumentsMixedTestParameterCount = 10; CLI::ArgumentsParser tooManyArgumentsMixedTestParser = CLI::ArgumentsParser(tooManyArgumentsMixedTestParameterCount, tooManyArgumentsTestParameterList, tooManyArgumentsTestFlags, tooManyArgumentsTestUnpositionalArguments, tooManyArgumentsTestPositionalArguments); @@ -584,6 +629,9 @@ int main(int argc, char* argv[]) { ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('d').isError); ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('d').errorCode == ErrorCodes::WRONG_USAGE); ASSERT(tooManyArgumentsMixedTestParser.getUnpositionalArgument('d').value == "ddd"); + ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.size() == 2); + ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.at(0) == "Too many positional arguments! Unexpected encounter of: positional_arg_1"); + ASSERT(tooManyArgumentsMixedTestParser.wrongUsageMessages.at(1) == "Too many positional arguments! Unexpected encounter of: positional_arg_2"); delete[] tooManyArgumentsTestParameterList; std::cout << "Passed too many positional arguments test." << std::endl;