TestCaller.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #ifndef CPPUNIT_TESTCALLER_H // -*- C++ -*-
  2. #define CPPUNIT_TESTCALLER_H
  3. #include <cppunit/Exception.h>
  4. #include <cppunit/TestCase.h>
  5. #if CPPUNIT_USE_TYPEINFO_NAME
  6. # include <cppunit/extensions/TypeInfoHelper.h>
  7. #endif
  8. CPPUNIT_NS_BEGIN
  9. #if 0
  10. /*! \brief Marker class indicating that no exception is expected by TestCaller.
  11. * This class is an implementation detail. You should never use this class directly.
  12. */
  13. class CPPUNIT_API NoExceptionExpected
  14. {
  15. private:
  16. //! Prevent class instantiation.
  17. NoExceptionExpected();
  18. };
  19. /*! \brief (Implementation) Traits used by TestCaller to expect an exception.
  20. *
  21. * This class is an implementation detail. You should never use this class directly.
  22. */
  23. template<class ExceptionType>
  24. struct ExpectedExceptionTraits
  25. {
  26. static void expectedException()
  27. {
  28. #if CPPUNIT_USE_TYPEINFO_NAME
  29. throw Exception( Message(
  30. "expected exception not thrown",
  31. "Expected exception type: " +
  32. TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
  33. #else
  34. throw Exception( "expected exception not thrown" );
  35. #endif
  36. }
  37. };
  38. /*! \brief (Implementation) Traits specialization used by TestCaller to
  39. * expect no exception.
  40. *
  41. * This class is an implementation detail. You should never use this class directly.
  42. */
  43. template<>
  44. struct ExpectedExceptionTraits<NoExceptionExpected>
  45. {
  46. static void expectedException()
  47. {
  48. }
  49. };
  50. #endif
  51. //*** FIXME: rework this when class Fixture is implemented. ***//
  52. /*! \brief Generate a test case from a fixture method.
  53. * \ingroup WritingTestFixture
  54. *
  55. * A test caller provides access to a test case method
  56. * on a test fixture class. Test callers are useful when
  57. * you want to run an individual test or add it to a
  58. * suite.
  59. * Test Callers invoke only one Test (i.e. test method) on one
  60. * Fixture of a TestFixture.
  61. *
  62. * Here is an example:
  63. * \code
  64. * class MathTest : public CppUnit::TestFixture {
  65. * ...
  66. * public:
  67. * void setUp();
  68. * void tearDown();
  69. *
  70. * void testAdd();
  71. * void testSubtract();
  72. * };
  73. *
  74. * CppUnit::Test *MathTest::suite() {
  75. * CppUnit::TestSuite *suite = new CppUnit::TestSuite;
  76. *
  77. * suite->addTest( new CppUnit::TestCaller<MathTest>( "testAdd", testAdd ) );
  78. * return suite;
  79. * }
  80. * \endcode
  81. *
  82. * You can use a TestCaller to bind any test method on a TestFixture
  83. * class, as long as it accepts void and returns void.
  84. *
  85. * \see TestCase
  86. */
  87. template <class Fixture>
  88. class TestCaller : public TestCase
  89. {
  90. typedef void (Fixture::*TestMethod)();
  91. public:
  92. /*!
  93. * Constructor for TestCaller. This constructor builds a new Fixture
  94. * instance owned by the TestCaller.
  95. * \param name name of this TestCaller
  96. * \param test the method this TestCaller calls in runTest()
  97. */
  98. TestCaller( std::string name, TestMethod test ) :
  99. TestCase( name ),
  100. m_ownFixture( true ),
  101. m_fixture( new Fixture() ),
  102. m_test( test )
  103. {
  104. }
  105. /*!
  106. * Constructor for TestCaller.
  107. * This constructor does not create a new Fixture instance but accepts
  108. * an existing one as parameter. The TestCaller will not own the
  109. * Fixture object.
  110. * \param name name of this TestCaller
  111. * \param test the method this TestCaller calls in runTest()
  112. * \param fixture the Fixture to invoke the test method on.
  113. */
  114. TestCaller(std::string name, TestMethod test, Fixture& fixture) :
  115. TestCase( name ),
  116. m_ownFixture( false ),
  117. m_fixture( &fixture ),
  118. m_test( test )
  119. {
  120. }
  121. /*!
  122. * Constructor for TestCaller.
  123. * This constructor does not create a new Fixture instance but accepts
  124. * an existing one as parameter. The TestCaller will own the
  125. * Fixture object and delete it in its destructor.
  126. * \param name name of this TestCaller
  127. * \param test the method this TestCaller calls in runTest()
  128. * \param fixture the Fixture to invoke the test method on.
  129. */
  130. TestCaller(std::string name, TestMethod test, Fixture* fixture) :
  131. TestCase( name ),
  132. m_ownFixture( true ),
  133. m_fixture( fixture ),
  134. m_test( test )
  135. {
  136. }
  137. ~TestCaller()
  138. {
  139. if (m_ownFixture)
  140. delete m_fixture;
  141. }
  142. void runTest()
  143. {
  144. // try {
  145. (m_fixture->*m_test)();
  146. // }
  147. // catch ( ExpectedException & ) {
  148. // return;
  149. // }
  150. // ExpectedExceptionTraits<ExpectedException>::expectedException();
  151. }
  152. void setUp()
  153. {
  154. m_fixture->setUp ();
  155. }
  156. void tearDown()
  157. {
  158. m_fixture->tearDown ();
  159. }
  160. std::string toString() const
  161. {
  162. return "TestCaller " + getName();
  163. }
  164. private:
  165. TestCaller( const TestCaller &other );
  166. TestCaller &operator =( const TestCaller &other );
  167. private:
  168. bool m_ownFixture;
  169. Fixture *m_fixture;
  170. TestMethod m_test;
  171. };
  172. CPPUNIT_NS_END
  173. #endif // CPPUNIT_TESTCALLER_H