在 gmock 中是否有匹配类型而不是值的方法?该类是这样的:
struct Blob {
template<class T> bool is(); // if blob holds data of type T
template<class T> T get(); // get data as type T
}
我的匹配器看起来像这样:
MATCHER_P(BlobIs, T, "") {
return arg->is<T>();
}
但是构建失败了:
error: expected primary-expression before ')' token
最佳答案
您不能将类型作为参数传递给任何函数 - 包括那些由 MATCHER_P
生成的函数宏。
但是您可以传递将使用正确类型的 lambda(函数对象)。
喜欢这里:
MATCHER_P(BlobIsImpl, isForForType, "") {
return isForType(arg);
}
使用以下功能模板 - 您将实现预期目标:
template <typename T>
auto BlobIs()
{
auto isForType = [](Blob& arg) -> bool
{
return arg->template is<T>();
};
return BlobIsImpl(isForType);
}
像这样使用:BlobIs<SomeType>()
还有 2 个问题:
- 你必须使用
template
关键字指定is
是一个函数模板。更多信息 here - 你应该定义
is
作为常量函数。
关于c++ - Gmock 匹配器匹配类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57319458/