谷歌模拟:如何从模拟参数中的指针复制内存?

时间:2017-02-24 13:43:00

标签: c++ unit-testing googlemock

我有以下用签名模拟的功能:

uint32 Icc_u_SendMessage(te_IccTransferType e_DataTxType, SW_e_IccMsgType e_MsgType, uint8 *const pui8_DataRef, const uint32 ui32_DataSize, const SW_e_CoreIdent e_MsgDestination)

对我来说有趣的论点是Arg2(pui8_DataRef)。

调用被测函数:

MessageBuffer<ET_t_SignalToGs> s_locPayloadToGs; (Local)
iccReturnValue = c_ICC_Messages::ICC_ui32_SendMessage(c_ICC_Messages::e_TxSendRefCopyData, e_msg, (uint8*)&locPayloadToGs, u_size, e_dst);

我想复制s_locPayloadToGs的全部内容以进行检查。 SaveArgPointee()的问题是第三个参数是(uint8 *)而我只得到第一个字节。

我怎么能在memcpy的模拟参数中做一种EXPECT_CALL()?如下:

c_event_transceiver::MessageBuffer<c_event_transceiver::ET_t_SignalToGs> expectedPayload;
EXPECT_CALL(obj_MockIccMessages, ICC_ui32_SendMessage(c_ICC_Messages::e_TxSendRefCopyData,
                                                            e_MsgToGs,
                                                            _,
                                                            sizeof(expectedLocPayload),
                                                            c_event_transceiver::a_ConfigMapGsToCore[0].e_core)).WillOnce(DoAll(SaveArgPointee<2>(&expectedPayload), Return(retSendMessage)));

由于

1 个答案:

答案 0 :(得分:0)

您可以定义own action

ACTION_P(SavePayload, expectedPayload)
{
    auto beginOfArray = arg2;
    auto sizeOfArray = arg3;
    auto endOfArray = beginOfArray  + sizeOfArray ;

    expectedPayload->assign(beginOfArray , endOfArray);
    //               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    //  that only my guess how it is possible to store payload
}

只需在测试中将SaveArgPointee<2>(&expectedPayload)替换为SavePayload(&expectedPayload)

但是,你真正需要的是multi argument matcher - 它将匹配该有效载荷,就像这样:

MATCHER_P(matchPayload, expectedPayload)
{
    auto beginOfArray = get<2>(arg);
    auto sizeOfArray = get<3>(arg);
    auto endOfArray = beginOfArray  + sizeOfArray ;

    return expectedPayload->equalTo(beginOfArray , endOfArray);
           //               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
           //  that only my guess how it is possible to compare payloads   
}

并将其与With(matchPayload()) clausure一起使用:

c_event_transceiver::MessageBuffer<c_event_transceiver::ET_t_SignalToGs> expectedPayload;
// expectedPayload - build it to be as this you expect
...

EXPECT_CALL(obj_MockIccMessages, ICC_ui32_SendMessage(c_ICC_Messages::e_TxSendRefCopyData,
                     e_MsgToGs,
                     _,
                     sizeof(expectedLocPayload),
                     c_event_transceiver::a_ConfigMapGsToCore[0].e_core))
.With(matchPayload(&expectedPayload)).WillOnce(Return(retSendMessage));