Merge pull request #3596 from pocoproject/poco-1.12-cppunit-improvements

CppUnit: add generic assertEquals
This commit is contained in:
Günter Obiltschnig 2022-05-17 09:10:58 +02:00 committed by GitHub
commit 2cccd9fede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 27 deletions

View File

@ -124,10 +124,15 @@ protected:
long data2LineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
void assertEquals(long expected,
long actual,
template <typename T1, typename T2>
void assertEquals(T1 expected,
T2 actual,
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME)
{
if (expected != actual)
assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName);
}
void assertEquals(double expected,
double actual,
@ -140,13 +145,22 @@ protected:
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
void assertEquals(const char* expected,
const std::string& actual,
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
void assertEquals(const void* expected,
const void* actual,
long lineNumber = CppUnitException::CPPUNIT_UNKNOWNLINENUMBER,
const std::string& fileName = CppUnitException::CPPUNIT_UNKNOWNFILENAME);
std::string notEqualsMessage(long expected, long actual);
std::string notEqualsMessage(double expected, double actual);
template <typename T1, typename T2>
std::string notEqualsMessage(T1 expected, T2 actual)
{
return "expected: " + std::to_string(expected) + " but was: " + std::to_string(actual);
}
std::string notEqualsMessage(const void* expected, const void* actual);
std::string notEqualsMessage(const std::string& expected, const std::string& actual);

View File

@ -47,14 +47,6 @@ void TestCase::loop2assertImplementation(bool condition, const std::string& cond
}
// Check for a failed equality assertion
void TestCase::assertEquals(long expected, long actual, long lineNumber, const std::string& fileName)
{
if (expected != actual)
assertImplementation(false, notEqualsMessage(expected, actual), lineNumber, fileName);
}
// Check for a failed equality assertion
void TestCase::assertEquals(double expected, double actual, double delta, long lineNumber, const std::string& fileName)
{
@ -79,6 +71,14 @@ void TestCase::assertEquals(const std::string& expected, const std::string& actu
}
// Check for a failed equality assertion
void TestCase::assertEquals(const char* expected, const std::string& actual, long lineNumber, const std::string& fileName)
{
if (std::string(expected) != actual)
assertImplementation(false, notEqualsMessage(std::string(expected), actual), lineNumber, fileName);
}
void TestCase::assertNotNull(const void* pointer, const std::string& pointerExpression, long lineNumber, const std::string& fileName)
{
if (pointer == NULL)
@ -152,20 +152,6 @@ void TestCase::runTest()
}
// Build a message about a failed equality check
std::string TestCase::notEqualsMessage(long expected, long actual)
{
return "expected: " + estring(expected) + " but was: " + estring(actual);
}
// Build a message about a failed equality check
std::string TestCase::notEqualsMessage(double expected, double actual)
{
return "expected: " + estring(expected) + " but was: " + estring(actual);
}
// Build a message about a failed equality check
std::string TestCase::notEqualsMessage(const void* expected, const void* actual)
{