From 274ee524cbd969b0d1a1350fd93a79d5f145bd8d Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Sun, 17 Feb 2019 20:05:59 +0100 Subject: [PATCH] Add help to testrunner For now, only print the ways of running testrunner and the few options that are available. Also, refactor to remove an unneeded const_cast and use a range for loop. Partially fixes #8514. --- test/options.cpp | 10 +++++++++- test/options.h | 5 ++++- test/testoptions.cpp | 20 ++++++++++++++++++++ test/testrunner.cpp | 6 +++++- test/testsuite.cpp | 26 ++++++++++++++++++++++---- test/testsuite.h | 1 + 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/test/options.cpp b/test/options.cpp index d6ed69211e5..ccf3ef7ec3d 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -18,12 +18,15 @@ #include -options::options(int argc, const char* argv[]) +options::options(int argc, const char* const argv[]) :_options(argv + 1, argv + argc) ,_which_test("") ,_quiet(_options.count("-q") != 0) + ,_help(_options.count("-h") != 0 || _options.count("--help")) { _options.erase("-q"); + _options.erase("-h"); + _options.erase("--help"); if (! _options.empty()) { _which_test = *_options.rbegin(); } @@ -34,6 +37,11 @@ bool options::quiet() const return _quiet; } +bool options::help() const +{ + return _help; +} + const std::string& options::which_test() const { return _which_test; diff --git a/test/options.h b/test/options.h index 7fafd1c3005..5a7e6b7510a 100644 --- a/test/options.h +++ b/test/options.h @@ -28,9 +28,11 @@ class options { public: /** Call from main() to populate object */ - options(int argc, const char* argv[]); + options(int argc, const char* const argv[]); /** Don't print the name of each method being tested. */ bool quiet() const; + /** Print help. */ + bool help() const; /** Which test should be run. Empty string means 'all tests' */ const std::string& which_test() const; @@ -43,6 +45,7 @@ class options { std::set _options; std::string _which_test; const bool _quiet; + const bool _help; }; #endif diff --git a/test/testoptions.cpp b/test/testoptions.cpp index 0c2712485a1..d7373582049 100644 --- a/test/testoptions.cpp +++ b/test/testoptions.cpp @@ -32,6 +32,9 @@ class TestOptions: public TestFixture { TEST_CASE(no_test_method); TEST_CASE(not_quiet); TEST_CASE(quiet); + TEST_CASE(not_help); + TEST_CASE(help); + TEST_CASE(help_long); TEST_CASE(multiple_testcases); TEST_CASE(invalid_switches); } @@ -71,8 +74,25 @@ class TestOptions: public TestFixture { ASSERT_EQUALS(true, args.quiet()); } + void not_help() const { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS(false, args.help()); + } + void help() const { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-h"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS(true, args.help()); + } + + + void help_long() const { + const char* argv[] = {"./test_runner", "TestClass::TestMethod", "--help"}; + options args(sizeof argv / sizeof argv[0], argv); + ASSERT_EQUALS(true, args.help()); + } void multiple_testcases() const { const char* argv[] = {"./test_runner", "TestClass::TestMethod", "Ignore::ThisOne"}; diff --git a/test/testrunner.cpp b/test/testrunner.cpp index 4043c33c855..9bee733f627 100644 --- a/test/testrunner.cpp +++ b/test/testrunner.cpp @@ -35,8 +35,12 @@ int main(int argc, char *argv[]) #endif Preprocessor::macroChar = '$'; // While macroChar is char(1) per default outside test suite, we require it to be a human-readable character here. - options args(argc, const_cast(argv)); + options args(argc, argv); + if (args.help()) { + TestFixture::printHelp(); + return EXIT_SUCCESS; + } const std::size_t failedTestsCount = TestFixture::runTests(args); return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE; #ifdef NDEBUG diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 9b6240339e7..f398f0a2013 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -275,6 +275,24 @@ void TestFixture::complainMissingLib(const char * const libname) const missingLibs.insert(libname); } +void TestFixture::printHelp() +{ + std::cout << "Testrunner - run Cppcheck tests\n" + "\n" + "Syntax:\n" + " testrunner [OPTIONS] [TestClass::TestCase]\n" + " run all test cases:\n" + " testrunner\n" + " run all test cases in TestClass:\n" + " testrunner TestClass\n" + " run TestClass::TestCase:\n" + " testrunner TestClass::TestCase\n" + "\n" + "Options:\n" + " -q Do not print the test cases that have run.\n" + " -h, --help Print this help.\n"; +} + void TestFixture::run(const std::string &str) { testToRun = str; @@ -305,10 +323,10 @@ std::size_t TestFixture::runTests(const options& args) const TestSet &tests = TestRegistry::theInstance().tests(); - for (TestSet::const_iterator it = tests.begin(); it != tests.end(); ++it) { - if (classname.empty() || (*it)->classname == classname) { - (*it)->processOptions(args); - (*it)->run(testname); + for (TestFixture * test : tests) { + if (classname.empty() || test->classname == classname) { + test->processOptions(args); + test->run(testname); } } diff --git a/test/testsuite.h b/test/testsuite.h index f4b82c260ea..4ec384cd4b5 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -85,6 +85,7 @@ class TestFixture : public ErrorLogger { virtual void reportOut(const std::string &outmsg) OVERRIDE; virtual void reportErr(const ErrorLogger::ErrorMessage &msg) OVERRIDE; void run(const std::string &str); + static void printHelp(); const std::string classname; explicit TestFixture(const char * const _name);