Protector.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef CPPUNIT_PROTECTOR_H
  2. #define CPPUNIT_PROTECTOR_H
  3. #include <cppunit/SourceLine.h>
  4. CPPUNIT_NS_BEGIN
  5. class Exception;
  6. class Message;
  7. class ProtectorContext;
  8. class TestResult;
  9. class CPPUNIT_API Functor
  10. {
  11. public:
  12. virtual ~Functor();
  13. virtual bool operator()() const =0;
  14. };
  15. /*! \brief Protects one or more test case run.
  16. *
  17. * Protector are used to globably 'decorate' a test case. The most common
  18. * usage of Protector is to catch exception that do not subclass std::exception,
  19. * such as MFC CException class or Rogue Wave RWXMsg class, and capture the
  20. * message associated to the exception. In fact, CppUnit capture message from
  21. * Exception and std::exception using a Protector.
  22. *
  23. * Protector are chained. When you add a Protector using
  24. * TestResult::pushProtector(), your protector is in fact passed as a Functor
  25. * to the first protector of the chain.
  26. *
  27. * TestCase protects call to setUp(), runTest() and tearDown() by calling
  28. * TestResult::protect().
  29. *
  30. * Because the protector chain is handled by TestResult, a protector can be
  31. * active for a single test, or a complete test run.
  32. *
  33. * Here are some possible usages:
  34. * - run all test case in a separate thread and assumes the test failed if it
  35. * did not finish in a given time (infinite loop work around)
  36. * - performance tracing : time only the runTest() time.
  37. * \sa TestResult, TestCase, TestListener.
  38. */
  39. class CPPUNIT_API Protector
  40. {
  41. public:
  42. virtual ~Protector();
  43. virtual bool protect( const Functor &functor,
  44. const ProtectorContext &context ) =0;
  45. protected:
  46. void reportError( const ProtectorContext &context,
  47. const Exception &error ) const;
  48. void reportError( const ProtectorContext &context,
  49. const Message &message,
  50. const SourceLine &sourceLine = SourceLine() ) const;
  51. void reportFailure( const ProtectorContext &context,
  52. const Exception &failure ) const;
  53. Message actualMessage( const Message &message,
  54. const ProtectorContext &context ) const;
  55. };
  56. /*! \brief Scoped protector push to TestResult.
  57. *
  58. * Adds the specified Protector to the specified TestResult for the object
  59. * life-time.
  60. */
  61. class CPPUNIT_API ProtectorGuard
  62. {
  63. public:
  64. /// Pushes the specified protector.
  65. ProtectorGuard( TestResult *result,
  66. Protector *protector );
  67. /// Pops the protector.
  68. ~ProtectorGuard();
  69. private:
  70. TestResult *m_result;
  71. };
  72. CPPUNIT_NS_END
  73. #endif // CPPUNIT_PROTECTOR_H