4

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?

3
  • Just to clarify: Is your question "how to return a custom struct in mocked function?" If yes, please, edit the title, it is not clear. The title says you want to pass a argument to the mock, the body says you want to return custom type from the mock. This is a good question, it will be awesome if you can make it a little clearer. Commented Dec 21, 2016 at 12:57
  • I think so. I've built a struct which fields I fill with values (in the test side) and then I need to say that mock call will return the struct, with those values. The idea I have or the thing I would like to find is if there is any way to do the same I put above, in example, first of all, but type structs instead the integer, char or bool, type, etc. Commented Dec 21, 2016 at 13:00
  • Ok then. Try my answer, let me know the results. Commented Dec 21, 2016 at 13:00

1 Answer 1

4

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);
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Felipe L. I will try this. As soon as I can I will share my results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.