Blame include/cppunit/extensions/ExceptionTestCaseDecorator.h

Packit Service e31359
#ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
Packit Service e31359
#define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
Packit Service e31359
Packit Service e31359
#include <cppunit/Portability.h>
Packit Service e31359
#include <cppunit/Exception.h>
Packit Service e31359
#include <cppunit/extensions/TestCaseDecorator.h>
Packit Service e31359
Packit Service e31359
CPPUNIT_NS_BEGIN
Packit Service e31359
Packit Service e31359
Packit Service e31359
/*! \brief Expected exception test case decorator.
Packit Service e31359
 *
Packit Service e31359
 * A decorator used to assert that a specific test case should throw an
Packit Service e31359
 * exception of a given type.
Packit Service e31359
 *
Packit Service e31359
 * You should use this class only if you need to check the exception object
Packit Service e31359
 * state (that a specific cause is set for example). If you don't need to
Packit Service e31359
 * do that, you might consider using CPPUNIT_TEST_EXCEPTION() instead.
Packit Service e31359
 *
Packit Service e31359
 * Intended use is to subclass and override checkException(). Example:
Packit Service e31359
 *
Packit Service e31359
 * \code
Packit Service e31359
 *
Packit Service e31359
 * class NetworkErrorTestCaseDecorator : 
Packit Service e31359
 *           public ExceptionTestCaseDecorator<NetworkError>
Packit Service e31359
 * {
Packit Service e31359
 * public:
Packit Service e31359
 *   NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause )
Packit Service e31359
 *       : m_expectedCause( expectedCause )
Packit Service e31359
 *   {
Packit Service e31359
 *   }
Packit Service e31359
 * private:
Packit Service e31359
 *   void checkException( ExpectedExceptionType &e )
Packit Service e31359
 *   {
Packit Service e31359
 *     CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() );
Packit Service e31359
 *   }
Packit Service e31359
 *
Packit Service e31359
 *   NetworkError::Cause m_expectedCause;
Packit Service e31359
 * };
Packit Service e31359
 * \endcode
Packit Service e31359
 *
Packit Service e31359
 */ 
Packit Service e31359
template<class ExpectedException>
Packit Service e31359
class ExceptionTestCaseDecorator : public TestCaseDecorator
Packit Service e31359
{
Packit Service e31359
public:
Packit Service e31359
  typedef ExpectedException ExpectedExceptionType;
Packit Service e31359
Packit Service e31359
  /*! \brief Decorates the specified test.
Packit Service e31359
   * \param test TestCase to decorate. Assumes ownership of the test.
Packit Service e31359
   */
Packit Service e31359
  ExceptionTestCaseDecorator( TestCase *test )
Packit Service e31359
      : TestCaseDecorator( test )
Packit Service e31359
  {
Packit Service e31359
  }
Packit Service e31359
Packit Service e31359
  /*! \brief Checks that the expected exception is thrown by the decorated test.
Packit Service e31359
   * is thrown.
Packit Service e31359
   *
Packit Service e31359
   * Calls the decorated test runTest() and checks that an exception of
Packit Service e31359
   * type ExpectedException is thrown. Call checkException() passing the
Packit Service e31359
   * exception that was caught so that some assertions can be made if
Packit Service e31359
   * needed.
Packit Service e31359
   */
Packit Service e31359
  void runTest()
Packit Service e31359
  {
Packit Service e31359
    try
Packit Service e31359
    {
Packit Service e31359
      TestCaseDecorator::runTest();
Packit Service e31359
    }
Packit Service e31359
    catch ( ExpectedExceptionType &e )
Packit Service e31359
    {
Packit Service e31359
      checkException( e );
Packit Service e31359
      return;
Packit Service e31359
    }
Packit Service e31359
Packit Service e31359
    // Moved outside the try{} statement to handle the case where the
Packit Service e31359
    // expected exception type is Exception (expecting assertion failure).
Packit Service e31359
#if defined(CPPUNIT_USE_TYPEINFO_NAME)
Packit Service e31359
      throw Exception( Message(
Packit Service e31359
                         "expected exception not thrown",
Packit Service e31359
                         "Expected exception type: " + 
Packit Service e31359
                           TypeInfoHelper::getClassName( 
Packit Service e31359
                               typeid( ExpectedExceptionType ) ) ) );
Packit Service e31359
#else
Packit Service e31359
      throw Exception( Message("expected exception not thrown") );
Packit Service e31359
#endif
Packit Service e31359
  }
Packit Service e31359
Packit Service e31359
private:
Packit Service e31359
  /*! \brief Called when the exception is caught.
Packit Service e31359
   *
Packit Service e31359
   * Should be overriden to check the exception.
Packit Service e31359
   */
Packit Service e31359
  virtual void checkException( ExpectedExceptionType & )
Packit Service e31359
  {
Packit Service e31359
  }
Packit Service e31359
};
Packit Service e31359
Packit Service e31359
Packit Service e31359
CPPUNIT_NS_END
Packit Service e31359
Packit Service e31359
#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
Packit Service e31359