00001 #ifndef CPPUT_ASSERT_H_INCLUDED
00002 # define CPPUT_ASSERT_H_INCLUDED
00003
00004 # include <cpput/equality.h>
00005 # include <cpput/message.h>
00006 # include <cpput/stringize.h>
00007 # include <cpput/testinfo.h>
00008 # include <cpput/translate.h>
00009 # include <cpptl/typename.h>
00010
00011 namespace CppUT
00012 {
00013 CheckerResult CPPUT_API pass();
00014
00015 CheckerResult CPPUT_API fail();
00016
00017 CheckerResult CPPUT_API fail( const LazyMessage &message );
00018
00019 CheckerResult CPPUT_API checkTrue( bool shouldBeTrue,
00020 const LazyMessage &message = LazyMessage::none );
00021
00022 CheckerResult CPPUT_API checkFalse( bool shouldBeFalse,
00023 const LazyMessage &message = LazyMessage::none );
00024
00025 CheckerResult CPPUT_API checkAssertionFail( bool assertionFailed,
00026 const LazyMessage &message = LazyMessage::none );
00027
00028 CheckerResult CPPUT_API checkAssertionPass( bool assertionFailed,
00029 const LazyMessage &message = LazyMessage::none );
00030
00031 void CPPUT_API buildEqualityFailedMessage( CheckerResult &result,
00032 const std::string &expected,
00033 const std::string &actual,
00034 const LazyMessage &message = LazyMessage::none );
00035
00036 template<typename FirstType
00037 ,typename SecondType>
00038 void makeEqualityFailedMessage( CheckerResult &result,
00039 const FirstType &expected,
00040 const SecondType &actual,
00041 const LazyMessage &message = LazyMessage::none )
00042
00043 {
00044 std::string strExpected = stringize( expected );
00045 std::string strActual = stringize( actual );
00046 buildEqualityFailedMessage( result, strExpected, strActual, message );
00047 }
00048
00049 template<typename FirstType
00050 ,typename SecondType
00051 ,typename EqualityFunctorType>
00052 CheckerResult checkEquals( const FirstType &expected,
00053 const SecondType &actual,
00054 EqualityFunctorType equality,
00055 const Message &message )
00056 {
00057 CheckerResult result;
00058 if ( !equality( expected, actual ) )
00059 makeEqualityFailedMessage( result, expected, actual, message );
00060 return result;
00061 }
00062
00063 template<typename FirstType
00064 ,typename SecondType>
00065 CheckerResult checkEquals( const FirstType &expected,
00066 const SecondType &actual,
00067 const LazyMessage &message = LazyMessage::none )
00068 {
00069 CheckerResult result;
00070 if ( !equalityTest( expected, actual ) )
00071 makeEqualityFailedMessage( result, expected, actual, message );
00072 return result;
00073 }
00074
00075 void CPPUT_API buildUnequalityFailedMessage( CheckerResult &result,
00076 const std::string &expected,
00077 const std::string &actual,
00078 const LazyMessage &message = LazyMessage::none );
00079
00080 template<typename FirstType
00081 ,typename SecondType>
00082 void makeUnequalityFailedMessage( CheckerResult &result,
00083 const FirstType &expected,
00084 const SecondType &actual,
00085 const LazyMessage &message = LazyMessage::none )
00086
00087 {
00088 std::string strExpected = stringize( expected );
00089 std::string strActual = stringize( actual );
00090 buildUnequalityFailedMessage( result, strExpected, strActual, message );
00091 }
00092
00093 template<typename FirstType
00094 ,typename SecondType
00095 ,typename EqualityFunctorType>
00096 CheckerResult checkNotEquals( const FirstType &expected,
00097 const SecondType &actual,
00098 EqualityFunctorType equality,
00099 const Message &message )
00100 {
00101 CheckerResult result;
00102 if ( equality( expected, actual ) )
00103 makeUnequalityFailedMessage( result, expected, actual, message );
00104 return result;
00105 }
00106
00107 template<typename FirstType
00108 ,typename SecondType>
00109 CheckerResult checkNotEquals( const FirstType &expected,
00110 const SecondType &actual,
00111 const LazyMessage &message = LazyMessage::none )
00112 {
00113 CheckerResult result;
00114 if ( equalityTest( expected, actual ) )
00115 makeUnequalityFailedMessage( result, expected, actual, message );
00116 return result;
00117 }
00118
00119 CheckerResult CPPUT_API checkDoubleEquals( double expected,
00120 double actual,
00121 double tolerance,
00122 const LazyMessage &message = LazyMessage::none );
00123
00124 void CPPUT_API skipCurrentTest();
00125
00126 }
00127
00128
00129
00130
00131
00136 # define CPPUT_FAIL \
00137 CPPUT_BEGIN_ASSERTION_MACRO() \
00138 ::CppUT::fail
00139
00144 # define CPPUT_CHECKING_FAIL \
00145 CPPUT_BEGIN_CHECKING_MACRO() \
00146 ::CppUT::fail
00147
00153 # define CPPUT_PASS \
00154 CPPUT_BEGIN_CHECKING_MACRO() \
00155 ::CppUT::pass
00156
00157
00162 # define CPPUT_ASSERT \
00163 CPPUT_BEGIN_ASSERTION_MACRO() \
00164 ::CppUT::checkTrue
00165
00170 # define CPPUT_CHECK \
00171 CPPUT_BEGIN_CHECKING_MACRO() \
00172 ::CppUT::checkTrue
00173
00179 # define CPPUT_ASSERT_EXPR( expression ) \
00180 CPPUT_ASSERT( (expression), #expression )
00181
00187 # define CPPUT_CHECK_EXPR( expression ) \
00188 CPPUT_CHECK( (expression), #expression )
00189
00194 # define CPPUT_ASSERT_FALSE \
00195 CPPUT_BEGIN_ASSERTION_MACRO() \
00196 ::CppUT::checkFalse
00197
00202 # define CPPUT_CHECK_FALSE \
00203 CPPUT_BEGIN_CHECKING_MACRO() \
00204 ::CppUT::checkFalse
00205
00211 # define CPPUT_ASSERT_EXPR_FALSE( expression ) \
00212 CPPUT_ASSERT_FALSE( (expression), #expression )
00213
00219 # define CPPUT_CHECK_EXPR_FALSE( expression ) \
00220 CPPUT_CHECK_FALSE( (expression), #expression )
00221
00228 # define CPPUT_ASSERT_EQUAL \
00229 CPPUT_BEGIN_ASSERTION_MACRO() \
00230 ::CppUT::checkEquals
00231
00238 # define CPPUT_CHECK_EQUAL \
00239 CPPUT_BEGIN_CHECKING_MACRO() \
00240 ::CppUT::checkEquals
00241
00248 # define CPPUT_ASSERT_NOT_EQUAL \
00249 CPPUT_BEGIN_ASSERTION_MACRO() \
00250 ::CppUT::checkNotEquals
00251
00258 # define CPPUT_CHECK_NOT_EQUAL \
00259 CPPUT_BEGIN_CHECKING_MACRO() \
00260 ::CppUT::checkNotEquals
00261
00266 # define CPPUT_ASSERT_DOUBLE_EQUAL \
00267 CPPUT_BEGIN_ASSERTION_MACRO() \
00268 ::CppUT::checkDoubleEquals
00269
00274 # define CPPUT_CHECK_DOUBLE_EQUAL \
00275 CPPUT_BEGIN_CHECKING_MACRO() \
00276 ::CppUT::checkDoubleEquals
00277
00283 # define _CPPUT_ASSERT_THROW_IMPL( failMacro, expression, ExceptionType ) \
00284 try { \
00285 bool noExceptionThrown = true; \
00286 try { \
00287 expression; \
00288 } catch ( const ExceptionType & ) { \
00289 CPPUT_PASS(); \
00290 noExceptionThrown = false; \
00291 } \
00292 if ( noExceptionThrown ) \
00293 failMacro( "Expected exception: " #ExceptionType \
00294 " not thrown." ); \
00295 } catch ( ... ) { \
00296 throw; \
00297 }
00298
00299
00303 # define CPPUT_ASSERT_THROW( expression, ExceptionType ) \
00304 _CPPUT_ASSERT_THROW_IMPL( CPPUT_FAIL, expression, ExceptionType )
00305
00309 # define CPPUT_CHECK_THROW( expression, ExceptionType ) \
00310 _CPPUT_CHECK_THROW_IMPL( CPPUT_CHECKING_FAIL, expression, ExceptionType )
00311
00315 # define _CPPUT_ASSERT_NO_THROW_IMPL( failMacro, expression ) \
00316 try { \
00317 expression; \
00318 } catch ( const std::exception &e ) { \
00319 ::CppUT::Message message( "Unexpected exception caught" ); \
00320 message.add( "Type: " + \
00321 ::CppTL::getObjectTypeName( e, "std::exception" ) ); \
00322 message.add( std::string("What: ") + e.what() ); \
00323 failMacro( message ); \
00324 } catch ( ... ) { \
00325 failMacro( "Unexpected exception caught" ); \
00326 }
00327
00328
00332 # define CPPUT_ASSERT_NO_THROW( expression ) \
00333 _CPPUT_ASSERT_NO_THROW_IMPL( CPPUT_FAIL, expression )
00334
00338 # define CPPUT_CHECK_NO_THROW( expression ) \
00339 _CPPUT_ASSERT_NO_THROW_IMPL( CPPUT_CHECKING_FAIL, expression )
00340
00341
00352 # define _CPPUT_ASSERT_ASSERTION_FAIL_MESSAGE_IMPL( beginMacro, assertion, message ) \
00353 { \
00354 bool assertionFailedCppUT_ = false; \
00355 { \
00356 ::CppUT::TestInfo::ScopedContextOverride contextSwitchCppUT_; \
00357 try { \
00358 assertion; \
00359 } catch ( const ::CppUT::AbortingAssertionException & ) { \
00360 } \
00361 assertionFailedCppUT_ = ::CppUT::TestInfo::threadInstance().testStatus().hasFailed(); \
00362 } \
00363 beginMacro() ::CppUT::checkAssertionFail( assertionFailedCppUT_, message ); \
00364 }
00365
00369 # define CPPUT_ASSERT_ASSERTION_FAIL_MESSAGE( assertion, message ) \
00370 _CPPUT_ASSERT_ASSERTION_FAIL_MESSAGE_IMPL( CPPUT_BEGIN_ASSERTION_MACRO, assertion, message )
00371
00375 # define CPPUT_CHECK_ASSERTION_FAIL_MESSAGE( assertion, message ) \
00376 _CPPUT_CHECK_ASSERTION_FAIL_MESSAGE_IMPL( CPPUT_BEGIN_CHECKING_MACRO, assertion, message )
00377
00381 # define CPPUT_ASSERT_ASSERTION_FAIL( assertion ) \
00382 CPPUT_ASSERT_ASSERTION_FAIL_MESSAGE( assertion, ::CppUT::Message() )
00383
00387 # define CPPUT_CHECK_ASSERTION_FAIL( assertion ) \
00388 CPPUT_CHECK_ASSERTION_FAIL_MESSAGE( assertion, ::CppUT::Message() )
00389
00392 # define _CPPUT_ASSERT_ASSERTION_PASS_MESSAGE_IMPL( beginMacro, assertion, message ) \
00393 { \
00394 bool assertionFailedCppUT_ = false; \
00395 { \
00396 ::CppUT::TestInfo::ScopedContextOverride contextSwitchCppUT_; \
00397 try { \
00398 assertion; \
00399 } catch ( const ::CppUT::AbortingAssertionException & ) { \
00400 } \
00401 assertionFailedCppUT_ = ::CppUT::TestInfo::threadInstance().testStatus().hasFailed(); \
00402 } \
00403 beginMacro() ::CppUT::checkAssertionPass( assertionFailedCppUT_, message ); \
00404 }
00405
00406
00410 # define CPPUT_ASSERT_ASSERTION_PASS_MESSAGE( assertion, message ) \
00411 _CPPUT_ASSERT_ASSERTION_PASS_MESSAGE_IMPL( CPPUT_BEGIN_ASSERTION_MACRO, assertion, message )
00412
00413
00417 # define CPPUT_CHECK_ASSERTION_PASS_MESSAGE( assertion, message ) \
00418 _CPPUT_CHECK_ASSERTION_PASS_MESSAGE_IMPL( CPPUT_BEGIN_CHECKING_MACRO, assertion, message )
00419
00420
00424 # define CPPUT_ASSERT_ASSERTION_PASS( assertion ) \
00425 CPPUT_ASSERT_ASSERTION_PASS_MESSAGE( assertion, ::CppUT::Message() )
00426
00430 # define CPPUT_CHECK_ASSERTION_PASS( assertion ) \
00431 CPPUT_CHECK_ASSERTION_PASS_MESSAGE( assertion, ::CppUT::Message() )
00432
00439 # define CPPUT_SKIP_TEST \
00440 ::CppUT::skipCurrentTest
00441
00460 # define CPPUT_IGNORE_FAILURE( assertion ) \
00461 { \
00462 bool failedCppUT_; \
00463 { \
00464 ::CppUT::TestInfo::IgnoreFailureScopedContextOverride contextSwitchCppUT_( failedCppUT_ );\
00465 try { \
00466 assertion; \
00467 } catch ( const ::CppUT::AbortingAssertionException & ) { \
00468 } \
00469 } \
00470 CPPUT_BEGIN_CHECKING_MACRO() \
00471 ::CppUT::checkAssertionFail( failedCppUT_, #assertion ); \
00472 }
00473
00474 #endif // CPPUT_ASSERT_H_INCLUDED
00475