Reputation: 670
I like to write a matcher for a structure which holds some float values:
struct Point3D
{
float x;
float y;
float z;
}
class Interface
{
virtual void SetPoint(Point3D point) = 0;
}
class MockInterface:
public Interface
{
MOCK_METHOD1(SetPoint, void(Point3D point));
}
MATCHER_P(Point3DEq, point, "Comparison of a Point3D")
{
return point.x == arg.x && point.y == arg.y && point.z == arg.z;
}
TEST(Point3DComparison, TestIfPoint3DAreEqual)
{
MockInterface interfaceFake;
Point setPoint = { 1.0, 1.0, 1.0 }
EXPECT_CALL(interfaceFake, SetPoint(Point3DEq(setPoint)).WillOnce(Return());
}
I don't like this Matcher because he doesn't respected good float comparison. I would like to write a Matcher with using the Floating Comparison of gmock or gtest. It should look similiar to that.
MATCHER_P(Point3DEq, point, "Comparison of a Point3D")
{
return EXPECT_THAT(point.x, FloatEq(arg.x) && EXPECT_THAT(point.y, FloatEq(arg.y) && EXPECT_THAT(point.z, FloatEq(arg.z);
}
The problem is that EXPECT_THAT is not returning any boolean value. Is there a nice clean way to do this using the functionality of gmock and gtest?
Upvotes: 2
Views: 4200
Reputation: 24412
nice way is to forget about MATCHER_P
...
Use ::testing::AllOf
- and use proper matchers from gmock - like mentioned FloatEq
/DoubleEq
- see:
auto Point3DEq(Point3D const& p_expected)
{
using namespace testing;
return AllOf(Field(&Point3D::x, DoubleEq(p_expected.x)),
Field(&Point3D::y, DoubleEq(p_expected.y)),
Field(&Point3D::z, DoubleEq(p_expected.z)));
}
Upvotes: 3