Open dkDeman opened 11 months ago
Its not a bug, its bad coding from your side.
Rather then using ifdef-else clause use define for WEAK
and then once its defined __attribute (( weak ))__
and other time as weak
or in unit test case as empty. You will have to add some strippables
in this case (you could do it in yourcase as well).
Secondly, you are mixing inline implementation of the functions with declaration. I would propose that you split declarations (in gpio.h
) and then put gpio_inline_impl.h
for implementation/definition. Then you can conditionally include gpio_inline_impl.h
with #ifndef TEST
to enable the inline functions when not running in test mode and to get mocks.
Maybe you are rigth, but the sample has been built on basic the zephyr spi.h file for spi_transceive_stats
function.
#if defined(CONFIG_SPI_STATS)
//code
static inline void spi_transceive_stats(const struct device *dev, int error,
const struct spi_buf_set *tx_bufs,
const struct spi_buf_set *rx_bufs)
{
// code
}
#else /*CONFIG_SPI_STATS*/
#define spi_transceive_stats(dev, error, tx_bufs, rx_bufs)
#endif /*CONFIG_SPI_STATS*/
We can't change the zephyr source code.
If you're using Ceedling, you can enable preprocessing to preprocess your files before they get mocked. This solves most of these kinds of issues.
The root problem, that CMock doesn't fully parse C code with full preprocessing capabilities, etc, is a known issue and is one we mean to address. I apologize that it's still an issue after this long.
Sometimes developer use the macros to include/exclude the functions in the source code, like as:
gpio.h
If
GPIO_BLA_BLA
is defined thengpio_test
function should be used in the source code. IfGPIO_BLA_BLA
is NOT defined thengpio_test
function should NOT be used in the source code.The cmock functions are generated for all types in the header file regardless of macros. If we run unit test for modules which uses gpio driver, then cmock function is generated for
gpio_test
function even ifGPIO_BLA_BLA
is not defined.mock_gpio.h:
mock_gpio.c:
Let's try to build it. Since
GPIO_BLA_BLA
is not defined then compiler replace thegpio_test
string to the empty place (please see gpio.h file), as result we will get the following error:If we comment out the "#define gpio_test" line in the gpio.h file than it is working.
gpio.h
To fix it, CMock should add the GPIO_BLA_BLA to the mock_gpio.c: file, like as:
Or, CMock should not generate the cmock functions for
gpio_test
function ifGPIO_BLA_BLA
is not defined.All source files: src.zip