0

Let's say I want to have 2 modules, which are loosely coupled. The first module is depending on the interface of the other module. I would like to decouple them by introducing defines for the interfaces.

In module1_cfg.h

#define FUNC1_MODULE2(x) Func1_module2_impl(x)

In module1.c I would use this define to access the other module.

if(TRUE != FUNC1_MODULE2(arg))
{
    //do something
}

In module2.c then I would implement this function:

BOOL Func1_module2_impl(uint8_t i)
{
   //do something
}

This decoupling strategy works really well until I would like to write Unit Tests for module1. If I want to write my Unit Tests with Hippomocks they would look like this:

void test_func1_module2_ok()
{ 
    MockRepository mocks;
    mocks.ExpectCallFunc(Func2_module2_impl).Return(TRUE);
    ...
}

But now my unit tests for module1 are depending on module2. Would there be a possibility to use the introduced function like macro (FUNC1_MODULE2) in the unit test, but with some macro magic omit the parenthesis, so I can pass the defined function pointer to hippomocks? I would like to stick to function like macros and avoid object macros in the real code to redefine functions, but would use any tricks necessary in the unit test to get rid of the parenthesis.

Thank you for your help!

Yaxley
  • 1
  • Why don't you just use `#define FUNC1_MODULE2 Func1_module2_impl` so that you can use that also in your unit test? – the busybee Feb 26 '20 at 11:01
  • Why defines? Wouldn't it be simpler to just take function pointers? With function pointers and a proper vtable you can easily write a mock of one of the modules. `BOOL` `TRUE` why not `bool` and `true`? – KamilCuk Feb 26 '20 at 11:06

0 Answers0