diff options
Diffstat (limited to 'src/testlib')
-rw-r--r-- | src/testlib/qtestcase.h | 14 | ||||
-rw-r--r-- | src/testlib/qtestcase.qdoc | 24 |
2 files changed, 32 insertions, 6 deletions
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index fbb756fe23..c5a90c8e1b 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -93,11 +93,21 @@ do {\ #ifndef QT_NO_EXCEPTIONS +#if QT_DEPRECATED_SINCE(6, 3) +namespace QTest { +QT_DEPRECATED_VERSION_X_6_3("Don't use QVERIFY_EXCEPTION_THROWN(expr, type) anymore, " + "use QVERIFY_THROWS_EXCEPTION(type, expr...) instead") +inline void useVerifyThrowsException() {} +} // namespace QTest # define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \ + QVERIFY_THROWS_EXCEPTION(exceptiontype, QTest::useVerifyThrowsException(); expression) +#endif + +# define QVERIFY_THROWS_EXCEPTION(exceptiontype, ...) \ do {\ QT_TRY {\ QT_TRY {\ - expression;\ + __VA_ARGS__;\ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ " but no exception caught", __FILE__, __LINE__);\ return;\ @@ -125,7 +135,7 @@ do {\ * So, users must use Qt with exception support enabled if they use exceptions * in their code. */ -# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \ +# define QVERIFY_THROWS_EXCEPTION(...) \ static_assert(false, "Support of exceptions is disabled") #endif // !QT_NO_EXCEPTIONS diff --git a/src/testlib/qtestcase.qdoc b/src/testlib/qtestcase.qdoc index 2990be7d77..a9cd745438 100644 --- a/src/testlib/qtestcase.qdoc +++ b/src/testlib/qtestcase.qdoc @@ -158,17 +158,23 @@ \since 5.3 \relates QTest + \deprecated [6.3] Use \c{QVERIFY_THROWS_EXCEPTION(exceptiontype, expression)} instead. +*/ + +/*! + \macro QVERIFY_THROWS_EXCEPTION(exceptiontype, ...) + \since 6.3 - The QVERIFY_EXCEPTION_THROWN macro executes \a expression - and tries to catch an exception thrown from \a expression. + The QVERIFY_THROWS_EXCEPTION macro executes the expression given in the variadic + argument and expects to catch an exception thrown from the expression. There are several possible outcomes: \list - \li If \a expression throws an exception that is either the same as + \li If the expression throws an exception that is either the same as \a exceptiontype or derived from \a exceptiontype, then execution will continue. - \li Otherwise, if \a expression throws no exception, or the + \li Otherwise, if the expression throws no exception, or the exception thrown derives from \c{std::exception}, then a failure will be recorded in the test log and the macro returns early (from enclosing function). @@ -178,6 +184,16 @@ re-thrown. This avoids problems with e.g. pthread cancellation exceptions. \endlist + The macro uses variadic arguments so the expression can contain commas that the + preprocessor considers argument separators, e.g. as in + \code + QVERIFY_THROWS_EXCEPTION(std::bad_alloc, + // macro arguments: ^ exceptiontype + std::vector<std::pair<int, long>>{42'000'000'000, {42, 42L}}); + // macro arguments: \---------- 1 ----------/ \-------- 2 --------/ \3/ \ 4 / + // \----------------------- expression -----------------------/ + \endcode + \note This macro can only be used in a test function that is invoked by the test framework. */ |