From 5d2fab5663876a38783c4ee8408954bc69fde6ed Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 29 Nov 2023 14:24:52 +0100 Subject: [PATCH] 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 (cherry picked from commit 3a57885c3766d4ae203650d60881988c374e4148) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/testlib/tostring/tst_tostring.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/auto/testlib/tostring/tst_tostring.cpp b/tests/auto/testlib/tostring/tst_tostring.cpp index 3f12e0c7984..65e50fa511d 100644 --- a/tests/auto/testlib/tostring/tst_tostring.cpp +++ b/tests/auto/testlib/tostring/tst_tostring.cpp @@ -21,25 +21,34 @@ private slots: void tst_toString::addColumns() { QTest::addColumn("fn"); - QTest::addColumn("expected"); + QTest::addColumn("expected"); QTest::addColumn("expr"); QTest::addColumn("file"); QTest::addColumn("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 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 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;