Search code examples
cunit-testingcmakelinker-flagscmocka

set_target_properties does not work with multiple --wrap functions


#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <cmocka.h>
#include "stdint.h"

typedef enum {
    eSTATE_STARTUP,
    eSTATE_ADDRESSING,
    eSTATE_RANDOMISE,
    eSTATE_SELECTED
}e_State_t;

void test_main()
{
    static e_State_t e_state = eSTATE_STARTUP;

    if(get_rx_dali_flag())
      {
          application_process(&e_state);
      }

      switch(e_state)
      {
          case eSTATE_STARTUP:
          {
              set_led_frequency(0);
              break;
          }
          case eSTATE_ADDRESSING:
          {
              set_led_frequency(1);
              break;
          }
          case eSTATE_RANDOMISE:
          {
              set_led_frequency(10);
              break;
          }
          case eSTATE_SELECTED:
          {
              set_led_frequency(100);
              break;
          }
          default:
              break;
      }
}



int __wrap_get_rx_dali_flag(void)
{
    int data_available = mock_type(uint8_t);
    return data_available;
}

void __wrap_application_process(e_State_t * ptr_state)
{   
    check_expected_ptr(ptr_state);
    *ptr_state = mock_type(e_State_t);
}

void __wrap_set_led_frequency(uint16_t frequency)
{
    assert_int_equal(frequency, 1);
}



void main_should_setLedFreq_1_when_stateADDRESSING()
{
    will_return(__wrap_get_rx_dali_flag, 1);
    will_return(__wrap_application_process, eSTATE_ADDRESSING);

    test_main();
}



int main()
{
    const struct CMUnitTest tests[] =
    {
        cmocka_unit_test(main_should_setLedFreq_1_when_stateADDRESSING),
    };

    return cmocka_run_group_tests(tests, NULL, NULL);
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
    
SET(CMAKE_C_COMPILER "C:/msys64/mingw64/bin/gcc.exe")
SET(GCC_COVERAGE_COMPILE_FLAGS "-g")
    
project(beTheLight-unit-tests VERSION 1.0 LANGUAGES C)

include(cmake/FetchCMocka.cmake)

add_executable(beTheLight-unit-tests main.c)
target_compile_features(beTheLight-unit-tests PRIVATE c_std_99)
target_link_libraries(beTheLight-unit-tests PRIVATE cmocka-static)

enable_testing()
add_test(NAME beTheLight-unit-tests COMMAND beTheLight-unit-tests)

set_target_properties(beTheLight-unit-tests PROPERTIES LINK_FLAGS "-Wl, --wrap=get_rx_dali_flag, --wrap=set_led_frequency, --wrap=application_process")

==============================================================

 [ 50%] Linking C executable beTheLight-unit-tests.exe
    gcc.exe: error: unrecognized command-line option '--wrap=get_rx_dali_flag,'
    gcc.exe: error: unrecognized command-line option '--wrap=set_led_frequency,'
    gcc.exe: error: unrecognized command-line option '--wrap=application_process'
    make[2]: *** [beTheLight-unit-tests.exe] Error 1
    make[1]: *** [CMakeFiles/beTheLight-unit-tests.dir/all] Error 2
    make: *** [all] Error 2

I am trying to get cmocka unit testing to work but for some reason the linker does not accept multiple --wrap linker flags. I have seen people doing it this way at another post in stackoverflow. I am using gcc version 10.3.0 built by MSYS2 project on win10.


Solution

  • Just use:

    add_executable(beTheLight-unit-tests main.c)
    target_link_options(beTheLight-unit-tests PUBLIC
       -Wl,--wrap=get_rx_dali_flag
       -Wl,--wrap=set_led_frequency
       -Wl,--wrap=application_process
    )
    

    on older cmake use target_link_libraries. No need for set_target_properties.