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