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
00015 class NoExceptionExpected
00016 {
00017 private:
00018
00019 NoExceptionExpected();
00020 };
00021
00022
00023 template<typename ExceptionType>
00024 struct ExpectedExceptionTraits
00025 {
00026 static void expectedException()
00027 {
00028 #if CPPUNIT_USE_TYPEINFO_NAME
00029 std::string message( "Expected exception of type " );
00030 message += TypeInfoHelper::getClassName( typeid( ExceptionType ) );
00031 message += ", but got none";
00032 #else
00033 std::string message( "Expected exception but got none" );
00034 #endif
00035 throw Exception( message );
00036 }
00037 };
00038
00039
00040 template<>
00041 struct ExpectedExceptionTraits<NoExceptionExpected>
00042 {
00043 static void expectedException()
00044 {
00045 }
00046 };
00047
00048
00049
00050
00051
00052
00088 template <typename Fixture,
00089 typename ExpectedException = NoExceptionExpected>
00090 class TestCaller : public TestCase
00091 {
00092 typedef void (Fixture::*TestMethod)();
00093
00094 public:
00101 TestCaller (std::string name, TestMethod test) :
00102 TestCase (name),
00103 m_ownFixture(true),
00104 m_fixture (new Fixture ()),
00105 m_test (test)
00106 {}
00107
00117 TestCaller(std::string name, TestMethod test, Fixture& fixture) :
00118 TestCase (name),
00119 m_ownFixture(false),
00120 m_fixture (&fixture),
00121 m_test (test)
00122 {}
00123
00133 TestCaller(std::string name, TestMethod test, Fixture* fixture) :
00134 TestCase (name),
00135 m_ownFixture(true),
00136 m_fixture (fixture),
00137 m_test (test)
00138 {}
00139
00140 ~TestCaller() {
00141 if (m_ownFixture) {
00142 if (m_fixture) {
00143 delete m_fixture;
00144 m_fixture = NULL;
00145 }
00146 }
00147 }
00148
00149 protected:
00150 void runTest ()
00151 {
00152 try {
00153 (m_fixture->*m_test)();
00154 }
00155 catch ( ExpectedException & ) {
00156 return;
00157 }
00158
00159 ExpectedExceptionTraits<ExpectedException>::expectedException();
00160 }
00161
00162 void setUp ()
00163 {
00164 m_fixture->setUp ();
00165 }
00166
00167 void tearDown ()
00168 {
00169 m_fixture->tearDown ();
00170 }
00171
00172 std::string toString () const
00173 {
00174 return "TestCaller " + getName();
00175 }
00176
00177 private:
00178 TestCaller (const TestCaller& other);
00179 TestCaller& operator= (const TestCaller& other);
00180
00181 private:
00182 bool m_ownFixture;
00183 Fixture* m_fixture;
00184 TestMethod m_test;
00185
00186 };
00187
00188
00189
00190 }
00191
00192 #endif // CPPUNIT_TESTCALLER_H