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()): <null>
   Expected (QKeySequence())    : <null>

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 <volker.hilsheimer@qt.io>
This commit is contained in:
Laszlo Papp 2022-07-01 06:15:17 +01:00
parent 14ce67629b
commit 07ada0b971
2 changed files with 13 additions and 0 deletions

View File

@ -22,6 +22,7 @@
#include <QtGui/qcolor.h>
#include <QtGui/qpixmap.h>
#include <QtGui/qimage.h>
#include <QtGui/qkeysequence.h>
#include <QtGui/qregion.h>
#include <QtGui/qvector2d.h>
#include <QtGui/qvector3d.h>
@ -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)
{

View File

@ -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 <typename Tuple, int... I> char *QTest::toString(const Tuple &tuple, QtPrivate::IndexesList<I...> )
\internal