Is there a means of redefining free functions?

I'm writing some unit tests for some extension methods, and some of the other extension methods call each other. The only problem is my fake extension methods override the real ones when I need them.

The mocks are implemented in the form of:

1
2
3
4
5
6
7
namespace StringManipulation
{
    FastString Func1(FastString& str, const char* chr)
    {
        mockStringManipulation.Func1(str, chr);
    }
}


Once I add a mock for a test I lose the ability to test that method, unless I comment the mock out. Is there some type of preprocessor hack or something I can do with CMake to keep both implementations around? Maybe, a function pointer?
Last edited on
yes,
you seek preprocessor, look up an example of #ifdef -- it forms an if/else on which code is in play based off project level defines (like how debug define works etc). Its used a bit for cross platform when you need something from an OS that could be win or unix or whatnot.

you can also swap out which .cpp file you #include, have one full of stubs and the other real.. depends on which is easier for you to deal with.

you could use a function pointer. But how do you know what to set it to? You could use a command line arg for that, and have a setup in main that unravels it. This seems like a complicated way to do it. THIS way of doing it would avoid a recompile, the other 2 above are per-compile.

on that note, you can also swap out dynamic libraries. also seems complicated, have to rename the files each time, which you can script up.
Last edited on
Topic archived. No new replies allowed.