summaryrefslogtreecommitdiff
path: root/include/cppunit
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2004-11-05 21:47:21 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2004-11-05 21:47:21 +0000
commite298c653900aa83d02c50b6edd541668ba5002f0 (patch)
tree8765c3a4ce3c04b9a3462de4c570779816f6fd7c /include/cppunit
parent23538b49b25d11680566e8203a618b21acc83c77 (diff)
downloadcppunit-e298c653900aa83d02c50b6edd541668ba5002f0.tar.gz
Include/cppunit/TestAssert.
include/cppunit/TestAssert.h: * src/cppunit/TestAssert.cpp: integrated Neil Ferguson patch for missing _MESSAGE assertion variants. Also enhanced the failure message of a few assertions.
Diffstat (limited to 'include/cppunit')
-rw-r--r--include/cppunit/TestAssert.h179
1 files changed, 136 insertions, 43 deletions
diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h
index 364d170..3490b40 100644
--- a/include/cppunit/TestAssert.h
+++ b/include/cppunit/TestAssert.h
@@ -12,7 +12,7 @@ CPPUNIT_NS_BEGIN
/*! \brief Traits used by CPPUNIT_ASSERT_EQUAL().
*
- * Here is an example of specialization of that traits:
+ * Here is an example of specialising these traits:
*
* \code
* template<>
@@ -69,6 +69,7 @@ void assertEquals( const T& expected,
}
}
+
/*! \brief (Implementation) Asserts that two double are equals given a tolerance.
* Use CPPUNIT_ASSERT_DOUBLES_EQUAL instead of this function.
* \sa Asserter::failNotEqual().
@@ -76,7 +77,8 @@ void assertEquals( const T& expected,
void CPPUNIT_API assertDoubleEquals( double expected,
double actual,
double delta,
- SourceLine sourceLine );
+ SourceLine sourceLine,
+ const std::string &message );
/* A set of macros which allow us to get the line number
@@ -107,9 +109,12 @@ void CPPUNIT_API assertDoubleEquals( double expected,
* \param condition If this condition evaluates to \c false then the
* test failed.
*/
-#define CPPUNIT_ASSERT_MESSAGE(message,condition) \
- ( CPPUNIT_NS::Asserter::failIf( !(condition), \
- (message), \
+#define CPPUNIT_ASSERT_MESSAGE(message,condition) \
+ ( CPPUNIT_NS::Asserter::failIf( !(condition), \
+ CPPUNIT_NS::Message( "assertion failed", \
+ "Expression: " \
+ #condition, \
+ message ), \
CPPUNIT_SOURCELINE() ) )
/** Fails with the specified message.
@@ -150,7 +155,7 @@ void CPPUNIT_API assertDoubleEquals( double expected,
CPPUNIT_SOURCELINE(), \
"" ) )
-/** Asserts that two values are equals, provides additional messafe on failure.
+/** Asserts that two values are equals, provides additional message on failure.
* \ingroup Assertions
*
* Equality and string representation can be defined with
@@ -175,17 +180,30 @@ void CPPUNIT_API assertDoubleEquals( double expected,
(message) ) )
#endif
-/*! \brief Macro for primitive value comparisons
+/*! \brief Macro for primitive double value comparisons.
* \ingroup Assertions
*/
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \
- ( CPPUNIT_NS::assertDoubleEquals( (expected), \
- (actual), \
- (delta), \
- CPPUNIT_SOURCELINE() ) )
+ ( CPPUNIT_NS::assertDoubleEquals( (expected), \
+ (actual), \
+ (delta), \
+ CPPUNIT_SOURCELINE(), \
+ "" ) )
+
+
+/*! \brief Macro for primitive double value comparisons, setting a
+ * user-supplied message in case of failure.
+ * \ingroup Assertions
+ */
+#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \
+ ( CPPUNIT_NS::assertDoubleEquals( (expected), \
+ (actual), \
+ (delta), \
+ CPPUNIT_SOURCELINE(), \
+ (message) ) )
-/** Asserts that the given expression throws an exception of the specified type.
+/** Asserts that the given expression throws an exception of the specified type.
* \ingroup Assertions
* Example of usage:
* \code
@@ -193,23 +211,10 @@ void CPPUNIT_API assertDoubleEquals( double expected,
* CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range );
* \endcode
*/
-# define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
- do { \
- bool cpputExceptionThrown_ = false; \
- try { \
- expression; \
- } catch ( const ExceptionType & ) { \
- cpputExceptionThrown_ = true; \
- } \
- \
- if ( cpputExceptionThrown_ ) \
- break; \
- \
- CPPUNIT_NS::Asserter::fail( \
- "Expected exception: " #ExceptionType \
- " not thrown.", \
- CPPUNIT_SOURCELINE() ); \
- } while ( false )
+# define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \
+ CPPUNIT_ASSERT_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
+ expression, \
+ ExceptionType )
// implementation detail
@@ -221,6 +226,47 @@ void CPPUNIT_API assertDoubleEquals( double expected,
std::string( no_rtti_message )
#endif // CPPUNIT_USE_TYPEINFO_NAME
+// implementation detail
+#define CPPUNIT_GET_PARAMETER_STRING( parameter ) #parameter
+
+/** Asserts that the given expression throws an exception of the specified type,
+ * setting a user supplied message in case of failure.
+ * \ingroup Assertions
+ * Example of usage:
+ * \code
+ * std::vector<int> v;
+ * CPPUNIT_ASSERT_THROW_MESSAGE( "- std::vector<int> v;", v.at( 50 ), std::out_of_range );
+ * \endcode
+ */
+# define CPPUNIT_ASSERT_THROW_MESSAGE( message, expression, ExceptionType ) \
+ do { \
+ bool cpputCorrectExceptionThrown_ = false; \
+ CPPUNIT_NS::Message cpputMsg_( "expected exception not thrown" ); \
+ cpputMsg_.addDetail( message ); \
+ cpputMsg_.addDetail( "Expected: " \
+ CPPUNIT_GET_PARAMETER_STRING( ExceptionType ) ); \
+ \
+ try { \
+ expression; \
+ } catch ( const ExceptionType & ) { \
+ cpputCorrectExceptionThrown_ = true; \
+ } catch ( const std::exception &e) { \
+ cpputMsg_.addDetail( "Actual : " + \
+ CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
+ "std::exception or derived") ); \
+ cpputMsg_.addDetail( std::string("What() : ") + e.what() ); \
+ } catch ( ... ) { \
+ cpputMsg_.addDetail( "Actual : unknown."); \
+ } \
+ \
+ if ( cpputCorrectExceptionThrown_ ) \
+ break; \
+ \
+ CPPUNIT_NS::Asserter::fail( cpputMsg_, \
+ CPPUNIT_SOURCELINE() ); \
+ } while ( false )
+
+
/** Asserts that the given expression does not throw any exceptions.
* \ingroup Assertions
* Example of usage:
@@ -231,20 +277,41 @@ void CPPUNIT_API assertDoubleEquals( double expected,
* \endcode
*/
# define CPPUNIT_ASSERT_NO_THROW( expression ) \
- try { \
- expression; \
- } catch ( const std::exception &e ) { \
- CPPUNIT_NS::Message message( "Unexpected exception caught" ); \
- message.addDetail( "Type: " + \
- CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
- "std::exception or derived" ) ); \
- message.addDetail( std::string("What: ") + e.what() ); \
- CPPUNIT_NS::Asserter::fail( message, \
- CPPUNIT_SOURCELINE() ); \
- } catch ( ... ) { \
- CPPUNIT_NS::Asserter::fail( "Unexpected exception caught", \
- CPPUNIT_SOURCELINE() ); \
- }
+ CPPUNIT_ASSERT_NO_THROW_MESSAGE( CPPUNIT_NS::AdditionalMessage(), \
+ expression )
+
+
+/** Asserts that the given expression does not throw any exceptions,
+ * setting a user supplied message in case of failure.
+ * \ingroup Assertions
+ * Example of usage:
+ * \code
+ * std::vector<int> v;
+ * v.push_back( 10 );
+ * CPPUNIT_ASSERT_NO_THROW( "std::vector<int> v;", v.at( 0 ) );
+ * \endcode
+ */
+# define CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, expression ) \
+ do { \
+ CPPUNIT_NS::Message cpputMsg_( "unexpected exception caught" ); \
+ cpputMsg_.addDetail( message ); \
+ \
+ try { \
+ expression; \
+ } catch ( const std::exception &e ) { \
+ cpputMsg_.addDetail( "Caught: " + \
+ CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \
+ "std::exception or derived" ) ); \
+ cpputMsg_.addDetail( std::string("What(): ") + e.what() ); \
+ CPPUNIT_NS::Asserter::fail( cpputMsg_, \
+ CPPUNIT_SOURCELINE() ); \
+ } catch ( ... ) { \
+ cpputMsg_.addDetail( "Caught: unknown." ); \
+ CPPUNIT_NS::Asserter::fail( cpputMsg_, \
+ CPPUNIT_SOURCELINE() ); \
+ } \
+ } while ( false )
+
/** Asserts that an assertion fail.
* \ingroup Assertions
@@ -258,6 +325,19 @@ void CPPUNIT_API assertDoubleEquals( double expected,
CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
+/** Asserts that an assertion fail, with a user-supplied message in
+ * case of error.
+ * \ingroup Assertions
+ * Use to test assertions.
+ * Example of usage:
+ * \code
+ * CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( "1 == 2", CPPUNIT_ASSERT( 1 == 2 ) );
+ * \endcode
+ */
+# define CPPUNIT_ASSERT_ASSERTION_FAIL_MESSAGE( message, assertion ) \
+ CPPUNIT_ASSERT_THROW_MESSAGE( message, assertion, CPPUNIT_NS::Exception )
+
+
/** Asserts that an assertion pass.
* \ingroup Assertions
* Use to test assertions.
@@ -270,6 +350,19 @@ void CPPUNIT_API assertDoubleEquals( double expected,
CPPUNIT_ASSERT_NO_THROW( assertion )
+/** Asserts that an assertion pass, with a user-supplied message in
+ * case of failure.
+ * \ingroup Assertions
+ * Use to test assertions.
+ * Example of usage:
+ * \code
+ * CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( "1 != 1", CPPUNIT_ASSERT( 1 == 1 ) );
+ * \endcode
+ */
+# define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( message, assertion ) \
+ CPPUNIT_ASSERT_NO_THROW_MESSAGE( message, assertion )
+
+
// Backwards compatibility