...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
BOOST_WARN_EXCEPTION(expression, exception_type, predicate); BOOST_CHECK_EXCEPTION(expression, exception_type, predicate); BOOST_REQUIRE_EXCEPTION(expression, exception_type, predicate);
As for BOOST_<level>_THROW
, these assertions validate
that expression
raises
an exception of the type specified by exception_type
or any of its child type, with additional checks on the exception instance.
expression
,
the instance of the exception is passed to predicate
for further validation.
BOOST_<level>_THROW
if expression
does not raise any exception, or an unrelated exception is raised.
predicate
should be a unary
function accepting the an instance of exception_type
or any of its child, and that should return a boolean indicating the success
(true
) or failure (false
).
Warning | |
---|---|
the assertion catches only the expected exceptions. |
Tip | |
---|---|
It is possible to test for complex expressions with the use of constructs
such as |
The example below checks that the exception carries the proper error code.
Code |
---|
#define BOOST_TEST_MODULE example #include <boost/test/included/unit_test.hpp> struct my_exception { explicit my_exception( int ec = 0 ) : m_error_code( ec ) {} int m_error_code; }; bool is_critical( my_exception const& ex ) { return ex.m_error_code < 0; } void some_func( int i ) { if( i>0 ) throw my_exception( i ); } BOOST_AUTO_TEST_CASE( test ) { BOOST_CHECK_EXCEPTION( some_func(1), my_exception, is_critical ); } |
Output |
---|
> example Running 1 test case... test.cpp(18): error in "test": incorrect exception my_exception is caught *** 1 failures is detected in test suite "example" |
See also: