tst_tostring: support computed expected values

We need an owning container to keep the data alive when the _data
function computes the expected value instead of passing it as a
literal string, so replace the QByteArrayView we used so far with a
const QByteArray.

If a test failed, print the tail ends of the failing strings. This
is useful when toString() implementations printing the operands of
the failed toString()-under-test result comparison decide to
truncate the output. It works around the fact that we both test
toString() and use toString() to report any failure.

As a drive-by, add a missing blank line between function definitions.

Amends a2551c45d496c23045eb8451e080e75b2f8b42c1.

Change-Id: I0b217aeb1a6d475df8e8b9fb821046f1cceb1a3e
Reviewed-by: Jason McDonald <macadder1@gmail.com>
(cherry picked from commit 3a57885c3766d4ae203650d60881988c374e4148)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-11-29 14:24:52 +01:00 committed by Qt Cherry-pick Bot
parent 754683dfe9
commit 5d2fab5663

View File

@ -21,25 +21,34 @@ private slots:
void tst_toString::addColumns()
{
QTest::addColumn<ToStringFunction>("fn");
QTest::addColumn<QByteArrayView>("expected");
QTest::addColumn<QByteArray>("expected");
QTest::addColumn<QByteArrayView>("expr");
QTest::addColumn<QByteArrayView>("file");
QTest::addColumn<int>("line");
}
void tst_toString::testRows()
{
QFETCH(ToStringFunction, fn);
QFETCH(QByteArrayView, expected);
QFETCH(const QByteArray, expected);
QFETCH(QByteArrayView, expr);
QFETCH(QByteArrayView, file);
QFETCH(int, line);
std::unique_ptr<char []> ptr{fn()};
const auto len = qstrlen(ptr.get());
QTest::qCompare(ptr.get(), expected, expr.data(), expected.data(), file.data(), line);
if (QTest::currentTestFailed()) {
qDebug("tail diff:\n"
" actual:%s\n"
" expected:%s",
ptr.get() + len - std::min(size_t{40}, len),
expected.data() + expected.size() - std::min(qsizetype{40}, expected.size()));
}
}
template <typename T> void addRow(QByteArrayView name, T &&value, QByteArrayView expression,
QByteArrayView expected, QByteArrayView file, int line)
const QByteArray &expected, QByteArrayView file, int line)
{
ToStringFunction fn = [v = std::move(value)]() { return QTest::toString(v); };
QTest::newRow(name.data()) << fn << expected << expression << file << line;