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 <mitch.curtis@qt.io>
This commit is contained in:
Edward Welbourne 2024-03-20 10:09:56 +01:00
parent 5a09ade176
commit a2566e139f
5 changed files with 32 additions and 2 deletions

View File

@ -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

View File

@ -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);

View File

@ -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));

View File

@ -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);

View File

@ -2981,7 +2981,7 @@ void tst_QItemSelectionModel::destroyModel()
selectionModel->setCurrentIndex(itemModel->index(1, 0), QItemSelectionModel::Select);
QVERIFY(selectionModel->currentIndex().isValid());
QTest::failOnWarning(QRegularExpression(".*"));
QTest::failOnWarning();
itemModel.reset();
QVERIFY(!selectionModel->currentIndex().isValid());
QVERIFY(selectionModel->selection().isEmpty());