Pretty print Qt classes in Googletest
-
wrote on 28 Mar 2014, 12:56 last edited by
I am attempting to dump Qt classes in the Googletest framework.
The basics of how to pretty print custom classes in Googletest is "here":http://code.google.com/p/googletest/wiki/AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values
I've tried an example with QSize, but have been unable to pretty print it so far.
My code attempt:
@
QT_BEGIN_NAMESPACEvoid PrintTo (const QSize& size, ::std::ostream* stream)
{
const QString dump = QString("[Size w:") % QString::number(size.width()) %
QString(" h:") % QString::number(size.height()) %
QChar(']');
*stream << dump.toStdString();
}QT_END_NAMESPACE
@However, my output is this:
@....\tpeb\test\tpeb_test_graphiccomponents.cpp(475): error: Value of: preferredSize.toSize()
Actual: 8-byte object <80-00 00-00 80-00 00-00>
Expected: QPixmap(pixmapName).size()
Which is: 8-byte object <18-00 00-00 18-00 00-00>@Any ideas?
-
wrote on 3 Nov 2014, 06:54 last edited by
Hi everyone,
I have a similar problem. I try to print a QString, for which I implemented a PrintTo method
@void PrintTo(const QString& str, ::std::ostream* os)
{
*os << "QString(" << str.toStdString() << ")";
}@But I also still get the default output of google test
[----------] 1 test from One
[ RUN ] One.Two
../UnitTests/tests/printtotest.cpp:21: Failure
Value of: second
Actual: { 2-byte object <57-00>, 2-byte object <6F-00>, 2-byte object <72-00>, 2-byte object <6C-00>, 2-byte object <64-00> }
Expected: first
Which is: { 2-byte object <68-00>, 2-byte object <65-00>, 2-byte object <6C-00>, 2-byte object <6C-00>, 2-byte object <6F-00> }
[ FAILED ] One.Two (0 ms)Does anyone have an idea why we cannot get this to work although in the "googletest documentation":https://code.google.com/p/googletest/wiki/AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values this is described as the way to go?
-
wrote on 3 Nov 2014, 07:50 last edited by
Since that posting, I made it work, although I don't remember exactly how I did that, unfortunately.
One hint I found in my code is that the PrintTo must not be declared/defined within a namespace. It's possible that this was my original problem.
-
wrote on 3 Nov 2014, 21:26 last edited by
Hi Asperamanca,
thank you for your answer. In my code, the PrintTo function is not defined inside a namespace. Could you maybe post a part of your code that is already working? That would be a great help for me. Thanks!
-
wrote on 4 Nov 2014, 08:12 last edited by
I would have posted them right away had I seen something interesting. But here you go:
Example for pretty printing of QString
Header:
@void PrintTo(const QString& dumpObject, ::std::ostream* os);@
Cpp:
@void PrintTo(const QString& dumpObject, std::ostream* os)
{
*os << dumpObject.toStdString();
}@Nothing else really. No defines, no namespaces before that code. Just including the header file in the cpp file.
-
wrote on 7 Nov 2014, 06:36 last edited by
Hm, that is the same way I did it, but it doesn't seem to work for me. Maybe it's a compiler specific issue? I use clang on MacOS and Qt 5.3.0
-
wrote on 7 Nov 2014, 08:11 last edited by
Windows and MSVC 2010. In my case, I do have the PrintTo in a separate library. Although I feel having them in the same project should actually make it easier, not harder.