You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's no sane method to store a function pointer passed to a mocked function, you are forced to use myFunction_AddCallback(myCallback) to then grab the function pointer within myCallback. Surely cMock is capable of identifying an argument that itself is a function pointer, and then adding a generated method such as myFunction_InvokeCallbackArg_<ARGNAME>.
static void myCallback(int value)
{
printf("I have been called, value=%d\n", value);
}
void methodUnderTest(void)
{
// Register my callback:
registerCallback(myCallback);
}
Test Code:
/// START: BOILER PLATE TEST CODE FOR STORING A CALLBACK PASSED TO A MOCKED METHOD
gRegisteredCallback = NULL;
void registerCallbackStub(callback_fn callback, int numCalls)
{
(void)numCalls; // not used.
// store the registered callback method
gRegisteredCallback = callback;
}
/// END: BOILER PLATE TEST CODE FOR STORING A CALLBACK PASSED TO A MOCKED METHOD
void test_something(void)
{
// setup the cmock
registerCallback_StubWithCallback(registerCallbackStub);
// Call the method under test, which should register a callback (which will be stored for later use in the test)
methodUnderTest();
}
The text was updated successfully, but these errors were encountered:
Interesting. I'd not considered testing a callback function in this way. It has a certain elegance to it... in that the callback doesn't have to become public in order to test it.
I think this makes sense to be another CMock plugin. It's not a feature that would get used often, but certainly often enough to make it worth building! Thanks for the idea!
I think this also lends itself to pointer arguments which are pointers to a data structure, in some cases I've come across complex structures where the values of all members are not known or required (so the memory compare doesn't apply for this case where just several members need to be compared). This considered, maybe rather than InvokeCallback something like GetPointerArg would be better (i.e. capture the function pointer, or pointer to context data etc.), if the test knows its a function pointer it can "get" it and then call it.
There's no sane method to store a function pointer passed to a mocked function, you are forced to use
myFunction_AddCallback(myCallback)
to then grab the function pointer withinmyCallback
. Surely cMock is capable of identifying an argument that itself is a function pointer, and then adding a generated method such asmyFunction_InvokeCallbackArg_<ARGNAME>
.Consider the following header file:
Code Under Test:
Test Code:
The text was updated successfully, but these errors were encountered: