00001 #ifndef CPPUNIT_TESTCALLER_H // -*- C++ -*-
00002 #define CPPUNIT_TESTCALLER_H
00003
00004 #include <cppunit/Exception.h>
00005 #include <cppunit/TestCase.h>
00006
00007 #include <functional>
00008
00009
00010 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
00011 # include <cppunit/extensions/TypeInfoHelper.h>
00012 #endif
00013
00014
00015 CPPUNIT_NS_BEGIN
00016
00017 #if 0
00018
00021 class CPPUNIT_API NoExceptionExpected
00022 {
00023 private:
00025 NoExceptionExpected();
00026 };
00027
00028
00033 template<class ExceptionType>
00034 struct ExpectedExceptionTraits
00035 {
00036 static void expectedException()
00037 {
00038 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
00039 throw Exception( Message(
00040 "expected exception not thrown",
00041 "Expected exception type: " +
00042 TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
00043 #else
00044 throw Exception( "expected exception not thrown" );
00045 #endif
00046 }
00047 };
00048
00049
00055 template<>
00056 struct ExpectedExceptionTraits<NoExceptionExpected>
00057 {
00058 static void expectedException()
00059 {
00060 }
00061 };
00062
00063
00064 #endif
00065
00066
00067
00068
00105 template <class Fixture>
00106 class TestCaller : public TestCase
00107 {
00108 typedef void (Fixture::*TestMethod)();
00109
00110 public:
00117 TestCaller( std::string name, TestMethod test ) :
00118 TestCase( name ),
00119 m_ownFixture( true ),
00120 m_fixture( new Fixture() ),
00121 m_test_function( std::bind(test, m_fixture) )
00122 {
00123 }
00124
00134 TestCaller(std::string name, TestMethod test, Fixture& fixture) :
00135 TestCase( name ),
00136 m_ownFixture( false ),
00137 m_fixture( &fixture ),
00138 m_test_function( std::bind(test, &fixture) )
00139 {
00140 }
00141
00151 TestCaller(std::string name, TestMethod test, Fixture* fixture) :
00152 TestCase( name ),
00153 m_ownFixture( true ),
00154 m_fixture( fixture ),
00155 m_test_function( std::bind(test, fixture) )
00156 {
00157 }
00158
00159 TestCaller(std::string name, std::function<void()> test_function, Fixture* fixture):
00160 TestCase(name),
00161 m_ownFixture(true),
00162 m_fixture(fixture),
00163 m_test_function(test_function)
00164 {
00165 }
00166
00167 ~TestCaller()
00168 {
00169 if (m_ownFixture)
00170 delete m_fixture;
00171 }
00172
00173 void runTest()
00174 {
00175 m_test_function();
00176 }
00177
00178 void setUp()
00179 {
00180 m_fixture->setUp ();
00181 }
00182
00183 void tearDown()
00184 {
00185 m_fixture->tearDown ();
00186 }
00187
00188 std::string toString() const
00189 {
00190 return "TestCaller " + getName();
00191 }
00192
00193 private:
00194 TestCaller( const TestCaller &other );
00195 TestCaller &operator =( const TestCaller &other );
00196
00197 private:
00198 bool m_ownFixture;
00199 Fixture *m_fixture;
00200 std::function<void()> m_test_function;
00201 };
00202
00203
00204
00205 CPPUNIT_NS_END
00206
00207 #endif // CPPUNIT_TESTCALLER_H