mirror of
https://github.com/boostorg/test.git
synced 2026-02-02 09:12:10 +00:00
70 lines
3.4 KiB
HTML
70 lines
3.4 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<TITLE>The Test Tools</TITLE>
|
|
<LINK rel="stylesheet" type="text/css" href="../../../style/btl.css" media="screen">
|
|
<LINK rel="stylesheet" type="text/css" href="../../../style/btl-print.css" media="print">
|
|
<META http-equiv="Content-Language" content="en-us">
|
|
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<STYLE type="text/css">
|
|
H4
|
|
{
|
|
margin: 0px;
|
|
}
|
|
</STYLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<DIV class="body">
|
|
<H3>BOOST_WARN_EXCEPTION( statement, exception, predicate )<BR>
|
|
BOOST_CHECK_EXCEPTION( statement, exception, predicate )<BR>
|
|
BOOST_REQUIRE_EXCEPTION( statement, exception, predicate )</H3>
|
|
<P class="first-line-indented">These tools are used to perform an exception
|
|
detection and validation check. Tools execute the supplied statement
|
|
and check that it throw the supplied exception (or one inherited from it)
|
|
and that the exception object comply to the supplied predicate. If the statement
|
|
throw any other unrelated exception, doesn't throw at all or predicate evaluates
|
|
to <I>false</I> with
|
|
caught exception, check fails. In comparison with BOOST_<level>_THROW
|
|
tools these tools allow to perform more fine grained checks, for example
|
|
that an expected exception has specific error message etc.</P>
|
|
<P class="first-line-indented">If check is successful, tools produce a confirmation
|
|
message. In case if no exception thrown it produces an
|
|
error message in a form "error in <test case name>: exception
|
|
<<I>exception</I>> expected. In case if exception does not comply to
|
|
specified predicate the tool produces an error message in a form "error
|
|
in <test case name>: incorrect exception
|
|
<<I>exception</I>> is caught.</P>
|
|
<P class="first-line-indented">The first parameter is the statement to execute.
|
|
Use block statement if you want to execute more than one statement. The second
|
|
parameter is an expected exception. The last third parameter is a predicate
|
|
function used to validate caught exception.</P>
|
|
<H4>Example: test.cpp</H4>
|
|
<PRE class="code">#<SPAN class="reserv-word">define</SPAN> BOOST_TEST_MAIN<BR>#<SPAN class="reserv-word">include</SPAN> <<SPAN class="literal">boost/test/unit_test.hpp</SPAN>><BR>
|
|
<SPAN class="reserv-word">struct</SPAN> my_exception {
|
|
<SPAN class="reserv-word">explicit</SPAN> my_exception( <SPAN class="cpp-type">int</SPAN> ec = <SPAN class="literal">0</SPAN> ) : m_error_code( ec ) {}
|
|
|
|
<SPAN class="cpp-type">int</SPAN> m_error_code;
|
|
};
|
|
|
|
<SPAN class="cpp-type">bool</SPAN> is_critical( my_exception <SPAN class="reserv-word">const</SPAN>& ex ) { <SPAN class="reserv-word">return </SPAN>ex.m_error_code < <SPAN class="literal">0</SPAN>; }
|
|
<SPAN class="reserv-word">
|
|
</SPAN>BOOST_AUTO_TEST_CASE( test )
|
|
{
|
|
BOOST_CHECK_EXCEPTION( throw my_exception( 1 ), my_exception, is_critical );
|
|
}</PRE>
|
|
<H4>Output:</H4>
|
|
<P class="test-output">Running 1 test case...<BR>
|
|
test.cpp(14): error in "test": incorrect exception my_exception
|
|
is caught<BR>
|
|
<BR>
|
|
*** 1 failure detected in test suite "Master Test Suite"</P>
|
|
<DIV class="see_also">
|
|
<H4>See Also</H4>
|
|
<P class="see-also-content"><A href="BOOST_CHECK_THROW.html" target="descr">BOOST_CHECK_THROW</A></P>
|
|
</DIV>
|
|
</DIV>
|
|
</BODY>
|
|
</HTML>
|
|
<!-- Copyright Gennadiy Rozental 2001-2006.
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
(See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt) -->
|