BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bsls_fuzztestpreconditionexception

Detailed Description

Provide an exception type for handling failed preconditions.

Outline

Purpose

Provide an exception type for handling failed preconditions.

Classes

See also
bsls_fuzztest

Description

This component implements an exception class, bsls::FuzzTestPreconditionException, that provides a mechanism to convey context information from a failing precondition to a test handler. The context that is captured consists of the program source of the failing expression, the name of the file containing the assertion/review, the line number within that file where the asserted/reviewed expression may be found, and the level of the assertion/review that has failed.

Usage

First we write a macro to act as a precondition testing assert facility that will throw an exception of type bsls::FuzzTestPreconditionException if the asserted expression fails. The thrown exception will capture the source-code of the expression, the filename and line number of the failing expression.

#define TEST_PRECONDITION(EXPRESSION) \$
if (!(EXPRESSION)) { \$
throw bsls::FuzzTestPreconditionException(#EXPRESSION, __FILE__, \$
__LINE__, "LEVEL", \$
false); \$
}
Definition bsls_fuzztestpreconditionexception.h:115

Next we use the macro inside a try-block, so that we can catch the exception thrown if the tested expression fails.

try {
void *p = NULL;
TEST_PRECONDITION(0 != p);
}

If the assertion fails, catch the exception and confirm that it correctly recorded the context of where the assertion failed.

catch (const bsls::FuzzTestPreconditionException& exception) {
assert(0 == strcmp("0 != p", exception.expression()));
assert(0 == strcmp(__FILE__, exception.filename()));
assert(9 == __LINE__ - exception.lineNumber());
assert(0 == strcmp("LEVEL", exception.level()));
assert(false == exception.isReview());
}
const char * filename() const
Definition bsls_fuzztestpreconditionexception.h:223
const char * expression() const
Definition bsls_fuzztestpreconditionexception.h:217
const char * level() const
Definition bsls_fuzztestpreconditionexception.h:229
bool isReview() const
Return a flag indicating if the failure is a review.
Definition bsls_fuzztestpreconditionexception.h:235
int lineNumber() const
Definition bsls_fuzztestpreconditionexception.h:241