Skip to content

Commit 004b4e4

Browse files
committed
TestSuite: The ASSERT and ASSERT_EQUALS will now stop executing the test case upon failure. This can be used to avoid extra guard logic in tests.
1 parent 0336403 commit 004b4e4

5 files changed

Lines changed: 77 additions & 41 deletions

File tree

test/testsuite.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ static std::string writestr(const std::string &str, bool gccStyle = false)
134134
return ostr.str();
135135
}
136136

137-
void TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const
137+
bool TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const
138138
{
139139
if (!condition) {
140140
++fails_counter;
141141
errmsg << getLocationStr(filename, linenr) << ": Assertion failed." << std::endl << "_____" << std::endl;
142142
}
143+
return condition;
143144
}
144145

145-
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
146+
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
146147
{
147148
if (expected != actual) {
148149
++fails_counter;
@@ -155,6 +156,7 @@ void TestFixture::assertEquals(const char * const filename, const unsigned int l
155156
errmsg << "Hint:" << std::endl << msg << std::endl;
156157
errmsg << "_____" << std::endl;
157158
}
159+
return expected == actual;
158160
}
159161

160162
std::string TestFixture::deleteLineNumber(const std::string &message) const
@@ -186,20 +188,20 @@ void TestFixture::assertEqualsWithoutLineNumbers(const char * const filename, co
186188
assertEquals(filename, linenr, deleteLineNumber(expected), deleteLineNumber(actual), msg);
187189
}
188190

189-
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg) const
191+
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg) const
190192
{
191-
assertEquals(filename, linenr, std::string(expected), actual, msg);
193+
return assertEquals(filename, linenr, std::string(expected), actual, msg);
192194
}
193-
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg) const
195+
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg) const
194196
{
195-
assertEquals(filename, linenr, std::string(expected), std::string(actual), msg);
197+
return assertEquals(filename, linenr, std::string(expected), std::string(actual), msg);
196198
}
197-
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg) const
199+
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg) const
198200
{
199-
assertEquals(filename, linenr, expected, std::string(actual), msg);
201+
return assertEquals(filename, linenr, expected, std::string(actual), msg);
200202
}
201203

202-
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg) const
204+
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg) const
203205
{
204206
if (expected != actual) {
205207
std::ostringstream ostr1;
@@ -208,6 +210,7 @@ void TestFixture::assertEquals(const char * const filename, const unsigned int l
208210
ostr2 << actual;
209211
assertEquals(filename, linenr, ostr1.str(), ostr2.str(), msg);
210212
}
213+
return expected == actual;
211214
}
212215

213216
void TestFixture::assertEqualsDouble(const char * const filename, const unsigned int linenr, const double expected, const double actual, const double tolerance, const std::string &msg) const

test/testsuite.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ class TestFixture : public ErrorLogger {
5151
bool prepareTest(const char testname[]);
5252
std::string getLocationStr(const char * const filename, const unsigned int linenr) const;
5353

54-
void assert_(const char * const filename, const unsigned int linenr, const bool condition) const;
54+
bool assert_(const char * const filename, const unsigned int linenr, const bool condition) const;
5555

56-
void assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
56+
bool assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
5757
void assertEqualsWithoutLineNumbers(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
58-
void assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg = emptyString) const;
59-
void assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg = emptyString) const;
60-
void assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg = emptyString) const;
61-
void assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg = emptyString) const;
58+
bool assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg = emptyString) const;
59+
bool assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg = emptyString) const;
60+
bool assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg = emptyString) const;
61+
bool assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg = emptyString) const;
6262
void assertEqualsDouble(const char * const filename, const unsigned int linenr, const double expected, const double actual, const double tolerance, const std::string &msg = emptyString) const;
6363

6464
void todoAssertEquals(const char * const filename, const unsigned int linenr, const std::string &wanted,
@@ -98,8 +98,8 @@ extern std::ostringstream errout;
9898
extern std::ostringstream output;
9999

100100
#define TEST_CASE( NAME ) if ( prepareTest(#NAME) ) { setVerbose(false); NAME(); }
101-
#define ASSERT( CONDITION ) assert_(__FILE__, __LINE__, CONDITION)
102-
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
101+
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, CONDITION)) return
102+
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) if (!assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)) return
103103
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED , ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
104104
#define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
105105
#define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
@@ -108,6 +108,7 @@ extern std::ostringstream output;
108108
#define TODO_ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; } catch (const EXCEPTION&) { } catch (...) { assertThrow(__FILE__, __LINE__); }
109109
#define TODO_ASSERT( CONDITION ) { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); }
110110
#define TODO_ASSERT_EQUALS( WANTED , CURRENT , ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
111+
#define EXPECT_EQ( EXPECTED, ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
111112
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance_##CLASSNAME; }
112113

113114
#ifdef _WIN32

