diff options
-rw-r--r-- | src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp | 11 | ||||
-rw-r--r-- | src/testlib/qtestcase.cpp | 42 | ||||
-rw-r--r-- | src/testlib/qtestcase.h | 1 | ||||
-rw-r--r-- | tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp | 8 | ||||
-rw-r--r-- | tests/auto/corelib/thread/qmutex/tst_qmutex.cpp | 2 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp | 8 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qchar/tst_qchar.cpp | 2 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp | 6 | ||||
-rw-r--r-- | tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp | 8 |
9 files changed, 71 insertions, 17 deletions
diff --git a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp index 2c57550c3d..01ee8102f4 100644 --- a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp +++ b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp @@ -208,6 +208,17 @@ void myTestFunction_data() //! [20] +//! [addRow] +void myTestFunction_data() +{ + QTest::addColumn<int>("input"); + QTest::addColumn<QString>("output"); + QTest::addRow("%d", 0) << 0 << QString("0"); + QTest::addRow("%d", 1) << 1 << QString("1"); +} +//! [addRow] + + //! [21] void myTestFunction_data() { QTest::addColumn<int>("intval"); diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 1f07ff3f91..98faf55266 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2195,6 +2195,48 @@ QTestData &QTest::newRow(const char *dataTag) return *tbl->newData(dataTag); } +/*! + \since 5.9 + + Appends a new row to the current test data. The function's arguments are passed + to qsnprintf() for formatting according to \a format. See the qvsnprintf() + documentation for caveats and limitations. + + The formatted string will appear as the name of this test data in the test output. + + Returns a QTestData reference that can be used to stream in data. + + Example: + \snippet code/src_qtestlib_qtestcase.cpp addRow + + \b {Note:} This function can only be used in a test's data function + that is invoked by the test framework. + + See \l {Chapter 2: Data Driven Testing}{Data Driven Testing} for + a more extensive example. + + \sa addColumn(), QFETCH() +*/ +QTestData &QTest::addRow(const char *format, ...) +{ + QTEST_ASSERT_X(format, "QTest::addRow()", "Format string cannot be null"); + QTestTable *tbl = QTestTable::currentTestTable(); + QTEST_ASSERT_X(tbl, "QTest::addRow()", "Cannot add testdata outside of a _data slot."); + QTEST_ASSERT_X(tbl->elementCount(), "QTest::addRow()", "Must add columns before attempting to add rows."); + + char buf[1024]; + + va_list va; + va_start(va, format); + // we don't care about failures, we accept truncation, as well as trailing garbage. + // Names with more than 1K characters are nonsense, anyway. + (void)qvsnprintf(buf, sizeof buf, format, va); + buf[sizeof buf - 1] = '\0'; + va_end(va); + + return *tbl->newData(buf); +} + /*! \fn void QTest::addColumn(const char *name, T *dummy = 0) Adds a column with type \c{T} to the current test data. diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 4c226830e9..058c0b3295 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -316,6 +316,7 @@ namespace QTest addColumnInternal(qMetaTypeId<T>(), name); } Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag); + Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2); template <typename T> inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected, diff --git a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp index 7ffb5d0fd7..66fc578d5f 100644 --- a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp +++ b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp @@ -112,13 +112,13 @@ void tst_QGetPutEnv::intValue_data() ROW(0xffffffff, 0, false); const int bases[] = {10, 8, 16}; for (size_t i = 0; i < sizeof bases / sizeof *bases; ++i) { - QTest::newRow(qPrintable(QString::asprintf("INT_MAX, base %d", bases[i]))) + QTest::addRow("INT_MAX, base %d", bases[i]) << QByteArray::number(INT_MAX) << INT_MAX << true; - QTest::newRow(qPrintable(QString::asprintf("INT_MAX+1, base %d", bases[i]))) + QTest::addRow("INT_MAX+1, base %d", bases[i]) << QByteArray::number(qlonglong(INT_MAX) + 1) << 0 << false; - QTest::newRow(qPrintable(QString::asprintf("INT_MIN, base %d", bases[i]))) + QTest::addRow("INT_MIN, base %d", bases[i]) << QByteArray::number(INT_MIN) << INT_MIN << true; - QTest::newRow(qPrintable(QString::asprintf("INT_MIN-1, base %d", bases[i]))) + QTest::addRow("INT_MIN-1, base %d", bases[i]) << QByteArray::number(qlonglong(INT_MIN) - 1) << 0 << false; }; } diff --git a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp index bf778e9fd1..e6c3a0cfd7 100644 --- a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp +++ b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp @@ -143,7 +143,7 @@ void tst_QMutex::convertToMilliseconds_data() auto add = [](TimeUnit unit, double d, long long i, qint64 expected) { const QScopedArrayPointer<char> enumName(QTest::toString(unit)); - QTest::newRow(qPrintable(QString::asprintf("%s:%f:%lld", enumName.data(), d, i))) + QTest::addRow("%s:%f:%lld", enumName.data(), d, i) << unit << d << qint64(i) << expected; }; diff --git a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp index c14e9fadf7..e13c2894af 100644 --- a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -1076,22 +1076,22 @@ void tst_QAlgorithms::popCount_data_impl(size_t sizeof_T_Int) const uint bits = bitsSetInByte(byte); const quint64 value = static_cast<quint64>(byte); const quint64 input = value << ((i % sizeof_T_Int) * 8U); - newRow(qPrintable(QString::asprintf("0x%016llx", input))) << input << bits; + QTest::addRow("0x%016llx", input) << input << bits; } // and some random ones: if (sizeof_T_Int >= 8) for (size_t i = 0; i < 1000; ++i) { const quint64 input = quint64(qrand()) << 32 | quint32(qrand()); - newRow(qPrintable(QString::asprintf("0x%016llx", input))) << input << bitsSetInInt64(input); + QTest::addRow("0x%016llx", input) << input << bitsSetInInt64(input); } else if (sizeof_T_Int >= 2) for (size_t i = 0; i < 1000 ; ++i) { const quint32 input = qrand(); if (sizeof_T_Int >= 4) - newRow(qPrintable(QString::asprintf("0x%08x", input))) << quint64(input) << bitsSetInInt(input); + QTest::addRow("0x%08x", input) << quint64(input) << bitsSetInInt(input); else - newRow(qPrintable(QString::asprintf("0x%04x", quint16(input & 0xFFFF)))) << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); + QTest::addRow("0x%04x", quint16(input & 0xFFFF)) << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); } } diff --git a/tests/auto/corelib/tools/qchar/tst_qchar.cpp b/tests/auto/corelib/tools/qchar/tst_qchar.cpp index fb436b67d6..13898ace7b 100644 --- a/tests/auto/corelib/tools/qchar/tst_qchar.cpp +++ b/tests/auto/corelib/tools/qchar/tst_qchar.cpp @@ -108,7 +108,7 @@ void tst_QChar::operators_data() for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) - QTest::newRow(qPrintable(QString::asprintf("'\\%d' (op) '\\%d'", i, j))) + QTest::addRow("'\\%d' (op) '\\%d'", i, j) << QChar(ushort(i)) << QChar(ushort(j)); } } diff --git a/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp b/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp index 06e2e1cc45..a68671d899 100644 --- a/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp +++ b/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp @@ -176,9 +176,9 @@ void tst_QLatin1String::relationalOperators_data() for (Data *lhs = data; lhs != data + sizeof data / sizeof *data; ++lhs) { for (Data *rhs = data; rhs != data + sizeof data / sizeof *data; ++rhs) { QLatin1StringContainer l = { lhs->l1 }, r = { rhs->l1 }; - QTest::newRow(qPrintable(QString::asprintf("\"%s\" <> \"%s\"", - lhs->l1.data() ? lhs->l1.data() : "nullptr", - rhs->l1.data() ? rhs->l1.data() : "nullptr"))) + QTest::addRow("\"%s\" <> \"%s\"", + lhs->l1.data() ? lhs->l1.data() : "nullptr", + rhs->l1.data() ? rhs->l1.data() : "nullptr") << l << lhs->order << r << rhs->order; } } diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index 9e5fc55379..cd5aa03689 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -151,10 +151,10 @@ static void makeRow(QSizePolicy sp, QSizePolicy::Policy hp, QSizePolicy::Policy int hst, int vst, QSizePolicy::ControlType ct, bool hfw, bool wfh, Qt::Orientations orients) { - QTest::newRow(qPrintable(QString::asprintf("%s-%s-%d-%d-%s-%s-%s", - PrettyPrint(hp).s(), PrettyPrint(vp).s(), hst, vst, - PrettyPrint(ct).s(), - hfw ? "true" : "false", wfh ? "true" : "false"))) + QTest::addRow("%s-%s-%d-%d-%s-%s-%s", + PrettyPrint(hp).s(), PrettyPrint(vp).s(), hst, vst, + PrettyPrint(ct).s(), + hfw ? "true" : "false", wfh ? "true" : "false") << sp << hp << vp << hst << vst << ct << hfw << wfh << orients; } |