Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

#include <iterator>

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();
}
Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion test/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -43,6 +45,7 @@ class options {
std::set<std::string> _options;
std::string _which_test;
const bool _quiet;
const bool _help;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to further cleanup this later ... I would like that the variable names are mQuiet, mHelp, etc..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look, for another PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1720.

};

#endif
20 changes: 20 additions & 0 deletions test/testoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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"};
Expand Down
6 changes: 5 additions & 1 deletion test/testrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char**>(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
Expand Down
26 changes: 22 additions & 4 deletions test/testsuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions test/testsuite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down