Skip to content

Commit 7be1dff

Browse files
authored
testrunner: removed unused and inconsistently used return value from assert*() functions (cppcheck-opensource#6708)
1 parent 97c5886 commit 7be1dff

7 files changed

Lines changed: 87 additions & 88 deletions

File tree

test/fixture.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,15 @@ static std::string writestr(const std::string &str, bool gccStyle = false)
162162
return ostr.str();
163163
}
164164

165-
bool TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const
165+
void TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const
166166
{
167167
if (!condition) {
168168
++fails_counter;
169169
errmsg << getLocationStr(filename, linenr) << ": Assertion failed." << std::endl << "_____" << std::endl;
170170
}
171-
return condition;
172171
}
173172

174-
void TestFixture::assertEqualsFailed(const char* const filename, const unsigned int linenr, const std::string& expected, const std::string& actual, const std::string& msg) const
173+
void TestFixture::assertFailure(const char* const filename, const unsigned int linenr, const std::string& expected, const std::string& actual, const std::string& msg) const
175174
{
176175
++fails_counter;
177176
errmsg << getLocationStr(filename, linenr) << ": Assertion failed. " << std::endl
@@ -184,12 +183,11 @@ void TestFixture::assertEqualsFailed(const char* const filename, const unsigned
184183
errmsg << "_____" << std::endl;
185184
}
186185

187-
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
186+
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
188187
{
189188
if (expected != actual) {
190-
assertEqualsFailed(filename, linenr, expected, actual, msg);
189+
assertFailure(filename, linenr, expected, actual, msg);
191190
}
192-
return expected == actual;
193191
}
194192

195193
std::string TestFixture::deleteLineNumber(const std::string &message)
@@ -216,42 +214,42 @@ std::string TestFixture::deleteLineNumber(const std::string &message)
216214
return result;
217215
}
218216

219-
bool TestFixture::assertEqualsWithoutLineNumbers(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
217+
void TestFixture::assertEqualsWithoutLineNumbers(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
220218
{
221-
return assertEquals(filename, linenr, deleteLineNumber(expected), deleteLineNumber(actual), msg);
219+
assertEquals(filename, linenr, deleteLineNumber(expected), deleteLineNumber(actual), msg);
222220
}
223221

224-
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg) const
222+
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg) const
225223
{
226-
return assertEquals(filename, linenr, std::string(expected), actual, msg);
224+
assertEquals(filename, linenr, std::string(expected), actual, msg);
227225
}
228-
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg) const
226+
227+
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg) const
229228
{
230-
return assertEquals(filename, linenr, std::string(expected), std::string(actual), msg);
229+
assertEquals(filename, linenr, std::string(expected), std::string(actual), msg);
231230
}
232-
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg) const
231+
232+
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg) const
233233
{
234-
return assertEquals(filename, linenr, expected, std::string(actual), msg);
234+
assertEquals(filename, linenr, expected, std::string(actual), msg);
235235
}
236236

237-
bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg) const
237+
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg) const
238238
{
239239
if (expected != actual) {
240-
return assertEquals(filename, linenr, std::to_string(expected), std::to_string(actual), msg);
240+
assertEquals(filename, linenr, std::to_string(expected), std::to_string(actual), msg);
241241
}
242-
return true;
243242
}
244243

245-
bool TestFixture::assertEqualsDouble(const char * const filename, const unsigned int linenr, const double expected, const double actual, const double tolerance, const std::string &msg) const
244+
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
246245
{
247246
if (expected < (actual - tolerance) || expected > (actual + tolerance)) {
248247
std::ostringstream ostr1;
249248
ostr1 << expected;
250249
std::ostringstream ostr2;
251250
ostr2 << actual;
252-
return assertEquals(filename, linenr, ostr1.str(), ostr2.str(), msg);
251+
assertEquals(filename, linenr, ostr1.str(), ostr2.str(), msg);
253252
}
254-
return false;
255253
}
256254

257255
void TestFixture::todoAssertEquals(const char * const filename, const unsigned int linenr,
@@ -265,7 +263,7 @@ void TestFixture::todoAssertEquals(const char * const filename, const unsigned i
265263

266264
++succeeded_todos_counter;
267265
} else {
268-
(void)assertEquals(filename, linenr, current, actual);
266+
assertEquals(filename, linenr, current, actual);
269267
++todos_counter;
270268
}
271269
}
@@ -289,15 +287,13 @@ void TestFixture::assertThrow(const char * const filename, const unsigned int li
289287
++fails_counter;
290288
errmsg << getLocationStr(filename, linenr) << ": Assertion succeeded. "
291289
<< "The expected exception was thrown" << std::endl << "_____" << std::endl;
292-
293290
}
294291

295292
void TestFixture::assertThrowFail(const char * const filename, const unsigned int linenr) const
296293
{
297294
++fails_counter;
298295
errmsg << getLocationStr(filename, linenr) << ": Assertion failed. "
299296
<< "The expected exception was not thrown" << std::endl << "_____" << std::endl;
300-
301297
}
302298

