diff options
author | Marc Mutz <marc.mutz@qt.io> | 2021-11-23 08:58:58 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2021-11-26 09:57:59 +0100 |
commit | 59600a514ba99ed62b46237d8f160dea84474190 (patch) | |
tree | 99daa7bda9085b6a540eb55891658a5fdd7315e1 /src | |
parent | 405adf3348b6d5ffb87f02550743050e4f0da4a6 (diff) |
QTest: de-inline QVERIFY_THROWS_EXCEPTION message formatting
Extract Method QTest::qCaught() to take the string handling out of the
header. This should help a bit in speeding up compilation of large
unit test files (provided they use QVERIFY_THROWS_EXCEPTION), although
I have no data to support that.
Since we changed the error message, update the selftest accordingly.
Change-Id: Id4a3c8c34d5df8d0c7a861106d269097f4a6de5c
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/testlib/qtestcase.cpp | 34 | ||||
-rw-r--r-- | src/testlib/qtestcase.h | 9 |
2 files changed, 38 insertions, 5 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 4d0b30a844..db7c4d4a3d 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2073,6 +2073,40 @@ bool QTest::qExpectFail(const char *dataIndex, const char *comment, return QTestResult::expectFail(dataIndex, qstrdup(comment), mode, file, line); } +/*! + \internal + + Executes qFail() following a failed QVERIFY_THROWS_EXCEPTION or + QVERIFY_THROWS_NO_EXCEPTION, passing a suitable message created from \a expected, + \a what, along with \a file and \a line. + + The \a expected parameter contains the type of the exception that is expected to + be thrown, or \nullptr, if no exception was expected. + + The \a what parameter contains the result of \c{std::exception::what()}, or nullptr, + if a non-\c{std::exception}-derived exception was caught. + + The \a file and \a line parameters hold expansions of the \c{__FILE__} and \c{__LINE__} + macros, respectively. +*/ +void QTest::qCaught(const char *expected, const char *what, const char *file, int line) +{ + auto message = [&] { + const auto exType = what ? "std::" : "unknown "; + const auto ofType = expected ? " of type " : ""; + const auto no = expected ? "an" : "no"; + const auto withMsg = what ? " with message " : ""; + const auto protect = [](const char *s) { return s ? s : ""; }; + + return QString::asprintf("Expected %s exception%s%s to be thrown, " + "but caught %sexception%s%s", + no, ofType, protect(expected), + exType, withMsg, protect(what)); + }; + qFail(message().toUtf8().constData(), file, line); +} + + #if QT_DEPRECATED_SINCE(6, 3) /*! \internal diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index eea99e26e4..be852bca94 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -115,13 +115,10 @@ inline void useVerifyThrowsException() {} /* success */\ }\ } QT_CATCH (const std::exception &e) {\ - QByteArray msg = QByteArray() + "Expected exception of type " #exceptiontype \ - " to be thrown but std::exception caught with message: " + e.what(); \ - QTest::qFail(msg.constData(), __FILE__, __LINE__);\ + QTest::qCaught(#exceptiontype, e.what(), __FILE__, __LINE__);\ return;\ } QT_CATCH (...) {\ - QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \ - " but unknown exception caught", __FILE__, __LINE__);\ + QTest::qCaught(#exceptiontype, nullptr, __FILE__, __LINE__);\ QT_RETHROW;\ }\ } while (false) @@ -323,6 +320,8 @@ namespace QTest Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line); Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, 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); #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); |