ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
653 stars 269 forks source link

Expecting only value parameters (not the pointer types) with ExpectAndReturn #406

Closed lafalexis closed 1 year ago

lafalexis commented 1 year ago

Hi! I am currently trying to integrate unit testing with Ceedling to the legacy code we have where I work. I have been researching quite a bit for issues ressembling mine, but it has not been quite fruitful... I want to use the ExpectAndReturn mock, but only to check the non-pointer variables. The reason for this is that the value pointed is to be set inside the mocked function. Therefore, the value pointed by the pointer is not set prior entering the said function.

Here is a little example of what I was trying to illustrate above :

// Mocked function
bool get_cb(
    const uint16_t reg_addr,
    uint32_t *const reg_val,
    HANDLE_User_data hUser_data);

// A test for the function that uses the mocked function
void test_real_function_under_test(void){
    uint16_t reg_add = 0xDEAD;
    uint32_t reg_val;

    get_cb_ExpectAndReturn(reg_add, <do not care about it's value when entering the function>, NULL, true);
    get_cb_ReturnThruPtr_reg_val(&reg_val);

    real_function_under_test(reg_add);
}

void real_function_under_test(uint16_t register_address){
    uint32_ t u32Val;

    if(get_cb(register_address, &u32Val, NULL))
        printf("%u", u32Val);
}

I know of the plugin ExpectAnyArgsAndReturn, but the thing is, I still want to check reg_add... Is there a way to do this or should I try and build myself a CMock plugin to generate the ExpectValueArgsAndReturn or something like that? Let me know if any of this was unclear and thanks ahead for the help :)

-Alexis

lafalexis commented 1 year ago

Turns out there is a builtin plugin that serves exactly this purpose. I write this for those who are looking for the same thing I did.

The plugin is ignore_arg and can be added to CMock configuration in the following manner :

:cmock:
  :plugins:
    - :ignore_arg

This feature is detailed here : https://github.com/ThrowTheSwitch/CMock/blob/master/docs/CMock_Summary.md#ignore-arg