CppUnit project page FAQ CppUnit home page

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

TestAssert.h

Go to the documentation of this file.
00001 #ifndef CPPUNIT_TESTASSERT_H
00002 #define CPPUNIT_TESTASSERT_H
00003 
00004 #include <cppunit/Portability.h>
00005 #include <cppunit/Exception.h>
00006 #include <cppunit/Asserter.h>
00007 
00008 
00009 CPPUNIT_NS_BEGIN
00010 
00011 
00035 template <class T>
00036 struct assertion_traits 
00037 {  
00038     static bool equal( const T& x, const T& y )
00039     {
00040         return x == y;
00041     }
00042 
00043     static std::string toString( const T& x )
00044     {
00045         OStringStream ost;
00046         ost << x;
00047         return ost.str();
00048     }
00049 };
00050 
00051 
00056 template <class T>
00057 void assertEquals( const T& expected,
00058                    const T& actual,
00059                    SourceLine sourceLine,
00060                    const std::string &message )
00061 {
00062   if ( !assertion_traits<T>::equal(expected,actual) ) // lazy toString conversion...
00063   {
00064     Asserter::failNotEqual( assertion_traits<T>::toString(expected),
00065                             assertion_traits<T>::toString(actual),
00066                             sourceLine,
00067                             message );
00068   }
00069 }
00070 
00075 void CPPUNIT_API assertDoubleEquals( double expected,
00076                                      double actual,
00077                                      double delta,
00078                                      SourceLine sourceLine );
00079 
00080 
00081 /* A set of macros which allow us to get the line number
00082  * and file name at the point of an error.
00083  * Just goes to show that preprocessors do have some
00084  * redeeming qualities.
00085  */
00086 #if CPPUNIT_HAVE_CPP_SOURCE_ANNOTATION
00087 
00090 #define CPPUNIT_ASSERT(condition)                                                 \
00091   ( CPPUNIT_NS::Asserter::failIf( !(condition),                                   \
00092                                  CPPUNIT_NS::Message( "assertion failed",         \
00093                                                       "Expression: " #condition), \
00094                                  CPPUNIT_SOURCELINE() ) )
00095 #else
00096 #define CPPUNIT_ASSERT(condition)                                            \
00097   ( CPPUNIT_NS::Asserter::failIf( !(condition),                              \
00098                                   CPPUNIT_NS::Message( "assertion failed" ), \
00099                                   CPPUNIT_SOURCELINE() ) )
00100 #endif
00101 
00109 #define CPPUNIT_ASSERT_MESSAGE(message,condition)          \
00110   ( CPPUNIT_NS::Asserter::failIf( !(condition),            \
00111                                   (message),               \
00112                                   CPPUNIT_SOURCELINE() ) )
00113 
00118 #define CPPUNIT_FAIL( message )                                         \
00119   ( CPPUNIT_NS::Asserter::fail( CPPUNIT_NS::Message( "forced failure",  \
00120                                                      message ),         \
00121                                 CPPUNIT_SOURCELINE() ) )
00122 
00123 #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
00124 
00125 #define CPPUNIT_ASSERT_EQUAL(expected,actual)                     \
00126   ( CPPUNIT_NS::assertEquals( (expected),             \
00127                               (actual),               \
00128                               __LINE__, __FILE__ ) )
00129 #else
00130 
00146 #define CPPUNIT_ASSERT_EQUAL(expected,actual)          \
00147   ( CPPUNIT_NS::assertEquals( (expected),              \
00148                               (actual),                \
00149                               CPPUNIT_SOURCELINE(),    \
00150                               "" ) )
00151 
00170 #define CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,actual)      \
00171   ( CPPUNIT_NS::assertEquals( (expected),              \
00172                               (actual),                \
00173                               CPPUNIT_SOURCELINE(),    \
00174                               (message) ) )
00175 #endif
00176 
00180 #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta)        \
00181   ( CPPUNIT_NS::assertDoubleEquals( (expected),        \
00182                                     (actual),          \
00183                                     (delta),           \
00184                                     CPPUNIT_SOURCELINE() ) )
00185 
00186 
00195 # define CPPUNIT_ASSERT_THROW( expression, ExceptionType )          \
00196    do {                                                             \
00197       bool cpputExceptionThrown_ = false;                           \
00198       try {                                                         \
00199          expression;                                                \
00200       } catch ( const ExceptionType & ) {                           \
00201          cpputExceptionThrown_ = true;                              \
00202       }                                                             \
00203                                                                     \
00204       if ( cpputExceptionThrown_ )                                  \
00205          break;                                                     \
00206                                                                     \
00207       CPPUNIT_NS::Asserter::fail(                                   \
00208                      "Expected exception: " #ExceptionType          \
00209                      " not thrown.",                                \
00210                      CPPUNIT_SOURCELINE() );                        \
00211    } while ( false )
00212 
00213 
00214 // implementation detail
00215 #if CPPUNIT_USE_TYPEINFO_NAME
00216 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00217    CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) )
00218 #else
00219 #define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \
00220    std::string( no_rtti_message )
00221 #endif // CPPUNIT_USE_TYPEINFO_NAME
00222 
00232 # define CPPUNIT_ASSERT_NO_THROW( expression )                             \
00233    try {                                                                   \
00234       expression;                                                          \
00235    } catch ( const std::exception &e ) {                                   \
00236       CPPUNIT_NS::Message message( "Unexpected exception caught" );        \
00237       message.addDetail( "Type: " +                                        \
00238                    CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e,                     \
00239                                        "std::exception or derived" ) );    \
00240       message.addDetail( std::string("What: ") + e.what() );               \
00241       CPPUNIT_NS::Asserter::fail( message,                                 \
00242                                   CPPUNIT_SOURCELINE() );                  \
00243    } catch ( ... ) {                                                       \
00244       CPPUNIT_NS::Asserter::fail( "Unexpected exception caught",           \
00245                                   CPPUNIT_SOURCELINE() );                  \
00246    }
00247 
00256 # define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion )                 \
00257    CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception )
00258 
00259 
00268 # define CPPUNIT_ASSERT_ASSERTION_PASS( assertion )                 \
00269    CPPUNIT_ASSERT_NO_THROW( assertion )
00270 
00271 
00272 
00273 
00274 // Backwards compatibility
00275 
00276 #if CPPUNIT_ENABLE_NAKED_ASSERT
00277 
00278 #undef assert
00279 #define assert(c)                 CPPUNIT_ASSERT(c)
00280 #define assertEqual(e,a)          CPPUNIT_ASSERT_EQUAL(e,a)
00281 #define assertDoublesEqual(e,a,d) CPPUNIT_ASSERT_DOUBLES_EQUAL(e,a,d)
00282 #define assertLongsEqual(e,a)     CPPUNIT_ASSERT_EQUAL(e,a)
00283 
00284 #endif
00285 
00286 
00287 CPPUNIT_NS_END
00288 
00289 #endif  // CPPUNIT_TESTASSERT_H

SourceForge Logo hosts this site. Send comments to:
CppUnit Developers