[Interest] Printing QObject-derived classes on QCOMPARE failure

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Sat Jun 24 17:54:54 CEST 2017


Il 24/06/2017 15:54, Mitch Curtis ha scritto:
> 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)]

Not without modifying QtTestLib. QCOMPARE in that case calls the 
qCompare specialization for pointers:

>     template <typename T>
>     inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
>                         const char *file, int line)
>     {
>         return compare_ptr_helper(t1, t2, actual, expected, file, line);
>     }

compare_ptr_helper loses information on T1/T2 and just uses comparison 
on void*:

>     inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual,
>                                    const char *expected, const char *file, int line)
>     {
>         return compare_helper(t1 == t2, "Compared pointers are not the same",
>                               toString(t1), toString(t2), actual, expected, file, line);
>     }


Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (UK) Ltd., a KDAB Group company | Tel: UK +44-1625-809908
KDAB - Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4007 bytes
Desc: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170624/1de1d383/attachment.bin>

More information about the Interest mailing list