How to mock a function that returns via pointer?

1,578 views
Skip to first unread message

Łukasz Przeniosło

unread,
Apr 9, 2021, 6:46:29 AM4/9/21
to ThrowTheSwitch Forums
Hello there,
I have a function that returns a value both via input pointer parameter and return statement:

uint16_t  fsm_Event_Get_Digital_Neutral_Green( bool* value );

What is the proper way of mocking it? CMock automatically generates the following candidates:

#define fsm_Event_Get_Digital_Neutral_Green_IgnoreAndReturn(cmock_retval) fsm_Event_Get_Digital_Neutral_Green_CMockIgnoreAndReturn(__LINE__, cmock_retval)
void fsm_Event_Get_Digital_Neutral_Green_CMockIgnoreAndReturn(UNITY_LINE_TYPE cmock_line, uint16_t cmock_to_return);
#define fsm_Event_Get_Digital_Neutral_Green_StopIgnore() fsm_Event_Get_Digital_Neutral_Green_CMockStopIgnore()
void fsm_Event_Get_Digital_Neutral_Green_CMockStopIgnore(void);
#define fsm_Event_Get_Digital_Neutral_Green_ExpectAndReturn(value, cmock_retval) fsm_Event_Get_Digital_Neutral_Green_CMockExpectAndReturn(__LINE__, value, cmock_retval)
void fsm_Event_Get_Digital_Neutral_Green_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, _Bool* value, uint16_t cmock_to_return);
typedef uint16_t (* CMOCK_fsm_Event_Get_Digital_Neutral_Green_CALLBACK)(_Bool* value, int cmock_num_calls);
void fsm_Event_Get_Digital_Neutral_Green_AddCallback(CMOCK_fsm_Event_Get_Digital_Neutral_Green_CALLBACK Callback);
void fsm_Event_Get_Digital_Neutral_Green_Stub(CMOCK_fsm_Event_Get_Digital_Neutral_Green_CALLBACK Callback);
#define fsm_Event_Get_Digital_Neutral_Green_StubWithCallback fsm_Event_Get_Digital_Neutral_Green_Stub

So I would like to make an "Expect" statement in the test, but I don;t really grasp the idea about handling the input pointer here. The function is called internally with a local variable that's address is inaccessible for me (so cannot supply it as the input parameter). 

Is the only way to ignore these calls? But here I am not sure why isn't there a function call generated with just "Ignore" suffix, but only "IgnoreAndReturn". The general idea is that I would like to decide what value will be returned via the input pointer- is this doable?

I would appreciate all help. 

Łukasz Przeniosło

unread,
Apr 9, 2021, 7:25:41 AM4/9/21
to ThrowTheSwitch Forums
By following the examples from here, I have enabled the ignore_arg and return_thru_ptr plugins. In my test I now call:

bool tmp;
fsm_Event_Get_Digital_Neutral_Green_ExpectAndReturn(&tmp, COMPLETE_OK);
fsm_Event_Get_Digital_Neutral_Green_IgnoreArg_value();
fsm_Event_Get_Digital_Neutral_Green_ReturnThruPtr_value(&tmp);
TEST_ASSERT_TRUE(tmp);

I don't know how can I control what is supposed to be returned under the "tmp" pointer. In my test the TEST_ASSERT_TRUE(tmp); is always failed (no matter if I assign the tmp value with true or false at declaration time). There is the flaw in my understanding?

Mark Vander Voord

unread,
Apr 9, 2021, 7:31:31 AM4/9/21
to ThrowTheSwitch Forums
Think of ReturnThruPtr as another queue. You're pushing data into the queue when you call ReturnThruPtr... and the value is getting pulled back out of the queue when the mock is actually called and is being copied into the argument. (This works just like return values, but for an argument instead).

Therefore if you want the value to be true the first time it's called and false the second time, you can call:


bool expected[] = { true, false };
fsm_Event_Get_Digital_Neutral_Green_ExpectAndReturn(NULL, COMPLETE_OK);
fsm_Event_Get_Digital_Neutral_Green_IgnoreArg_value();
fsm_Event_Get_Digital_Neutral_Green_ReturnThruPtr_value(&expected[0]);
fsm_Event_Get_Digital_Neutral_Green_ReturnThruPtr_value(&expected[1]);

CallFuncUnderTest()



--
You received this message because you are subscribed to the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this group and stop receiving emails from it, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/92dd318d-d127-4320-9fda-fa55e3fba02an%40googlegroups.com.

Łukasz Przeniosło

unread,
Apr 9, 2021, 7:37:01 AM4/9/21
to ThrowTheSwitch Forums
Hi Mark,
Ok, so my mistake was to supply the fsm_Event_Get_Digital_Neutral_Green_ExpectAndReturn function with the actual tmp pointer... When I refactored the test code to:

fsm_Event_Get_Digital_Neutral_Green_ExpectAndReturn(0, COMPLETE_OK);
fsm_Event_Get_Digital_Neutral_Green_IgnoreArg_value();
bool tmp = true;
fsm_Event_Get_Digital_Neutral_Green_ReturnThruPtr_value(&tmp);
TEST_ASSERT_TRUE(tmp);

The TEST_ASSERT_TRUE(tmp); statement is passed (and failed if bool tmp = false;).

Thank you!

Mark Vander Voord

unread,
Apr 9, 2021, 7:41:12 AM4/9/21
to ThrowTheSwitch Forums
You can use still pass the original pointer to the expect if you want... it's ignored, so it doesn't really matter what you pass to it. I just use a null to remind myself I'm ignoring it.

The important part was assigning tmp beforehand. :)

The TEST_ASSERT_TRUE isn't really doing anything for your test... as the ReturnThruPtr is part of the expectation, it really doesn't need to be checked with an assertion... just used by the unit under test.

Łukasz Przeniosło

unread,
Apr 9, 2021, 7:47:47 AM4/9/21
to throwth...@googlegroups.com, Mark Vander Voord
Hi Mark,
Ok, thank you for the clarification on the expect. As for the TEST_ASSERT_TRUE i fully agree with you- I know from the beginning what is going to be returned, this was just a sabity check for myself whether the test code is working as supposed to.

--
Pozdrawiam / Best Regards,
Łukasz Przeniosło

You received this message because you are subscribed to a topic in the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/throwtheswitch/m0xG3LD1nT8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/CAAu8-XGLugCgkzLKt01v99hoUQrrq50gw4Kp%3D3jP-1FXXGjMGQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages