summaryrefslogtreecommitdiffstats
path: root/src
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
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')
-rw-r--r--src/testlib/doc/snippets/code/src_qtestlib_qtestcase.cpp11
-rw-r--r--src/testlib/qtestcase.cpp42
-rw-r--r--src/testlib/qtestcase.h1
3 files changed, 54 insertions, 0 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,