⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptiontestcasedecorator.h

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -