summaryrefslogtreecommitdiffstats
path: root/src/testlib
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2022-10-04 15:17:00 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2023-01-31 17:35:12 +0100
commit2d1fd400fc34843690e889d0cae7e75c12abd1e0 (patch)
treef2ee9ce5aef41ee9fc6c8c5c3c3ebd0022ff1be5 /src/testlib
parent7c84550f3bfdf93cb8a071e6bb23d54d57109e84 (diff)
Object to creating duplicate entries in a test-data table
Tero Heikkinen caught tst_QQuaternion() using a duplicated test data tag and was surprised that testlib let it get away with that. That seems like a reasonable thing to discourage. While I'm at it, duplicate columns should be discouraged. [ChangeLog][QtTest] Duplicate data tags are now warned about. Every call to QTest::newRow() or QTest::addRow() should result in a distinct data tag. If they do not, a warning is produced. Likewise, duplicate column names are forbidden; each call to QTest::addColumn() should use a distinct name. Fixes: QTBUG-107185 Pick-to: 6.5 Change-Id: Idfdb7cdfdd71e1fe3db5cadb243eeecc83032922 Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'src/testlib')
-rw-r--r--src/testlib/doc/src/qttestlib-manual.qdoc4
-rw-r--r--src/testlib/qtesttable.cpp15
2 files changed, 19 insertions, 0 deletions
diff --git a/src/testlib/doc/src/qttestlib-manual.qdoc b/src/testlib/doc/src/qttestlib-manual.qdoc
index 0098fdcc80..6112d3e424 100644
--- a/src/testlib/doc/src/qttestlib-manual.qdoc
+++ b/src/testlib/doc/src/qttestlib-manual.qdoc
@@ -731,6 +731,10 @@
the type of the column whose value it supplies. If any assertion fails,
the test is aborted.
+ The names of rows and columns, in a given test function's data table, should
+ be unique: if two rows share a name, or two columns share a name, a warning
+ will (since Qt 6.5) be produced.
+
\section1 Rewriting the Test Function
Our test function can now be rewritten:
diff --git a/src/testlib/qtesttable.cpp b/src/testlib/qtesttable.cpp
index 934d1797c7..6950b2494e 100644
--- a/src/testlib/qtesttable.cpp
+++ b/src/testlib/qtesttable.cpp
@@ -37,6 +37,7 @@ public:
void addColumn(int elemType, const char *elemName) { elementList.push_back(Element(elemName, elemType)); }
void addRow(QTestData *data) { dataList.push_back(data); }
+ bool hasRow(const char *name) const;
static QTestTable *currentTestTable;
static QTestTable *gTable;
@@ -49,6 +50,8 @@ void QTestTable::addColumn(int type, const char *name)
{
QTEST_ASSERT(type);
QTEST_ASSERT(name);
+ if (indexOf(name) != -1)
+ qWarning() << "Duplicate data column" << name << "- please rename.";
d->addColumn(type, name);
}
@@ -70,6 +73,9 @@ bool QTestTable::isEmpty() const
QTestData *QTestTable::newData(const char *tag)
{
+ if (d->hasRow(tag))
+ qWarning() << "Duplicate data tag" << tag << "- please rename.";
+
QTestData *dt = new QTestData(tag, this);
d->addRow(dt);
return dt;
@@ -110,10 +116,19 @@ public:
bool operator()(const QTestTablePrivate::Element &e) const
{ return !strcmp(e.name, m_needle); }
+ bool operator()(const QTestData *e) const
+ { return !strcmp(e->dataTag(), m_needle); }
+
private:
const char *m_needle;
};
+bool QTestTablePrivate::hasRow(const char *rowName) const
+{
+ QTEST_ASSERT(rowName);
+ return std::find_if(dataList.begin(), dataList.end(), NamePredicate(rowName)) != dataList.end();
+}
+
int QTestTable::indexOf(const char *elementName) const
{
QTEST_ASSERT(elementName);