Skip to content

Commit 40ead7f

Browse files
rikardfalkeborndanmar
authored andcommitted
Add help to testrunner (danmar#1704)
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.
1 parent 1877476 commit 40ead7f

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

test/options.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
#include <iterator>
2020

21-
options::options(int argc, const char* argv[])
21+
options::options(int argc, const char* const argv[])
2222
:_options(argv + 1, argv + argc)
2323
,_which_test("")
2424
,_quiet(_options.count("-q") != 0)
25+
,_help(_options.count("-h") != 0 || _options.count("--help"))
2526
{
2627
_options.erase("-q");
28+
_options.erase("-h");
29+
_options.erase("--help");
2730
if (! _options.empty()) {
2831
_which_test = *_options.rbegin();
2932
}
@@ -34,6 +37,11 @@ bool options::quiet() const
3437
return _quiet;
3538
}
3639

40+
bool options::help() const
41+
{
42+
return _help;
43+
}
44+
3745
const std::string& options::which_test() const
3846
{
3947
return _which_test;

test/options.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
class options {
2929
public:
3030
/** Call from main() to populate object */
31-
options(int argc, const char* argv[]);
31+
options(int argc, const char* const argv[]);
3232
/** Don't print the name of each method being tested. */
3333
bool quiet() const;
34+
/** Print help. */
35+
bool help() const;
3436
/** Which test should be run. Empty string means 'all tests' */
3537
const std::string& which_test() const;
3638

@@ -43,6 +45,7 @@ class options {
4345
std::set<std::string> _options;
4446
std::string _which_test;
4547
const bool _quiet;
48+
const bool _help;
4649
};
4750

4851
#endif

test/testoptions.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class TestOptions: public TestFixture {
3232
TEST_CASE(no_test_method);
3333
TEST_CASE(not_quiet);
3434
TEST_CASE(quiet);
35+
TEST_CASE(not_help);
36+
TEST_CASE(help);
37+
TEST_CASE(help_long);
3538
TEST_CASE(multiple_testcases);
3639
TEST_CASE(invalid_switches);
3740
}
@@ -71,8 +74,25 @@ class TestOptions: public TestFixture {
7174
ASSERT_EQUALS(true, args.quiet());
7275
}
7376

77+
void not_help() const {
78+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"};
79+
options args(sizeof argv / sizeof argv[0], argv);
80+
ASSERT_EQUALS(false, args.help());
81+
}
7482

7583

84+
void help() const {
85+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-h"};
86+
options args(sizeof argv / sizeof argv[0], argv);
87+
ASSERT_EQUALS(true, args.help());
88+
}
89+
90+
91+
void help_long() const {
92+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "--help"};
93+
options args(sizeof argv / sizeof argv[0], argv);
94+
ASSERT_EQUALS(true, args.help());
95+
}
7696

7797
void multiple_testcases() const {
7898
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "Ignore::ThisOne"};

test/testrunner.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ int main(int argc, char *argv[])
3535
#endif
3636
Preprocessor::macroChar = '$'; // While macroChar is char(1) per default outside test suite, we require it to be a human-readable character here.
3737

38-
options args(argc, const_cast<const char**>(argv));
38+
options args(argc, argv);
3939

40+
if (args.help()) {
41+
TestFixture::printHelp();
42+
return EXIT_SUCCESS;
43+
}
4044
const std::size_t failedTestsCount = TestFixture::runTests(args);
4145
return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
4246
#ifdef NDEBUG

test/testsuite.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ void TestFixture::complainMissingLib(const char * const libname) const
275275
missingLibs.insert(libname);
276276
}
277277

278+
void TestFixture::printHelp()
279+
{
280+
std::cout << "Testrunner - run Cppcheck tests\n"
281+
"\n"
282+
"Syntax:\n"
283+
" testrunner [OPTIONS] [TestClass::TestCase]\n"
284+
" run all test cases:\n"
285+
" testrunner\n"
286+
" run all test cases in TestClass:\n"
287+
" testrunner TestClass\n"
288+
" run TestClass::TestCase:\n"
289+
" testrunner TestClass::TestCase\n"
290+
"\n"
291+
"Options:\n"
292+
" -q Do not print the test cases that have run.\n"
293+
" -h, --help Print this help.\n";
294+
}
295+
278296
void TestFixture::run(const std::string &str)
279297
{
280298
testToRun = str;
@@ -305,10 +323,10 @@ std::size_t TestFixture::runTests(const options& args)
305323

306324
const TestSet &tests = TestRegistry::theInstance().tests();
307325

308-
for (TestSet::const_iterator it = tests.begin(); it != tests.end(); ++it) {
309-
if (classname.empty() || (*it)->classname == classname) {
310-
(*it)->processOptions(args);
311-
(*it)->run(testname);
326+
for (TestFixture * test : tests) {
327+
if (classname.empty() || test->classname == classname) {
328+
test->processOptions(args);
329+
test->run(testname);
312330
}
313331
}
314332

test/testsuite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class TestFixture : public ErrorLogger {
8585
virtual void reportOut(const std::string &outmsg) OVERRIDE;
8686
virtual void reportErr(const ErrorLogger::ErrorMessage &msg) OVERRIDE;
8787
void run(const std::string &str);
88+
static void printHelp();
8889
const std::string classname;
8990

9091
explicit TestFixture(const char * const _name);

0 commit comments

Comments
 (0)