summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2021-11-23 08:58:58 +0100
committerMarc Mutz <marc.mutz@qt.io>2021-11-26 09:57:59 +0100
commit59600a514ba99ed62b46237d8f160dea84474190 (patch)
tree99daa7bda9085b6a540eb55891658a5fdd7315e1 /src/testlib/qtestcase.cpp
parent405adf3348b6d5ffb87f02550743050e4f0da4a6 (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/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp34
1 files changed, 34 insertions, 0 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