diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-06-11 19:00:52 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2001-06-11 19:00:52 +0000 |
| commit | da8e822d28d281276f4cef78b7e7c7f2660de232 (patch) | |
| tree | cc9e6073f27fd8e8754f237acb8cfb34dececea9 /include/cppunit | |
| parent | e38eb47e23d6106c32ee136351b0080313339270 (diff) | |
| download | cppunit-da8e822d28d281276f4cef78b7e7c7f2660de232.tar.gz | |
Include/cppunit/Exception.
include/cppunit/Exception.h: now inherit from std::exception instead
of ::exception. Added clone(), type(), and isInstanceOf()
methods for subclassing support. Changed UNKNOWNLINENUMBER type to long
for consistence with lineNumber().
* include/cppunit/NotEqualException.h: addded, exception to be used
with assertEquals().
* include/cppunit/TestAssert.h: changed TestAssert into a namespace
instead of a class. This remove the need of template member methods
and does not cause compiler internal error on VC++.
Macro CPPUNIT_ASSERT_MESSAGE has been added to fail test with
a specified message.
* include/cppunit/TestCaller.h: added "Expected exception" support.
Based on Tim Jansen patch (#403745), but use a traits instead of RTTI
to distingh between "No expected exception" and "Excepted exception".
Exception type name is reported using RTTI if CPPUNIT_USE_TYPEINFO is
defined.
Diffstat (limited to 'include/cppunit')
| -rw-r--r-- | include/cppunit/Exception.h | 47 | ||||
| -rw-r--r-- | include/cppunit/Makefile.am | 1 | ||||
| -rw-r--r-- | include/cppunit/NotEqualException.h | 41 | ||||
| -rw-r--r-- | include/cppunit/TestAssert.h | 55 | ||||
| -rw-r--r-- | include/cppunit/TestCaller.h | 53 | ||||
| -rw-r--r-- | include/cppunit/TestCase.h | 3 | ||||
| -rw-r--r-- | include/cppunit/config.h | 1 |
7 files changed, 151 insertions, 50 deletions
diff --git a/include/cppunit/Exception.h b/include/cppunit/Exception.h index eb43119..d324e9e 100644 --- a/include/cppunit/Exception.h +++ b/include/cppunit/Exception.h @@ -12,31 +12,50 @@ namespace CppUnit { * */ - class Exception : public exception + class Exception : public std::exception { public: - Exception (std::string message = "", - long lineNumber = UNKNOWNLINENUMBER, - std::string fileName = UNKNOWNFILENAME); + class Type + { + public: + Type( std::string type ) : m_type ( type ) {} + + bool operator ==( const Type &other ) const + { + return m_type == other.m_type; + } + private: + const std::string m_type; + }; + + + Exception( std::string message = "", + long lineNumber = UNKNOWNLINENUMBER, + std::string fileName = UNKNOWNFILENAME); Exception (const Exception& other); - virtual ~Exception (); + virtual ~Exception (); - Exception& operator= (const Exception& other); + Exception& operator= (const Exception& other); - const char *what() const throw (); + const char *what() const throw (); - long lineNumber (); - std::string fileName (); + long lineNumber (); + std::string fileName (); static const std::string UNKNOWNFILENAME; - static const int UNKNOWNLINENUMBER; + static const long UNKNOWNLINENUMBER; - private: - std::string m_message; - long m_lineNumber; - std::string m_fileName; + virtual Exception *clone() const; + virtual bool isInstanceOf( const Type &type ) const; + + static Type type(); + + private: + std::string m_message; + long m_lineNumber; + std::string m_fileName; }; } // namespace CppUnit diff --git a/include/cppunit/Makefile.am b/include/cppunit/Makefile.am index 57143bf..6856733 100644 --- a/include/cppunit/Makefile.am +++ b/include/cppunit/Makefile.am @@ -4,6 +4,7 @@ libcppunitincludedir = $(includedir)/cppunit libcppunitinclude_HEADERS = \ config.h \ Exception.h \ + NotEqualException.h \ Test.h \ TestAssert.h \ TestCaller.h \ diff --git a/include/cppunit/NotEqualException.h b/include/cppunit/NotEqualException.h new file mode 100644 index 0000000..ba39cd2 --- /dev/null +++ b/include/cppunit/NotEqualException.h @@ -0,0 +1,41 @@ +#ifndef NOTEQUALEXCEPTION_H +#define NOTEQUALEXCEPTION_H + +#include <cppunit/Exception.h> + + +namespace CppUnit { + + + class NotEqualException : public Exception + { + public: + NotEqualException( std::string expected, + std::string actual, + long lineNumber = UNKNOWNLINENUMBER, + std::string fileName = UNKNOWNFILENAME ); + + NotEqualException( const NotEqualException &other ); + + virtual ~NotEqualException(); + + /*! Copy operator. + * @param other Object to copy. + * @return Reference on this object. + */ + NotEqualException &operator =( const NotEqualException &other ); + + Exception *clone() const; + + bool isInstanceOf( const Type &type ) const; + + static Type type(); + + private: + std::string m_expected; + std::string m_actual; + }; + +} // namespace CppUnit + +#endif // NOTEQUALEXCEPTION_H diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h index b8143d3..c6ccb48 100644 --- a/include/cppunit/TestAssert.h +++ b/include/cppunit/TestAssert.h @@ -1,7 +1,6 @@ #ifndef CPPUNIT_TESTASSERT_H #define CPPUNIT_TESTASSERT_H -#include <math.h> #include <string> #include <sstream> #include <cppunit/config.h> @@ -29,26 +28,15 @@ namespace CppUnit { /*! \brief This class represents */ - class TestAssert + namespace TestAssert { - public: - virtual ~TestAssert() {} - - static void assertImplementation( - bool condition, - std::string conditionExpression = "", - long lineNumber = Exception::UNKNOWNLINENUMBER, - std::string fileName = Exception::UNKNOWNFILENAME) - { - if (!condition) - throw Exception (conditionExpression, - lineNumber, - fileName); - } - + void assertImplementation( bool condition, + std::string conditionExpression = "", + long lineNumber = Exception::UNKNOWNLINENUMBER, + std::string fileName = Exception::UNKNOWNFILENAME ); template <class T> - static std::string notEqualsMessage (const T& expected, + std::string notEqualsMessage (const T& expected, const T& actual) { return "expected: " + assertion_traits<T>::toString(expected) @@ -57,7 +45,7 @@ namespace CppUnit { template <class T> - static void assertEquals ( + void assertEquals ( const T& expected, const T& actual, long lineNumber = Exception::UNKNOWNLINENUMBER, @@ -69,18 +57,12 @@ namespace CppUnit { fileName); } - static void assertEquals (double expected, - double actual, - double delta, - long lineNumber = Exception::UNKNOWNLINENUMBER, - std::string fileName = Exception::UNKNOWNFILENAME) - { - assertImplementation( fabs(expected - actual) <= delta, - notEqualsMessage(expected, actual), - lineNumber, - fileName); - } - }; + void assertEquals( double expected, + double actual, + double delta, + long lineNumber = Exception::UNKNOWNLINENUMBER, + std::string fileName = Exception::UNKNOWNFILENAME); + } /** A set of macros which allow us to get the line number @@ -102,6 +84,17 @@ namespace CppUnit { #endif +/** Assertion with a user specified message. + * \param message Message reported in diagnostic if \a condition evaluates + * to \c false. + * \param condition If this condition evaluates to \c false then the + * test failed. + */ +#define CPPUNIT_ASSERT_MESSAGE(message,condition)\ + (CppUnit::TestAssert::assertImplementation( condition, \ + message, \ + __LINE__, \ + __FILE__ ) ) /// Generalized macro for primitive value comparisons /** Equality and string representation can be defined with diff --git a/include/cppunit/TestCaller.h b/include/cppunit/TestCaller.h index f71734e..4bbc128 100644 --- a/include/cppunit/TestCaller.h +++ b/include/cppunit/TestCaller.h @@ -2,9 +2,46 @@ #define CPPUNIT_TESTCALLER_H #include <cppunit/TestCase.h> +#include <cppunit/Exception.h> + +#if CPPUNIT_USE_TYPEINFO +# include <cppunit/extensions/TypeInfoHelper.h> +#endif // CPPUNIT_USE_TYPEINFO namespace CppUnit { + class NoExceptionExpected + { + private: + // Nobody must be able to construct an exception of this type. + NoExpectedException(); + }; + + + template<typename ExceptionType> + struct ExpectedExceptionTraits + { + static void expectedException() + { +#if CPPUNIT_USE_TYPEINFO + std::string message( "Expected exception of type " ); + message += TypeInfoHelper::getClassName( typeid( ExceptionType ) ); + message += ", but got none"; +#else + std::string message( "Expected exception but got none" ); +#endif // CPPUNIT_USE_TYPEINFO + throw new Exception( message ); + } + }; + + template<> + struct ExpectedExceptionTraits<NoExceptionExpected> + { + static void expectedException() + { + } + }; + /** Provides access to a test case method. * A test caller provides access to a test case method * on a test case class. Test callers are useful when @@ -39,10 +76,11 @@ namespace CppUnit { * \see TestCase */ - template <typename Fixture> + template <typename Fixture, + typename ExpectedException = NoExceptionExpected> class TestCaller : public TestCase { - typedef void (Fixture::*TestMethod)(); + typedef void (Fixture::*TestMethod)(); public: /** @@ -102,7 +140,16 @@ namespace CppUnit { protected: void runTest () { - (m_fixture->*m_test)(); + try + { + (m_fixture->*m_test)(); + } + catch ( ExpectedException & ) + { + return; + } + + ExpectedExceptionTraits<ExpectedException>::expectedException(); } void setUp () diff --git a/include/cppunit/TestCase.h b/include/cppunit/TestCase.h index e27ba31..dc85e7a 100644 --- a/include/cppunit/TestCase.h +++ b/include/cppunit/TestCase.h @@ -79,8 +79,7 @@ namespace CppUnit { * */ - class TestCase : public Test, - public TestAssert + class TestCase : public Test { public: TestCase (); diff --git a/include/cppunit/config.h b/include/cppunit/config.h index 0314ca2..e515a51 100644 --- a/include/cppunit/config.h +++ b/include/cppunit/config.h @@ -32,5 +32,6 @@ /* Define to 1 if the preprocessor expands (#foo) to "foo" (quotes incl.) */ #define CPPUNIT_HAVE_CPP_SOURCEANNOTATION 1 +#pragma warning( disable: 4786 ) #endif |
