Blame include/cppunit/Protector.h

Packit 8c9aa0
#ifndef CPPUNIT_PROTECTOR_H
Packit 8c9aa0
#define CPPUNIT_PROTECTOR_H
Packit 8c9aa0
Packit 8c9aa0
#include <cppunit/SourceLine.h>
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS_BEGIN
Packit 8c9aa0
Packit 8c9aa0
class Exception;
Packit 8c9aa0
class Message;
Packit 8c9aa0
class ProtectorContext;
Packit 8c9aa0
class TestResult;
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
class CPPUNIT_API Functor
Packit 8c9aa0
{
Packit 8c9aa0
public:
Packit 8c9aa0
  virtual ~Functor();
Packit 8c9aa0
Packit 8c9aa0
  virtual bool operator()() const =0;
Packit 8c9aa0
};
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
/*! \brief Protects one or more test case run.
Packit 8c9aa0
 *
Packit 8c9aa0
 * Protector are used to globably 'decorate' a test case. The most common
Packit 8c9aa0
 * usage of Protector is to catch exception that do not subclass std::exception,
Packit 8c9aa0
 * such as MFC CException class or Rogue Wave RWXMsg class, and capture the
Packit 8c9aa0
 * message associated to the exception. In fact, CppUnit capture message from
Packit 8c9aa0
 * Exception and std::exception using a Protector.
Packit 8c9aa0
 *
Packit 8c9aa0
 * Protector are chained. When you add a Protector using 
Packit 8c9aa0
 * TestResult::pushProtector(), your protector is in fact passed as a Functor
Packit 8c9aa0
 * to the first protector of the chain.
Packit 8c9aa0
 *
Packit 8c9aa0
 * TestCase protects call to setUp(), runTest() and tearDown() by calling
Packit 8c9aa0
 * TestResult::protect().
Packit 8c9aa0
 *
Packit 8c9aa0
 * Because the protector chain is handled by TestResult, a protector can be
Packit 8c9aa0
 * active for a single test, or a complete test run.
Packit 8c9aa0
 *
Packit 8c9aa0
 * Here are some possible usages:
Packit 8c9aa0
 * - run all test case in a separate thread and assumes the test failed if it
Packit 8c9aa0
 *   did not finish in a given time (infinite loop work around)
Packit 8c9aa0
 * - performance tracing : time only the runTest() time.
Packit 8c9aa0
 * \sa TestResult, TestCase, TestListener.
Packit 8c9aa0
 */
Packit 8c9aa0
class CPPUNIT_API Protector
Packit 8c9aa0
{
Packit 8c9aa0
public:
Packit 8c9aa0
  virtual ~Protector();
Packit 8c9aa0
  
Packit 8c9aa0
  virtual bool protect( const Functor &functor,
Packit 8c9aa0
                        const ProtectorContext &context ) =0;
Packit 8c9aa0
Packit 8c9aa0
protected:
Packit 8c9aa0
  void reportError( const ProtectorContext &context,
Packit 8c9aa0
                    const Exception &error ) const;
Packit 8c9aa0
Packit 8c9aa0
  void reportError( const ProtectorContext &context,
Packit 8c9aa0
                    const Message &message,
Packit 8c9aa0
                    const SourceLine &sourceLine = SourceLine() ) const;
Packit 8c9aa0
Packit 8c9aa0
  void reportFailure( const ProtectorContext &context,
Packit 8c9aa0
                      const Exception &failure ) const;
Packit 8c9aa0
Packit 8c9aa0
  Message actualMessage( const Message &message,
Packit 8c9aa0
                         const ProtectorContext &context ) const;
Packit 8c9aa0
};
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
/*! \brief Scoped protector push to TestResult.
Packit 8c9aa0
 *
Packit 8c9aa0
 * Adds the specified Protector to the specified TestResult for the object
Packit 8c9aa0
 * life-time.
Packit 8c9aa0
 */
Packit 8c9aa0
class CPPUNIT_API ProtectorGuard
Packit 8c9aa0
{
Packit 8c9aa0
public:
Packit 8c9aa0
  /// Pushes the specified protector.
Packit 8c9aa0
  ProtectorGuard( TestResult *result,
Packit 8c9aa0
                  Protector *protector );
Packit 8c9aa0
Packit 8c9aa0
  /// Pops the protector.
Packit 8c9aa0
  ~ProtectorGuard();
Packit 8c9aa0
Packit 8c9aa0
private:
Packit 8c9aa0
  ProtectorGuard( const ProtectorGuard& ); /* not copyable */
Packit 8c9aa0
  ProtectorGuard& operator=( const ProtectorGuard& ); /* not assignable */
Packit 8c9aa0
  TestResult *m_result;
Packit 8c9aa0
};
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS_END
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
#endif // CPPUNIT_PROTECTOR_H
Packit 8c9aa0