I want to do this…
#if IS_TESTING
#define MOCKABLE virtual
#else #define MOCKABLE
#endif
…but in CMake.
I have two executables defined in CMake, MAIN and TEST. I have tried doing this:
MAIN CMake file:
target_compile_definitions(${CMAKE_PROJECT_NAME}
MOCKABLE=
)
TEST CMake file:
target_compile_definitions(${TEST_PROJECT_NAME}
MOCKABLE=virtual
)
The TEST version works, but in MAIN, MOCKABLE
expands to “” and so it won’t compile. Is it possible to define it somehow so that MOCKABLE
expands to nothing, like with a regular C or C++ macro?
In case it isn’t obvious, my overall goal is for unit testing, so I can do something like this:
class A
{
MOCKABLE void go() { ... }
}
…so that go()
is virtual inside the testing executable, and non-virtual inside the main executable. The macro code at the beginning of this post works perfectly well, but I’d prefer to do it in CMake so that I don’t have to worry about headers.