CppUnit project page CppUnit home page

Creating new assertions


Classes

class  CppUT::Assertion
 Represents a failed assertion. More...
class  CppUT::TestStatus
 Represents the status of the current test. More...
class  CppUT::TestInfo
 Heart of the test system: hold current test status and the state of the current assertion. More...

Defines

#define CPPUT_BEGIN_ASSERTION_MACRO()   CppUT::Impl::AssertionTrigger::aborting += (const CppUT::Impl::FileLocationMarker *)__FILE__ + __LINE__ /
#define CPPUT_BEGIN_CHECKING_MACRO()   CppUT::Impl::AssertionTrigger::checking += (const CppUT::Impl::FileLocationMarker *)__FILE__ + __LINE__ /

Enumerations

enum  CppUT::AssertionType { CppUT::abortingAssertion = 1, CppUT::checkingAssertion }
 Indiciates if on failure the assertion it will abort the current test. More...

Define Documentation

 
#define CPPUT_BEGIN_ASSERTION_MACRO (  )     CppUT::Impl::AssertionTrigger::aborting += (const CppUT::Impl::FileLocationMarker *)__FILE__ + __LINE__ /

Starts an aborting assertion macro (throw an exception on assertion failure).

This macro provides the following properties for assertion:

  • condition is evaluated first to ease debugging of unit tests
  • assertion can take a variable number of parameters (typicaly an optional message).

Typically, as little code as possible is put in assertion macro code to ease debugging. Usually, an assertion macro just start a function call:

 #define CPPUT_ASSERT_IS_EVEN \
    CPPUT_BEGIN_ASSERTION_MACRO() checkIsEven
 #define CPPUT_ASSERT_IS_EVEN \
    CPPUT_BEGIN_CHECKING_MACRO() checkIsEven

 CheckerResult checkIsEven( int x, const char *message = "" )
 {
    CheckerResult result;
    if ( x % 2 != 0 )
    {
       result.setFailed();
       result.message_.add( message );
    }
    return result;
 }
 void someTest() 
 {
    CPPUT_CHECK_IS_EVEN( 1 );
    CPPUT_CHECK_IS_EVEN( 2 );
 }

In the example above, both a checking and asserting macro have been implemented. The code to check the assertion is factorized in a single function than can take a variable number of parameters and be easily debugged.

Implementation notes:

This is achieved using a trick based on operator evaluation order described below: Basicaly an assertion has the following form: AssertionTrigger::aborting += __FILE__ + __LINE__ / checkCondition( condition expression ); For simplicity, we will use the following compact form for reference : x += f + l / checkCond( condition ); checkCond must returns an CheckerResult which as overloaded operator /. operator = evaluate right to left operator + evaluate left to right operator / evaluate left to right and as priority over +. So 'condition expression' is evaluted first, then checkCond(condition) is evaluated. Then 'l / checkCond(condition)' is evaluated, followed by f + l/ checkCond(condition) which yield to an CheckerFileLocation. Finally, x += f + l / checkCond( condition ) is evaluated, basically calling AssertionTrigger overloaded operator += with the final CheckerFileLocation that contains details about the assertion, as well as file/line location.

See also:
CPPUT_BEGIN_CHECKING_MACRO.

 
#define CPPUT_BEGIN_CHECKING_MACRO (  )     CppUT::Impl::AssertionTrigger::checking += (const CppUT::Impl::FileLocationMarker *)__FILE__ + __LINE__ /

Starts a checking assertion macro (continue test execution even on failure).

See also:
CPPUT_BEGIN_ASSERTION_MACRO() for example and implementation notes.


Enumeration Type Documentation

Indiciates if on failure the assertion it will abort the current test.

Enumerator:
abortingAssertion  The assertion will abort the test on failure.
checkingAssertion  The test will continue on assertion failure.


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