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.

This commit is contained in:
Daniel Marjamäki 2019-09-16 06:34:45 +02:00
parent 033640310b
commit 004b4e4dbe
5 changed files with 77 additions and 41 deletions

View File

@ -134,15 +134,16 @@ static std::string writestr(const std::string &str, bool gccStyle = false)
return ostr.str(); return ostr.str();
} }
void TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const bool TestFixture::assert_(const char * const filename, const unsigned int linenr, const bool condition) const
{ {
if (!condition) { if (!condition) {
++fails_counter; ++fails_counter;
errmsg << getLocationStr(filename, linenr) << ": Assertion failed." << std::endl << "_____" << std::endl; errmsg << getLocationStr(filename, linenr) << ": Assertion failed." << std::endl << "_____" << std::endl;
} }
return condition;
} }
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg) const
{ {
if (expected != actual) { if (expected != actual) {
++fails_counter; ++fails_counter;
@ -155,6 +156,7 @@ void TestFixture::assertEquals(const char * const filename, const unsigned int l
errmsg << "Hint:" << std::endl << msg << std::endl; errmsg << "Hint:" << std::endl << msg << std::endl;
errmsg << "_____" << std::endl; errmsg << "_____" << std::endl;
} }
return expected == actual;
} }
std::string TestFixture::deleteLineNumber(const std::string &message) const std::string TestFixture::deleteLineNumber(const std::string &message) const
@ -186,20 +188,20 @@ void TestFixture::assertEqualsWithoutLineNumbers(const char * const filename, co
assertEquals(filename, linenr, deleteLineNumber(expected), deleteLineNumber(actual), msg); assertEquals(filename, linenr, deleteLineNumber(expected), deleteLineNumber(actual), msg);
} }
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg) const bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg) const
{ {
assertEquals(filename, linenr, std::string(expected), actual, msg); return assertEquals(filename, linenr, std::string(expected), actual, msg);
} }
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg) const bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg) const
{ {
assertEquals(filename, linenr, std::string(expected), std::string(actual), msg); return assertEquals(filename, linenr, std::string(expected), std::string(actual), msg);
} }
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg) const bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg) const
{ {
assertEquals(filename, linenr, expected, std::string(actual), msg); return assertEquals(filename, linenr, expected, std::string(actual), msg);
} }
void TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg) const bool TestFixture::assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg) const
{ {
if (expected != actual) { if (expected != actual) {
std::ostringstream ostr1; std::ostringstream ostr1;
@ -208,6 +210,7 @@ void TestFixture::assertEquals(const char * const filename, const unsigned int l
ostr2 << actual; ostr2 << actual;
assertEquals(filename, linenr, ostr1.str(), ostr2.str(), msg); assertEquals(filename, linenr, ostr1.str(), ostr2.str(), msg);
} }
return expected == actual;
} }
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 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

View File

@ -51,14 +51,14 @@ protected:
bool prepareTest(const char testname[]); bool prepareTest(const char testname[]);
std::string getLocationStr(const char * const filename, const unsigned int linenr) const; std::string getLocationStr(const char * const filename, const unsigned int linenr) const;
void assert_(const char * const filename, const unsigned int linenr, const bool condition) const; bool assert_(const char * const filename, const unsigned int linenr, const bool condition) const;
void assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const; bool assertEquals(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
void assertEqualsWithoutLineNumbers(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const; void assertEqualsWithoutLineNumbers(const char * const filename, const unsigned int linenr, const std::string &expected, const std::string &actual, const std::string &msg = emptyString) const;
void assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg = emptyString) const; bool assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const std::string& actual, const std::string &msg = emptyString) const;
void assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg = emptyString) const; bool assertEquals(const char * const filename, const unsigned int linenr, const char expected[], const char actual[], const std::string &msg = emptyString) const;
void assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg = emptyString) const; bool assertEquals(const char * const filename, const unsigned int linenr, const std::string& expected, const char actual[], const std::string &msg = emptyString) const;
void assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg = emptyString) const; bool assertEquals(const char * const filename, const unsigned int linenr, const long long expected, const long long actual, const std::string &msg = emptyString) const;
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; 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;
void todoAssertEquals(const char * const filename, const unsigned int linenr, const std::string &wanted, void todoAssertEquals(const char * const filename, const unsigned int linenr, const std::string &wanted,
@ -98,8 +98,8 @@ extern std::ostringstream errout;
extern std::ostringstream output; extern std::ostringstream output;
#define TEST_CASE( NAME ) if ( prepareTest(#NAME) ) { setVerbose(false); NAME(); } #define TEST_CASE( NAME ) if ( prepareTest(#NAME) ) { setVerbose(false); NAME(); }
#define ASSERT( CONDITION ) assert_(__FILE__, __LINE__, CONDITION) #define ASSERT( CONDITION ) if (!assert_(__FILE__, __LINE__, CONDITION)) return
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL) #define ASSERT_EQUALS( EXPECTED , ACTUAL ) if (!assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)) return
#define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED , ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL) #define ASSERT_EQUALS_WITHOUT_LINENUMBERS( EXPECTED , ACTUAL ) assertEqualsWithoutLineNumbers(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE) #define ASSERT_EQUALS_DOUBLE( EXPECTED , ACTUAL, TOLERANCE ) assertEqualsDouble(__FILE__, __LINE__, EXPECTED, ACTUAL, TOLERANCE)
#define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG) #define ASSERT_EQUALS_MSG( EXPECTED , ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG)
@ -108,6 +108,7 @@ extern std::ostringstream output;
#define TODO_ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; } catch (const EXCEPTION&) { } catch (...) { assertThrow(__FILE__, __LINE__); } #define TODO_ASSERT_THROW( CMD, EXCEPTION ) try { CMD ; } catch (const EXCEPTION&) { } catch (...) { assertThrow(__FILE__, __LINE__); }
#define TODO_ASSERT( CONDITION ) { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); } #define TODO_ASSERT( CONDITION ) { const bool condition=(CONDITION); todoAssertEquals(__FILE__, __LINE__, true, false, condition); }
#define TODO_ASSERT_EQUALS( WANTED , CURRENT , ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL) #define TODO_ASSERT_EQUALS( WANTED , CURRENT , ACTUAL ) todoAssertEquals(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL)
#define EXPECT_EQ( EXPECTED, ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL)
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance_##CLASSNAME; } #define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance_##CLASSNAME; }
#ifdef _WIN32 #ifdef _WIN32

View File

@ -189,7 +189,7 @@ private:
settings.jointSuppressionReport = true; settings.jointSuppressionReport = true;
if (!suppression.empty()) { if (!suppression.empty()) {
std::string r = settings.nomsg.addSuppressionLine(suppression); std::string r = settings.nomsg.addSuppressionLine(suppression);
ASSERT_EQUALS("", r); EXPECT_EQ("", r);
} }
unsigned int exitCode = 0; unsigned int exitCode = 0;
@ -216,7 +216,7 @@ private:
settings.inlineSuppressions = true; settings.inlineSuppressions = true;
settings.addEnabled("information"); settings.addEnabled("information");
if (!suppression.empty()) { if (!suppression.empty()) {
ASSERT_EQUALS("", settings.nomsg.addSuppressionLine(suppression)); EXPECT_EQ("", settings.nomsg.addSuppressionLine(suppression));
} }
ThreadExecutor executor(files, settings, *this); ThreadExecutor executor(files, settings, *this);
for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i) for (std::map<std::string, std::size_t>::const_iterator i = files.begin(); i != files.end(); ++i)

