gmock支持右值引用的解决方法

Edm*_*und 6 c++ gmock

gmock不支持将右值引用作为模拟函数的参数(问题报告)。

例如,以下代码将无法编译:

MOCK_METHOD1(foo,void(std::string&&));
Run Code Online (Sandbox Code Playgroud)

我找不到有关gmock何时将对此添加支持的信息。

Edm*_*und 7

我想出了一种解决方法:使用非模拟函数foo(std::string&& s){foo_rvr(s)}将函数中继到模拟函数foo_rvr(std::string)。这是完整的程序。

#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

class RvalueRef
{
public:
    virtual void foo(const std::string&)=0;
    virtual void foo(std::string&&)=0;
};

class MockRvalueRef : public RvalueRef
{
public:
    void foo(std::string&& s){foo_rvr(s);}
    MOCK_METHOD1(foo,void(const std::string&));
    MOCK_METHOD1(foo_rvr,void(std::string));
};

TEST(RvalueRef, foo)
{
    MockRvalueRef r;
    {
        ::testing::InSequence sequence;
        EXPECT_CALL(r,foo("hello"));
        EXPECT_CALL(r,foo_rvr("hi"));
    }

    std::string hello("hello");
    r.foo(hello);
    r.foo("hi");    
}

int main(int argc, char* argv[])
{
    ::testing::InitGoogleMock(&argc,argv);

    int rc=RUN_ALL_TESTS();

    getchar();
    return rc;
}
Run Code Online (Sandbox Code Playgroud)