summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2021-11-23 09:01:13 +0100
committerMarc Mutz <marc.mutz@qt.io>2021-11-26 18:28:50 +0100
commited343669f7c219b89449762cce086cb036a9a8f2 (patch)
treec9d888772ef5418900dd20117798f4505cd39317 /src
parent6a1a9365371f6e456bafe5120467c100cf7ca105 (diff)
Long live QVERIFY_THROWS_NO_EXCEPTION!
Counter-part to QVERIFY_THROWS_EXCEPTION. [ChangeLog][QTest] Added QVERIFY_THROWS_NO_EXCEPTION macro. Change-Id: Ib6a80c8e810d5e2298ff00d608dae04e7a0c3e8f Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/testlib/qtestcase.h21
-rw-r--r--src/testlib/qtestcase.qdoc34
2 files changed, 53 insertions, 2 deletions
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index be852bca94..0416413420 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -93,6 +93,21 @@ do {\
#ifndef QT_NO_EXCEPTIONS
+# define QVERIFY_THROWS_NO_EXCEPTION(...) \
+ do { \
+ QT_TRY { \
+ __VA_ARGS__; \
+ /* success */ \
+ } QT_CATCH (const std::exception &e) { \
+ QTest::qCaught(nullptr, e.what(), __FILE__, __LINE__); \
+ return; \
+ } QT_CATCH (...) { \
+ QTest::qCaught(nullptr, nullptr, __FILE__, __LINE__); \
+ QT_RETHROW; \
+ } \
+ } while (false) \
+ /* end */
+
#if QT_DEPRECATED_SINCE(6, 3)
namespace QTest {
QT_DEPRECATED_VERSION_X_6_3("Don't use QVERIFY_EXCEPTION_THROWN(expr, type) anymore, "
@@ -126,14 +141,16 @@ inline void useVerifyThrowsException() {}
#else // QT_NO_EXCEPTIONS
/*
- * The expression passed to the macro should throw an exception and we can't
- * catch it because Qt has been compiled without exception support. We can't
+ * These macros check whether the expression passed throws exceptions, but we can't
+ * catch them to check because Qt has been compiled without exception support. We can't
* skip the expression because it may have side effects and must be executed.
* So, users must use Qt with exception support enabled if they use exceptions
* in their code.
*/
# define QVERIFY_THROWS_EXCEPTION(...) \
static_assert(false, "Support of exceptions is disabled")
+# define QVERIFY_THROWS_NO_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 a9cd745438..9c36d779fb 100644
--- a/src/testlib/qtestcase.qdoc
+++ b/src/testlib/qtestcase.qdoc
@@ -198,6 +198,40 @@
by the test framework.
*/
+/*!
+ \macro QVERIFY_THROWS_NO_EXCEPTION(...)
+ \since 6.3
+
+ \relates QTest
+
+ The QVERIFY_THROWS_NO_EXCEPTION macro executes the expression given in its
+ variadic argument and tries to catch any exception thrown from the expression.
+
+ There are several different outcomes:
+
+ \list
+ \li If the expression does not throw an exception, then execution will continue.
+
+ \li Otherwise, if an exception derived from \c{std::exception} is caught, a failure
+ will be recorded in the test log and the macro returns early (implicit return from
+ enclosing function).
+
+ \li If an exception not derived from \c{std::exception} is caught, a failure will be
+ recorded in the test log and the exception will be 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_NO_EXCEPTION(std::pair<int, long>{42, 42L});
+ // macro arguments: \---- 1 ----/ \-- 2 -/ \3 /
+ \endcode
+
+ \note This macro can only be used in a test function that is invoked
+ by the test framework.
+*/
+
/*! \macro QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
\since 5.0