CppUtest: how to return a custom struct from mocked function

2.8k views Asked by At

I'm starting using CppUTest for some C embedded projects. Now I'm dealing with mocked calls to the system. After learned how to pass C typicall types, in ex:

Example that works properly:

Mock file part of code:

uint32_t LL_TIM_GetPrescaler(TIM_TypeDef * TIMx){    

    mock().actualCall("LL_TIM_GetPrescaler"); 
    return mock().unsignedIntReturnValue(); 
}

Test file part of code related with this mocked call:

TEST (HAL_AS393,HAL_AS393x_Init_test)
{
    ...
    mock().expectOneCall("LL_TIM_GetPrescaler").andReturnValue(TEST_PRESCALER_VALUE);
    //being TEST_PRESCALER_VALUE an int initialized variable before
    ... 
}

There is no problem for me on understanding this.

But now I'm trying to pass a struct from the test to a mock call with the desired list of fields that I want the mock sends to the call function (of the production code under test). The case is testing a function with some system calls. And this thing is like:

Piece of code under test:

//struct type definition

typedef struct
{   
    bool_t                  WAKE_FLAG;
    bool_t                  DATA_READ_FLAG; 
}HAL_AS393X_Status;

//function under test   
RFIDDrvStatus RFID_DRV_GetStatus(void) 
{ 
    HAL_AS393X_Status HAL_Status; 
    ...       
    HAL_Status=HAL_AS393x_GetStatus();
    ...
}

Now the idea is -in my test file using mock-:

TEST(RFID_Drv,RFID_DRV_GetStatus_test )
{
    HAL_AS393X_Status FAKE_HAL_STATUS;
    FAKE_HAL_STATUS.WAKE_FLAG=TRUE;
    FAKE_HAL_STATUS.DATA_READ_FLAG=TRUE;
    ...
    mock().expectOneCall("HAL_AS393x_GetStatus").andReturnValue(FAKE_HAL_STATUS);
    ...
}

My question is, how can I build the mock.actualCall of this mock.expectOneCall("HAL_AS393x_GetStatus") in order to say that it must return a defined type struct (HAL_AS393X_Status)? I'm serching information for doing something like this if possible or if it exists:

mock().actualCall("HAL_AS393x_GetData");
return mock().XXXReturnValue();`

What mock syntax must be on just on XXX place?

1

There are 1 answers

1
Felipe Lavratti On

Use the the CppUMock returnPointerValueOrDefault, cast and dereference it before returning.

Your mocked function body can be like this:

static HAL_AS393X_Status default;
mock().actualCall("HAL_AS393x_GetData");
return *(HAL_AS393X_Status *)mock().returnPointerValueOrDefault(&default);`

And in the test you expect the mock like this:

TEST(RFID_Drv,RFID_DRV_GetStatus_test )
{
    HAL_AS393X_Status FAKE_HAL_STATUS;
    FAKE_HAL_STATUS.WAKE_FLAG=TRUE;
    FAKE_HAL_STATUS.DATA_READ_FLAG=TRUE;
    ...
    mock().expectOneCall("HAL_AS393x_GetStatus").andReturnValue(&FAKE_HAL_STATUS);
    ...
}