303299
void TestFixture::assertNoThrowFail(const char * const filename, const unsigned int linenr) const
@@ -322,7 +318,6 @@ void TestFixture::assertNoThrowFail(const char * const filename, const unsigned
322318

323319
errmsg << getLocationStr(filename, linenr) << ": Assertion failed. "
324320
<< "Unexpected exception was thrown: " << ex_msg << std::endl << "_____" << std::endl;
325-
326321
}
327322

328323
void TestFixture::printHelp()

test/fixture.h

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,35 @@ class TestFixture : public ErrorLogger {
6868
virtual void teardownTestInternal() {}
6969
std::string getLocationStr(const char * filename, unsigned int linenr) const;
7070

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

7373
template<typename T>
74-
bool assertEquals(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
74+
void assertEquals(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
7575
if (expected != actual) {
7676
std::ostringstream expectedStr;
7777
expectedStr << expected;
7878
std::ostringstream actualStr;
7979
actualStr << actual;
8080

81-
assertEqualsFailed(filename, linenr, expectedStr.str(), actualStr.str(), msg);
81+
assertFailure(filename, linenr, expectedStr.str(), actualStr.str(), msg);
8282
}
83-
return expected == actual;
8483
}
8584

8685
template<typename T>
87-
bool assertEqualsEnum(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
86+
void assertEqualsEnum(const char* const filename, const unsigned int linenr, const T& expected, const T& actual, const std::string& msg = emptyString) const {
8887
if (std::is_unsigned<T>())
89-
return assertEquals(filename, linenr, static_cast<std::uint64_t>(expected), static_cast<std::uint64_t>(actual), msg);
90-
return assertEquals(filename, linenr, static_cast<std::int64_t>(expected), static_cast<std::int64_t>(actual), msg);
88+
assertEquals(filename, linenr, static_cast<std::uint64_t>(expected), static_cast<std::uint64_t>(actual), msg);
89+
else
90+
assertEquals(filename, linenr, static_cast<std::int64_t>(expected), static_cast<std::int64_t>(actual), msg);
9191
}
9292

93-
//Helper function to be called when an assertEquals assertion fails.
94-
//Writes the appropriate failure message to errmsg and increments fails_counter
95-
void assertEqualsFailed(const char* filename, unsigned int linenr, const std::string& expected, const std::string& actual, const std::string& msg) const;
96-
97-
bool assertEquals(const char * filename, unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
98-
bool assertEqualsWithoutLineNumbers(const char * filename, unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
99-
bool assertEquals(const char * filename, unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg = emptyString) const;
100-
bool assertEquals(const char * filename, unsigned int linenr, const char expected[], const char actual[], const std::string &msg = emptyString) const;
101-
bool assertEquals(const char * filename, unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg = emptyString) const;
102-
bool assertEquals(const char * filename, unsigned int linenr, long long expected, long long actual, const std::string &msg = emptyString) const;
103-
bool assertEqualsDouble(const char * filename, unsigned int linenr, double expected, double actual, double tolerance, const std::string &msg = emptyString) const;
93+
void assertEquals(const char * filename, unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
94+
void assertEqualsWithoutLineNumbers(const char * filename, unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
95+
void assertEquals(const char * filename, unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg = emptyString) const;
96+
void assertEquals(const char * filename, unsigned int linenr, const char expected[], const char actual[], const std::string &msg = emptyString) const;
97+
void assertEquals(const char * filename, unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg = emptyString) const;
98+
void assertEquals(const char * filename, unsigned int linenr, long long expected, long long actual, const std::string &msg = emptyString) const;
99+
void assertEqualsDouble(const char * filename, unsigned int linenr, double expected, double actual, double tolerance, const std::string &msg = emptyString) const;
104100

105101
void todoAssertEquals(const char * filename, unsigned int linenr, const std::string &wanted,
106102
const std::string &current, const std::string &actual) const;
@@ -267,6 +263,10 @@ class TestFixture : public ErrorLogger {
267263
const Settings settingsDefault;
268264

269265
private:
266+
//Helper function to be called when an assertEquals assertion fails.
267+
//Writes the appropriate failure message to errmsg and increments fails_counter
268+
void assertFailure(const char* filename, unsigned int linenr, const std::string& expected, const std::string& actual, const std::string& msg) const;
269+
270270
std::ostringstream mOutput;
271271
std::ostringstream mErrout;
272272

@@ -295,31 +295,31 @@ class TestInstance {
295295
std::unique_ptr<TestFixture> impl;
296296
};
297297

298-
// TODO: most asserts do not actually assert i.e. do not return
299298
#define TEST_CASE( NAME ) do { if (prepareTest(#NAME)) { setVerbose(false); try { NAME(); teardownTest(); } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } } while (false)
300-
#define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, (CONDITION))) return
301-
#define ASSERT_LOC( CONDITION, FILE_, LINE_ ) (void)assert_(FILE_, LINE_, (CONDITION))
302-
#define CHECK_EQUALS( EXPECTED, ACTUAL ) (void)assertEquals(__FILE__, __LINE__, (EXPECTED), (ACTUAL))
299+
300+
// TODO: the asserts do not actually assert i.e. do stop executing the test
301+
#define ASSERT( CONDITION ) assert_(__FILE__, __LINE__, (CONDITION))
302+
#define ASSERT_LOC( CONDITION, FILE_, LINE_ ) assert_(FILE_, LINE_, (CONDITION))
303303
// *INDENT-OFF*
304-
#define ASSERT_EQUALS( EXPECTED, ACTUAL ) do { try { if (!assertEquals(__FILE__, __LINE__, (EXPECTED), (ACTUAL))) return; } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } while (false)
304+
#define ASSERT_EQUALS( EXPECTED, ACTUAL ) do { try { assertEquals(__FILE__, __LINE__, (EXPECTED), (ACTUAL)); } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } while (false)
305305
// *INDENT-ON*
306-
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED, ACTUAL ) (void)assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
307-
#define ASSERT_EQUALS_DOUBLE( EXPECTED, ACTUAL, TOLERANCE ) (void)assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
308-
#define ASSERT_EQUALS_LOC_MSG( EXPECTED, ACTUAL, MSG, FILE_, LINE_ ) (void)assertEquals(FILE_, LINE_, EXPECTED, ACTUAL, MSG)
309-
#define ASSERT_EQUALS_MSG( EXPECTED, ACTUAL, MSG ) (void)assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
310-
#define ASSERT_EQUALS_ENUM( EXPECTED, ACTUAL ) if (!assertEqualsEnum(__FILE__, __LINE__, (EXPECTED), (ACTUAL))) return
306+
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED, ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
307+
#define ASSERT_EQUALS_DOUBLE( EXPECTED, ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
308+
#define ASSERT_EQUALS_LOC_MSG( EXPECTED, ACTUAL, MSG, FILE_, LINE_ ) assertEquals(FILE_, LINE_, EXPECTED, ACTUAL, MSG)
309+
#define ASSERT_EQUALS_MSG( EXPECTED, ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
310+
#define ASSERT_EQUALS_ENUM( EXPECTED, ACTUAL ) assertEqualsEnum(__FILE__, __LINE__, (EXPECTED), (ACTUAL))
311311
#define ASSERT_THROW( CMD, EXCEPTION ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) {} catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
312-
#define ASSERT_THROW_EQUALS( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { (void)assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
313-
#define ASSERT_THROW_EQUALS_2( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { (void)assertEquals(__FILE__, __LINE__, EXPECTED, e.what()); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
314-
#define ASSERT_THROW_INTERNAL( CMD, TYPE ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const InternalError& e) { (void)assertEqualsEnum(__FILE__, __LINE__, InternalError::TYPE, e.type); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
315-
#define ASSERT_THROW_INTERNAL_EQUALS( CMD, TYPE, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const InternalError& e) { (void)assertEqualsEnum(__FILE__, __LINE__, InternalError::TYPE, e.type); (void)assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
312+
#define ASSERT_THROW_EQUALS( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
313+
#define ASSERT_THROW_EQUALS_2( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.what()); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
314+
#define ASSERT_THROW_INTERNAL( CMD, TYPE ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const InternalError& e) { assertEqualsEnum(__FILE__, __LINE__, InternalError::TYPE, e.type); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
315+
#define ASSERT_THROW_INTERNAL_EQUALS( CMD, TYPE, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const InternalError& e) { assertEqualsEnum(__FILE__, __LINE__, InternalError::TYPE, e.type); assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false)
316316
#define ASSERT_NO_THROW( CMD ) do { try { (void)(CMD); } catch (...) { assertNoThrowFail(__FILE__, __LINE__); } } while (false)
317317
#define TODO_ASSERT_THROW( CMD, EXCEPTION ) do { try { (void)(CMD); } catch (const EXCEPTION&) {} catch (...) { assertThrow(__FILE__, __LINE__); } } while (false)
318318
#define TODO_ASSERT( CONDITION ) do { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); } while (false)
319319
#define TODO_ASSERT_EQUALS( WANTED, CURRENT, ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
320-
#define EXPECT_EQ( EXPECTED, ACTUAL ) (void)assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
320+
321321
#define REGISTER_TEST( CLASSNAME ) namespace { class CLASSNAME ## Instance : public TestInstance { public: CLASSNAME ## Instance() : TestInstance(#CLASSNAME) {} TestFixture* create() override { impl.reset(new CLASSNAME); return impl.get(); } }; CLASSNAME ## Instance instance_ ## CLASSNAME; }
322322

323-
#define PLATFORM( P, T ) do { std::string errstr; (void)assertEquals(__FILE__, __LINE__, true, P.set(Platform::toString(T), errstr, {exename}), errstr); } while (false)
323+
#define PLATFORM( P, T ) do { std::string errstr; assertEquals(__FILE__, __LINE__, true, P.set(Platform::toString(T), errstr, {exename}), errstr); } while (false)
324324

325325
#endif // fixtureH

0 commit comments

Comments
 (0)