From a2566e139f8b2eed2b8cf7d90bc0a2ae6b289be8 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 20 Mar 2024 10:09:56 +0100 Subject: Extend QTest::failOnWarning() to a no-parameter fail-on-any-warning Many users (albeit mostly in qtdeclarative) of failOnWarning() are passing a catch-all regexp, which is only supported when regular expression support is enabled. Make their lives easier and those checks independent of the feature by adding a third overload, taking no parameter, that fails on any (unanticipated) warning. Implementation is trivial - just put a null QVariant in failOnWarningList; it won't match either check on the entry that might exempt a test from failing on a warning, so any warning at all will trigger failure. [ChangeLog][QtTest] QTest::failOnWarning() now has a no-parameter overload to support the common case of fail-on-any-warning, without needing to construct a match-everything regular expression. Change-Id: Ic693f1c8619fd6e495543b85737d566134cf9d20 Reviewed-by: Mitch Curtis --- src/testlib/qtestcase.cpp | 25 ++++++++++++++++++++++++- src/testlib/qtestcase.h | 1 + src/testlib/qtestlog.cpp | 5 +++++ src/testlib/qtestlog_p.h | 1 + 4 files changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 2ab55cac36..5648fedd63 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2201,6 +2201,19 @@ void QTest::ignoreMessage(QtMsgType type, const QRegularExpression &messagePatte } #endif // QT_CONFIG(regularexpression) +/*! + \since 6.8 + \overload failOnWarning() + + Appends a test failure to the test log if any warning is output. + + \sa failOnWarning() +*/ +void QTest::failOnWarning() +{ + return QTestLog::failOnWarning(); +} + /*! \since 6.3 \overload failOnWarning() @@ -2247,7 +2260,17 @@ void QTest::failOnWarning(const char *message) \code void FileTest::init() { - QTest::failOnWarning(QRegularExpression(".?")); + QTest::failOnWarning( + QRegularExpression("QFile::.*: File(.*) already open")); + } + \endcode + + For the common case of failing on \e any warning pass no parameter: + + \code + void FileTest::init() + { + QTest::failOnWarning(); } \endcode diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 0ae1a787e2..a855ace6a9 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -435,6 +435,7 @@ namespace QTest #if QT_CONFIG(regularexpression) Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern); #endif + Q_TESTLIB_EXPORT void failOnWarning(); Q_TESTLIB_EXPORT void failOnWarning(const char *message); #if QT_CONFIG(regularexpression) Q_TESTLIB_EXPORT void failOnWarning(const QRegularExpression &messagePattern); diff --git a/src/testlib/qtestlog.cpp b/src/testlib/qtestlog.cpp index 4b6df54e91..929ccb370b 100644 --- a/src/testlib/qtestlog.cpp +++ b/src/testlib/qtestlog.cpp @@ -629,6 +629,11 @@ void QTestLog::ignoreMessage(QtMsgType type, const QRegularExpression &expressio } #endif // QT_CONFIG(regularexpression) +void QTestLog::failOnWarning() +{ + QTest::failOnWarningList.push_back({}); +} + void QTestLog::failOnWarning(const char *msg) { QTest::failOnWarningList.push_back(QString::fromUtf8(msg)); diff --git a/src/testlib/qtestlog_p.h b/src/testlib/qtestlog_p.h index f9bbfa158d..ab5fb593d4 100644 --- a/src/testlib/qtestlog_p.h +++ b/src/testlib/qtestlog_p.h @@ -71,6 +71,7 @@ public: #ifndef QT_NO_REGULAREXPRESSION static void ignoreMessage(QtMsgType type, const QRegularExpression &expression); #endif + static void failOnWarning(); static void failOnWarning(const char *msg); #ifndef QT_NO_REGULAREXPRESSION static void failOnWarning(const QRegularExpression &expression); -- cgit v1.2.3