diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-07-16 21:59:22 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2002-07-16 21:59:22 +0000 |
| commit | 2a31073734be6e44e477079699578820282b7345 (patch) | |
| tree | 70b18c3fc851173fe5fd69a7d844640ab6b780d2 /src/cppunit | |
| parent | 251c1ff8aecaa608ef9e6041c2691d369430bf7b (diff) | |
| download | cppunit-2a31073734be6e44e477079699578820282b7345.tar.gz | |
Include/cppunit/Protector.
include/cppunit/Protector.h:
* src/cppunit/Protector.cpp: added. Base class for protectors.
* src/cppunit/DefaultProtector.h:
* src/cppunit/DefaultProtector.cpp: added. Implementation of the default
protector used to catch std::exception and any other exception.
* src/cppunit/ProtectorChain.h:
* src/cppunit/ProtectorChain.cpp: added. Implementation of a chain of
protector, allowing catching custom exception and implementation of
expected exception.
* src/cppunit/TestCase.cpp:
* src/cppunit/TestResult.cpp: updated to use protector.
Diffstat (limited to 'src/cppunit')
| -rw-r--r-- | src/cppunit/DefaultProtector.cpp | 44 | ||||
| -rw-r--r-- | src/cppunit/DefaultProtector.h | 17 | ||||
| -rw-r--r-- | src/cppunit/Makefile.am | 7 | ||||
| -rw-r--r-- | src/cppunit/Protector.cpp | 41 | ||||
| -rw-r--r-- | src/cppunit/ProtectorChain.cpp | 89 | ||||
| -rw-r--r-- | src/cppunit/ProtectorChain.h | 47 | ||||
| -rw-r--r-- | src/cppunit/ProtectorContext.h | 33 | ||||
| -rw-r--r-- | src/cppunit/TestCase.cpp | 47 | ||||
| -rw-r--r-- | src/cppunit/TestResult.cpp | 16 | ||||
| -rw-r--r-- | src/cppunit/cppunit.dsp | 32 | ||||
| -rw-r--r-- | src/cppunit/cppunit_dll.dsp | 32 |
11 files changed, 398 insertions, 7 deletions
diff --git a/src/cppunit/DefaultProtector.cpp b/src/cppunit/DefaultProtector.cpp new file mode 100644 index 0000000..4c8a3ab --- /dev/null +++ b/src/cppunit/DefaultProtector.cpp @@ -0,0 +1,44 @@ +#include <cppunit/Exception.h> +#include <cppunit/extensions/TypeInfoHelper.h> +#include "DefaultProtector.h" + + +CPPUNIT_NS_BEGIN + + +bool +DefaultProtector::protect( const Functor &functor, + const ProtectorContext &context ) +{ + try + { + functor(); + return true; + } + catch ( Exception &failure ) + { + reportTestFailure( failure.message(), context, false ); + } + catch ( std::exception &e ) + { + std::string shortDescription( "uncaught exception of type " ); +#if CPPUNIT_USE_TYPEINFO_NAME + shortDescription += TypeInfoHelper::getClassName( typeid(e) ); +#else + shortDescription += "std::exception (or derived)." +#endif + Message message( shortDescription, e.what() ); + reportTestFailure( message, context, true ); + } + catch ( ... ) + { + reportTestFailure( Message( "uncaught exception of unknown type"), + context, + true ); + } + + return false; +} + + +CPPUNIT_NS_END diff --git a/src/cppunit/DefaultProtector.h b/src/cppunit/DefaultProtector.h new file mode 100644 index 0000000..c2c3d02 --- /dev/null +++ b/src/cppunit/DefaultProtector.h @@ -0,0 +1,17 @@ +#ifndef CPPUNIT_DEFAULTPROTECTOR_H +#define CPPUNIT_DEFAULTPROTECTOR_H + +#include <cppunit/Protector.h> + +CPPUNIT_NS_BEGIN + +class DefaultProtector : public Protector +{ +public: + bool protect( const Functor &functor, + const ProtectorContext &context ); +}; + +CPPUNIT_NS_END + +#endif // CPPUNIT_DEFAULTPROTECTOR_H
\ No newline at end of file diff --git a/src/cppunit/Makefile.am b/src/cppunit/Makefile.am index 6dd72e2..33a29c2 100644 --- a/src/cppunit/Makefile.am +++ b/src/cppunit/Makefile.am @@ -1,5 +1,5 @@ # -# $Id: Makefile.am,v 1.36 2002-07-03 07:02:49 blep Exp $ +# $Id: Makefile.am,v 1.37 2002-07-16 22:59:22 blep Exp $ # EXTRA_DIST = cppunit.dsp cppunit_dll.dsp DllMain.cpp @@ -13,12 +13,17 @@ libcppunit_la_SOURCES = \ BeOsDynamicLibraryManager.cpp \ BriefTestProgressListener.cpp \ CompilerOutputter.cpp \ + DefaultProtector.h \ + DefaultProtector.cpp \ DynamicLibraryManager.cpp \ DynamicLibraryManagerException.cpp \ Exception.cpp \ Message.cpp \ RepeatedTest.cpp \ PlugInManager.cpp \ + ProtectorChain.h \ + ProtectorContext.h \ + ProtectorChain.cpp \ SourceLine.cpp \ StringTools.cpp \ SynchronizedObject.cpp \ diff --git a/src/cppunit/Protector.cpp b/src/cppunit/Protector.cpp new file mode 100644 index 0000000..626829c --- /dev/null +++ b/src/cppunit/Protector.cpp @@ -0,0 +1,41 @@ +#include <cppunit/Exception.h> +#include <cppunit/Message.h> +#include <cppunit/Protector.h> +#include <cppunit/TestResult.h> +#include "ProtectorContext.h" + +CPPUNIT_NS_BEGIN + +Functor::~Functor() +{ +} + + +Protector::~Protector() +{ +} + + +void +Protector::reportTestFailure( const Message &message, + const ProtectorContext &context, + bool isError ) +{ + Message actualMessage; + if ( context.m_shortDescription.empty() ) + actualMessage = message; + else + { + actualMessage = Message( context.m_shortDescription, + message.shortDescription() ); + actualMessage.addDetail( message ); + } + + if ( isError ) + context.m_result->addError( context.m_test, new Exception( message ) ); + else + context.m_result->addFailure( context.m_test, new Exception( message ) ); +} + + +CPPUNIT_NS_END diff --git a/src/cppunit/ProtectorChain.cpp b/src/cppunit/ProtectorChain.cpp new file mode 100644 index 0000000..087b938 --- /dev/null +++ b/src/cppunit/ProtectorChain.cpp @@ -0,0 +1,89 @@ +#include "ProtectorChain.h" + +CPPUNIT_NS_BEGIN + + +class ProtectorChain::ProtectFunctor : public Functor +{ +public: + ProtectFunctor( Protector *protector, + const Functor &functor, + const ProtectorContext &context ) + : m_protector( protector ) + , m_functor( functor ) + , m_context( context ) + { + } + + bool operator()() const + { + return m_protector->protect( m_functor, m_context ); + } + +private: + Protector *m_protector; + const Functor &m_functor; + const ProtectorContext &m_context; +}; + + +ProtectorChain::~ProtectorChain() +{ + while ( count() > 0 ) + pop(); +} + + +void +ProtectorChain::push( Protector *protector ) +{ + m_protectors.push_back( protector ); +} + + +void +ProtectorChain::pop() +{ + delete m_protectors.back(); + m_protectors.pop_back(); +} + +int +ProtectorChain::count() const +{ + return m_protectors.size(); +} + + +bool +ProtectorChain::protect( const Functor &functor, + const ProtectorContext &context ) +{ + if ( m_protectors.empty() ) + { + functor(); + return true; + } + + Functors functors; + for ( int index = 0; index < m_protectors.size(); ++index ) + { + const Functor &protectedFunctor = + functors.empty() ? functor : *functors.back(); + + functors.push_back( new ProtectFunctor( m_protectors[index], + protectedFunctor, + context ) ); + } + + const Functor &outermostFunctor = *functors.back(); + bool succeed = outermostFunctor(); + + for ( int deletingIndex = 0; deletingIndex < m_protectors.size(); ++deletingIndex ) + delete functors[deletingIndex]; + + return succeed; +} + + +CPPUNIT_NS_END diff --git a/src/cppunit/ProtectorChain.h b/src/cppunit/ProtectorChain.h new file mode 100644 index 0000000..7c947b6 --- /dev/null +++ b/src/cppunit/ProtectorChain.h @@ -0,0 +1,47 @@ +#ifndef CPPUNIT_PROTECTORCHAIN_H +#define CPPUNIT_PROTECTORCHAIN_H + +#include <cppunit/Protector.h> +#include <cppunit/portability/CppUnitDeque.h> + +#if CPPUNIT_NEED_DLL_DECL +#pragma warning( push ) +#pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z +#endif + + +CPPUNIT_NS_BEGIN + + +class CPPUNIT_API ProtectorChain : public Protector +{ +public: + ~ProtectorChain(); + + void push( Protector *protector ); + + void pop(); + + int count() const; + + bool protect( const Functor &functor, + const ProtectorContext &context ); + +private: + class ProtectFunctor; + +private: + typedef CppUnitDeque<Protector *> Protectors; + Protectors m_protectors; + + typedef CppUnitDeque<Functor *> Functors; +}; + + +CPPUNIT_NS_END + +#if CPPUNIT_NEED_DLL_DECL +#pragma warning( pop ) +#endif + +#endif // CPPUNIT_PROTECTORCHAIN_H
\ No newline at end of file diff --git a/src/cppunit/ProtectorContext.h b/src/cppunit/ProtectorContext.h new file mode 100644 index 0000000..6a8d97a --- /dev/null +++ b/src/cppunit/ProtectorContext.h @@ -0,0 +1,33 @@ +#ifndef CPPUNIT_PROTECTORCONTEXT_H +#define CPPUNIT_PROTECTORCONTEXT_H + +#include <cppunit/Portability.h> +#include <string> + +CPPUNIT_NS_BEGIN + +class Test; +class TestResult; + + +class CPPUNIT_API ProtectorContext +{ +public: + ProtectorContext( Test *test, + TestResult *result, + const std::string &shortDescription ) + : m_test( test ) + , m_result( result ) + , m_shortDescription( shortDescription ) + { + } + + Test *m_test; + TestResult *m_result; + std::string m_shortDescription; +}; + + +CPPUNIT_NS_END + +#endif // CPPUNIT_PROTECTORCONTEXT_H
\ No newline at end of file diff --git a/src/cppunit/TestCase.cpp b/src/cppunit/TestCase.cpp index f2379c5..88a1c5c 100644 --- a/src/cppunit/TestCase.cpp +++ b/src/cppunit/TestCase.cpp @@ -1,14 +1,37 @@ #include <cppunit/Portability.h> -#include <typeinfo> -#include <stdexcept> - -#include <cppunit/TestCase.h> #include <cppunit/Exception.h> +#include <cppunit/Protector.h> +#include <cppunit/TestCase.h> #include <cppunit/TestResult.h> +#include <typeinfo> +#include <stdexcept> CPPUNIT_NS_BEGIN +class TestCaseMethodFunctor : public Functor +{ +public: + typedef void (TestCase::*Method)(); + + TestCaseMethodFunctor( TestCase *target, + Method method ) + : m_target( target ) + , m_method( method ) + { + } + + bool operator()() const + { + (m_target->*m_method)(); + return true; + } + +private: + TestCase *m_target; + Method m_method; +}; + /** Constructs a test case. * \param name the name of the TestCase. @@ -24,7 +47,7 @@ void TestCase::run( TestResult *result ) { result->startTest(this); - +/* try { setUp(); @@ -54,7 +77,19 @@ TestCase::run( TestResult *result ) catch (...) { result->addError( this, new Exception( Message( "setUp() failed" ) ) ); } - +*/ + if ( result->protect( TestCaseMethodFunctor( this, &TestCase::setUp ), + this, + "setUp() failed" ) ) + { + result->protect( TestCaseMethodFunctor( this, &TestCase::runTest ), + this ); + } + + result->protect( TestCaseMethodFunctor( this, &TestCase::tearDown ), + this, + "tearDown() failed" ); + result->endTest( this ); } diff --git a/src/cppunit/TestResult.cpp b/src/cppunit/TestResult.cpp index 2b20e4c..cf03dd4 100644 --- a/src/cppunit/TestResult.cpp +++ b/src/cppunit/TestResult.cpp @@ -3,6 +3,9 @@ #include <cppunit/TestListener.h> #include <cppunit/TestResult.h> #include <algorithm> +#include "DefaultProtector.h" +#include "ProtectorChain.h" +#include "ProtectorContext.h" CPPUNIT_NS_BEGIN @@ -10,7 +13,9 @@ CPPUNIT_NS_BEGIN /// Construct a TestResult TestResult::TestResult( SynchronizationObject *syncObject ) : SynchronizedObject( syncObject ) + , m_protectorChain( new ProtectorChain() ) { + m_protectorChain->push( new DefaultProtector() ); reset(); } @@ -18,6 +23,7 @@ TestResult::TestResult( SynchronizationObject *syncObject ) /// Destroys a test result TestResult::~TestResult() { + delete m_protectorChain; } @@ -184,4 +190,14 @@ TestResult::endTestRun( Test *test ) } +bool +TestResult::protect( const Functor &functor, + Test *test, + const std::string &shortDescription ) +{ + ProtectorContext context( test, this, shortDescription ); + return m_protectorChain->protect( functor, context ); +} + + CPPUNIT_NS_END diff --git a/src/cppunit/cppunit.dsp b/src/cppunit/cppunit.dsp index e0bd8c6..f44e1bf 100644 --- a/src/cppunit/cppunit.dsp +++ b/src/cppunit/cppunit.dsp @@ -599,6 +599,38 @@ SOURCE=.\XmlElement.cpp SOURCE=..\..\include\cppunit\tools\XmlElement.h # End Source File # End Group +# Begin Group "protector" + +# PROP Default_Filter "" +# Begin Source File + +SOURCE=.\DefaultProtector.cpp +# End Source File +# Begin Source File + +SOURCE=.\DefaultProtector.h +# End Source File +# Begin Source File + +SOURCE=.\Protector.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\include\cppunit\Protector.h +# End Source File +# Begin Source File + +SOURCE=.\ProtectorChain.cpp +# End Source File +# Begin Source File + +SOURCE=.\ProtectorChain.h +# End Source File +# Begin Source File + +SOURCE=.\ProtectorContext.h +# End Source File +# End Group # Begin Source File SOURCE=..\..\configure.in diff --git a/src/cppunit/cppunit_dll.dsp b/src/cppunit/cppunit_dll.dsp index 71e720f..9da9ecd 100644 --- a/src/cppunit/cppunit_dll.dsp +++ b/src/cppunit/cppunit_dll.dsp @@ -594,6 +594,38 @@ SOURCE=.\XmlElement.cpp SOURCE=..\..\include\cppunit\tools\XmlElement.h # End Source File # End Group +# Begin Group "protector" + +# PROP Default_Filter "" +# Begin Source File + +SOURCE=.\DefaultProtector.cpp +# End Source File +# Begin Source File + +SOURCE=.\DefaultProtector.h +# End Source File +# Begin Source File + +SOURCE=.\Protector.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\include\cppunit\Protector.h +# End Source File +# Begin Source File + +SOURCE=.\ProtectorChain.cpp +# End Source File +# Begin Source File + +SOURCE=.\ProtectorChain.h +# End Source File +# Begin Source File + +SOURCE=.\ProtectorContext.h +# End Source File +# End Group # Begin Source File SOURCE="..\..\INSTALL-WIN32.txt" |
