Skip to content

Commit 52f62bc

Browse files
committed
testrunner: bail out on unknown options
1 parent 241cfcb commit 52f62bc

4 files changed

Lines changed: 70 additions & 27 deletions

File tree

test/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "fixture.h"
2323

2424
#include <cstdlib>
25+
#include <iostream>
2526

2627
int main(int argc, char *argv[])
2728
{
@@ -34,6 +35,13 @@ int main(int argc, char *argv[])
3435
TestFixture::printHelp();
3536
return EXIT_SUCCESS;
3637
}
38+
if (!args.errors().empty()) {
39+
for (const auto& error : args.errors())
40+
{
41+
std::cout << "error: " << error << '\n';
42+
}
43+
return EXIT_FAILURE;
44+
}
3745
const std::size_t failedTestsCount = TestFixture::runTests(args);
3846
return (failedTestsCount == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
3947
}

test/options.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@
1717
#include "options.h"
1818

1919
options::options(int argc, const char* const argv[])
20-
: mArgs(argv + 1, argv + argc)
21-
,mQuiet(mArgs.count("-q") != 0)
22-
,mHelp(mArgs.count("-h") != 0 || mArgs.count("--help"))
23-
,mSummary(mArgs.count("-n") == 0)
24-
,mDryRun(mArgs.count("-d") != 0)
25-
,mExcludeTests(mArgs.count("-x") != 0)
26-
,mExe(argv[0])
20+
: mExe(argv[0])
2721
{
28-
for (const auto& arg : mArgs) {
22+
const std::set<std::string> args(argv + 1, argv + argc);
23+
for (const auto& arg : args) {
2924
if (arg.empty())
3025
continue; // empty argument
31-
if (arg[0] == '-')
26+
if (arg[0] == '-') {
27+
if (arg == "-q")
28+
mQuiet = true;
29+
else if (arg == "-h" || arg == "--help")
30+
mHelp = true;
31+
else if (arg == "-n")
32+
mSummary = false;
33+
else if (arg == "-d")
34+
mDryRun = true;
35+
else if (arg == "-x")
36+
mExcludeTests = true;
37+
else
38+
mErrors.emplace_back("unknown option '" + arg + "'");
3239
continue; // command-line switch
40+
}
3341
const auto pos = arg.find("::");
3442
if (pos == std::string::npos) {
3543
mWhichTests[arg] = {}; // run whole fixture
@@ -78,3 +86,8 @@ bool options::exclude_tests() const
7886
{
7987
return mExcludeTests;
8088
}
89+
90+
const std::vector<std::string>& options::errors() const
91+
{
92+
return mErrors;
93+
}

test/options.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <map>
2121
#include <set>
2222
#include <string>
23+
#include <vector>
2324

2425
/**
2526
* @brief Class to parse command-line parameters for ./testrunner .
@@ -43,20 +44,23 @@ class options {
4344
/** Which tests should be run. */
4445
const std::map<std::string, std::set<std::string>>& which_tests() const;
4546

47+
/** Errors encountered during option processing. */
48+
const std::vector<std::string>& errors() const;
49+
4650
const std::string& exe() const;
4751

4852
options() = delete;
4953
options(const options&) = delete;
5054
options& operator =(const options&) = delete;
5155

5256
private:
53-
std::set<std::string> mArgs;
5457
std::map<std::string, std::set<std::string>> mWhichTests;
55-
const bool mQuiet;
56-
const bool mHelp;
57-
const bool mSummary;
58-
const bool mDryRun;
59-
const bool mExcludeTests;
58+
std::vector<std::string> mErrors;
59+
bool mQuiet{};
60+
bool mHelp{};
61+
bool mSummary{true};
62+
bool mDryRun{};
63+
bool mExcludeTests{};
6064
std::string mExe;
6165
};
6266

test/testoptions.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ class TestOptions : public TestFixture {
3535
TEST_CASE(which_test);
3636
TEST_CASE(which_test_method);
3737
TEST_CASE(no_test_method);
38-
TEST_CASE(not_quiet);
3938
TEST_CASE(quiet);
40-
TEST_CASE(not_help);
4139
TEST_CASE(help);
4240
TEST_CASE(help_long);
4341
TEST_CASE(multiple_testcases);
4442
TEST_CASE(multiple_testcases_ignore_duplicates);
4543
TEST_CASE(invalid_switches);
4644
TEST_CASE(summary);
4745
TEST_CASE(dry_run);
46+
TEST_CASE(exclude_tests);
4847
}
4948

5049

@@ -55,6 +54,7 @@ class TestOptions : public TestFixture {
5554
{ "TestClass", {} }
5655
};
5756
ASSERT(expected == args.which_tests());
57+
ASSERT(args.errors().empty());
5858
}
5959

6060

@@ -65,6 +65,7 @@ class TestOptions : public TestFixture {
6565
{ "TestClass", {"TestMethod"} }
6666
};
6767
ASSERT(expected == args.which_tests());
68+
ASSERT(args.errors().empty());
6869
}
6970

7071

@@ -73,40 +74,41 @@ class TestOptions : public TestFixture {
7374
options args(getArrayLength(argv), argv);
7475
const std::map<std::string, std::set<std::string>> expected{};
7576
ASSERT(expected == args.which_tests());
77+
ASSERT(args.errors().empty());
7678
}
7779

7880

79-
void not_quiet() const {
80-
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"};
81+
void defaults() const {
82+
const char* argv[] = {"./test_runner", "TestClass::TestMethod"};
8183
options args(getArrayLength(argv), argv);
8284
ASSERT_EQUALS(false, args.quiet());
85+
ASSERT_EQUALS(false, args.help());
86+
ASSERT_EQUALS(false, args.summary());
87+
ASSERT_EQUALS(false, args.dry_run());
88+
ASSERT_EQUALS(false, args.exclude_tests());
89+
ASSERT(args.errors().empty());
8390
}
8491

85-
8692
void quiet() const {
8793
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-q"};
8894
options args(getArrayLength(argv), argv);
8995
ASSERT_EQUALS(true, args.quiet());
96+
ASSERT(args.errors().empty());
9097
}
9198

92-
void not_help() const {
93-
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-v"};
94-
options args(getArrayLength(argv), argv);
95-
ASSERT_EQUALS(false, args.help());
96-
}
97-
98-
9999
void help() const {
100100
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-h"};
101101
options args(getArrayLength(argv), argv);
102102
ASSERT_EQUALS(true, args.help());
103+
ASSERT(args.errors().empty());
103104
}
104105

105106

106107
void help_long() const {
107108
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "--help"};
108109
options args(getArrayLength(argv), argv);
109110
ASSERT_EQUALS(true, args.help());
111+
ASSERT(args.errors().empty());
110112
}
111113

112114
void multiple_testcases() const {
@@ -116,6 +118,7 @@ class TestOptions : public TestFixture {
116118
{ "TestClass", { "TestMethod", "AnotherTestMethod" } }
117119
};
118120
ASSERT(expected == args.which_tests());
121+
ASSERT(args.errors().empty());
119122
}
120123

121124
void multiple_testcases_ignore_duplicates() const {
@@ -125,6 +128,7 @@ class TestOptions : public TestFixture {
125128
{ "TestClass", {} }
126129
};
127130
ASSERT(expected == args.which_tests());
131+
ASSERT(args.errors().empty());
128132
}
129133

130134
void invalid_switches() const {
@@ -135,18 +139,32 @@ class TestOptions : public TestFixture {
135139
};
136140
ASSERT(expected == args.which_tests());
137141
ASSERT_EQUALS(true, args.quiet());
142+
ASSERT_EQUALS(2, args.errors().size());
143+
auto it = args.errors().cbegin();
144+
ASSERT_EQUALS("unknown option '-a'", *it);
145+
++it;
146+
ASSERT_EQUALS("unknown option '-v'", *it);
138147
}
139148

140149
void summary() const {
141150
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-n"};
142151
options args(getArrayLength(argv), argv);
143152
ASSERT_EQUALS(false, args.summary());
153+
ASSERT(args.errors().empty());
144154
}
145155

146156
void dry_run() const {
147157
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-d"};
148158
options args(getArrayLength(argv), argv);
149159
ASSERT_EQUALS(true, args.dry_run());
160+
ASSERT(args.errors().empty());
161+
}
162+
163+
void exclude_tests() const {
164+
const char* argv[] = {"./test_runner", "TestClass::TestMethod", "-x"};
165+
options args(getArrayLength(argv), argv);
166+
ASSERT_EQUALS(true, args.exclude_tests());
167+
ASSERT(args.errors().empty());
150168
}
151169
};
152170

0 commit comments

Comments
 (0)