I have a data structure which is being notified to a listener. Among its many variables, it has an optional double which represents an angle:
struct Data
{
// ...other data fields
std::optional<double> angle;
// ...other data fields
};
I want to check that the listener is being called with the right angle. I am using the Google Test Framework, so I have defined my own matcher here:
MATCHER_P( HasAngle, angle, "" )
{
if ( !arg.angle.has_value( ) )
{
return false;
}
return arg.angle.value( ) == angle;
}
And checked if my listener is being called with the expected value.
EXPECT_CALL( listener, on_event( HasAngle( 45.0 ) ) ).Times( AtLeast( 1 ) );
It ended up that the listener is not called with the expected value, but the printouts are really unreadable:
Expected arg #0: has angle 45
Actual: 128-byte object <75-85 4D-52 FB-21 F9-BF 47-D9 48-53 FB-21 09-C0 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 01-17 23-83 FD-7F 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00>
The actual value is unintelligible. I was not expecting the framework to not support C++17 types by default, so I started reading the Google Test Framework Cookbook and I tried to add my own PrintTo overload like this:
void
PrintTo( const std::optional< double >& opt, std::ostream* os )
{
if ( !opt )
{
*os << "std::nullopt";
}
else
{
*os << *opt;
}
}
Needless to say, it did not help. What I am pretty much sure is that since the output states 128-byte object it is trying to print the sole std::optional<double> and not the entire struct Data, which is much bigger than that, but it is not succeeding despite the overloaded PrintTo.