Search code examples
cembeddedgoogletest

GoogleTest force #undef on particular test


I use GoogleTest, and I want to test some functions with #ifndef inside.

file a.c

bool myFunction() {
#ifndef FOO
   return true;
#else   
   return false;
#endif
}

Is it possible to force an #undef during a particular test ? Like that I can test the function in the 2 stats (with the define and without).


Solution

  • This is one of the reasons why people try to avoid having multiple versions of a function that you create with #define, #if, etc. It’s hard to test all these different versions.

    If you want to test both versions, you must compile your program twice (one with #define FOO and once without), and then run the tests separately. The only way to change the value of FOO, as written, is to recompile the program.

    Alternatively, you can refactor your code so the #define is unnecessary.