From 4157f8849a1d186e1610db9f0dcb1a596631da2b Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 25 Jun 2017 15:12:38 +0200 Subject: [PATCH] Improve argument parser tests --- application/argumentparser.h | 1 + tests/argumentparsertests.cpp | 45 ++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/application/argumentparser.h b/application/argumentparser.h index b9bb736..628e5c0 100644 --- a/application/argumentparser.h +++ b/application/argumentparser.h @@ -143,6 +143,7 @@ inline ArgumentOccurrence::ArgumentOccurrence(std::size_t index, const std::vect class CPP_UTILITIES_EXPORT Argument { friend ArgumentParser; friend ArgumentReader; + friend ArgumentParserTests; public: typedef std::function CallbackFunction; diff --git a/tests/argumentparsertests.cpp b/tests/argumentparsertests.cpp index 675164b..7702f0e 100644 --- a/tests/argumentparsertests.cpp +++ b/tests/argumentparsertests.cpp @@ -4,6 +4,7 @@ #include "../application/argumentparser.h" #include "../application/argumentparserprivate.h" +#include "../application/commandlineutils.h" #include "../application/failure.h" #include "../application/fakeqtconfigarguments.h" @@ -33,6 +34,7 @@ class ArgumentParserTests : public TestFixture { CPPUNIT_TEST(testCallbacks); CPPUNIT_TEST(testBashCompletion); CPPUNIT_TEST(testHelp); + CPPUNIT_TEST(testSetMainArguments); CPPUNIT_TEST_SUITE_END(); public: @@ -44,6 +46,7 @@ public: void testCallbacks(); void testBashCompletion(); void testHelp(); + void testSetMainArguments(); private: void callback(); @@ -73,13 +76,13 @@ void ArgumentParserTests::testArgument() CPPUNIT_ASSERT_EQUAL(subArg.parents().at(0), &argument); CPPUNIT_ASSERT(!subArg.conflictsWithArgument()); CPPUNIT_ASSERT(!argument.firstValue()); - argument.setEnvironmentVariable("PATH"); - if (getenv("PATH")) { - CPPUNIT_ASSERT(argument.firstValue()); - CPPUNIT_ASSERT(!strcmp(argument.firstValue(), getenv("PATH"))); - } else { - CPPUNIT_ASSERT(!argument.firstValue()); - } + argument.setEnvironmentVariable("FOO_ENV_VAR"); + setenv("FOO_ENV_VAR", "foo", true); + CPPUNIT_ASSERT(!strcmp(argument.firstValue(), "foo")); + ArgumentOccurrence occurrence(0, vector(), nullptr); + occurrence.values.emplace_back("bar"); + argument.m_occurrences.emplace_back(move(occurrence)); + CPPUNIT_ASSERT(!strcmp(argument.firstValue(), "bar")); } /*! @@ -598,6 +601,11 @@ void ArgumentParserTests::testBashCompletion() */ void ArgumentParserTests::testHelp() { + // identation + Indentation indent; + indent = indent + 3; + CPPUNIT_ASSERT_EQUAL(static_cast(4 + 3), indent.level); + // redirect cout to custom buffer stringstream buffer; streambuf *regularCoutBuffer = cout.rdbuf(buffer.rdbuf()); @@ -616,10 +624,11 @@ void ArgumentParserTests::testHelp() Argument filesArg("files", 'f', "specifies the path of the file(s) to be opened"); filesArg.setCombinable(true); filesArg.addSubArgument(&subArg); + filesArg.setSubArguments({ &subArg }); // test re-assignment btw Argument envArg("env", '\0', "env"); envArg.setEnvironmentVariable("FILES"); envArg.setRequiredValueCount(2); - envArg.setValueNames({ "file" }); + envArg.appendValueName("file"); parser.addMainArgument(&helpArg); parser.addMainArgument(&verboseArg); parser.addMainArgument(&filesArg); @@ -658,3 +667,23 @@ void ArgumentParserTests::testHelp() "Project website: " APP_URL "\n"s, buffer.str()); } + +/*! + * \brief Tests some corner cases in setMainArguments() which are not already checked in the other tests. + */ +void ArgumentParserTests::testSetMainArguments() +{ + ArgumentParser parser; + HelpArgument helpArg(parser); + Argument subArg("sub-arg", 's', "mandatory sub arg"); + subArg.setRequired(true); + helpArg.addSubArgument(&subArg); + parser.addMainArgument(&helpArg); + parser.setMainArguments({}); + CPPUNIT_ASSERT_MESSAGE("clear main args", parser.mainArguments().empty()); + parser.setMainArguments({ &helpArg }); + CPPUNIT_ASSERT_MESSAGE("no default due to required sub arg", !parser.defaultArgument()); + subArg.setConstraints(0, 20); + parser.setMainArguments({ &helpArg }); + CPPUNIT_ASSERT_MESSAGE("default if no required sub arg", &helpArg == parser.defaultArgument()); +}