arabica/tests/CppUnit/framework/TestCase.cpp

140 lines
3.5 KiB
C++
Raw Normal View History

#ifdef _MSC_VER
#pragma warning(disable: 4996)
#endif
2005-08-04 23:23:48 +02:00
#include <exception>
#include <stdexcept>
#include <cmath>
#include "TestCase.h"
#include "TestResult.h"
#include "estring.h"
// Create a default TestResult
TestResult *TestCase::defaultResult ()
{ return new TestResult; }
2007-07-19 18:57:04 +02:00
void TestCase::skipTest(std::string reason)
{
throw SkipException(reason);
}
2005-08-04 23:23:48 +02:00
// Check for a failed general assertion
void TestCase::assertImplementation (bool condition,
std::string conditionExpression,
long lineNumber,
std::string fileName)
{
if (!condition)
throw CppUnitException (conditionExpression, lineNumber, fileName);
}
// Check for a failed equality assertion
void TestCase::assertEquals (std::string expected,
std::string actual,
long lineNumber,
std::string fileName)
{
if (expected != actual)
assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName);
}
// Check for a failed equality assertion
void TestCase::assertEquals (long expected,
long actual,
long lineNumber,
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,
std::string fileName)
{
if (fabs (expected - actual) > delta)
assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName);
}
// Run the test and catch any exceptions that are triggered by it
void TestCase::run (TestResult *result)
{
result->startTest (this);
setUp ();
try {
runTest ();
}
2007-07-19 18:57:04 +02:00
catch (const SkipException& e) {
CppUnitException *copy = new CppUnitException(m_name + ": " + e.what());
result->addSkip(this, copy);
}
2005-08-04 23:23:48 +02:00
catch (const CppUnitException& e) {
CppUnitException *copy = new CppUnitException(m_name + ": ", e);
result->addFailure(this, copy);
2005-08-04 23:23:48 +02:00
}
catch (const std::exception& e) {
result->addError (this, new CppUnitException(m_name + ": " + e.what()));
2005-08-04 23:23:48 +02:00
}
catch (...) {
2005-11-17 16:46:52 +01:00
result->addError (this, new CppUnitException (m_name + ": unknown exception"));
2005-08-04 23:23:48 +02:00
}
tearDown ();
result->endTest (this);
}
// A default run method
TestResult *TestCase::run ()
{
TestResult *result = defaultResult ();
run (result);
return result;
}
// All the work for runTest is deferred to subclasses
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);
}
std::string TestCase::notEqualsMessage (std::string expected, std::string actual)
{
return "expected: " + expected + " but was: " + actual;
}