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?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Suvi_Eu
  • 255
  • 1
  • 3
  • 16
  • 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. – Felipe Lavratti Dec 21 '16 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. – Suvi_Eu Dec 21 '16 at 13:00
  • Ok then. Try my answer, let me know the results. – Felipe Lavratti Dec 21 '16 at 13:00

1 Answers1

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);
    ...
}
Felipe Lavratti
  • 2,887
  • 16
  • 34