summaryrefslogtreecommitdiff
path: root/include/cppunit
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2002-08-03 14:50:09 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2002-08-03 14:50:09 +0000
commite82ccd481800f8f0d36af5310c535b83a6cec788 (patch)
tree2b312dbac05563f3ed7baf63ddbd1fcdcbefc686 /include/cppunit
parent2a31073734be6e44e477079699578820282b7345 (diff)
downloadcppunit-e82ccd481800f8f0d36af5310c535b83a6cec788.tar.gz
Include/cppunit/Exception.
include/cppunit/Exception.h: * src/cppunit/Exception.h: added setMessage(). * include/cppunit/Protector.h: * src/cppunit/Protector.cpp: added class ProtectorGuard. Change the reportXXX() method to support Exception passing and SourceLine. * include/cppunit/TestCaller.h: removed 'expect exception' features. It is now handled by ExceptionTestCaseDecorator and TestCaller no longer need default template argument support. * include/cppunit/TestCase.h: * include/cppunit/extensions/TestCaller.h: runTest() is now public instead of protected, so that it can be decorated. * include/cppunit/TestResult.h: * src/cppunit/TestResult.h: added pushProtector() and popProtector() methods. This allow user to specify their own exception trap when running test case. * include/cppunit/extensions/TestDecorator.h: * src/cppunit/TestDecorator.cpp: added. Extracted from TestDecorator.h. The test passed to the constructor is now owned by the decorator. * include/cppunit/extensions/TestCaseDecorator.h: * src/cppunit/TestCaseDecorator.cpp: added. Decorator for TestCase setUp(), tearDown() and runTest(). * include/cppunit/extensions/ExceptionTestCaseDecorator.h: added. TestCaseDecorator to expect that a specific exception is thrown. * include/cppunit/extensions/HelperMacros.h: updated against TestCaller change. * src/cppunit/DefaultFunctor.h: fixed bug (did not return underlying test return code). * src/cppunit/ProtectorChain.cpp: fixed bug in chaing return code. * src/cppunit/DefaultFunctor.h: fixed bug. * src/msvc6/testrunner/ActiveTest.h: * src/msvc6/testrunner/ActiveTest.cpp: updated against TestCaseDecorator ownership policy change. Moved inline functions to .cpp. * examples/cppunittest/TestSetUpTest.cpp: updated to use MockTestCase and against the new ownership policy. * examples/cppunittest/TestDecoratorTest.cpp: * examples/cppunittest/RepeatedTestTest.cpp: updated against TestDecorator ownership policy change. * examples/cppunittest/ExceptionTestCaseDecoratorTest.h: * examples/cppunittest/ExceptionTestCaseDecoratorTest.cpp: added. Unit tests for ExceptionTestCaseDecoratorTest.
Diffstat (limited to 'include/cppunit')
-rw-r--r--include/cppunit/Exception.h5
-rw-r--r--include/cppunit/Protector.h56
-rw-r--r--include/cppunit/TestCaller.h19
-rw-r--r--include/cppunit/TestCase.h1
-rw-r--r--include/cppunit/TestResult.h5
-rw-r--r--include/cppunit/extensions/ExceptionTestCaseDecorator.h104
-rw-r--r--include/cppunit/extensions/HelperMacros.h13
-rw-r--r--include/cppunit/extensions/Makefile.am2
-rw-r--r--include/cppunit/extensions/TestCaseDecorator.h40
-rw-r--r--include/cppunit/extensions/TestDecorator.h48
10 files changed, 225 insertions, 68 deletions
diff --git a/include/cppunit/Exception.h b/include/cppunit/Exception.h
index da53cda..09e3198 100644
--- a/include/cppunit/Exception.h
+++ b/include/cppunit/Exception.h
@@ -24,7 +24,7 @@ public:
* \param sourceLine Source location related to the exception.
*/
Exception( const Message &message = Message(),
- const SourceLine &sourceLine = SourceLine() );
+ const SourceLine &sourceLine = SourceLine() );
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
/*!
@@ -55,6 +55,9 @@ public:
/// Message related to the exception.
Message message() const;
+ /// Set the message.
+ void setMessage( const Message &message );
+
#ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
/// The line on which the error occurred
long lineNumber() const;
diff --git a/include/cppunit/Protector.h b/include/cppunit/Protector.h
index f6073a2..f854878 100644
--- a/include/cppunit/Protector.h
+++ b/include/cppunit/Protector.h
@@ -1,13 +1,14 @@
#ifndef CPPUNIT_PROTECTOR_H
#define CPPUNIT_PROTECTOR_H
-#include <cppunit/Portability.h>
+#include <cppunit/SourceLine.h>
CPPUNIT_NS_BEGIN
-
+class Exception;
class Message;
class ProtectorContext;
+class TestResult;
class CPPUNIT_API Functor
@@ -19,6 +20,30 @@ public:
};
+/*! \brief Protects one or more test case run.
+ *
+ * Protector are used to globably 'decorate' a test case. The most common
+ * usage of Protector is to catch exception that do not subclass std::exception,
+ * such as MFC CException class or Rogue Wave RWXMsg class, and capture the
+ * message associated to the exception. In fact, CppUnit capture message from
+ * Exception and std::exception using a Protector.
+ *
+ * Protector are chained. When you add a Protector using
+ * TestResult::pushProtector(), your protector is in fact passed as a Functor
+ * to the first protector of the chain.
+ *
+ * TestCase protects call to setUp(), runTest() and tearDown() by calling
+ * TestResult::protect().
+ *
+ * Because the protector chain is handled by TestResult, a protector can be
+ * active for a single test, or a complete test run.
+ *
+ * Here are some possible usages:
+ * - run all test case in a separate thread and assumes the test failed if it
+ * did not finish in a given time (infinite loop work around)
+ * - performance tracing : time only the runTest() time.
+ * \sa TestResult, TestCase, TestListener.
+ */
class CPPUNIT_API Protector
{
public:
@@ -28,9 +53,30 @@ public:
const ProtectorContext &context ) =0;
protected:
- void reportTestFailure( const Message &message,
- const ProtectorContext &context,
- bool isError );
+ void reportError( const ProtectorContext &context,
+ const Exception &error ) const;
+
+ void reportError( const ProtectorContext &context,
+ const Message &message,
+ const SourceLine &sourceLine = SourceLine() ) const;
+
+ void reportFailure( const ProtectorContext &context,
+ const Exception &failure ) const;
+
+ Message actualMessage( const Message &message,
+ const ProtectorContext &context ) const;
+};
+
+
+class CPPUNIT_API ProtectorGuard
+{
+public:
+ ProtectorGuard( TestResult *result,
+ Protector *protector );
+ ~ProtectorGuard();
+
+private:
+ TestResult *m_result;
};
CPPUNIT_NS_END
diff --git a/include/cppunit/TestCaller.h b/include/cppunit/TestCaller.h
index f9f6516..dc4d82e 100644
--- a/include/cppunit/TestCaller.h
+++ b/include/cppunit/TestCaller.h
@@ -12,7 +12,7 @@
CPPUNIT_NS_BEGIN
-
+#if 0
/*! \brief Marker class indicating that no exception is expected by TestCaller.
* This class is an implementation detail. You should never use this class directly.
*/
@@ -59,6 +59,7 @@ struct ExpectedExceptionTraits<NoExceptionExpected>
};
+#endif
//*** FIXME: rework this when class Fixture is implemented. ***//
@@ -99,8 +100,7 @@ struct ExpectedExceptionTraits<NoExceptionExpected>
* \see TestCase
*/
-template <class Fixture,
- class ExpectedException = NoExceptionExpected>
+template <class Fixture>
class TestCaller : public TestCase
{
typedef void (Fixture::*TestMethod)();
@@ -160,17 +160,16 @@ public:
delete m_fixture;
}
-protected:
void runTest()
{
- try {
+// try {
(m_fixture->*m_test)();
- }
- catch ( ExpectedException & ) {
- return;
- }
+// }
+// catch ( ExpectedException & ) {
+// return;
+// }
- ExpectedExceptionTraits<ExpectedException>::expectedException();
+// ExpectedExceptionTraits<ExpectedException>::expectedException();
}
void setUp()
diff --git a/include/cppunit/TestCase.h b/include/cppunit/TestCase.h
index bd1529e..d4b7a46 100644
--- a/include/cppunit/TestCase.h
+++ b/include/cppunit/TestCase.h
@@ -39,7 +39,6 @@ public:
std::string getName() const;
-protected:
//! FIXME: this should probably be pure virtual.
virtual void runTest();
diff --git a/include/cppunit/TestResult.h b/include/cppunit/TestResult.h
index 7168409..4388847 100644
--- a/include/cppunit/TestResult.h
+++ b/include/cppunit/TestResult.h
@@ -16,6 +16,7 @@ CPPUNIT_NS_BEGIN
class Exception;
class Functor;
+class Protector;
class ProtectorChain;
class Test;
class TestFailure;
@@ -74,6 +75,10 @@ public:
Test *test,
const std::string &shortDescription = std::string("") );
+ virtual void pushProtector( Protector *protector );
+
+ virtual void popProtector();
+
protected:
void addFailure( const TestFailure &failure );
diff --git a/include/cppunit/extensions/ExceptionTestCaseDecorator.h b/include/cppunit/extensions/ExceptionTestCaseDecorator.h
new file mode 100644
index 0000000..d8d1e82
--- /dev/null
+++ b/include/cppunit/extensions/ExceptionTestCaseDecorator.h
@@ -0,0 +1,104 @@
+#ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
+#define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
+
+#include <cppunit/Portability.h>
+#include <cppunit/Exception.h>
+#include <cppunit/extensions/TestCaseDecorator.h>
+
+CPPUNIT_NS_BEGIN
+
+
+/*! \brief Expected exception test case decorator.
+ *
+ * A decorator used to assert that a specific test case should throw an
+ * exception of a given type.
+ *
+ * You should use this class only if you need to check the exception object
+ * state (that a specific cause is set for example). If you don't need to
+ * do that, you might consider using CPPUNIT_TEST_EXCEPTION() instead.
+ *
+ * Intended use is to subclass and override checkException(). Example:
+ *
+ * \code
+ *
+ * class NetworkErrorTestCaseDecorator :
+ * public ExceptionTestCaseDecorator<NetworkError>
+ * {
+ * public:
+ * NetworkErrorTestCaseDecorator( NetworkError::Cause expectedCause )
+ * : m_expectedCause( expectedCause )
+ * {
+ * }
+ * private:
+ * void checkException( ExpectedExceptionType &e )
+ * {
+ * CPPUNIT_ASSERT_EQUAL( m_expectedCause, e.getCause() );
+ * }
+ *
+ * NetworkError::Cause m_expectedCause;
+ * };
+ * \endcode
+ *
+ */
+template<class ExpectedException>
+class CPPUNIT_API ExceptionTestCaseDecorator : public TestCaseDecorator
+{
+public:
+ typedef ExpectedException ExpectedExceptionType;
+
+ /*! \brief Decorates the specified test.
+ * \param test TestCase to decorate. Assumes ownership of the test.
+ */
+ ExceptionTestCaseDecorator( TestCase *test )
+ : TestCaseDecorator( test )
+ {
+ }
+
+ /*! \brief Checks that the expected exception is thrown by the decorated test.
+ * is thrown.
+ *
+ * Calls the decorated test runTest() and checks that an exception of
+ * type ExpectedException is thrown. Call checkException() passing the
+ * exception that was caught so that some assertions can be made if
+ * needed.
+ */
+ void runTest()
+ {
+ try
+ {
+ TestCaseDecorator::runTest();
+ }
+ catch ( ExpectedExceptionType &e )
+ {
+ checkException( e );
+ return;
+ }
+
+ // Moved outside the try{} statement to handle the case where the
+ // expected exception type is Exception (expecting assertion failure).
+#if CPPUNIT_USE_TYPEINFO_NAME
+ throw Exception( Message(
+ "expected exception not thrown",
+ "Expected exception type: " +
+ TypeInfoHelper::getClassName(
+ typeid( ExpectedExceptionType ) ) ) );
+#else
+ throw Exception( "expected exception not thrown" );
+#endif
+ }
+
+private:
+ /*! \brief Called when the exception is caught.
+ *
+ * Should be overriden to check the exception.
+ */
+ virtual void checkException( ExpectedExceptionType &e )
+ {
+ }
+};
+
+
+CPPUNIT_NS_END
+
+#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
+
diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h
index 379187f..05b3631 100644
--- a/include/cppunit/extensions/HelperMacros.h
+++ b/include/cppunit/extensions/HelperMacros.h
@@ -9,6 +9,7 @@
#include <cppunit/Portability.h>
#include <cppunit/extensions/AutoRegisterSuite.h>
#include <cppunit/extensions/TestSuiteBuilder.h>
+#include <cppunit/extensions/ExceptionTestCaseDecorator.h>
#include <string>
CPPUNIT_NS_BEGIN
@@ -272,11 +273,17 @@ CPPUNIT_NS_END
* method.
*/
#define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType ) \
- CPPUNIT_TEST_ADD( (new CPPUNIT_NS::TestCaller<ThisTestFixtureType, \
- ExceptionType>( \
+ CPPUNIT_TEST_ADD( \
+ (new CPPUNIT_NS::ExceptionTestCaseDecorator< ExceptionType >( \
+ new CPPUNIT_NS::TestCaller< ThisTestFixtureType >( \
namer.getTestNameFor( #testMethod ), \
&ThisTestFixtureType::testMethod, \
- factory.makeFixture() ) ) )
+ factory.makeFixture() ) ) ) )
+// CPPUNIT_TEST_ADD( (new CPPUNIT_NS::TestCaller<ThisTestFixtureType, \
+// ExceptionType>( \
+// namer.getTestNameFor( #testMethod ), \
+// &ThisTestFixtureType::testMethod, \
+// factory.makeFixture() ) ) )
/*! \brief Adds a test case which is excepted to fail.
*
diff --git a/include/cppunit/extensions/Makefile.am b/include/cppunit/extensions/Makefile.am
index 684a439..1db3ec3 100644
--- a/include/cppunit/extensions/Makefile.am
+++ b/include/cppunit/extensions/Makefile.am
@@ -6,6 +6,8 @@ libcppunitinclude_HEADERS = \
HelperMacros.h \
Orthodox.h \
RepeatedTest.h \
+ ExceptionTestCaseDecorator.h \
+ TestCaseDecorator.h \
TestDecorator.h \
TestFactoryRegistry.h \
TestNamer.h \
diff --git a/include/cppunit/extensions/TestCaseDecorator.h b/include/cppunit/extensions/TestCaseDecorator.h
new file mode 100644
index 0000000..3a15ba9
--- /dev/null
+++ b/include/cppunit/extensions/TestCaseDecorator.h
@@ -0,0 +1,40 @@
+#ifndef CPPUNIT_EXTENSIONS_TESTCASEDECORATOR_H
+#define CPPUNIT_EXTENSIONS_TESTCASEDECORATOR_H
+
+#include <cppunit/Portability.h>
+#include <cppunit/TestCase.h>
+
+CPPUNIT_NS_BEGIN
+
+
+/*! \brief Decorator for Test cases.
+ *
+ * TestCaseDecorator provides an alternate means to extend functionality
+ * of a test class without subclassing the test. Instead, one can
+ * subclass the decorater and use it to wrap the test class.
+ *
+ * Does not assume ownership of the test it decorates
+ */
+class CPPUNIT_API TestCaseDecorator : public TestCase
+{
+public:
+ TestCaseDecorator( TestCase *test );
+ ~TestCaseDecorator();
+
+ std::string getName() const;
+
+ void setUp();
+
+ void tearDown();
+
+ void runTest();
+
+protected:
+ TestCase *m_test;
+};
+
+
+CPPUNIT_NS_END
+
+#endif
+
diff --git a/include/cppunit/extensions/TestDecorator.h b/include/cppunit/extensions/TestDecorator.h
index 408c743..59d9a30 100644
--- a/include/cppunit/extensions/TestDecorator.h
+++ b/include/cppunit/extensions/TestDecorator.h
@@ -43,54 +43,6 @@ private:
};
-inline
-TestDecorator::TestDecorator( Test *test )
-{
- m_test = test;
-}
-
-
-inline
-TestDecorator::~TestDecorator()
-{
-}
-
-
-inline int
-TestDecorator::countTestCases() const
-{
- return m_test->countTestCases();
-}
-
-
-inline void
-TestDecorator::run( TestResult *result )
-{
- m_test->run(result);
-}
-
-
-inline std::string
-TestDecorator::getName() const
-{
- return m_test->getName();
-}
-
-
-inline int
-TestDecorator::getChildTestCount() const
-{
- return m_test->getChildTestCount();
-}
-
-
-inline Test *
-TestDecorator::doGetChildTestAt( int index ) const
-{
- return m_test->getChildTestAt( index );
-}
-
-
CPPUNIT_NS_END
#endif