test/testsuppressions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class TestSuppressions : public TestFixture {
189189
settings.jointSuppressionReport = true;
190190
if (!suppression.empty()) {
191191
std::string r = settings.nomsg.addSuppressionLine(suppression);
192-
ASSERT_EQUALS("", r);
192+
EXPECT_EQ("", r);
193193
}
194194

195195
unsigned int exitCode = 0;
@@ -216,7 +216,7 @@ class TestSuppressions : public TestFixture {
216216
settings.inlineSuppressions = true;
217217
settings.addEnabled("information");
218218
if (!suppression.empty()) {
219-
ASSERT_EQUALS("", settings.nomsg.addSuppressionLine(suppression));
219+
EXPECT_EQ("", settings.nomsg.addSuppressionLine(suppression));
220220
}
221221
ThreadExecutor executor(files, settings, *this);
222222
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i)

test/testsymboldatabase.cpp

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5358,54 +5358,85 @@ class TestSymbolDatabase: public TestFixture {
53585358
"};");
53595359

53605360
ASSERT_EQUALS("", errout.str());
5361+
ASSERT(db);
53615362

53625363
const Token *f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v1 ) ) ;");
5363-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 4);
5364+
ASSERT(f);
5365+
ASSERT(f->function());
5366+
ASSERT_EQUALS(4, f->function()->tokenDef->linenr());
53645367

53655368
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v2 ) ) ;");
5366-
if (std::numeric_limits<char>::is_signed)
5367-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 5);
5368-
else
5369-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 10);
5369+
ASSERT(f);
5370+
ASSERT(f->function());
5371+
if (std::numeric_limits<char>::is_signed) {
5372+
ASSERT_EQUALS(5, f->function()->tokenDef->linenr());
5373+
} else {
5374+
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
5375+
}
53705376

53715377
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v3 ) ) ;");
5372-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 6);
5378+
ASSERT(f);
5379+
ASSERT(f->function());
5380+
ASSERT_EQUALS(6, f->function()->tokenDef->linenr());
53735381

53745382
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v4 ) ) ;");
5375-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 7);
5383+
ASSERT(f);
5384+
ASSERT(f->function());
5385+
ASSERT_EQUALS(7, f->function()->tokenDef->linenr());
53765386

53775387
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v5 ) ) ;");
5378-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 8);
5388+
ASSERT(f);
5389+
ASSERT(f->function());
5390+
ASSERT_EQUALS(8, f->function()->tokenDef->linenr());
53795391

53805392
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v6 ) ) ;");
5381-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 9);
5393+
ASSERT(f);
5394+
ASSERT(f->function());
5395+
ASSERT_EQUALS(9, f->function()->tokenDef->linenr());
53825396

53835397
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v7 ) ) ;");
5384-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 10);
5398+
ASSERT(f);
5399+
ASSERT(f->function());
5400+
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
53855401

53865402
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v8 ) ) ;");
5387-
if (std::numeric_limits<char>::is_signed)
5388-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 5);
5389-
else
5390-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 10);
5403+
ASSERT(f);
5404+
ASSERT(f->function());
5405+
if (std::numeric_limits<char>::is_signed) {
5406+
ASSERT_EQUALS(5, f->function()->tokenDef->linenr());
5407+
} else {
5408+
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
5409+
}
53915410

53925411
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v9 ) ) ;");
5393-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 12);
5412+
ASSERT(f);
5413+
ASSERT(f->function());
5414+
ASSERT_EQUALS(12, f->function()->tokenDef->linenr());
53945415

53955416
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v10 ) ) ;");
5396-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 13);
5417+
ASSERT(f);
5418+
ASSERT(f->function());
5419+
ASSERT_EQUALS(13, f->function()->tokenDef->linenr());
53975420

53985421
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v11 ) ) ;");
5399-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 14);
5422+
ASSERT(f);
5423+
ASSERT(f->function());
5424+
ASSERT_EQUALS(14, f->function()->tokenDef->linenr());
54005425

54015426
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v12 ) ) ;");
5402-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 15);
5427+
ASSERT(f);
5428+
ASSERT(f->function());
5429+
ASSERT_EQUALS(15, f->function()->tokenDef->linenr());
54035430

54045431
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v13 ) ) ;");
5405-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 16);
5432+
ASSERT(f);
5433+
ASSERT(f->function());
5434+
ASSERT_EQUALS(16, f->function()->tokenDef->linenr());
54065435

54075436
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v14 ) ) ;");
5408-
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 17);
5437+
ASSERT(f);
5438+
ASSERT(f->function());
5439+
ASSERT_EQUALS(17, f->function()->tokenDef->linenr());
54095440
}
54105441

54115442
void findFunction20() { // # 8280

test/testunusedfunctions.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ class TestUnusedFunctions : public TestFixture {
8181
CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this);
8282
checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings);
8383
// check() returns error if and only if errout is not empty.
84-
if (checkUnusedFunctions.check(this, settings))
84+
if (checkUnusedFunctions.check(this, settings)) {
8585
ASSERT(errout.str() != "");
86-
else
86+
} else {
8787
ASSERT_EQUALS("", errout.str());
88+
}
8889
}
8990

9091
void incondition() {

0 commit comments

Comments
 (0)