summaryrefslogtreecommitdiff
path: root/src/cppunit/TestCase.cpp
diff options
context:
space:
mode:
authorBastiaan Bakker <bastiaan.bakker@lifeline.nl>2001-04-28 17:23:32 +0000
committerBastiaan Bakker <bastiaan.bakker@lifeline.nl>2001-04-28 17:23:32 +0000
commit6d95c46d9dc342bea176c8fbcd101db8eba24bef (patch)
tree3a42ea08625f64972f520cbeda2da3c68bbe9692 /src/cppunit/TestCase.cpp
parent7e4ccacdbcf2f78005447f16e49d339d2a70e9ca (diff)
downloadcppunit-6d95c46d9dc342bea176c8fbcd101db8eba24bef.tar.gz
Moved files in subdir cppunit to src/cppunit.
Diffstat (limited to 'src/cppunit/TestCase.cpp')
-rw-r--r--src/cppunit/TestCase.cpp173
1 files changed, 173 insertions, 0 deletions
diff --git a/src/cppunit/TestCase.cpp b/src/cppunit/TestCase.cpp
new file mode 100644
index 0000000..f1e5198
--- /dev/null
+++ b/src/cppunit/TestCase.cpp
@@ -0,0 +1,173 @@
+#include <typeinfo>
+#include <stdexcept>
+#include <cmath>
+
+#include "cppunit/TestCase.h"
+#include "cppunit/Exception.h"
+#include "cppunit/TestResult.h"
+#include "estring.h"
+
+namespace CppUnit {
+
+/// Create a default TestResult
+CppUnit::TestResult* TestCase::defaultResult ()
+{ return new TestResult; }
+
+
+/// Check for a failed general assertion
+void TestCase::assertImplementation (bool condition,
+ std::string conditionExpression,
+ long lineNumber,
+ std::string fileName)
+{
+ if (!condition)
+ throw Exception (conditionExpression, 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 ();
+
+ }
+ catch (Exception& e) {
+ Exception *copy = new Exception (e);
+ result->addFailure (this, copy);
+
+ }
+ catch (exception& e) {
+ result->addError (this, new Exception (e.what ()));
+
+ }
+ catch (...) {
+ Exception *e = new Exception ("unknown exception");
+ result->addError (this, e);
+
+ }
+
+ 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);
+}
+
+
+
+/** Constructs a test case.
+ * \param name the name of the TestCase.
+ */
+TestCase::TestCase (std::string name)
+ : m_name (name)
+{
+}
+
+/** Constructs a test case for a suite.
+ * This TestCase is intended for use by the TestCaller and should not
+ * be used by a test case for which run() is called.
+ **/
+TestCase::TestCase ()
+ : m_name ("")
+{
+}
+
+
+/// Destructs a test case
+TestCase::~TestCase ()
+{}
+
+
+/// Returns a count of all the tests executed
+int TestCase::countTestCases () const
+{ return 1; }
+
+
+/// Returns the name of the test case
+std::string
+ TestCase::getName () const
+{
+ return m_name;
+}
+
+
+/// A hook for fixture set up
+void TestCase::setUp ()
+{}
+
+
+/// A hook for fixture tear down
+void TestCase::tearDown ()
+{}
+
+
+/// Returns the name of the test case instance
+std::string
+ TestCase::toString () const
+{
+ const type_info& thisClass = typeid (*this);
+ return std::string (thisClass.name ()) + "." + getName ();
+}
+
+} // namespace CppUnit