source: trunk/contrib/cppunit-1.12.2/include/cppunit/TestCaller.h @ 876

Last change on this file since 876 was 876, checked in by sam, 9 years ago

contrib: add a precompiled Win32 version of CppUnit and update build system
accordingly.

File size: 4.8 KB
Line 
1#ifndef CPPUNIT_TESTCALLER_H    // -*- C++ -*-
2#define CPPUNIT_TESTCALLER_H
3
4#include <cppunit/Exception.h>
5#include <cppunit/TestCase.h>
6
7
8#if CPPUNIT_USE_TYPEINFO_NAME
9#  include <cppunit/extensions/TypeInfoHelper.h>
10#endif
11
12
13CPPUNIT_NS_BEGIN
14
15#if 0
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.
18 */
19class CPPUNIT_API NoExceptionExpected
20{
21private:
22  //! Prevent class instantiation.
23  NoExceptionExpected();
24};
25
26
27/*! \brief (Implementation) Traits used by TestCaller to expect an exception.
28 *
29 * This class is an implementation detail. You should never use this class directly.
30 */
31template<class ExceptionType>
32struct ExpectedExceptionTraits
33{
34  static void expectedException()
35  {
36#if CPPUNIT_USE_TYPEINFO_NAME
37    throw Exception( Message(
38                         "expected exception not thrown",
39                         "Expected exception type: " +
40                           TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
41#else
42    throw Exception( "expected exception not thrown" );
43#endif
44  }
45};
46
47
48/*! \brief (Implementation) Traits specialization used by TestCaller to
49 * expect no exception.
50 *
51 * This class is an implementation detail. You should never use this class directly.
52 */
53template<>
54struct ExpectedExceptionTraits<NoExceptionExpected>
55{
56  static void expectedException()
57  {
58  }
59};
60
61
62#endif
63
64//*** FIXME: rework this when class Fixture is implemented. ***//
65
66
67/*! \brief Generate a test case from a fixture method.
68 * \ingroup WritingTestFixture
69 *
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
73 * suite.
74 * Test Callers invoke only one Test (i.e. test method) on one
75 * Fixture of a TestFixture.
76 *
77 * Here is an example:
78 * \code
79 * class MathTest : public CppUnit::TestFixture {
80 *         ...
81 *     public:
82 *         void         setUp();
83 *         void         tearDown();
84 *
85 *         void         testAdd();
86 *         void         testSubtract();
87 * };
88 *
89 * CppUnit::Test *MathTest::suite() {
90 *     CppUnit::TestSuite *suite = new CppUnit::TestSuite;
91 *
92 *     suite->addTest( new CppUnit::TestCaller<MathTest>( "testAdd", testAdd ) );
93 *     return suite;
94 * }
95 * \endcode
96 *
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.
99 *
100 * \see TestCase
101 */
102
103template <class Fixture>
104class TestCaller : public TestCase
105{
106  typedef void (Fixture::*TestMethod)();
107   
108public:
109  /*!
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()
114   */
115  TestCaller( std::string name, TestMethod test ) :
116            TestCase( name ),
117            m_ownFixture( true ),
118            m_fixture( new Fixture() ),
119            m_test( test )
120  {
121  }
122
123  /*!
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
127   * Fixture object.
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.
131   */
132  TestCaller(std::string name, TestMethod test, Fixture& fixture) :
133            TestCase( name ),
134            m_ownFixture( false ),
135            m_fixture( &fixture ),
136            m_test( test )
137  {
138  }
139   
140  /*!
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.
148   */
149  TestCaller(std::string name, TestMethod test, Fixture* fixture) :
150            TestCase( name ),
151            m_ownFixture( true ),
152            m_fixture( fixture ),
153            m_test( test )
154  {
155  }
156   
157  ~TestCaller()
158  {
159    if (m_ownFixture)
160      delete m_fixture;
161  }
162
163  void runTest()
164  {
165//        try {
166            (m_fixture->*m_test)();
167//        }
168//        catch ( ExpectedException & ) {
169//          return;
170//        }
171
172//      ExpectedExceptionTraits<ExpectedException>::expectedException();
173  } 
174
175  void setUp()
176  {
177        m_fixture->setUp ();
178  }
179
180  void tearDown()
181  {
182          m_fixture->tearDown ();
183  }
184
185  std::string toString() const
186  {
187        return "TestCaller " + getName();
188  }
189
190private:
191  TestCaller( const TestCaller &other );
192  TestCaller &operator =( const TestCaller &other );
193
194private:
195  bool m_ownFixture;
196  Fixture *m_fixture;
197  TestMethod m_test;
198};
199
200
201
202CPPUNIT_NS_END
203
204#endif // CPPUNIT_TESTCALLER_H
Note: See TracBrowser for help on using the repository browser.