[Cppunit-cvs] cppunit2/include/cpput forwards.h,1.11,1.12 testcase.h,1.6,1.7 testfailureguard.h,1.12
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-02-22 21:15:27
|
Update of /cvsroot/cppunit/cppunit2/include/cpput In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23938/include/cpput Modified Files: forwards.h testcase.h testfailureguard.h Log Message: * simplified TestExceptionGuard implementation * renamed TestExceptionGuardChain to ExceptionGuard * added facility to easily register an exception translator. Index: testcase.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpput/testcase.h,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** testcase.h 20 Nov 2004 15:26:37 -0000 1.6 --- testcase.h 22 Feb 2005 21:14:38 -0000 1.7 *************** *** 21,25 **** /// @warning: TestInfo::startNewTest() must be called before each test ! bool runTest( const TestExceptionGuardChain &guardsChain ); public: // overridden from Test --- 21,25 ---- /// @warning: TestInfo::startNewTest() must be called before each test ! bool runTest( const ExceptionGuard &guardsChain ); public: // overridden from Test Index: testfailureguard.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpput/testfailureguard.h,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** testfailureguard.h 20 Nov 2004 15:26:37 -0000 1.12 --- testfailureguard.h 22 Feb 2005 21:14:39 -0000 1.13 *************** *** 6,10 **** # include <deque> ! /* Tests setUp(), tearDown(), run() call are protected by a TestExceptionGuardChain. * * This guard chain ensures that any exceptions thrown by the call is caught and properly handled. --- 6,10 ---- # include <deque> ! /* Tests setUp(), tearDown(), run() call are protected by a ExceptionGuard. * * This guard chain ensures that any exceptions thrown by the call is caught and properly handled. *************** *** 12,16 **** * std::exception, and any other exception. * ! * A TestExceptionGuard can be added to the guard chain to handle exception type unknown * to the test framework which are not derived from std::exception (MFC CException * for example). */ --- 12,16 ---- * std::exception, and any other exception. * ! * A ExceptionGuardElement can be added to the guard chain to handle exception type unknown * to the test framework which are not derived from std::exception (MFC CException * for example). */ *************** *** 18,39 **** namespace CppUT { ! class CPPUT_API TestExceptionGuard { public: ! virtual ~TestExceptionGuard() ! { ! } ! ! void setNextInChain( const TestExceptionGuardPtr &deleguate ); ! struct Context { ! Context( Functor0 test ) ! : test_( test ) ! { ! } ! Functor0 test_; ! }; /// Returns \c false if a failure or a fault occurred, \c true otherwise. --- 18,31 ---- namespace CppUT { ! class CPPUT_API ExceptionGuardElement { public: ! struct Context; ! virtual ~ExceptionGuardElement() { ! } ! void setNextInChain( const ExceptionGuardElementPtr &deleguate ); /// Returns \c false if a failure or a fault occurred, \c true otherwise. *************** *** 44,71 **** private: ! TestExceptionGuardPtr deleguate_; }; ! class CPPUT_API TestExceptionGuardChain { public: ! TestExceptionGuardChain(); ! void appendGuard( const TestExceptionGuardPtr &guard ); - void removeLastGuard(); ! bool protect( Functor0 test ) const; ! private: ! void connectChain(); private: ! typedef std::deque<TestExceptionGuardPtr> Guards; Guards guards_; }; } // namespace CppUT --- 36,121 ---- private: ! ExceptionGuardElementPtr deleguate_; }; ! /** Helpers to provide simple exception translation. ! */ ! template<class ExceptionType ! ,class Translator> ! class ExceptionTranslatorGuard : public ExceptionGuardElement { public: ! ExceptionTranslatorGuard( Translator translator ) ! : translator_( translator ) ! { ! } ! // overridden from ExceptionGuardElement ! bool protect( Context &context ) ! { ! try ! { ! return callNextInChain( context ); ! } ! catch ( const ExceptionType &e ) ! { ! translator_( e ); ! return false; ! } ! } ! private: ! Translator translator_; ! }; ! /** ! * @see registerExceptionTranslation() to easily register custom exception translator. ! */ ! class CPPUT_API ExceptionGuard ! { ! public: ! ExceptionGuard(); ! void append( const ExceptionGuardElementPtr &guard ); ! ! void removeLast(); ! ! bool protect( Functor0 test ) const; private: ! typedef std::deque<ExceptionGuardElementPtr> Guards; Guards guards_; }; + + /** Register an exception translator. + * @todo provides some helper to generate faul result... + * \code + * static void translateMFCException( CException *e ) { + * TCHAR szCause[255]; + * ex.GetErrorMessage(szCause, 255); + * CString str( szCause ); + * + * OpenTest::Properties fault; + * fault["type"] = "unexpected exception"; + * fault["exception_type"] = "CException"; + * fault["message"].append( (const char *)str ); + * TestInfo::appendFaultToResult( fault ); + * } + * \endcode + */ + template<class Translator + ,class ExceptionType> + void registerExceptionTranslation( ExceptionGuard &guard, + Translator translator, + CppTL::Type<ExceptionType> ) + { + typedef ExceptionTranslatorGuard<ExceptionType,Translator> GuardType; + guard.append( ExceptionGuardElementPtr( new GuardType( translator ) ) ); + } + + } // namespace CppUT Index: forwards.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpput/forwards.h,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** forwards.h 18 Nov 2004 09:20:34 -0000 1.11 --- forwards.h 22 Feb 2005 21:14:37 -0000 1.12 *************** *** 24,29 **** class TestResult; class TestExceptionHandler; ! class TestExceptionGuard; ! class TestExceptionGuardChain; class TestInfoData; class TestListener; --- 24,29 ---- class TestResult; class TestExceptionHandler; ! class ExceptionGuardElement; ! class ExceptionGuard; class TestInfoData; class TestListener; *************** *** 33,37 **** typedef CppTL::SharedPtr<Test> TestPtr; ! typedef CppTL::SharedPtr<TestExceptionGuard> TestExceptionGuardPtr; typedef CppTL::SharedPtr<TestInfoData> TestInfoDataPtr; typedef CppTL::SharedPtr<AbstractTestSuite> AbstractTestSuitePtr; --- 33,37 ---- typedef CppTL::SharedPtr<Test> TestPtr; ! typedef CppTL::SharedPtr<ExceptionGuardElement> ExceptionGuardElementPtr; typedef CppTL::SharedPtr<TestInfoData> TestInfoDataPtr; typedef CppTL::SharedPtr<AbstractTestSuite> AbstractTestSuitePtr; |