c++googletestgooglemock

Nested Matchers in GTest


I would like to use some existing matchers in other matcher. I know about the MatcherInterface solution but I was wondering can I use matchers which were defined by MATCHER_P. If found this solution:

struct Foo
{
    double x;
    double y;
};

struct Bar
{
    Foo foo;
    int i;
};

MATCHER_P(EqFoo, foo, "")
{
    ::testing::Matcher<double> x_matcher = ::testing::DoubleNear(foo.x, 0.0001);
    if (!x_matcher.MatchAndExplain(arg.x, result_listener))
    {
        return false;
    }

    ::testing::Matcher<double> y_matcher = ::testing::DoubleNear(foo.y, 0.0001);
    if (!y_matcher.MatchAndExplain(arg.y, result_listener))
    {
        return false;
    }

    return true;
}

MATCHER_P(EqBar, bar, "")
{
    ::testing::Matcher<Foo> foo_matcher = EqFooMatcherP<Foo>(bar.foo);

    if (!foo_matcher.MatchAndExplain(arg.foo, result_listener))
    {
        return false;
    }

    if (bar.i != arg.i)
    {
        return false;
    }

    return true;
}

TEST_F(TestClass, BarTest)
{
    Bar bar_val{{10.12, 76.43}, 78};
    Bar bar_exp{{10.12, 99.99}, 78};

    EXPECT_THAT(bar_val, EqBar(bar_exp));
}

I am just wondering, is there any better and nicer solution to


Solution

  • The correct way is to use, as much as possible, the matchers from gtest/gmock. Only if there is no already provided matchers - use your own.

    In your example - it is just as simple as this:

    auto EqFoo(const Foo& expected)
    {
       return ::testing::AllOf(
             ::testing::Field("x", &Foo::x, ::testing::DoubleNear(expected.x, 0.0001)),
             ::testing::Field("y", &Foo::y, ::testing::DoubleNear(expected.y, 0.0001))
         );
    }
    
    auto EqBar(const Bar& expected)
    {
       return ::testing::AllOf(
             ::testing::Field("foo", &Bar::foo, EqFoo(expected.foo)),
             ::testing::Field("i", &Bar::i, expected.i)
         );
    }
    

    More general approach is to use overloads:

    auto MatchDouble(double expected)   
    {
        return ::testing::DoubleNear(expected.x, 0.0001);
    }
    auto MatchFoo(::testing::Matcher<double> x, ::testing::Matcher<double> y)
    {
       return ::testing::AllOf(
             ::testing::Field("x", &Foo::x, x),
             ::testing::Field("y", &Foo::y, y)
         );
    }
    auto MatchFoo(double x, double y)
    {
       return MatchFoo(MatchDouble(x), MatchDouble(y));
    }
    
    auto MatchBar(::testing::Matcher<Foo> foo, ::testing::Matcher<int> i)
    {
       return ::testing::AllOf(
             ::testing::Field("foo", &Bar::foo, foo),
             ::testing::Field("i", &Bar::i, expected.i),
         );
    }
    auto MatchBar(const Bar& expected)
    {
       return MatchBar(expected.foo, expected.i);
    }
    

    So your test:

    TEST_F(TestClass, BarTest)
    {
        Bar bar_val{{10.12, 76.43}, 78};
        Bar bar_exp{{10.12, 99.99}, 78};
    
        EXPECT_THAT(bar_val, MatchBar(bar_exp));
    
        // or - e.g. you can match only Bar::y if other things are irrelevant in your test
        EXPECT_THAT(bar_val, MatchBar(MatchFoo(_, MatchDouble(2.001)), _);
    }
    

    Anyway - using MATCHER_P should be rather rare case, my own observation is that this macro is really overused.

    In case your project is pre-C++14 - use ::testing::Matcher<T> instead of auto as return type for all of these functions.