如何在 gmock expect_call 中对结构参数进行部分匹配
How to do the partial match for a struct parameter in gmock expect_call
struct obj
{
int a;
string str;
string str2;
bool operator==(const obj& o) const
{
if(a == o.a && str == o.str && str2 == o.str2) return true;
return false;
}
}
然后在class中的函数中,它使用结构对象作为输入参数:
bool functionNeedsToBeMocked(obj& input)
{
//do something
}
现在我要做的是,
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked( /* if input.a == 1 && input.str == "test" && input.str2.contains("first")*/ )).Times(1).WillOnce(Return(true));
输入值为
inputFirst.a = 1;
inputFirst.str = "test";
inputFirst.str2 = "something first";
我希望 inputFirst 可以匹配到我的 EXPECT_CALL。
我如何使用 EXPECT_CALL 匹配器来做到这一点?
我看到了
EXPECT_CALL(foo, DoThat(Not(HasSubstr("blah")),
NULL));
在 gmock 食谱上,但我不知道如何为结构参数执行 HasSubStr。
您可以为 obj
结构实现自己的匹配器。
当您输入时:
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked(some_obj)).Times(1).WillOnce(Return(true));
然后 gmock 使用默认匹配器 Eq
,使用 some_obj
作为其预期参数,实际 functionNeedsToBeMocked
参数作为匹配器中的 arg
。 Eq
匹配器默认会为预期和实际对象调用 bool operator==
:
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked(Eq(some_obj))).Times(1).WillOnce(Return(true));
但是,由于您不想使用 bool operator==
,您可以编写自定义匹配器(删除 Times(1)
,因为它也是默认匹配器):
// arg is passed to the matcher implicitly
// arg is the actual argument that the function was called with
MATCHER_P3(CustomObjMatcher, a, str, str2, "") {
return arg.a == a and arg.str == str and (arg.str2.find(str2) != std::string::npos);
}
[...]
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked(CustomObjMatcher(1, "test", "first"))).WillOnce(Return(true));
可以使用 Field
匹配器和内置匹配器 HasString
来组合自定义匹配器,但让我们 'leave it as an excercise to the reader' :P
struct obj
{
int a;
string str;
string str2;
bool operator==(const obj& o) const
{
if(a == o.a && str == o.str && str2 == o.str2) return true;
return false;
}
}
然后在class中的函数中,它使用结构对象作为输入参数:
bool functionNeedsToBeMocked(obj& input)
{
//do something
}
现在我要做的是,
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked( /* if input.a == 1 && input.str == "test" && input.str2.contains("first")*/ )).Times(1).WillOnce(Return(true));
输入值为
inputFirst.a = 1;
inputFirst.str = "test";
inputFirst.str2 = "something first";
我希望 inputFirst 可以匹配到我的 EXPECT_CALL。
我如何使用 EXPECT_CALL 匹配器来做到这一点?
我看到了
EXPECT_CALL(foo, DoThat(Not(HasSubstr("blah")),
NULL));
在 gmock 食谱上,但我不知道如何为结构参数执行 HasSubStr。
您可以为 obj
结构实现自己的匹配器。
当您输入时:
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked(some_obj)).Times(1).WillOnce(Return(true));
然后 gmock 使用默认匹配器 Eq
,使用 some_obj
作为其预期参数,实际 functionNeedsToBeMocked
参数作为匹配器中的 arg
。 Eq
匹配器默认会为预期和实际对象调用 bool operator==
:
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked(Eq(some_obj))).Times(1).WillOnce(Return(true));
但是,由于您不想使用 bool operator==
,您可以编写自定义匹配器(删除 Times(1)
,因为它也是默认匹配器):
// arg is passed to the matcher implicitly
// arg is the actual argument that the function was called with
MATCHER_P3(CustomObjMatcher, a, str, str2, "") {
return arg.a == a and arg.str == str and (arg.str2.find(str2) != std::string::npos);
}
[...]
EXPECT_CALL(*mockedPointer, functionNeedsToBeMocked(CustomObjMatcher(1, "test", "first"))).WillOnce(Return(true));
可以使用 Field
匹配器和内置匹配器 HasString
来组合自定义匹配器,但让我们 'leave it as an excercise to the reader' :P