diff --git a/test/main.cpp b/test/main.cpp index 571f7742f76..e0495583494 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -22,6 +22,7 @@ #include "fixture.h" #include +#include int main(int argc, char *argv[]) { @@ -34,6 +35,13 @@ int main(int argc, char *argv[]) TestFixture::printHelp(); return EXIT_SUCCESS; } + if (!args.errors().empty()) { + for (const auto& error : args.errors()) + { + std::cout << "error: " << error << '\n'; + } + return EXIT_FAILURE; + } const std::size_t failedTestsCount = TestFixture::runTests(args); return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/test/options.cpp b/test/options.cpp index 658c4c76d38..51e2b63b0ff 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -17,19 +17,27 @@ #include "options.h" options::options(int argc, const char* const argv[]) - : mArgs(argv + 1, argv + argc) - ,mQuiet(mArgs.count("-q") != 0) - ,mHelp(mArgs.count("-h") != 0 || mArgs.count("--help")) - ,mSummary(mArgs.count("-n") == 0) - ,mDryRun(mArgs.count("-d") != 0) - ,mExcludeTests(mArgs.count("-x") != 0) - ,mExe(argv[0]) + : mExe(argv[0]) { - for (const auto& arg : mArgs) { + const std::set args(argv + 1, argv + argc); + for (const auto& arg : args) { if (arg.empty()) continue; // empty argument - if (arg[0] == '-') + if (arg[0] == '-') { + if (arg == "-q") + mQuiet = true; + else if (arg == "-h" || arg == "--help") + mHelp = true; + else if (arg == "-n") + mSummary = false; + else if (arg == "-d") + mDryRun = true; + else if (arg == "-x") + mExcludeTests = true; + else + mErrors.emplace_back("unknown option '" + arg + "'"); continue; // command-line switch + } const auto pos = arg.find("::"); if (pos == std::string::npos) { mWhichTests[arg] = {}; // run whole fixture @@ -78,3 +86,8 @@ bool options::exclude_tests() const { return mExcludeTests; } + +const std::vector& options::errors() const +{ + return mErrors; +} diff --git a/test/options.h b/test/options.h index 5bba8e70115..d0438b67c4d 100644 --- a/test/options.h +++ b/test/options.h @@ -20,6 +20,7 @@ #include #include #include +#include /** * @brief Class to parse command-line parameters for ./testrunner . @@ -43,6 +44,9 @@ class options { /** Which tests should be run. */ const std::map>& which_tests() const; + /** Errors encountered during option processing. */ + const std::vector& errors() const; + const std::string& exe() const; options() = delete; @@ -50,13 +54,13 @@ class options { options& operator =(const options&) = delete; private: - std::set mArgs; std::map> mWhichTests; - const bool mQuiet; - const bool mHelp; - const bool mSummary; - const bool mDryRun; - const bool mExcludeTests; + std::vector mErrors; + bool mQuiet{}; + bool mHelp{}; + bool mSummary{true}; + bool mDryRun{}; + bool mExcludeTests{}; std::string mExe; }; diff --git a/test/testoptions.cpp b/test/testoptions.cpp index 6c290823a17..6ee644f94b8 100644 --- a/test/testoptions.cpp +++ b/test/testoptions.cpp @@ -35,9 +35,8 @@ class TestOptions : public TestFixture { TEST_CASE(which_test); TEST_CASE(which_test_method); TEST_CASE(no_test_method); - TEST_CASE(not_quiet); + TEST_CASE(defaults); TEST_CASE(quiet); - TEST_CASE(not_help); TEST_CASE(help); TEST_CASE(help_long); TEST_CASE(multiple_testcases); @@ -45,6 +44,7 @@ class TestOptions : public TestFixture { TEST_CASE(invalid_switches); TEST_CASE(summary); TEST_CASE(dry_run); + TEST_CASE(exclude_tests); } @@ -55,6 +55,7 @@ class TestOptions : public TestFixture { { "TestClass", {} } }; ASSERT(expected == args.which_tests()); + ASSERT(args.errors().empty()); } @@ -65,6 +66,7 @@ class TestOptions : public TestFixture { { "TestClass", {"TestMethod"} } }; ASSERT(expected == args.which_tests()); + ASSERT(args.errors().empty()); } @@ -73,33 +75,33 @@ class TestOptions : public TestFixture { options args(getArrayLength(argv), argv); const std::map> expected{}; ASSERT(expected == args.which_tests()); + ASSERT(args.errors().empty()); } - void not_quiet() const { - const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"}; + void defaults() const { + const char* argv[] = {"./test_runner", "TestClass::TestMethod"}; options args(getArrayLength(argv), argv); ASSERT_EQUALS(false, args.quiet()); + ASSERT_EQUALS(false, args.help()); + ASSERT_EQUALS(true, args.summary()); + ASSERT_EQUALS(false, args.dry_run()); + ASSERT_EQUALS(false, args.exclude_tests()); + ASSERT(args.errors().empty()); } - void quiet() const { const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-q"}; options args(getArrayLength(argv), argv); ASSERT_EQUALS(true, args.quiet()); + ASSERT(args.errors().empty()); } - void not_help() const { - const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"}; - options args(getArrayLength(argv), argv); - ASSERT_EQUALS(false, args.help()); - } - - void help() const { const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-h"}; options args(getArrayLength(argv), argv); ASSERT_EQUALS(true, args.help()); + ASSERT(args.errors().empty()); } @@ -107,6 +109,7 @@ class TestOptions : public TestFixture { const char* argv[] = {"./test_runner", "TestClass::TestMethod", "--help"}; options args(getArrayLength(argv), argv); ASSERT_EQUALS(true, args.help()); + ASSERT(args.errors().empty()); } void multiple_testcases() const { @@ -116,6 +119,7 @@ class TestOptions : public TestFixture { { "TestClass", { "TestMethod", "AnotherTestMethod" } } }; ASSERT(expected == args.which_tests()); + ASSERT(args.errors().empty()); } void multiple_testcases_ignore_duplicates() const { @@ -125,6 +129,7 @@ class TestOptions : public TestFixture { { "TestClass", {} } }; ASSERT(expected == args.which_tests()); + ASSERT(args.errors().empty()); } void invalid_switches() const { @@ -135,18 +140,32 @@ class TestOptions : public TestFixture { }; ASSERT(expected == args.which_tests()); ASSERT_EQUALS(true, args.quiet()); + ASSERT_EQUALS(2, args.errors().size()); + auto it = args.errors().cbegin(); + ASSERT_EQUALS("unknown option '-a'", *it); + ++it; + ASSERT_EQUALS("unknown option '-v'", *it); } void summary() const { const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-n"}; options args(getArrayLength(argv), argv); ASSERT_EQUALS(false, args.summary()); + ASSERT(args.errors().empty()); } void dry_run() const { const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-d"}; options args(getArrayLength(argv), argv); ASSERT_EQUALS(true, args.dry_run()); + ASSERT(args.errors().empty()); + } + + void exclude_tests() const { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-x"}; + options args(getArrayLength(argv), argv); + ASSERT_EQUALS(true, args.exclude_tests()); + ASSERT(args.errors().empty()); } };