Skip to content

Commit 2995c4b

Browse files
authored
testrunner: cleaned up tests by using objects to pass the options (#7189)
1 parent 9544434 commit 2995c4b

34 files changed

+1452
-1306
lines changed

Diff for: test/test64bit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class Test64BitPortability : public TestFixture {
4141
TEST_CASE(assignment);
4242
}
4343

44-
#define check(code) check_(code, __FILE__, __LINE__)
44+
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
4545
template<size_t size>
46-
void check_(const char (&code)[size], const char* file, int line) {
46+
void check_(const char* file, int line, const char (&code)[size]) {
4747
// Tokenize..
4848
SimpleTokenizer tokenizer(settings, *this);
4949
ASSERT_LOC(tokenizer.tokenize(code), file, line);

Diff for: test/testastutils.cpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ class TestAstUtils : public TestFixture {
8888
ASSERT_EQUALS(true, findLambdaEndToken("int i = 5 * []{ return 7; }();", "[", /*checkNext*/ false));
8989
}
9090

91-
#define findLambdaStartToken(code) findLambdaStartToken_(code, __FILE__, __LINE__)
91+
#define findLambdaStartToken(...) findLambdaStartToken_(__FILE__, __LINE__, __VA_ARGS__)
9292
template<size_t size>
93-
bool findLambdaStartToken_(const char (&code)[size], const char* file, int line) {
93+
bool findLambdaStartToken_(const char* file, int line, const char (&code)[size]) {
9494
SimpleTokenizer tokenizer(settingsDefault, *this);
9595
ASSERT_LOC(tokenizer.tokenize(code), file, line);
9696
const Token * const tokStart = (::findLambdaStartToken)(tokenizer.list.back());
@@ -120,9 +120,9 @@ class TestAstUtils : public TestFixture {
120120
ASSERT_EQUALS(true, findLambdaStartToken("[](void) constexpr -> const * const* int { return x; }"));
121121
}
122122

123-
#define isNullOperand(code) isNullOperand_(code, __FILE__, __LINE__)
123+
#define isNullOperand(...) isNullOperand_(__FILE__, __LINE__, __VA_ARGS__)
124124
template<size_t size>
125-
bool isNullOperand_(const char (&code)[size], const char* file, int line) {
125+
bool isNullOperand_(const char* file, int line, const char (&code)[size]) {
126126
SimpleTokenizer tokenizer(settingsDefault, *this);
127127
ASSERT_LOC(tokenizer.tokenize(code), file, line);
128128
return (::isNullOperand)(tokenizer.tokens());
@@ -141,9 +141,9 @@ class TestAstUtils : public TestFixture {
141141
ASSERT_EQUALS(false, isNullOperand("(void*)1;"));
142142
}
143143

144-
#define isReturnScope(code, offset) isReturnScope_(code, offset, __FILE__, __LINE__)
144+
#define isReturnScope(...) isReturnScope_(__FILE__, __LINE__, __VA_ARGS__)
145145
template<size_t size>
146-
bool isReturnScope_(const char (&code)[size], int offset, const char* file, int line) {
146+
bool isReturnScope_(const char* file, int line, const char (&code)[size], int offset) {
147147
SimpleTokenizer tokenizer(settingsDefault, *this);
148148
ASSERT_LOC(tokenizer.tokenize(code), file, line);
149149
const Token * const tok = (offset < 0)
@@ -219,9 +219,9 @@ class TestAstUtils : public TestFixture {
219219
isSameExpressionTestInternal(false);
220220
}
221221

222-
#define isVariableChanged(code, startPattern, endPattern) isVariableChanged_(code, startPattern, endPattern, __FILE__, __LINE__)
222+
#define isVariableChanged(...) isVariableChanged_(__FILE__, __LINE__, __VA_ARGS__)
223223
template<size_t size>
224-
bool isVariableChanged_(const char (&code)[size], const char startPattern[], const char endPattern[], const char* file, int line) {
224+
bool isVariableChanged_(const char* file, int line, const char (&code)[size], const char startPattern[], const char endPattern[]) {
225225
SimpleTokenizer tokenizer(settingsDefault, *this);
226226
ASSERT_LOC(tokenizer.tokenize(code), file, line);
227227
const Token * const tok1 = Token::findsimplematch(tokenizer.tokens(), startPattern, strlen(startPattern));
@@ -252,9 +252,9 @@ class TestAstUtils : public TestFixture {
252252
ASSERT_EQUALS(false, isVariableChanged("const int A[] = { 1, 2, 3 };", "[", "]"));
253253
}
254254

255-
#define isVariableChangedByFunctionCall(code, pattern, inconclusive) isVariableChangedByFunctionCall_(code, pattern, inconclusive, __FILE__, __LINE__)
255+
#define isVariableChangedByFunctionCall(...) isVariableChangedByFunctionCall_( __FILE__, __LINE__, __VA_ARGS__)
256256
template<size_t size>
257-
bool isVariableChangedByFunctionCall_(const char (&code)[size], const char pattern[], bool *inconclusive, const char* file, int line) {
257+
bool isVariableChangedByFunctionCall_(const char* file, int line, const char (&code)[size], const char pattern[], bool *inconclusive) {
258258
SimpleTokenizer tokenizer(settingsDefault, *this);
259259
ASSERT_LOC(tokenizer.tokenize(code), file, line);
260260
const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern);
@@ -412,15 +412,14 @@ class TestAstUtils : public TestFixture {
412412
}
413413
}
414414

415-
#define isExpressionChanged(code, var, startPattern, endPattern) \
416-
isExpressionChanged_(code, var, startPattern, endPattern, __FILE__, __LINE__)
415+
#define isExpressionChanged(...) isExpressionChanged_(__FILE__, __LINE__, __VA_ARGS__)
417416
template<size_t size>
418-
bool isExpressionChanged_(const char (&code)[size],
417+
bool isExpressionChanged_(const char* file,
418+
int line,
419+
const char (&code)[size],
419420
const char var[],
420421
const char startPattern[],
421-
const char endPattern[],
422-
const char* file,
423-
int line)
422+
const char endPattern[])
424423
{
425424
const Settings settings = settingsBuilder().library("std.cfg").build();
426425
SimpleTokenizer tokenizer(settings, *this);
@@ -451,9 +450,9 @@ class TestAstUtils : public TestFixture {
451450
"}"));
452451
}
453452

454-
#define nextAfterAstRightmostLeaf(code, parentPattern, rightPattern) nextAfterAstRightmostLeaf_(code, parentPattern, rightPattern, __FILE__, __LINE__)
453+
#define nextAfterAstRightmostLeaf(...) nextAfterAstRightmostLeaf_(__FILE__, __LINE__, __VA_ARGS__)
455454
template<size_t size>
456-
bool nextAfterAstRightmostLeaf_(const char (&code)[size], const char parentPattern[], const char rightPattern[], const char* file, int line) {
455+
bool nextAfterAstRightmostLeaf_(const char* file, int line, const char (&code)[size], const char parentPattern[], const char rightPattern[]) {
457456
SimpleTokenizer tokenizer(settingsDefault, *this);
458457
ASSERT_LOC(tokenizer.tokenize(code), file, line);
459458
const Token * tok = Token::findsimplematch(tokenizer.tokens(), parentPattern, strlen(parentPattern));

Diff for: test/testautovariables.cpp

+34-39
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,21 @@ class TestAutoVariables : public TestFixture {
3131
private:
3232
const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).library("std.cfg").library("qt.cfg").build();
3333

34+
struct CheckOptions
35+
{
36+
CheckOptions() = default;
37+
bool inconclusive = true;
38+
bool cpp = true;
39+
};
40+
3441
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
3542
template<size_t size>
36-
void check_(const char* file, int line, const char (&code)[size], bool inconclusive = true, bool cpp = true) {
37-
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();
43+
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
44+
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, options.inconclusive).build();
3845

3946
// Tokenize..
4047
SimpleTokenizer tokenizer(settings1, *this);
41-
ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line);
48+
ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line);
4249

4350
runChecks<CheckAutoVariables>(tokenizer, this);
4451
}
@@ -248,14 +255,14 @@ class TestAutoVariables : public TestFixture {
248255
"{\n"
249256
" char a[10];\n"
250257
" x->str = a;\n"
251-
"}", false);
258+
"}", dinit(CheckOptions, $.inconclusive = false));
252259
ASSERT_EQUALS("", errout_str());
253260

254261
check("void foo(struct X *x)\n"
255262
"{\n"
256263
" char a[10];\n"
257264
" x->str = a;\n"
258-
"}", true);
265+
"}");
259266
ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Address of local auto-variable assigned to a function parameter.\n", errout_str());
260267
}
261268

@@ -265,20 +272,20 @@ class TestAutoVariables : public TestFixture {
265272
" struct txt_scrollpane_s * scrollpane;\n"
266273
" target->parent = &scrollpane->widget;\n"
267274
" return scrollpane;\n"
268-
"}", false);
275+
"}", dinit(CheckOptions, $.inconclusive = false));
269276
ASSERT_EQUALS("", errout_str());
270277
}
271278

272279
void testautovar8() {
273280
check("void foo(int*& p) {\n"
274281
" int i = 0;\n"
275282
" p = &i;\n"
276-
"}", false);
283+
"}", dinit(CheckOptions, $.inconclusive = false));
277284
ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout_str());
278285

279286
check("void foo(std::string& s) {\n"
280287
" s = foo;\n"
281-
"}", false);
288+
"}", dinit(CheckOptions, $.inconclusive = false));
282289
ASSERT_EQUALS("", errout_str());
283290
}
284291

@@ -289,7 +296,7 @@ class TestAutoVariables : public TestFixture {
289296
" FN fn;\n"
290297
" FP fp;\n"
291298
" p = &fn.i;\n"
292-
"}", false);
299+
"}", dinit(CheckOptions, $.inconclusive = false));
293300
ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout_str());
294301

