diff options
-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 | ||||
-rw-r--r-- | tests/auto/qmltest/selftests/tst_selftests.qml | 31 |
4 files changed, 83 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); diff --git a/tests/auto/qmltest/selftests/tst_selftests.qml b/tests/auto/qmltest/selftests/tst_selftests.qml index 5555876014..5da58bddf9 100644 --- a/tests/auto/qmltest/selftests/tst_selftests.qml +++ b/tests/auto/qmltest/selftests/tst_selftests.qml @@ -76,6 +76,10 @@ TestCase { function stringify(str) { return str; } + + function failOnWarning(msg) { + failmsg = msg; // use failmsg property for simplicity + } } TestCase { @@ -309,4 +313,31 @@ TestCase { function test_blacklistWithData(row) { verify(row.success) } + + function test_failOnWarning() { + compare(functions.failmsg, "invalid") // Checks that init() was run + + failOnWarning("Warning that will never appear") // shouldn't fail the test + + // without going to C++ or QTestLog introspection, we can kind of only + // make sure that TestCase does correct job by duck-typing TestResult + testCase.failOnWarning(undefined) + compare(functions.failmsg, "") + + testCase.failOnWarning("foobar") + compare(functions.failmsg, "foobar") + + // one case that is actually testable is whether ignoreWarning() + // suppresses the failure of failOnWarning() + var pattern = new RegExp("This warning has to happen") + var string = "And this one too!" + + failOnWarning(pattern) + ignoreWarning(pattern) + console.warn("This warning has to happen!!") + + failOnWarning(string) + ignoreWarning(string) + console.warn(string) + } } |