From 07ada0b971a84483f3676b5fef46ff2bd6d26dbb Mon Sep 17 00:00:00 2001 From: Laszlo Papp Date: Fri, 1 Jul 2022 06:15:17 +0100 Subject: [PATCH] QTest: Support QKeySequence compare Add a QTest::toString() override for QKeySequence. This is just calling QKeySequence::toString(). The default format is PortableText, which should be ascii. Deliberately avoided NativeText as it can return unicode, e.g. on Mac. This is necessary to get helpful information for test failures when using QCOMPARE for QKeySequence instances. Currently, the returned output would not only be not helpful, but even misleading: Actual (edit.keySequence()): Expected (QKeySequence()) : After adding the override, the output is neither misleading nor unhelpful: Actual (edit.keySequence()): "" Expected (keySequence) : "Return" Some special characters would be escaped in the output, like the literal double quote: Actual (edit.keySequence()): "Ctrl+N" Expected (QKeySequence("\"")): "\"" Change-Id: Ib4b28fca30f6f2ad86c62530767f94a151332e0a Reviewed-by: Volker Hilsheimer --- src/testlib/qtest_gui.h | 6 ++++++ src/testlib/qtestcase.qdoc | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/testlib/qtest_gui.h b/src/testlib/qtest_gui.h index 26cf17c6ef..d68324d91e 100644 --- a/src/testlib/qtest_gui.h +++ b/src/testlib/qtest_gui.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -103,6 +104,11 @@ template<> inline char *toString(const QVector4D &v) } #endif // !QT_NO_VECTOR4D +template<> inline char *toString(const QKeySequence &keySequence) +{ + return toString(keySequence.toString()); +} + inline bool qCompare(QIcon const &t1, QIcon const &t2, const char *actual, const char *expected, const char *file, int line) { diff --git a/src/testlib/qtestcase.qdoc b/src/testlib/qtestcase.qdoc index 2c46453968..654687c880 100644 --- a/src/testlib/qtestcase.qdoc +++ b/src/testlib/qtestcase.qdoc @@ -1596,6 +1596,13 @@ Returns a textual representation of size policy \a sp. */ +/*! + \fn char *QTest::toString(const QKeySequence &ks) + \overload + \since 6.5 + Returns a textual representation of the key sequence \a ks. +*/ + /*! \fn template char *QTest::toString(const Tuple &tuple, QtPrivate::IndexesList ) \internal