[Interest] Printing QObject-derived classes on QCOMPARE failure
Mitch Curtis
mitch.curtis at qt.io
Sat Jun 24 15:54:28 CEST 2017
Is it possible to have QCOMPARE call the operator<< overload for my custom QObject-derived class upon failure?
The following test only prints:
FAIL! : Untitled1Test::testCase1() Compared pointers are not the same
Loc: [../untitled1/tst_untitled1test.cpp(44)]
Code:
#include <QDebug>
#include <QString>
#include <QtTest>
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject() {}
private:
// ...
};
QDebug operator<<(QDebug debug, const MyObject *myObject)
{
debug.nospace() << "(MyObject objectName=" << myObject->objectName()
<< ", other MyObject-specific stuff...)";
return debug.space();
}
class Untitled1Test : public QObject
{
Q_OBJECT
public:
Untitled1Test();
private Q_SLOTS:
void testCase1();
};
Untitled1Test::Untitled1Test()
{
qRegisterMetaType<MyObject*>();
}
void Untitled1Test::testCase1()
{
MyObject object1;
MyObject object2;
QCOMPARE(&object1, &object2);
}
QTEST_APPLESS_MAIN(Untitled1Test)
#include "tst_untitled1test.moc"
Cheers.
More information about the Interest
mailing list