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. Change-Id: I1e1627c6341b09197a8a79669fde061c47e6ba47 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> (cherry picked from commit f0dcbb66e8c54d497d5d118a13a7a0b4fc1b3d94) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
d51033b374
commit
65a1becad2
@ -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)
|
||||
/*!
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user