source: trunk/contrib/cppunit-1.12.2/include/cppunit/Protector.h @ 876

Last change on this file since 876 was 876, checked in by sam, 9 years ago

contrib: add a precompiled Win32 version of CppUnit and update build system
accordingly.

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