diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2004-03-13 11:52:57 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2004-03-13 11:52:57 +0000 |
| commit | 224cf85f2b7fd7ec47cda4788902996349f8c754 (patch) | |
| tree | f1a835ff25dcd4a6efc46d83b775cdf7d5ba13b1 /include/cppunit | |
| parent | 943bbb17d1339a3cae3b930e3f7f1cb2f9fec297 (diff) | |
| download | cppunit-224cf85f2b7fd7ec47cda4788902996349f8c754.tar.gz | |
Examples/cppunittest/TestAssertTest.
examples/cppunittest/TestAssertTest.h:
* examples/cppunittest/TestAssertTest.cpp:
* examples/cppunittest/XmlUniformiserTest.h:
* examples/cppunittest/XmlUniformiserTest.cpp:
* include/cppunit/TestAssert.h: add the exception assertion macros
from cppunit 2: CPPUNIT_ASSERT_THROW, CPPUNIT_ASSERT_NO_THROW,
CPPUNIT_ASSERT_ASSERTION_FAIL, CPPUNIT_ASSERT_ASSERTION_PASS.
Updated unit test to use and test the new macros.
* include/cppunit/extensions/HelperMacros.h: deprecated the
test case factory that check for exception (CPPUNIT_TEST_FAIL &
CPPUNIT_TEST_EXCEPTION).
Diffstat (limited to 'include/cppunit')
| -rw-r--r-- | include/cppunit/TestAssert.h | 88 | ||||
| -rw-r--r-- | include/cppunit/extensions/HelperMacros.h | 2 |
2 files changed, 90 insertions, 0 deletions
diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h index 4583cfd..695ccb5 100644 --- a/include/cppunit/TestAssert.h +++ b/include/cppunit/TestAssert.h @@ -183,6 +183,94 @@ void CPPUNIT_API assertDoubleEquals( double expected, (delta), \ CPPUNIT_SOURCELINE() ) ) + +/** Asserts that the given expression throws an exception of the specified type. + * \ingroup Assertions + * Example of usage: + * \code + * std::vector<int> v; + * CPPUNIT_ASSERT_THROW( v.at( 50 ), std::out_of_range ); + * \endcode + */ +# define CPPUNIT_ASSERT_THROW( expression, ExceptionType ) \ + do { \ + bool cpputExceptionThrown_ = false; \ + try { \ + expression; \ + } catch ( const ExceptionType & ) { \ + cpputExceptionThrown_ = true; \ + } \ + \ + if ( cpputExceptionThrown_ ) \ + break; \ + \ + CPPUNIT_NS::Asserter::fail( \ + "Expected exception: " #ExceptionType \ + " not thrown.", \ + CPPUNIT_SOURCELINE() ); \ + } while ( false ) + + +// implementation detail +#if CPPUNIT_USE_TYPEINFO_NAME +#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \ + CPPUNIT_NS::TypeInfoHelper::getClassName( typeid(exception) ) +#else +#define CPPUNIT_EXTRACT_EXCEPTION_TYPE_( exception, no_rtti_message ) \ + std::string( no_rtti_message ) +#endif // CPPUNIT_USE_TYPEINFO_NAME + +/** Asserts that the given expression does not throw any exceptions. + * \ingroup Assertions + * Example of usage: + * \code + * std::vector<int> v; + * v.push_back( 10 ); + * CPPUNIT_ASSERT_NO_THROW( v.at( 0 ) ); + * \endcode + */ +# define CPPUNIT_ASSERT_NO_THROW( expression ) \ + try { \ + expression; \ + } catch ( const std::exception &e ) { \ + CPPUNIT_NS::Message message( "Unexpected exception caught" ); \ + message.addDetail( "Type: " + \ + CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e, \ + "std::exception or derived" ) ); \ + message.addDetail( std::string("What: ") + e.what() ); \ + CPPUNIT_NS::Asserter::fail( message, \ + CPPUNIT_SOURCELINE() ); \ + } catch ( ... ) { \ + CPPUNIT_NS::Asserter::fail( "Unexpected exception caught", \ + CPPUNIT_SOURCELINE() ); \ + } + +/** Asserts that an assertion fail. + * \ingroup Assertions + * Use to test assertions. + * Example of usage: + * \code + * CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( 1 == 2 ) ); + * \endcode + */ +# define CPPUNIT_ASSERT_ASSERTION_FAIL( assertion ) \ + CPPUNIT_ASSERT_THROW( assertion, CPPUNIT_NS::Exception ) + + +/** Asserts that an assertion pass. + * \ingroup Assertions + * Use to test assertions. + * Example of usage: + * \code + * CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( 1 == 1 ) ); + * \endcode + */ +# define CPPUNIT_ASSERT_ASSERTION_PASS( assertion ) \ + CPPUNIT_ASSERT_NO_THROW( assertion ) + + + + // Backwards compatibility #if CPPUNIT_ENABLE_NAKED_ASSERT diff --git a/include/cppunit/extensions/HelperMacros.h b/include/cppunit/extensions/HelperMacros.h index 3f13a3c..12431e4 100644 --- a/include/cppunit/extensions/HelperMacros.h +++ b/include/cppunit/extensions/HelperMacros.h @@ -322,6 +322,7 @@ * \param testMethod Name of the method of the test case to add to the suite. * \param ExceptionType Type of the exception that must be thrown by the test * method. + * \deprecated Use the assertion macro CPPUNIT_ASSERT_THROW instead. */ #define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType ) \ CPPUNIT_TEST_SUITE_ADD_TEST( \ @@ -345,6 +346,7 @@ * } * \endcode * \see CreatingNewAssertions. + * \deprecated Use the assertion macro CPPUNIT_ASSERT_ASSERTION_FAIL instead. */ #define CPPUNIT_TEST_FAIL( testMethod ) \ CPPUNIT_TEST_EXCEPTION( testMethod, CPPUNIT_NS::Exception ) |