295302
check("struct FN {int i;};\n"
@@ -298,7 +305,7 @@ class TestAutoVariables : public TestFixture {
298305
" FN fn;\n"
299306
" FP fp;\n"
300307
" p = &p_fp->i;\n"
301-
"}", false);
308+
"}", dinit(CheckOptions, $.inconclusive = false));
302309
ASSERT_EQUALS("", errout_str());
303310

304311
check("struct FN {int i;};\n"
@@ -307,7 +314,7 @@ class TestAutoVariables : public TestFixture {
307314
" FN fn;\n"
308315
" FP fp;\n"
309316
" p = &fp.f->i;\n"
310-
"}", false);
317+
"}", dinit(CheckOptions, $.inconclusive = false));
311318
ASSERT_EQUALS("", errout_str());
312319
}
313320

@@ -385,7 +392,7 @@ class TestAutoVariables : public TestFixture {
385392
" int i = d;\n"
386393
" d = i;\n"
387394
" return d;"
388-
"}",false);
395+
"}",dinit(CheckOptions, $.inconclusive = false));
389396
ASSERT_EQUALS("", errout_str());
390397

391398
check("void foo(int* ptr) {\n" // #4793
@@ -482,7 +489,7 @@ class TestAutoVariables : public TestFixture {
482489
" if (lumdiff > 5.0f)\n"
483490
" return &darkOutline;\n"
484491
" return 0;\n"
485-
"}", false);
492+
"}", dinit(CheckOptions, $.inconclusive = false));
486493
ASSERT_EQUALS("", errout_str());
487494
}
488495

