tst_QRestAccessManager: leak less on failure

Move some deleteLater() calls to before the first QCOMPARE/QVERIFY
macros, so they get executed even if the macros return on failure.

This isn't an all-encompassing solution, but I lack the time to port
all this to smart pointers, and this is some improvement.

As a drive-by, port QVERIFY(opt.has_value()) to QCOMPARE(opt, nullopt).

Task-number: QTBUG-120307
Change-Id: Ia5a2cabd273a779938b22badc91b109fcc545203
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit d4aaf61b3333b48b4e79ad60b63bb50dbea72617)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-04-17 18:02:15 +02:00 committed by Qt Cherry-pick Bot
parent eed30ff352
commit 2581730d88

View File

@ -636,12 +636,13 @@ void tst_QRestAccessManager::json()
QTRY_VERIFY(networkReply);
QRestReply restReply(networkReply);
parseError.error = QJsonParseError::ParseError::DocumentTooLarge;
QVERIFY(!restReply.readJson(&parseError).has_value()); // std::nullopt returned
const auto json = restReply.readJson(&parseError);
networkReply->deleteLater();
networkReply = nullptr;
QCOMPARE_EQ(json, std::nullopt);
QCOMPARE_NE(parseError.error, QJsonParseError::ParseError::NoError);
QCOMPARE_NE(parseError.error, QJsonParseError::ParseError::DocumentTooLarge);
QCOMPARE_GT(parseError.offset, 0);
networkReply->deleteLater();
networkReply = nullptr;
}
{
@ -652,6 +653,8 @@ void tst_QRestAccessManager::json()
QRestReply restReply(networkReply);
parseError.error = QJsonParseError::ParseError::DocumentTooLarge;
json = restReply.readJson(&parseError);
networkReply->deleteLater();
networkReply = nullptr;
QCOMPARE(parseError.error, QJsonParseError::ParseError::NoError);
QVERIFY(json);
responseJsonDocument = *json;
@ -659,8 +662,6 @@ void tst_QRestAccessManager::json()
QCOMPARE(responseJsonDocument.array().size(), 2);
QCOMPARE(responseJsonDocument[0].toString(), "foo"_L1);
QCOMPARE(responseJsonDocument[1].toString(), "bar"_L1);
networkReply->deleteLater();
networkReply = nullptr;
}
}
@ -670,9 +671,9 @@ void tst_QRestAccessManager::json()
QTRY_VERIFY(networkReply); \
QRestReply restReply(networkReply); \
responseString = restReply.readText(); \
QCOMPARE(responseString, sourceString); \
networkReply->deleteLater(); \
networkReply = nullptr; \
QCOMPARE(responseString, sourceString); \
}
#define VERIFY_TEXT_REPLY_ERROR(WARNING_MESSAGE) \
@ -682,9 +683,9 @@ void tst_QRestAccessManager::json()
QTest::ignoreMessage(QtWarningMsg, WARNING_MESSAGE); \
QRestReply restReply(networkReply); \
responseString = restReply.readText(); \
QVERIFY(responseString.isEmpty()); \
networkReply->deleteLater(); \
networkReply = nullptr; \
QVERIFY(responseString.isEmpty()); \
}
void tst_QRestAccessManager::text()