我想SomeFunction
用. 看起来可以与结合使用,但我不知道如何。SetArg
boost
bind
lambda
这段代码非常简单,但我想替换它的原因是我需要一个 for2
和3
etc 参数。
template<class T>
struct SomeFunction
{
T value;
SomeFunction(T s)
: value(s) {}
void operator()(T& s)
{
s = value;
}
};
template<class T>
SomeFunction<T> SetArg(T value)
{
return SomeFunction<T>(value);
}
要求:
- 我想要一个返回函数对象的函数。
- 当我调用这个函数对象时,参数是通过引用传递的。
- 该函数通过将引用传入的对象设置为预设值来修改它们。
- 在上面的代码中,预设值是按值传入的
ctor
,但任何其他方式也可以。
下面的代码演示了用法:
void main()
{
std::string t;
SetArg(std::string("hello"))(t);
assert(t == "hello");
}
一些上下文:
我想测试 class 的客户端代码Foo
。所以我想func1
用我自己的替换实现,但是以一种灵活的方式。
struct Foo
{
virtual void func1(std::string& s)
{
}
};
struct MockFoo : public Foo {
MOCK_METHOD1(func1, void(std::string&));
};
void ExampleTestCase::example()
{
MockFoo f;
std::string s;
EXPECT_CALL(f, func1(_))
.WillOnce(Invoke(SetArg(std::string("hello"))));
f.func1(s);
CPPUNIT_ASSERT_EQUAL(std::string("hello"), s);
}
Invoke 接受一个函数或函数对象。在它的新实现中func1
调用由返回的函数对象SetArg
并将其参数设置为字符串"hello"
。
Invoke 是 gmock/gtest 的一部分,但SetArg
不是。