ThrowTheSwitch / CMock

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

Cmock discards const qualifier with custom types #484

Open astro-stan opened 1 month ago

astro-stan commented 1 month ago

Consider the following example:

// MyHeader.h

typedef enum {
    a,
    b,
    c,
} MyType_t;

int myFunc(
    const MyType_t t_MyType
);

When myFunc gets mocked it results in the following code being generated:

// MockMyHeader.c

...

void CMockExpectParameters_MyFunc(CMOCK_MyFunc_CALL_INSTANCE* cmock_call_instance, const MyType_t t_MyType)
{
  memcpy((void*)(&cmock_call_instance->Expected_t_MyType), (void*)(&t_MyType), // <---- NO CONST WHEN CASTING
         sizeof(MyType_t[sizeof(t_MyType) == sizeof(MyType_t) ? 1 : -1])); /* add MyType_t to :treat_as_array if this causes an error */
  cmock_call_instance->IgnoreArg_t_MyType = 0;
}

ITC_Status_t MyFunc(const MyType_t t_MyType)
{
  ...
  if (!cmock_call_instance->IgnoreArg_t_MyType)
  {
    UNITY_SET_DETAILS(CMockString_MyFunc,CMockString_t_MyType);
    // v AGAIN NO CONST WHEN CASTING v
    UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(&cmock_call_instance->Expected_t_MyType), (void*)(&t_MyType), sizeof(MyType_t), cmock_line, CMockStringMismatch);
  }
  }
  ...
}