summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2024-01-10 10:49:44 +0100
committerMarc Mutz <marc.mutz@qt.io>2024-01-23 10:20:53 +0000
commitf0dcbb66e8c54d497d5d118a13a7a0b4fc1b3d94 (patch)
tree019a685f98a5ef55c7089417025c22ede58ed517 /src/testlib/qtestcase.cpp
parent79e1389fb9ea4e43e61ab35e41ed275cb76eedbc (diff)
QTest: Extract Method qCaught() from THROWS_(NO_)EXCEPTION
We've already done a lot in 59600a514ba99ed62b46237d8f160dea84474190, but we can do more: Thanks to std::exception_ptr, we can drag the handling of unexpected exceptions completely to out-of-line code, leaving only the catch(...) as inline. As a nice side-effect, we no longer need the nested try blocks in QVERIFY_THROWS_EXCEPTION to work around GCC -Wexceptions or MSVC C2312 complaining about two handlers for the same exception type (which can happen when exceptiontype is std::exception itself). This may not handle __cxxabi::__forced_unwind correctly, but it doesn't need to: Tests should not need to call THROWS_EXECPTION on code that could emit a pthread cancellation; tests are controlled environments. Keep the old qCaught() function for BC. We don't promise BC in QtTestLib, but it doesn't cost much. Pick-to: 6.7 Change-Id: I1e1627c6341b09197a8a79669fde061c47e6ba47 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index f42400c9800..fd47db3f553 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -2614,6 +2614,39 @@ void QTest::qCaught(const char *expected, const char *what, const char *file, in
qFail(message().toUtf8().constData(), file, line);
}
+/*!
+ \internal
+
+ Contains the implementation of the catch(...) block of
+ QVERIFY_THROWS_EXCEPTION.
+
+ The function inspects std::current_exception() by rethrowing it using
+ std::rethrow_exception().
+
+ The function must be called from a catch handler.
+
+ If the exception inherits std::exception, its what() message is logged and
+ this function returns normally. The caller of this function must then
+ execute a \c{return} to exit from the test function.
+
+ Otherwise, a message saying an unknown exception was caught is logged and
+ this function rethrows the exception, skipping the \c{return} that follows
+ this function call in the caller.
+*/
+void QTest::qCaught(const char *expected, const char *file, int line)
+{
+ try {
+ // let's see what the cat brought us:
+ std::rethrow_exception(std::current_exception());
+ } catch (const std::exception &e) {
+ qCaught(expected, e.what(), file, line);
+ } catch (...) {
+ qCaught(expected, nullptr, file, line);
+ throw;
+ }
+ // caller shall invoke `return` if control reached here
+}
+
#if QT_DEPRECATED_SINCE(6, 3)
/*!