From 8ce4643518c23e13dabafb5746aa01af69f131bd Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 12 Sep 2021 21:13:44 +0200 Subject: [PATCH] Test: Add optional argument test cases --- test/testutilargparser.cpp | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/testutilargparser.cpp b/test/testutilargparser.cpp index f3c9070..25dc783 100644 --- a/test/testutilargparser.cpp +++ b/test/testutilargparser.cpp @@ -232,6 +232,67 @@ TEST_CASE(VectorStringArguments) // ----------------------------------------- +TEST_CASE(CombinationOfNonRequiredArguments) +{ + // Optional arguments, one given + int intArg1 = 0; + double doubleArg1 = 0; + std::string stringArg1 = ""; + auto result = runParser({ "optional argument" }, [&](auto& parser) { + parser.addArgument(intArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(doubleArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(stringArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + }); + EXPECT_EQ(result, true); + EXPECT_EQ(intArg1, 0); + EXPECT_EQ(doubleArg1, 0); + EXPECT_EQ(stringArg1, "optional argument"); + + // Optional arguments, two given + intArg1 = 0; + doubleArg1 = 0; + stringArg1 = ""; + result = runParser({ "999.999", "optional argument" }, [&](auto& parser) { + parser.addArgument(intArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(doubleArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(stringArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + }); + EXPECT_EQ(result, true); + EXPECT_EQ(intArg1, 0); + EXPECT_EQ(doubleArg1, 999.999); + EXPECT_EQ(stringArg1, "optional argument"); + + // Optional arguments, two given, one valid + intArg1 = 0; + doubleArg1 = 0; + stringArg1 = ""; + result = runParser({ "999,999", "optional argument" }, [&](auto& parser) { + parser.addArgument(intArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(doubleArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(stringArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + }); + EXPECT_EQ(result, false); + EXPECT_EQ(intArg1, 0); + EXPECT_EQ(doubleArg1, 0); + EXPECT_EQ(stringArg1, "999,999"); + + // Optional arguments, two given, both valid but wrong order + stringArg1 = ""; + intArg1 = 0; + doubleArg1 = 0; + result = runParser({ "999.999", "optional argument" }, [&](auto& parser) { + parser.addArgument(stringArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(intArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + parser.addArgument(doubleArg1, nullptr, nullptr, nullptr, Util::ArgParser::Required::Optional); + }); + EXPECT_EQ(result, false); + EXPECT_EQ(stringArg1, "999.999"); + EXPECT_EQ(intArg1, 0); + EXPECT_EQ(doubleArg1, 0); +} + +// ----------------------------------------- + TEST_CASE(BoolOptions) { // Short option