00001 #ifndef CPPUNIT_TESTCALLER_H // -*- C++ -*-
00002 #define CPPUNIT_TESTCALLER_H
00003
00004 #include <cppunit/Exception.h>
00005 #include <cppunit/TestCase.h>
00006
00007
00008 #if CPPUNIT_USE_TYPEINFO_NAME
00009 # include <cppunit/extensions/TypeInfoHelper.h>
00010 #endif
00011
00012
00013 namespace CppUnit {
00014
00018 class CPPUNIT_API NoExceptionExpected
00019 {
00020 private:
00022 NoExceptionExpected();
00023 };
00024
00025
00030 template<typename ExceptionType>
00031 struct ExpectedExceptionTraits
00032 {
00033 static void expectedException()
00034 {
00035 #if CPPUNIT_USE_TYPEINFO_NAME
00036 std::string message( "Expected exception of type " );
00037 message += TypeInfoHelper::getClassName( typeid( ExceptionType ) );
00038 message += ", but got none";
00039 #else
00040 std::string message( "Expected exception but got none" );
00041 #endif
00042 throw Exception( message );
00043 }
00044 };
00045
00046
00052 template<>
00053 struct ExpectedExceptionTraits<NoExceptionExpected>
00054 {
00055 static void expectedException()
00056 {
00057 }
00058 };
00059
00060
00061
00062
00063
00064
00101 template <typename Fixture,
00102 typename ExpectedException = NoExceptionExpected>
00103 class TestCaller : public TestCase
00104 {
00105 typedef void (Fixture::*TestMethod)();
00106
00107 public:
00114 TestCaller( std::string name, TestMethod test ) :
00115 TestCase( name ),
00116 m_ownFixture( true ),
00117 m_fixture( new Fixture() ),
00118 m_test( test )
00119 {
00120 }
00121
00131 TestCaller(std::string name, TestMethod test, Fixture& fixture) :
00132 TestCase( name ),
00133 m_ownFixture( false ),
00134 m_fixture( &fixture ),
00135 m_test( test )
00136 {
00137 }
00138
00148 TestCaller(std::string name, TestMethod test, Fixture* fixture) :
00149 TestCase( name ),
00150 m_ownFixture( true ),
00151 m_fixture( fixture ),
00152 m_test( test )
00153 {
00154 }
00155
00156 ~TestCaller()
00157 {
00158 if (m_ownFixture)
00159 delete m_fixture;
00160 }
00161
00162 protected:
00163 void runTest()
00164 {
00165 try {
00166 (m_fixture->*m_test)();
00167 }
00168 catch ( ExpectedException & ) {
00169 return;
00170 }
00171
00172 ExpectedExceptionTraits<ExpectedException>::expectedException();
00173 }
00174
00175 void setUp()
00176 {
00177 m_fixture->setUp ();
00178 }
00179
00180 void tearDown()
00181 {
00182 m_fixture->tearDown ();
00183 }
00184
00185 std::string toString() const
00186 {
00187 return "TestCaller " + getName();
00188 }
00189
00190 private:
00191 TestCaller( const TestCaller &other );
00192 TestCaller &operator =( const TestCaller &other );
00193
00194 private:
00195 bool m_ownFixture;
00196 Fixture *m_fixture;
00197 TestMethod m_test;
00198 };
00199
00200
00201
00202 }
00203
00204 #endif // CPPUNIT_TESTCALLER_H