@@ -917,7 +924,7 @@ class TestAutoVariables : public TestFixture {
917924
check("void svn_repos_dir_delta2() {\n"
918925
" struct context c;\n"
919926
" SVN_ERR(delete(&c, root_baton, src_entry, pool));\n"
920-
"}\n", false, /* cpp= */ false);
927+
"}\n", dinit(CheckOptions, $.inconclusive = false, $.cpp = false));
921928
ASSERT_EQUALS("", errout_str());
922929
}
923930

@@ -1338,7 +1345,7 @@ class TestAutoVariables : public TestFixture {
13381345
" double ret = getValue();\n"
13391346
" rd = ret;\n"
13401347
" return rd;\n"
1341-
"}", false);
1348+
"}", dinit(CheckOptions, $.inconclusive = false));
13421349
ASSERT_EQUALS("", errout_str());
13431350
}
13441351

@@ -1814,8 +1821,7 @@ class TestAutoVariables : public TestFixture {
18141821
"const int& bar(const std::unordered_map<int, int>& m, int k) {\n"
18151822
" auto x = 0;\n"
18161823
" return get_default(m, k, x);\n"
1817-
"}\n",
1818-
true);
1824+
"}\n");
18191825
ASSERT_EQUALS(
18201826
"[test.cpp:2] -> [test.cpp:4] -> [test.cpp:9] -> [test.cpp:9]: (error, inconclusive) Reference to local variable returned.\n",
18211827
errout_str());
@@ -1828,8 +1834,7 @@ class TestAutoVariables : public TestFixture {
18281834
"}\n"
18291835
"const int& bar(const std::unordered_map<int, int>& m, int k) {\n"
18301836
" return get_default(m, k, 0);\n"
1831-
"}\n",
1832-
true);
1837+
"}\n");
18331838
ASSERT_EQUALS(
18341839
"[test.cpp:2] -> [test.cpp:4] -> [test.cpp:8] -> [test.cpp:8]: (error, inconclusive) Reference to temporary returned.\n",
18351840
errout_str());
@@ -2579,8 +2584,7 @@ class TestAutoVariables : public TestFixture {
25792584
"const int* bar(const std::unordered_map<int, int>& m, int k) {\n"
25802585
" auto x = 0;\n"
25812586
" return get_default(m, k, &x);\n"
2582-
"}\n",
2583-
true);
2587+
"}\n");
25842588
ASSERT_EQUALS(
25852589
"[test.cpp:9] -> [test.cpp:9] -> [test.cpp:8] -> [test.cpp:9]: (error, inconclusive) Returning pointer to local variable 'x' that will be invalid when returning.\n",
25862590
errout_str());
@@ -2813,15 +2817,13 @@ class TestAutoVariables : public TestFixture {
28132817

28142818
check("std::string f(std::string Str, int first, int last) {\n"
28152819
" return { Str.begin() + first, Str.begin() + last + 1 };\n"
2816-
"}\n",
2817-
true);
2820+
"}\n");
28182821
ASSERT_EQUALS("", errout_str());
28192822

