Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Pretty print Qt classes in Googletest

Pretty print Qt classes in Googletest

Scheduled Pinned Locked Moved 3rd Party Software
8 Posts 2 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    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_NAMESPACE

    void 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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gurney
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gurney
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gurney
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gurney
                  wrote on last edited by
                  #8

                  Funny thing is, it works with a QDateTime object, like this:

                  @void PrintTo(const QDateTime& dumpObject, std::ostream* os)
                  {
                  *os << dumpObject.toString().toStdString();
                  }@

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved