QTest: hold WatchDog in optional<> instead of QScopedPointer

Means we don't need to allocate it on the heap and optional<> fits the
semantics of an optional object better.

Change-Id: Id02c4847c2357c3033dce94b68787ed37d6ca276
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 9a08f2fbc8476111b1d7a42f0b9b9eb4e71deeaa)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-01-26 10:50:43 +01:00 committed by Qt Cherry-pick Bot
parent 1b559b25a6
commit 12fdb3bc1f

View File

@ -64,6 +64,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <numeric> #include <numeric>
#include <optional>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
@ -493,7 +494,7 @@ public:
static QMetaMethod findMethod(const QObject *obj, const char *signature); static QMetaMethod findMethod(const QObject *obj, const char *signature);
private: private:
bool invokeTest(int index, QLatin1StringView tag, WatchDog *watchDog) const; bool invokeTest(int index, QLatin1StringView tag, std::optional<WatchDog> &watchDog) const;
void invokeTestOnData(int index) const; void invokeTestOnData(int index) const;
QMetaMethod m_initTestCaseMethod; // might not exist, check isValid(). QMetaMethod m_initTestCaseMethod; // might not exist, check isValid().
@ -1387,7 +1388,7 @@ static void printUnknownDataTagError(QLatin1StringView name, QLatin1StringView t
If the function was successfully called, true is returned, otherwise If the function was successfully called, true is returned, otherwise
false. false.
*/ */
bool TestMethods::invokeTest(int index, QLatin1StringView tag, WatchDog *watchDog) const bool TestMethods::invokeTest(int index, QLatin1StringView tag, std::optional<WatchDog> &watchDog) const
{ {
QBenchmarkTestMethodData benchmarkData; QBenchmarkTestMethodData benchmarkData;
QBenchmarkTestMethodData::current = &benchmarkData; QBenchmarkTestMethodData::current = &benchmarkData;
@ -1776,13 +1777,13 @@ void TestMethods::invokeTests(QObject *testObject) const
QTestResult::setCurrentTestFunction("initTestCase"); QTestResult::setCurrentTestFunction("initTestCase");
invokeTestMethodIfValid(m_initTestCaseDataMethod, testObject); invokeTestMethodIfValid(m_initTestCaseDataMethod, testObject);
QScopedPointer<WatchDog> watchDog; std::optional<WatchDog> watchDog = std::nullopt;
if (!alreadyDebugging() if (!alreadyDebugging()
#if QT_CONFIG(valgrind) #if QT_CONFIG(valgrind)
&& QBenchmarkGlobalData::current->mode() != QBenchmarkGlobalData::CallgrindChildProcess && QBenchmarkGlobalData::current->mode() != QBenchmarkGlobalData::CallgrindChildProcess
#endif #endif
) { ) {
watchDog.reset(new WatchDog); watchDog.emplace();
} }
QSignalDumper::startDump(); QSignalDumper::startDump();
@ -1801,7 +1802,7 @@ void TestMethods::invokeTests(QObject *testObject) const
const char *data = nullptr; const char *data = nullptr;
if (i < QTest::testTags.size() && !QTest::testTags.at(i).isEmpty()) if (i < QTest::testTags.size() && !QTest::testTags.at(i).isEmpty())
data = qstrdup(QTest::testTags.at(i).toLatin1().constData()); data = qstrdup(QTest::testTags.at(i).toLatin1().constData());
const bool ok = invokeTest(i, QLatin1StringView(data), watchDog.data()); const bool ok = invokeTest(i, QLatin1StringView(data), watchDog);
delete [] data; delete [] data;
if (!ok) if (!ok)
break; break;