Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GMock does not use PrintTo() to print argument expectations of type QString #1149

Closed
Knitschi opened this issue Jul 13, 2017 · 9 comments
Closed

Comments

@Knitschi
Copy link

It looks like some other print function for arrays is used instead:

  Expected arg #0: is equal to { 2-byte object <42-00>, 2-byte object <6C-00>, 2-byte object <75-00>, 2-byte object <62-00> }
           Actual: { 2-byte object <42-00>, 2-byte object <6C-00>, 2-byte object <69-00>, 2-byte object <62-00> }
         Expected: to be called once
           Actual: never called - unsatisfied and active

here is a longer discussion of the problem:
https://groups.google.com/forum/#!topic/googletestframework/FMyzItAJ5KE

@Knitschi
Copy link
Author

Knitschi commented Jul 13, 2017

overloading std::string testing::PrintToString(const QString& ) seems to do the trick.

Edit:
This did not fix the problem, I moved the PrintTo() to another file while playing around, which caused things to work all of a sudden.

@Knitschi Knitschi changed the title GMock does not use PrintTo() to print out false Argument values of type QString GMock does not use PrintTo() to print argument expectations of type QString Jul 13, 2017
@sbenzaquen
Copy link
Collaborator

sbenzaquen commented Jul 13, 2017 via email

@Knitschi
Copy link
Author

Thank you for your answer.

I played more around with it and found out that I had another test that did not have my PrintTo( const QString& str, ...) declaration available. Compiling this test seemed to cause the usage of the standard PrintTo(). When I commented this test out, my definition was used. The usage of my definition also depended on the compilation order and filenames of the files that contained the tests.

So is there no way to get a warning if this happens? Usually the linker warns when multiple definitions for a function exist. Why not here?
Changing the headers of an external library does not seem to be a clean solution.

@sbenzaquen
Copy link
Collaborator

sbenzaquen commented Jul 13, 2017 via email

@Knitschi
Copy link
Author

Thank you again for the elaborate answer.

At least after all this trouble I will not forget the Problem and be more cautious in the future. I have not yet experienced such kind of "cross talk" between two compilation units. I guess I never accidently violated the ODR rule before.

I am using the hunter-package version of googletest which mainly adds an install target and a cmake config file. I guess I have to figure out now how clients of the package can add a custom gtest-printers.h. But that is another story.

You can close this issue if you want.

@bobkocisko
Copy link

@sbenzaquen Thank you, this worked perfectly for printing boost::optional<T> (after countless failed attempts to define this elsewhere).

For anyone else who might be interested, here is my working code (in custom/gtest-printers.h). It makes all the difference in the world to see the actual printout in the error messages.

// ** Custom implementation starts here **

#ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_
#define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_

#include <boost/optional.hpp>

namespace boost
{
template <class T>
inline void PrintTo(const optional<T>& o, ::std::ostream* os)
{
  *os << (o ? ::testing::PrintToString(o.get()) : "(boost::none)");
}
}

#endif  // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_

@sbenzaquen
Copy link
Collaborator

sbenzaquen commented Dec 19, 2017 via email

@bobkocisko
Copy link

bobkocisko commented Dec 19, 2017

Thanks, good to know. I tried it and it works too (yay!) but I actually prefer the implementation I posted because it does a nicer job formatting boost::optional<std::string> if the string has multiple lines.

@gennadiycivil
Copy link
Contributor

I have been cleaning up older and inactive GitHub googletest issues. You may see an issue "closed" if it appears to be inactive/abandoned
Thank you

bors bot added a commit to canonical/mir that referenced this issue Jun 19, 2021
2071: Add stream operator for miral::Window r=AlanGriffiths a=wmww

This makes window's correctly appear in test failures, and may have other uses. google/googletest#1149 explains why the stream operators really need to be declared in the same place as the class. I tried a number of other ways, and this is the only one I found to be successful.

Co-authored-by: William Wold <[email protected]>
bors bot added a commit to canonical/mir that referenced this issue Jun 23, 2021
2071: Add stream operator for miral::Window r=AlanGriffiths a=wmww

This makes window's correctly appear in test failures, and may have other uses. google/googletest#1149 explains why the stream operators really need to be declared in the same place as the class. I tried a number of other ways, and this is the only one I found to be successful.

Co-authored-by: William Wold <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants