Menu

#59 Please consider a CPPUNIT_ASSERT_LAMBDA macro

open
nobody
C++11 (1)
5
2017-05-31
2017-05-31
No

Hello,

It would be handy to have a CPPUNIT_ASSERT_LAMBDA(condition, lambda) macro where the lambda is only called if the condition is false. This makes extracting error messages from objects easier and the test code simpler.

#if (defined(_MSC_VER) && _MSC_VER >= 1800) || (defined(__cplusplus) && __cplusplus > 199711L)
/** Assertion with a user specified lamda.
* \ingroup Assertions
* \param condition If this condition evaluates to \c false then the
*                  test failed.
* \param lambda Message detail is generated when needed and reported in
*  diagnostic if \a condition evaluates to \c false.
*/
#define CPPUNIT_ASSERT_LAMBDA(condition, lambda)                                        \
  ( CPPUNIT_NS::Asserter::failIf( !(condition),                                         \
                                  CPPUNIT_NS::Message( "assertion failed",              \
                                                       "Expression: "                   \
                                                       #condition,                      \
                                                       !(condition) ? lambda() : "" ),  \
                                  CPPUNIT_SOURCELINE() ) )
#endif

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.