View File

@ -5358,54 +5358,85 @@ private:
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
ASSERT(db);
const Token *f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v1 ) ) ;"); const Token *f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v1 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 4); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(4, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v2 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v2 ) ) ;");
if (std::numeric_limits<char>::is_signed) ASSERT(f);
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 5); ASSERT(f->function());
else if (std::numeric_limits<char>::is_signed) {
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 10); ASSERT_EQUALS(5, f->function()->tokenDef->linenr());
} else {
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
}
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v3 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v3 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 6); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(6, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v4 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v4 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 7); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(7, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v5 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v5 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 8); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(8, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v6 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v6 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 9); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(9, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v7 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v7 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 10); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v8 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v8 ) ) ;");
if (std::numeric_limits<char>::is_signed) ASSERT(f);
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 5); ASSERT(f->function());
else if (std::numeric_limits<char>::is_signed) {
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 10); ASSERT_EQUALS(5, f->function()->tokenDef->linenr());
} else {
ASSERT_EQUALS(10, f->function()->tokenDef->linenr());
}
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v9 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v9 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 12); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(12, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v10 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v10 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 13); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(13, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v11 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v11 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 14); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(14, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v12 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v12 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 15); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(15, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v13 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v13 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 16); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(16, f->function()->tokenDef->linenr());
f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v14 ) ) ;"); f = Token::findsimplematch(tokenizer.tokens(), "get ( get ( v14 ) ) ;");
ASSERT_EQUALS(true, db && f && f->function() && f->function()->tokenDef->linenr() == 17); ASSERT(f);
ASSERT(f->function());
ASSERT_EQUALS(17, f->function()->tokenDef->linenr());
} }
void findFunction20() { // # 8280 void findFunction20() { // # 8280

View File

@ -81,10 +81,11 @@ private:
CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this); CheckUnusedFunctions checkUnusedFunctions(&tokenizer, &settings, this);
checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings); checkUnusedFunctions.parseTokens(tokenizer, "someFile.c", &settings);
// check() returns error if and only if errout is not empty. // check() returns error if and only if errout is not empty.
if (checkUnusedFunctions.check(this, settings)) if (checkUnusedFunctions.check(this, settings)) {
ASSERT(errout.str() != ""); ASSERT(errout.str() != "");
else } else {
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
}
} }
void incondition() { void incondition() {