Customise actual/expected "Value of" string in Google Test failure output messages
In order to print custom types you could "teach" Google Test how to print your custom types which as described in the section Teaching Google Test How to Print Your Values.
The header in the gtest-printers.h
source file provides an answer:
This file implements a universal value printer that can print a value of any type T:
void ::testing::internal::UniversalPrinter::Print(value, ostream_ptr);
A user can teach this function how to print a class type T by defining either operator<<() or PrintTo() in the namespace that defines T. More specifically, the FIRST defined function in the following list will be used (assuming T is defined in namespace foo):
- foo::PrintTo(const T&, ostream*)
- operator<<(ostream&, const T&) defined in either foo or the global namespace.
If none of the above is defined, it will print the debug string of the value if it is a protocol buffer, or print the raw bytes in the value otherwise.
So it looks like the operator override needs to be a non-member function.
std::ostream& operator<<(std::ostream& stream, Line const& line)
{
return stream << "Line (radius=" << line.radius() << " theta=" << line.theta() << ")";
}