Skip to content

Commit f4442ed

Browse files
committed
testrunner: use structs with designated initialization to pass options
1 parent 86469c0 commit f4442ed

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

test/testprocessexecutor.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,41 @@ class TestProcessExecutor : public TestFixture {
4646
return "process";
4747
}
4848

49+
struct CheckOptions
50+
{
51+
CheckOptions() noexcept = default;
52+
SHOWTIME_MODES showtime = SHOWTIME_MODES::SHOWTIME_NONE;
53+
const char* plistOutput = nullptr;
54+
std::vector<std::string> filesList;
55+
};
56+
4957
/**
5058
* Execute check using n jobs for y files which are have
5159
* identical data, given within data.
5260
*/
53-
void check(unsigned int jobs, int files, int result, const std::string &data, SHOWTIME_MODES showtime = SHOWTIME_MODES::SHOWTIME_NONE, const char* const plistOutput = nullptr, const std::vector<std::string>& filesList = {}) {
61+
void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions &opt = {}) {
5462
errout.str("");
5563
output.str("");
5664

5765
std::map<std::string, std::size_t> filemap;
58-
if (filesList.empty()) {
66+
if (opt.filesList.empty()) {
5967
for (int i = 1; i <= files; ++i) {
6068
std::ostringstream oss;
6169
oss << fprefix() << "_" << i << ".cpp";
6270
filemap[oss.str()] = data.size();
6371
}
6472
}
6573
else {
66-
for (const auto& f : filesList)
74+
for (const auto& f : opt.filesList)
6775
{
6876
filemap[f] = data.size();
6977
}
7078
}
7179

7280
settings.jobs = jobs;
73-
settings.showtime = showtime;
74-
if (plistOutput)
75-
settings.plistOutput = plistOutput;
81+
settings.showtime = opt.showtime;
82+
if (opt.plistOutput)
83+
settings.plistOutput = opt.plistOutput;
7684
// TODO: test with settings.project.fileSettings;
7785
ProcessExecutor executor(filemap, settings, *this);
7886
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
@@ -127,7 +135,7 @@ class TestProcessExecutor : public TestFixture {
127135
"{\n"
128136
" char *a = malloc(10);\n"
129137
" return 0;\n"
130-
"}", SHOWTIME_MODES::SHOWTIME_SUMMARY);
138+
"}", dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_SUMMARY));
131139
}
132140

133141
void many_threads_plist() {
@@ -139,7 +147,7 @@ class TestProcessExecutor : public TestFixture {
139147
"{\n"
140148
" char *a = malloc(10);\n"
141149
" return 0;\n"
142-
"}", SHOWTIME_MODES::SHOWTIME_NONE, plistOutput);
150+
"}", dinit(CheckOptions, $.plistOutput = plistOutput));
143151
}
144152

145153
void no_errors_more_files() {
@@ -184,7 +192,6 @@ class TestProcessExecutor : public TestFixture {
184192
"}");
185193
}
186194

187-
188195
void markup() {
189196
const Settings settingsOld = settings;
190197
settings.library.mMarkupExtensions.emplace(".cp1");
@@ -201,7 +208,7 @@ class TestProcessExecutor : public TestFixture {
201208
" char *a = malloc(10);\n"
202209
" return 0;\n"
203210
"}",
204-
SHOWTIME_MODES::SHOWTIME_NONE, nullptr, files);
211+
dinit(CheckOptions, $.filesList = files));
205212
// TODO: order of "Checking" and "checked" is affected by thread
206213
/*TODO_ASSERT_EQUALS("Checking " + fprefix() + "_2.cpp ...\n"
207214
"1/4 files checked 25% done\n"

test/testsingleexecutor.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ class TestSingleExecutorBase : public TestFixture {
6060
return std::to_string(i);
6161
}
6262

63-
void check(int files, int result, const std::string &data, SHOWTIME_MODES showtime = SHOWTIME_MODES::SHOWTIME_NONE, const char* const plistOutput = nullptr, const std::vector<std::string>& filesList = {}) {
63+
struct CheckOptions
64+
{
65+
CheckOptions() noexcept = default;
66+
SHOWTIME_MODES showtime = SHOWTIME_MODES::SHOWTIME_NONE;
67+
const char* plistOutput = nullptr;
68+
std::vector<std::string> filesList;
69+
};
70+
71+
void check(int files, int result, const std::string &data, const CheckOptions &opt = {}) {
6472
errout.str("");
6573
output.str("");
6674
settings.project.fileSettings.clear();
6775

6876
std::map<std::string, std::size_t> filemap;
69-
if (filesList.empty()) {
77+
if (opt.filesList.empty()) {
7078
for (int i = 1; i <= files; ++i) {
7179
const std::string s = fprefix() + "_" + zpad3(i) + ".cpp";
7280
filemap[s] = data.size();
@@ -78,7 +86,7 @@ class TestSingleExecutorBase : public TestFixture {
7886
}
7987
}
8088
else {
81-
for (const auto& f : filesList)
89+
for (const auto& f : opt.filesList)
8290
{
8391
filemap[f] = data.size();
8492
if (useFS) {
@@ -89,9 +97,9 @@ class TestSingleExecutorBase : public TestFixture {
8997
}
9098
}
9199

92-
settings.showtime = showtime;
93-
if (plistOutput)
94-
settings.plistOutput = plistOutput;
100+
settings.showtime = opt.showtime;
101+
if (opt.plistOutput)
102+
settings.plistOutput = opt.plistOutput;
95103
// NOLINTNEXTLINE(performance-unnecessary-value-param)
96104
CppCheck cppcheck(*this, true, [](std::string,std::vector<std::string>,std::string,std::string&){
97105
return false;
@@ -151,7 +159,7 @@ class TestSingleExecutorBase : public TestFixture {
151159
"{\n"
152160
" char *a = malloc(10);\n"
153161
" return 0;\n"
154-
"}", SHOWTIME_MODES::SHOWTIME_SUMMARY);
162+
"}", dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_SUMMARY));
155163
}
156164

157165
void many_files_plist() {
@@ -163,7 +171,7 @@ class TestSingleExecutorBase : public TestFixture {
163171
"{\n"
164172
" char *a = malloc(10);\n"
165173
" return 0;\n"
166-
"}", SHOWTIME_MODES::SHOWTIME_NONE, plistOutput);
174+
"}", dinit(CheckOptions, $.plistOutput = plistOutput));
167175
}
168176

169177
void no_errors_more_files() {
@@ -224,7 +232,7 @@ class TestSingleExecutorBase : public TestFixture {
224232
" char *a = malloc(10);\n"
225233
" return 0;\n"
226234
"}",
227-
SHOWTIME_MODES::SHOWTIME_NONE, nullptr, files);
235+
dinit(CheckOptions, $.filesList = files));
228236
// TODO: filter out the "files checked" messages
229237
ASSERT_EQUALS("Checking " + fprefix() + "_2.cpp ...\n"
230238
"1/4 files checked 25% done\n"

test/testthreadexecutor.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,42 @@ class TestThreadExecutor : public TestFixture {
4646
return "thread";
4747
}
4848

49+
struct CheckOptions
50+
{
51+
CheckOptions() noexcept = default;
52+
SHOWTIME_MODES showtime = SHOWTIME_MODES::SHOWTIME_NONE;
53+
const char* plistOutput = nullptr;
54+
std::vector<std::string> filesList;
55+
};
56+
4957
/**
5058
* Execute check using n jobs for y files which are have
5159
* identical data, given within data.
5260
*/
53-
void check(unsigned int jobs, int files, int result, const std::string &data, SHOWTIME_MODES showtime = SHOWTIME_MODES::SHOWTIME_NONE, const char* const plistOutput = nullptr, const std::vector<std::string>& filesList = {}) {
61+
void check(unsigned int jobs, int files, int result, const std::string &data, const CheckOptions &opt = {}) {
5462
errout.str("");
5563
output.str("");
5664

5765
std::map<std::string, std::size_t> filemap;
58-
if (filesList.empty()) {
66+
if (opt.filesList.empty()) {
5967
for (int i = 1; i <= files; ++i) {
6068
std::ostringstream oss;
6169
oss << fprefix() << "_" << i << ".cpp";
6270
filemap[oss.str()] = data.size();
6371
}
6472
}
6573
else {
66-
for (const auto& f : filesList)
74+
for (const auto& f : opt.filesList)
6775
{
6876
filemap[f] = data.size();
6977
}
7078
}
7179

7280
Settings settings1 = settings;
7381
settings1.jobs = jobs;
74-
settings1.showtime = showtime;
75-
if (plistOutput)
76-
settings1.plistOutput = plistOutput;
82+
settings1.showtime = opt.showtime;
83+
if (opt.plistOutput)
84+
settings1.plistOutput = opt.plistOutput;
7785
// TODO: test with settings.project.fileSettings;
7886
ThreadExecutor executor(filemap, settings1, *this);
7987
std::vector<std::unique_ptr<ScopedFile>> scopedfiles;
@@ -126,7 +134,7 @@ class TestThreadExecutor : public TestFixture {
126134
"{\n"
127135
" char *a = malloc(10);\n"
128136
" return 0;\n"
129-
"}", SHOWTIME_MODES::SHOWTIME_SUMMARY);
137+
"}", dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_SUMMARY));
130138
}
131139

132140
void many_threads_plist() {
@@ -138,7 +146,7 @@ class TestThreadExecutor : public TestFixture {
138146
"{\n"
139147
" char *a = malloc(10);\n"
140148
" return 0;\n"
141-
"}", SHOWTIME_MODES::SHOWTIME_NONE, plistOutput);
149+
"}", dinit(CheckOptions, $.plistOutput = plistOutput));
142150
}
143151

144152
void no_errors_more_files() {
@@ -199,7 +207,7 @@ class TestThreadExecutor : public TestFixture {
199207
" char *a = malloc(10);\n"
200208
" return 0;\n"
201209
"}",
202-
SHOWTIME_MODES::SHOWTIME_NONE, nullptr, files);
210+
dinit(CheckOptions, $.filesList = files));
203211
// TODO: order of "Checking" and "checked" is affected by thread
204212
/*TODO_ASSERT_EQUALS("Checking " + fprefix() + "_2.cpp ...\n"
205213
"1/4 files checked 25% done\n"

0 commit comments

Comments
 (0)