diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2021-12-02 14:09:44 +0100 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2021-12-04 03:50:06 +0100 |
commit | ab287508d56a6735fc877ff0efa8da517e849ac3 (patch) | |
tree | 39fafb8429fb0995db86ac5907cb916be9b5ae4d /src/qmltest | |
parent | e64c04b1409769118a9324f9c07a30a0bb81013a (diff) |
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 <mitch.curtis@qt.io>
Diffstat (limited to 'src/qmltest')
-rw-r--r-- | src/qmltest/TestCase.qml | 39 | ||||
-rw-r--r-- | src/qmltest/quicktestresult.cpp | 13 | ||||
-rw-r--r-- | src/qmltest/quicktestresult_p.h | 1 |
3 files changed, 52 insertions, 1 deletions
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 @@ -1179,6 +1179,45 @@ Item { } /*! + \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) Waits for \a ms milliseconds while processing Qt events. 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<QRegularExpression>(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<QRegularExpression>(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); |