C ++ lambda测试拦截器/模拟

时间:2018-01-05 16:09:28

标签: c++ unit-testing lambda

我正在寻找一个解决方案,我可以创建一个拦截器/模拟lambda来检查它是否被调用。我测试的是:

// test.hpp
template<typename R, typename ...Args>
std::function<R(Args&&...)> make_test_lambda(
    bool &called,
    std::function<R(Args&&...)> fn
) {
  called = false;

  return [&called, f=std::move(fn)](Args&&... args) {
    called = true;
    return f(std::forward<Args>(args)...);
  };
}

// test.cpp: test with "catch"
bool called;
function_under_test(make_test_lambda(called, [](int x) {
    REQUIRE(x == 4);
}));

REQUIRE(called);

它不起作用no matching function for call。你能救我吗?

非常感谢!

1 个答案:

答案 0 :(得分:3)

问题是每个lambda都有一个只有编译器知道的唯一类型。模板参数推导发生在编译器选择候选函数时,确定可行函数集之前。粗略地说,只有当所有类型都能完全匹配时,模板函数才会包含在候选集中。不允许转换。

在您的情况下,用更通用的模板参数Func替换std::function<>就可以了。如下代码:

template<class Func>
struct wrapper
{
    wrapper(Func f, bool &called)
        : f_(f)
        , called_(called)
    {}

    template<class... Args>
    decltype(auto)
    operator() (Args&& ...args)
    {
        called_ = true;
        return f_(std::forward<Args>(args)...);
    }
private:
    Func f_;
    bool &called_;
};

template<class Func>
auto 
make_test_lambda(bool &called, Func func)
{
    called = false;
    return wrapper<Func>(func, called);
}