1#include <cppunit/Portability.h> 2#include <typeinfo> 3#include <stdexcept> 4 5#include "cppunit/TestCase.h" 6#include "cppunit/Exception.h" 7#include "cppunit/TestResult.h" 8 9 10using std::exception; 11using std::string; 12 13namespace CppUnit { 14 15/// Create a default TestResult 16CppUnit::TestResult* 17TestCase::defaultResult() 18{ 19 return new TestResult; 20} 21 22 23/// Run the test and catch any exceptions that are triggered by it 24void 25TestCase::run( TestResult *result ) 26{ 27 result->startTest(this); 28 29 try { 30 setUp(); 31 32 try { 33 runTest(); 34 } 35 catch ( Exception &e ) { 36 Exception *copy = e.clone(); 37 result->addFailure( this, copy ); 38 } 39 catch ( exception &e ) { 40 result->addError( this, new Exception( e.what() ) ); 41 } 42 catch (...) { 43 Exception *e = new Exception( "caught unknown exception" ); 44 result->addError( this, e ); 45 } 46 47 try { 48 tearDown(); 49 } 50 catch (...) { 51 result->addError( this, new Exception( "tearDown() failed" ) ); 52 } 53 } 54 catch (...) { 55 result->addError( this, new Exception( "setUp() failed" ) ); 56 } 57 58 result->endTest( this ); 59} 60 61 62/// A default run method 63TestResult * 64TestCase::run() 65{ 66 TestResult *result = defaultResult(); 67 68 run (result); 69 return result; 70} 71 72 73/// All the work for runTest is deferred to subclasses 74void 75TestCase::runTest() 76{ 77} 78 79 80/** Constructs a test case. 81 * \param name the name of the TestCase. 82 **/ 83TestCase::TestCase( string name ) 84 : m_name(name) 85{ 86} 87 88 89/** Constructs a test case for a suite. 90 * This TestCase is intended for use by the TestCaller and should not 91 * be used by a test case for which run() is called. 92 **/ 93TestCase::TestCase() 94 : m_name( "" ) 95{ 96} 97 98 99/// Destructs a test case 100TestCase::~TestCase() 101{ 102} 103 104 105/// Returns a count of all the tests executed 106int 107TestCase::countTestCases() const 108{ 109 return 1; 110} 111 112 113/// Returns the name of the test case 114string 115TestCase::getName() const 116{ 117 return m_name; 118} 119 120 121/// Returns the name of the test case instance 122string 123TestCase::toString() const 124{ 125 string className; 126 127#if CPPUNIT_USE_TYPEINFO_NAME 128 const std::type_info& thisClass = typeid( *this ); 129 className = thisClass.name(); 130#else 131 className = "TestCase"; 132#endif 133 134 return className + "." + getName(); 135} 136 137 138} // namespace CppUnit 139