28202823
check("std::string f(std::string s) {\n"
28212824
" std::string r = { s.begin(), s.end() };\n"
28222825
" return r;\n"
2823-
"}\n",
2824-
true);
2826+
"}\n");
28252827
ASSERT_EQUALS("", errout_str());
28262828

28272829
check("struct A {\n"
@@ -3599,8 +3601,7 @@ class TestAutoVariables : public TestFixture {
35993601
" int i = 0;\n"
36003602
" A a{i};\n"
36013603
" return a;\n"
3602-
"}\n",
3603-
true);
3604+
"}\n");
36043605
ASSERT_EQUALS(
36053606
"[test.cpp:7] -> [test.cpp:6] -> [test.cpp:8]: (error, inconclusive) Returning object that points to local variable 'i' that will be invalid when returning.\n",
36063607
errout_str());
@@ -3613,8 +3614,7 @@ class TestAutoVariables : public TestFixture {
36133614
" int i = 0;\n"
36143615
" A a{i};\n"
36153616
" return a;\n"
3616-
"}\n",
3617-
true);
3617+
"}\n");
36183618
ASSERT_EQUALS("", errout_str());
36193619

36203620
check("struct A {\n"
@@ -3767,8 +3767,7 @@ class TestAutoVariables : public TestFixture {
37673767
"S f() {\n"
37683768
" std::string m(\"abc\");\n"
37693769
" return S(m);\n"
3770-
"}\n",
3771-
true);
3770+
"}\n");
37723771
ASSERT_EQUALS("", errout_str());
37733772

