Google Test and printing QVector3D



  • Hey everyone,

    I have had a lot of success using google test's unit testing framework with Qt4/5. I have it set up with cmake and I am using QtCreator. One issue I am having is writing custom print functions. The documentation for it is "here":https://code.google.com/p/googletest/wiki/V1_6_AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values.

    I have tried every which way I can think of, but no matter what gtest will not use my custom print functions for QVector3D. I define these at the top of my unit_tests.cpp file:

    @
    #include "gmock/gmock.h"
    #include "gtest/gtest.h"

    #include <QtGui/QVector3D>

    ::std::ostream& operator<<(::std::ostream& os, const QVector3D& vector)
    {
    return os << "(" << vector.x() <<", " << vector.y() << ", " << vector.z() << ")";
    }

    void PrintTo(const QVector3D& vector, ::std::ostream* os)
    {
    *os << "(" << vector.x() <<", " << vector.y() << ", " << vector.z() << ")";
    }
    @

    and then have a test case such as:

    @TEST(Test, WhyTheHellWontQVector3DPrint)
    {
    std::cout << "Testing 1 " << QVector3D(1,1,1) << std::endl;
    std::cout << "Testing 2 ";
    PrintTo(QVector3D(2, 2, 2), &std::cout);
    std::cout << std::endl;
    EXPECT_EQ(QVector3D(1,1,1), QVector3D(2,2,2));
    }
    @

    Which tosses this:
    @
    [ RUN ] Test.WhyTheHellWontQVector3DPrint
    Testing 1 (1, 1, 1)
    Testing 2 (2, 2, 2)
    unit_tests.cpp:144: Failure
    Value of: QVector3D(2,2,2)
    Actual: 12-byte object <00-00 00-40 00-00 00-40 00-00 00-40>
    Expected: QVector3D(1,1,1)
    Which is: 12-byte object <00-00 80-3F 00-00 80-3F 00-00 80-3F>
    @

    So it looks like my functions are correct since they successfully print the strings, but gtest isn't finding them. It just prints the byte-string of the object. I step through the code and it eventually gets to an internal fall-back printer

    template <typename T> void PrintTo(const T& value, ::std::ostream* os) as part of the namespace ::testing::internal.

    I have tried wrapping the functions in testing::internal and a ton of other mixes of namespaces. It just won't print it. Anyone have any ideas?

    Thanks for your time and help!


  • Lifetime Qt Champion

    Hi,
    Do you use a namespace Qt ? If that's the case you could try to surround the function definitions with:
    QT_BEGIN_NAMESPACE
    QT_END_NAMESPACE

    Hope it helps



  • Thanks for the suggestion. I didn't specify any namespace when compiling Qt 5.0.1 (I think that is where that would be done?), but I also gave that a shot since I saw those macros in qvector3d.h. No luck =/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.