Skip to content

Ut

Note: auto-generated from comments in: ./target/ut.cmake

target/ut.cmake:

UTs target implementation.

Function _ut_setup_clean_file:

Setup a file that cleans out *.gcda files before running tests. This is run before testing as registered through TEST_INCLUDE_FILES.

ut_add_global_target:

Implementation defines the target using add_custom_target and nothing more.

function(ut_add_deployment_target MODULE TARGET SOURCES DEPENDENCIES FULL_DEPENDENCIES)

# For the deployment augment the tests in this directory to include the recursive set tests and write to the test files
set(DEPLOYMENT_TESTS)
foreach(DEPENDENCY IN LISTS FULL_DEPENDENCIES)
    get_property(DEPENDENCY_UNIT_TESTS TARGET ${DEPENDENCY} PROPERTY FPRIME_UNIT_TESTS)
    if(DEPENDENCY_UNIT_TESTS)
        foreach(UNIT_TEST IN LISTS DEPENDENCY_UNIT_TESTS)
            fprime_util_metadata_add_test("${UNIT_TEST}")
            list(APPEND DEPLOYMENT_TESTS "${UNIT_TEST}")
        endforeach()
    endif()
endforeach()
add_custom_target("${MODULE}_${FPRIME__INTERNAL_UT_TARGET}")
# Add dependencies if tests have been defined
if (DEPLOYMENT_TESTS)
    add_dependencies("${MODULE}_${FPRIME__INTERNAL_UT_TARGET}" ${DEPLOYMENT_TESTS})
endif()
fprime_util_metadata_add_build_target("${MODULE}_${FPRIME__INTERNAL_UT_TARGET}")

endfunction(ut_add_deployment_target)

function(ut_setup_unit_test_include_directories UT_EXE_NAME SOURCE_FILES)

set(UT_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}")
# When running with auto-helpers, we need to include the .hpp directories as things are imported without path
# e.g. "#include <Tester.hpp>" and there is no guarantee for the location of these files
get_target_property(UT_AUTO_HELPERS "${UT_EXE_NAME}" FPRIME_UT_AUTO_HELPERS)
if (DEFINED UT_AUTO_HELPERS AND UT_AUTO_HELPERS)
    foreach(SOURCE_FILE IN LISTS SOURCE_FILES)
        get_filename_component(SOURCE_EXT "${SOURCE_FILE}" LAST_EXT)
        get_filename_component(SOURCE_DIR "${SOURCE_FILE}" DIRECTORY)
        if (SOURCE_EXT STREQUAL ".cpp" AND NOT SOURCE_DIR IN_LIST UT_INCLUDE_DIRECTORIES)
            list(APPEND UT_INCLUDE_DIRECTORIES "${SOURCE_DIR}")
        endif()
    endforeach()
endif()
target_include_directories("${UT_EXE_NAME}" PRIVATE ${UT_INCLUDE_DIRECTORIES})

endfunction(ut_setup_unit_test_include_directories)

function(ut_executable_build UT_EXECUTABLE_TARGET)

# Run the autocoders and set up the standard build properties on the executable target
run_ac_set("${UT_EXECUTABLE_TARGET}" autocoder/fpp autocoder/fpp_ut)
fprime__internal_standard_build_target_setup("${UT_EXECUTABLE_TARGET}" "-ut")
target_link_libraries("${UT_EXECUTABLE_TARGET}" PUBLIC gtest_main)

# Automatic dependency if possible
is_target_library(IS_LIBRARY "${FPRIME_CURRENT_MODULE}")
if (IS_LIBRARY)
    target_link_libraries("${UT_EXECUTABLE_TARGET}" PUBLIC "${FPRIME_CURRENT_MODULE}")
endif()
# Automatic include directories
ut_setup_unit_test_include_directories("${UT_EXECUTABLE_TARGET}" "${SOURCE_FILES}")

endfunction(ut_executable_build)

function(ut_add_ctest UT_EXECUTABLE_TARGET)

# Add a CTEST which will run the clean script, and the test within the current source directory
set_property(DIRECTORY APPEND PROPERTY
    TEST_INCLUDE_FILES "${FPRIME__INTERNAL_UT_CLEAN_SCRIPT}"
)
add_test(NAME ${UT_EXECUTABLE_TARGET} COMMAND ${UT_EXECUTABLE_TARGET} WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
fprime_util_metadata_add_test("${UT_EXECUTABLE_TARGET}")

endfunction(ut_add_ctest)

function(ut_add_module_target UT_EXECUTABLE_TARGET TARGET_NAME SOURCE_FILES DEPENDENCIES)

get_target_property(FPRIME_TYPE "${UT_EXECUTABLE_TARGET}" FPRIME_TYPE)
# Protects against multiple calls to fprime_register_ut()
if (NOT BUILD_TESTING OR NOT FPRIME_TYPE STREQUAL "Unit Test")
    return()
endif()

# Set up the executable
ut_executable_build("${UT_EXECUTABLE_TARGET}")

# Add the CTEST
ut_add_ctest("${UT_EXECUTABLE_TARGET}")

# Register the test to the tested module, assuming "current module" unless previously specified
get_target_property(FPRIME_TESTED_MODULE "${UT_EXECUTABLE_TARGET}" FPRIME_TESTED_MODULE)
if (NOT FPRIME_TESTED_MODULE)
    set(FPRIME_TESTED_MODULE "${FPRIME_CURRENT_MODULE}")
endif()
if (TARGET "${FPRIME_TESTED_MODULE}")
    append_list_property("${UT_EXECUTABLE_TARGET}" TARGET "${FPRIME_TESTED_MODULE}" PROPERTY FPRIME_UNIT_TESTS)
endif()

# Module introspection when in debug mode
if (CMAKE_DEBUG_OUTPUT)
    introspect("${UT_EXECUTABLE_TARGET}")
endif()

endfunction(ut_add_module_target)