ThrowTheSwitch / CMock

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

How to not mock functions inside a guard? #176

Open metc opened 6 years ago

metc commented 6 years ago

Hi,

I have a similar header file as the example below:

#ifdef USE_API
#include "api.h"
#endif

void function(void);

#ifdef USE_API
// Here I need to include "api.h" file. I would like to include this file only when needed.
void function2(api_t api);
#endif

Using a define, I want to expose more functions to my existing API. I would like to include the required header file only if those functions are enabled.

Observation: It seems CMock ignores the "#ifdef USE_API" guard and always generates a mock of function2. Workaround: The "api.h" file has to be included (without the guard) or my unit test will not compile.

Is there a way to tell to CMock to ignore function2 when a define is set?

Thank you in advance for your help.

Letme commented 6 years ago

Just run above file through C pre-processor and you will get all the defines nicely defined. Then put that file to CMock.

Or just use Ceedling for building.

Xunnamius commented 6 years ago

Also referenced here: https://github.com/ThrowTheSwitch/CMock/issues/126

metc commented 6 years ago

@Letme we have a quite complex tools/flow to do CMock and run our unit-tests. I could add a step to run the C processor first. I was hoping the processor was part of CMock. @Xunnamius thanks for the link. Looking forward for the next version!