From ab287508d56a6735fc877ff0efa8da517e849ac3 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Thu, 2 Dec 2021 14:09:44 +0100 Subject: QML test lib: propagate failOnWarning() feature to QML With efb283fb7f72e950c8ecf755b960a3c1b36b5507 in qtbase, we can now fail a test if a particular warning is encountered. Let's also have this functionality in QML as it seems useful there as well [ChangeLog][QtQuickTest][TestCase] Added failOnWarning() for allowing to fail a test if a particular warning is output during that test execution Change-Id: I011ed119c318859a2bccd98f0d491904b10305e5 Reviewed-by: Mitch Curtis --- src/qmltest/TestCase.qml | 39 +++++++++++++++++++++++++++++++++++++++ src/qmltest/quicktestresult.cpp | 13 ++++++++++++- src/qmltest/quicktestresult_p.h | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) (limited to 'src/qmltest') diff --git a/src/qmltest/TestCase.qml b/src/qmltest/TestCase.qml index e0e3be1021..795833a467 100644 --- a/src/qmltest/TestCase.qml +++ b/src/qmltest/TestCase.qml @@ -1178,6 +1178,45 @@ Item { qtest_results.ignoreWarning(msg) } + /*! + \qmlmethod TestCase::failOnWarning(message) + \since 6.3 + + Fails the test if a warning \a message appears during the test run. + Similar to \c{QTest::failOnWarning(message)} in C++. + + \a message can be either a string, or a regular expression providing a + pattern of messages. In the latter case, the first encountered message + would fail the test. + + For example, the following snippet will fail a test if a warning is + produced: + \qml + failOnWarning("Something bad happened") + \endqml + + And the following snippet will fail a test if any warning matching a + pattern is encountered: + \qml + failOnWarning(new RegExp("[0-9]+ bad things happened")) + \endqml + + \note Despite being a JavaScript RegExp object, it will not be + interpreted as such; instead, the pattern will be passed to \l + QRegularExpression. + + \note ignoreMessage() takes precedence over this function, so any + warnings that match a pattern given to both \c ignoreMessage() and \c + failOnWarning() will be ignored. + + \sa warn() + */ + function failOnWarning(msg) { + if (msg === undefined) + msg = "" + qtest_results.failOnWarning(msg) + } + /*! \qmlmethod TestCase::wait(ms) diff --git a/src/qmltest/quicktestresult.cpp b/src/qmltest/quicktestresult.cpp index 0853c26f51..e4ef4e68ae 100644 --- a/src/qmltest/quicktestresult.cpp +++ b/src/qmltest/quicktestresult.cpp @@ -645,13 +645,24 @@ void QuickTestResult::ignoreWarning(const QJSValue &message) { if (message.isRegExp()) { #if QT_CONFIG(regularexpression) - QTestLog::ignoreMessage(QtWarningMsg, message.toVariant().toRegularExpression()); + QTestLog::ignoreMessage(QtWarningMsg, qjsvalue_cast(message)); #endif } else { QTestLog::ignoreMessage(QtWarningMsg, message.toString().toUtf8()); } } +void QuickTestResult::failOnWarning(const QJSValue &message) +{ + if (message.isRegExp()) { +#if QT_CONFIG(regularexpression) + QTestLog::failOnWarning(qjsvalue_cast(message)); +#endif + } else { + QTestLog::failOnWarning(message.toString().toUtf8()); + } +} + void QuickTestResult::wait(int ms) { QTest::qWait(ms); diff --git a/src/qmltest/quicktestresult_p.h b/src/qmltest/quicktestresult_p.h index 54799e40aa..58b53d7b6b 100644 --- a/src/qmltest/quicktestresult_p.h +++ b/src/qmltest/quicktestresult_p.h @@ -143,6 +143,7 @@ public Q_SLOTS: void warn(const QString &message, const QUrl &location, int line); void ignoreWarning(const QJSValue &message); + void failOnWarning(const QJSValue &message); void wait(int ms); void sleep(int ms); -- cgit v1.2.3