CppUnit project page | FAQ

ExceptionTestCaseDecorator.h
Go to the documentation of this file.
1#ifndef CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
2#define CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
3
4#include <cppunit/Portability.h>
5#include <cppunit/Exception.h>
6#include <cppunit/extensions/TestCaseDecorator.h>
7
8 CPPUNIT_NS_BEGIN
9
10
43 template<class ExpectedException>
44 class ExceptionTestCaseDecorator : public TestCaseDecorator
45{
46 public:
47 typedef ExpectedException ExpectedExceptionType;
48
52 ExceptionTestCaseDecorator( TestCase *test )
53 : TestCaseDecorator( test )
54 {
55 }
56
65 void runTest()
66 {
67 try
68 {
69 TestCaseDecorator::runTest();
70 }
71 catch ( ExpectedExceptionType &e )
72 {
73 checkException( e );
74 return;
75 }
76
77 // Moved outside the try{} statement to handle the case where the
78 // expected exception type is Exception (expecting assertion failure).
79#if defined(CPPUNIT_USE_TYPEINFO_NAME)
80 throw Exception( Message(
81 "expected exception not thrown",
82 "Expected exception type: " +
83 TypeInfoHelper::getClassName(
84 typeid( ExpectedExceptionType ) ) ) );
85#else
86 throw Exception( Message("expected exception not thrown") );
87#endif
88 }
89
90 private:
95 virtual void checkException( ExpectedExceptionType & )
96 {
97 }
98};
99
100
101 CPPUNIT_NS_END
102
103#endif // CPPUNIT_EXTENSIONS_EXCEPTIONTESTCASEDECORATOR_H
104
Exception.h
Portability.h
CPPUNIT_NS_END
#define CPPUNIT_NS_END
Definition Portability.h:106
CPPUNIT_NS_BEGIN
#define CPPUNIT_NS_BEGIN
Definition Portability.h:105
TestCaseDecorator.h
Exception
Exceptions thrown by failed assertions.
Definition Exception.h:20
ExceptionTestCaseDecorator
Expected exception test case decorator.
Definition ExceptionTestCaseDecorator.h:45
ExceptionTestCaseDecorator::ExceptionTestCaseDecorator
ExceptionTestCaseDecorator(TestCase *test)
Decorates the specified test.
Definition ExceptionTestCaseDecorator.h:52
ExceptionTestCaseDecorator::checkException
virtual void checkException(ExpectedExceptionType &)
Called when the exception is caught.
Definition ExceptionTestCaseDecorator.h:95
ExceptionTestCaseDecorator::runTest
void runTest()
Checks that the expected exception is thrown by the decorated test. is thrown.
Definition ExceptionTestCaseDecorator.h:65
ExceptionTestCaseDecorator::ExpectedExceptionType
ExpectedException ExpectedExceptionType
Definition ExceptionTestCaseDecorator.h:47
Message
Message associated to an Exception.A message is composed of two items:
Definition Message.h:35
TestCaseDecorator
Decorator for Test cases.
Definition TestCaseDecorator.h:19
TestCaseDecorator::runTest
void runTest()
FIXME: this should probably be pure virtual.
Definition TestCaseDecorator.cpp:41
TestCase
A single test object.
Definition TestCase.h:29
TypeInfoHelper::getClassName
static std::string getClassName(const std::type_info &info)
Get the class name of the specified type_info.
Definition TypeInfoHelper.cpp:15

| Send comments to: CppUnit Developers