#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
我试图让 cmocka 单元测试工作,但由于某种原因,链接器不接受多个 --wrap 链接器标志。我在 stackoverflow 的另一篇文章中看到有人这样做。我在win10上使用由MSYS2项目构建的gcc 10.3.0版本。