summaryrefslogtreecommitdiffstats
path: root/src/testlib
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
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')
-rw-r--r--src/testlib/qtestcase.cpp33
-rw-r--r--src/testlib/qtestcase.h30
2 files changed, 45 insertions, 18 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index f42400c980..fd47db3f55 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)
/*!
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index 05ab56231d..fcd9ad8cfc 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -89,12 +89,9 @@ 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; \
+ QTest::qCaught(nullptr, __FILE__, __LINE__); \
+ return; \
} \
} while (false) \
/* end */
@@ -112,20 +109,15 @@ inline void useVerifyThrowsException() {}
# define QVERIFY_THROWS_EXCEPTION(exceptiontype, ...) \
do {\
QT_TRY {\
- QT_TRY {\
- __VA_ARGS__;\
- QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
- " but no exception caught", __FILE__, __LINE__);\
- return;\
- } QT_CATCH (const exceptiontype &) {\
- /* success */\
- }\
- } QT_CATCH (const std::exception &e) {\
- QTest::qCaught(#exceptiontype, e.what(), __FILE__, __LINE__);\
- return;\
+ __VA_ARGS__; \
+ QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
+ " but no exception caught", __FILE__, __LINE__); \
+ return; \
+ } QT_CATCH (const exceptiontype &) { \
+ /* success */ \
} QT_CATCH (...) {\
- QTest::qCaught(#exceptiontype, nullptr, __FILE__, __LINE__);\
- QT_RETHROW;\
+ QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \
+ return; \
}\
} while (false)
@@ -392,6 +384,8 @@ namespace QTest
const char *file, int line);
Q_DECL_COLD_FUNCTION
Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *what, const char *file, int line);
+ Q_DECL_COLD_FUNCTION
+ Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *file, int line);
#if QT_DEPRECATED_SINCE(6, 3)
QT_DEPRECATED_VERSION_X_6_3("Use qWarning() instead")
Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = nullptr, int line = 0);