37743773
check("struct S {\n"
@@ -3778,8 +3777,7 @@ class TestAutoVariables : public TestFixture {
37783777
"S f() {\n"
37793778
" std::string s(\"abc\");\n"
37803779
" return S(s.c_str());\n"
3781-
"}\n",
3782-
true);
3780+
"}\n");
37833781
ASSERT_EQUALS("", errout_str());
37843782

37853783
check("struct S {\n"
@@ -3791,8 +3789,7 @@ class TestAutoVariables : public TestFixture {
37913789
"void f(const std::stringstream& buffer) {\n"
37923790
" S s(buffer.str().c_str());\n"
37933791
" s.g();\n"
3794-
"}\n",
3795-
true);
3792+
"}\n");
37963793
ASSERT_EQUALS("", errout_str());
37973794
}
37983795

@@ -4174,16 +4171,14 @@ class TestAutoVariables : public TestFixture {
41744171
"void T::f() {\n"
41754172
" U u(p->g().c_str());\n"
41764173
" if (u.h()) {}\n"
4177-
"}\n",
4178-
true);
4174+
"}\n");
41794175
ASSERT_EQUALS("", errout_str());
41804176

41814177
// #11442
41824178
check("const std::string& f(const P< std::string >& value) {\n"
41834179
" static const std::string empty;\n"
41844180
" return value.get() == nullptr ? empty : *value;\n"
4185-
"}\n",
4186-
true);
4181+
"}\n");
41874182
ASSERT_EQUALS("", errout_str());
41884183

41894184
// #11472

Diff for: test/testbool.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,18 @@ class TestBool : public TestFixture {
7575
TEST_CASE(returnNonBoolClass);
7676
}
7777

78+
struct CheckOptions
79+
{
80+
CheckOptions() = default;
81+
bool cpp = true;
82+
};
83+
7884
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
7985
template<size_t size>
80-
void check_(const char* file, int line, const char (&code)[size], bool cpp = true) {
86+
void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) {
8187
// Tokenize..
8288
SimpleTokenizer tokenizer(settings, *this);
83-
ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line);
89+
ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line);
8490

8591
// Check...
8692
runChecks<CheckBool>(tokenizer, this);
@@ -146,7 +152,7 @@ class TestBool : public TestFixture {
146152
" const int *rmat = n < 4 ? " /* OK */
147153
" ctx->q_intra_matrix :"
148154
" ctx->q_chroma_intra_matrix;\n"
149-
"}", false);
155+
"}", dinit(CheckOptions, $.cpp = false));
150156
ASSERT_EQUALS("[test.c:3]: (error) Boolean value assigned to pointer.\n", errout_str());
151157

152158
// ticket #6588 (c++ mode)
@@ -165,7 +171,7 @@ class TestBool : public TestFixture {
165171
" char* m1 = compare(a, b) < 0\n"
166172
" ? (compare(b, c) < 0 ? b : (compare(a, c) < 0 ? c : a))\n"
167173
" : (compare(a, c) < 0 ? a : (compare(b, c) < 0 ? c : b));\n"
168-
"}", false);
174+
"}", dinit(CheckOptions, $.cpp = false));
169175
ASSERT_EQUALS("", errout_str());
170176

171177
// #7381

0 commit comments

Comments
 (0)