How to set a value to void * argument of a mock method in google mock testing?

只谈情不闲聊 提交于 2019-12-05 17:01:53

Rather then doing that, you could invoke a function (or a method) and copy the parameter.

Something like this in the source file where the test is :

int invokedPObjData;

bool FakeGetDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)
{
  // copy data. here I assumed it is an int
  invokedPObjData = *static_cast< int* >( pObjData );

  return true; // or whatever makes sense
}

in test :

EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) )
            .WillOnce(Call(FakeGetDictItem))
            .RetiresOnSaturation();

then later in test check what needs to be checked.

The ACTION_P approach is basically OK. But as you are dealing with a C string, you can't just use the assignment operation (which just copies the first character) but instead you should use a string copy function like ACTION_P(SetArg2ToCharWithSizeArg3, value) { strcpy_s(static_cast<char*>(arg2), arg3, value); } (I couldn't resist to slightly rename the action).

I recently had a similar need and came up with this as a generic solution. It's based on the built-in SetArgPointee and has the same syntax:

template <size_t N, typename A>
class SetArgumentPointeeVoidAction {
public:
    explicit SetArgumentPointeeVoidAction(const A& value) : value_(value) {}
    void operator=(SetArgumentPointeeVoidAction const&) = delete;

    template <typename Result, typename ArgumentTuple>
    void Perform(const ArgumentTuple& args) const
    {
        ::testing::StaticAssertTypeEq<void, Result>();
        ::testing::StaticAssertTypeEq<void*,
            std::decay<decltype(::testing::get<N>(args))>::type>();
        *static_cast<A*>(::testing::get<N>(args)) = value_;
    }

private:
    const A value_;
};

/**
 * \brief  Sets a \c void* output argument to the contents of the
 *         supplied object.  It's on you to ensure this is safe.
 * \tparam N The argument index.
 * \tparam T The real argument type.
 * \param  x The argument to assign to the output argument.
 * \return A GMock Action that performs the requested assignment.
 * \note   Use \c SetArgPointee when it's not a \c void*.
 */
template <size_t N, typename T>
::testing::PolymorphicAction< SetArgumentPointeeVoidAction<N, T> >
SetArgPointeeVoid(const T& x)
{
    return ::testing::MakePolymorphicAction(
        SetArgumentPointeeVoidAction<N, T>(x));
}

It will give you a compile error if you try to use this on an argument that isn't a void*, so it should be relatively safe as long as you ensure you supply the correct argument.

It's also possible to implement this using ACTION_TEMPLATE, which is a bit shorter, but it generates unused argument warnings, which can be irritating.

(In older versions of GMock you might have to use ::std::tr1::get instead of ::testing::get.)

Left as an exercise for the reader: it's possible to enhance this with perfect forwarding to allow this to move-construct and move-assign for a slight efficiency boost. Although if you're passing anything other than PODs around as void*s then you're probably doing it wrong.

Here is an example using ACTION_TEMPLATE allowing a string to be assigned to a void *, for reference...

ACTION_TEMPLATE(StrCpyArgToVoidPointer,
                HAS_1_TEMPLATE_PARAMS(int, k),
                AND_2_VALUE_PARAMS(value, size))
{
    strncpy(static_cast<char *>(::testing::get<k>(args)), value, size);
    return;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!