summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-01-05 15:43:21 +0100
committerMarc Mutz <marc.mutz@kdab.com>2017-01-10 07:47:26 +0000
commitb2173b54ef9ede85d88cb0fdf025b26e732ed44a (patch)
tree924daecbd0281944634838e2b19c6296c8fffd3d /src/testlib/qtestcase.cpp
parentd135acfa43810b2f7a2dba12688bbaa07a2e08cc (diff)
Long live QTest::addRow()!
This new function does the same as newRow(), except that it has a less confusing name (in line with _add_Column()), and accepts printf-style arguments to avoid the need to newRow(qPrintable(QString::asprintf())), a common pattern in client code. It uses qvsnprintf() under the hoods, avoiding the need for the QString const char* round-trip. Port all in-tree users of newRow(qPrintable(QString::asnprintf())) to the new function. Change-Id: Icd5de9b7ea4f6759d98080ec30f5aecadb8bec39 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp42
1 files changed, 42 insertions, 0 deletions
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.