gpt4 book ai didi

c++ - GTest 中的嵌套匹配器

转载 作者:搜寻专家 更新时间:2023-10-31 01:29:56 26 4
gpt4 key购买 nike

我想在其他匹配器中使用一些现有的匹配器。我知道 MatcherInterface 解决方案,但我想知道我能否使用 MATCHER_P 定义的匹配器。如果找到这个解决方案:

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));
}

我只是想知道,有没有更好更好的解决方案

  • 在另一个中使用我自己的 MATCHER_P 匹配器
  • 在另一个中使用原始 GTest 匹配器。

最佳答案

正确的方法是尽可能多地使用 gtest/gmock 中的匹配器。仅当没有已提供的匹配器时 - 使用您自己的匹配器。

在您的示例中 - 就这么简单:

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

auto EqBar(const Bar& expected)
{
return ::testing::AllOf(
::testing::Field(&Bar::foo, EqFoo(expected.foo)),
::testing::Field(&Bar::i, expected.i)
);
}

更通用的方法是使用重载:

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(&Foo::x, x),
::testing::Field(&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(&Bar::foo, foo),
::testing::Field(&Bar::i, expected.i),
);
}
auto MatchBar(const Bar& expected)
{
return MatchBar(expected.foo, expected.i);
}

所以你的测试:

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)), _);
}

无论如何 - 使用 MATCHER_P应该是相当罕见的情况,我自己的观察是这个宏确实被过度使用了。

如果您的项目是 C++14 之前的项目 - 使用 ::testing::Matcher<T>而不是 auto作为所有这些函数的返回类型。

关于c++ - GTest 中的嵌套匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48988205/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com