QTest: simplify qPrintDataTags() string building

Use higher-level API (QByteArray::chopped()/operator+()) instead
of new[] and qsnprintf().

methodSignature() already heavily depends on QByteArray concatenation,
therefore trying to avoid using Qt code in QtTest is pointless, at
least here.

Drive-by indent lines so a follow-up port to std::fsprintf() will not
have to re-indent these lines again.

Amends b68bae1132b5dc5c8f55435c72cd48e24aa52ec9.

Pick-to: 6.7 6.5
Change-Id: I94ec440d7e6d09196cf87cd3dbbfd765e0df69d0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit a15ff49be73228bbbc72989736d32059322c414f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-07-22 11:06:27 +02:00
parent 13ed410121
commit 2a72fb6d81

View File

@ -555,11 +555,8 @@ static void qPrintDataTags(FILE *stream)
// Retrieve local tags:
QStringList localTags;
QTestTable table;
char *slot = qstrdup(tf.methodSignature().constData());
slot[strlen(slot) - 2] = '\0';
QByteArray member;
member.resize(qstrlen(slot) + qstrlen("_data()") + 1);
qsnprintf(member.data(), member.size(), "%s_data()", slot);
const QByteArray slot = tf.methodSignature().chopped(2);
const QByteArray member = slot + "_data()";
invokeTestMethodIfExists(member.constData());
const int dataCount = table.dataCount();
localTags.reserve(dataCount);
@ -570,13 +567,15 @@ static void qPrintDataTags(FILE *stream)
if (gTable->dataCount() == 0) {
if (localTags.size() == 0) {
// No tags at all, so just print the test function:
fprintf(stream, "%s %s\n", currTestMetaObj->className(), slot);
fprintf(stream, "%s %s\n", currTestMetaObj->className(), slot.data());
} else {
// Only local tags, so print each of them:
for (int k = 0; k < localTags.size(); ++k)
fprintf(
stream, "%s %s %s\n",
currTestMetaObj->className(), slot, localTags.at(k).toLatin1().data());
currTestMetaObj->className(),
slot.data(),
localTags.at(k).toLatin1().data());
}
} else {
for (int j = 0; j < gTable->dataCount(); ++j) {
@ -584,19 +583,21 @@ static void qPrintDataTags(FILE *stream)
// Only global tags, so print the current one:
fprintf(
stream, "%s %s __global__ %s\n",
currTestMetaObj->className(), slot, gTable->testData(j)->dataTag());
currTestMetaObj->className(),
slot.data(),
gTable->testData(j)->dataTag());
} else {
// Local and global tags, so print each of the local ones and
// the current global one:
for (int k = 0; k < localTags.size(); ++k)
fprintf(
stream, "%s %s %s __global__ %s\n", currTestMetaObj->className(), slot,
stream, "%s %s %s __global__ %s\n",
currTestMetaObj->className(),
slot.data(),
localTags.at(k).toLatin1().data(), gTable->testData(j)->dataTag());
}
}
}
delete[] slot;
}
}
}