1
#ifndef CPPUNIT_TESTCALLER_H // -*- C++ -*-
2
#define CPPUNIT_TESTCALLER_H
4
#include <cppunit/Exception.h>
5
#include <cppunit/TestCase.h>
8
#if CPPUNIT_USE_TYPEINFO_NAME
9
# include <cppunit/extensions/TypeInfoHelper.h>
16
/*! \brief Marker class indicating that no exception is expected by TestCaller.
17
* This class is an implementation detail. You should never use this class directly.
19
class CPPUNIT_API NoExceptionExpected
22
//! Prevent class instantiation.
23
NoExceptionExpected();
27
/*! \brief (Implementation) Traits used by TestCaller to expect an exception.
29
* This class is an implementation detail. You should never use this class directly.
31
template<class ExceptionType>
32
struct ExpectedExceptionTraits
34
static void expectedException()
36
#if CPPUNIT_USE_TYPEINFO_NAME
37
throw Exception( Message(
38
"expected exception not thrown",
39
"Expected exception type: " +
40
TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
42
throw Exception( "expected exception not thrown" );
48
/*! \brief (Implementation) Traits specialization used by TestCaller to
49
* expect no exception.
51
* This class is an implementation detail. You should never use this class directly.
54
struct ExpectedExceptionTraits<NoExceptionExpected>
56
static void expectedException()
64
//*** FIXME: rework this when class Fixture is implemented. ***//
67
/*! \brief Generate a test case from a fixture method.
68
* \ingroup WritingTestFixture
70
* A test caller provides access to a test case method
71
* on a test fixture class. Test callers are useful when
72
* you want to run an individual test or add it to a
74
* Test Callers invoke only one Test (i.e. test method) on one
75
* Fixture of a TestFixture.
79
* class MathTest : public CppUnit::TestFixture {
86
* void testSubtract();
89
* CppUnit::Test *MathTest::suite() {
90
* CppUnit::TestSuite *suite = new CppUnit::TestSuite;
92
* suite->addTest( new CppUnit::TestCaller<MathTest>( "testAdd", testAdd ) );
97
* You can use a TestCaller to bind any test method on a TestFixture
98
* class, as long as it accepts void and returns void.
103
template <class Fixture>
104
class TestCaller : public TestCase
106
typedef void (Fixture::*TestMethod)();
110
* Constructor for TestCaller. This constructor builds a new Fixture
111
* instance owned by the TestCaller.
112
* \param name name of this TestCaller
113
* \param test the method this TestCaller calls in runTest()
115
TestCaller( std::string name, TestMethod test ) :
117
m_ownFixture( true ),
118
m_fixture( new Fixture() ),
124
* Constructor for TestCaller.
125
* This constructor does not create a new Fixture instance but accepts
126
* an existing one as parameter. The TestCaller will not own the
128
* \param name name of this TestCaller
129
* \param test the method this TestCaller calls in runTest()
130
* \param fixture the Fixture to invoke the test method on.
132
TestCaller(std::string name, TestMethod test, Fixture& fixture) :
134
m_ownFixture( false ),
135
m_fixture( &fixture ),
141
* Constructor for TestCaller.
142
* This constructor does not create a new Fixture instance but accepts
143
* an existing one as parameter. The TestCaller will own the
144
* Fixture object and delete it in its destructor.
145
* \param name name of this TestCaller
146
* \param test the method this TestCaller calls in runTest()
147
* \param fixture the Fixture to invoke the test method on.
149
TestCaller(std::string name, TestMethod test, Fixture* fixture) :
151
m_ownFixture( true ),
152
m_fixture( fixture ),
166
(m_fixture->*m_test)();
168
// catch ( ExpectedException & ) {
172
// ExpectedExceptionTraits<ExpectedException>::expectedException();
182
m_fixture->tearDown ();
185
std::string toString() const
187
return "TestCaller " + getName();
191
TestCaller( const TestCaller &other );
192
TestCaller &operator =( const TestCaller &other );
204
#endif // CPPUNIT_TESTCALLER_H