summaryrefslogtreecommitdiffstats
path: root/src/testlib
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-05-19 10:25:45 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-05-26 11:02:48 +0000
commitae021882330abc5f6fbaadca290e6e5670c89028 (patch)
tree99821d2caae31319e00a89a5ecf858e38ebf4f6e /src/testlib
parent25a7034d78aeb12726a1052d64c0aa3314a1f69d (diff)
QTestlib: Add formatting for QObject * in QCOMPARE
Output object name and class in QCOMPARE(). This should help to debug flaky QWidget tests that for example check on focusWidget(). [ChangeLog][QtTestLib] QCOMPARE() now reports QObject * values by class and objectName(). Task-number: QTBUG-64446 Change-Id: Ife04e89bba04fc78d077c8f0f07af17a17c9cf8c Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/testlib')
-rw-r--r--src/testlib/qtestcase.cpp18
-rw-r--r--src/testlib/qtestcase.h22
2 files changed, 40 insertions, 0 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index daaa1bc390..596f521ffa 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -2822,6 +2822,24 @@ char *QTest::toString(const void *p)
return msg;
}
+/*! \internal
+ */
+char *QTest::toString(const volatile QObject *vo)
+{
+ if (vo == nullptr)
+ return qstrdup("<null>");
+
+ auto *o = const_cast<const QObject*>(vo);
+ const QString &name = o->objectName();
+ const char *className = o->metaObject()->className();
+ char *msg = new char[256];
+ if (name.isEmpty())
+ qsnprintf(msg, 256, "%s/%p", className, o);
+ else
+ qsnprintf(msg, 256, "%s/\"%s\"", className, qPrintable(name));
+ return msg;
+}
+
/*! \fn char *QTest::toString(const QColor &color)
\internal
*/
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index 3fe6d2d867..b046232c65 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -297,6 +297,7 @@ namespace QTest
Q_TESTLIB_EXPORT char *toString(const char *);
Q_TESTLIB_EXPORT char *toString(const volatile void *);
Q_TESTLIB_EXPORT char *toString(const void *); // ### FIXME: Qt 7: Remove
+ Q_TESTLIB_EXPORT char *toString(const volatile QObject *);
Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc = 0, char **argv = nullptr);
Q_TESTLIB_EXPORT int qRun();
@@ -421,6 +422,27 @@ namespace QTest
toString(t1), toString(t2), actual, expected, file, line);
}
+ inline bool compare_ptr_helper(const volatile QObject *t1, const volatile QObject *t2, const char *actual,
+ const char *expected, const char *file, int line)
+ {
+ return compare_helper(t1 == t2, "Compared QObject pointers are not the same",
+ toString(t1), toString(t2), actual, expected, file, line);
+ }
+
+ inline bool compare_ptr_helper(const volatile QObject *t1, std::nullptr_t, const char *actual,
+ const char *expected, const char *file, int line)
+ {
+ return compare_helper(t1 == nullptr, "Compared QObject pointers are not the same",
+ toString(t1), toString(nullptr), actual, expected, file, line);
+ }
+
+ inline bool compare_ptr_helper(std::nullptr_t, const volatile QObject *t2, const char *actual,
+ const char *expected, const char *file, int line)
+ {
+ return compare_helper(nullptr == t2, "Compared QObject pointers are not the same",
+ toString(nullptr), toString(t2), actual, expected, file, line);
+ }
+
inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
const char *expected, const char *file, int line)
{