Skip to content

Commit

Permalink
Add and use add_boost_test CMake helper function (#2545)
Browse files Browse the repository at this point in the history
Allow adding unit test in a single line, avoiding repetition of CMake
targets.

TODO 
- [x] Add optional argument for `target_include_directories`
- [ ] Allow more labels / directories (IDE specific) ?
- [x] Use `add_boost_test` in place of `add_executable` where needed
This pull request includes significant changes to the CMake build system
for various tests, primarily focusing on simplifying and standardizing
the test definitions by introducing a new macro. The main changes
involve replacing existing test setup code with a new `add_boost_test`
function, which consolidates the test setup into a single line.
  • Loading branch information
flomnes authored Dec 20, 2024
1 parent 03508de commit 212167b
Show file tree
Hide file tree
Showing 35 changed files with 398 additions and 976 deletions.
40 changes: 10 additions & 30 deletions src/tests/end-to-end/binding_constraints/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
enable_testing()

#bigobj support needed for windows compilation
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif(MSVC)

add_executable(tests-binding_constraints
test_binding_constraints.cpp
)

target_link_libraries(tests-binding_constraints
PRIVATE
Boost::unit_test_framework
model_antares
antares-solver-simulation
antares-solver-hydro
antares-solver-ts-generator
Antares::tests::in-memory-study
)

target_include_directories(tests-binding_constraints
PRIVATE
${CMAKE_SOURCE_DIR}/solver
)

add_test(NAME end-to-end-binding_constraints COMMAND tests-binding_constraints)
set_property(TEST end-to-end-binding_constraints PROPERTY LABELS end-to-end)
set_target_properties(tests-binding_constraints PROPERTIES FOLDER Unit-tests/end_to_end)
include(${CMAKE_SOURCE_DIR}/tests/macros.cmake)

add_boost_test(tests-binding_constraints
SRC test_binding_constraints.cpp
LIBS
model_antares
antares-solver-simulation
antares-solver-hydro
antares-solver-ts-generator
Antares::tests::in-memory-study)
46 changes: 11 additions & 35 deletions src/tests/end-to-end/simple_study/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
enable_testing()

#bigobj support needed for windows compilation
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif(MSVC)

add_executable(tests-simple-study
simple-study.cpp
)

target_link_libraries(tests-simple-study
PRIVATE
antares-solver-hydro
antares-solver-variable
antares-solver-simulation
antares-solver-ts-generator
model_antares
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
Antares::tests::in-memory-study
)

target_include_directories(tests-simple-study
PRIVATE
${CMAKE_SOURCE_DIR}/solver
)

add_test(NAME end-to-end-simple-study COMMAND tests-simple-study)
set_property(TEST end-to-end-simple-study PROPERTY LABELS end-to-end)
set_target_properties(tests-simple-study PROPERTIES FOLDER Unit-tests/end_to_end)

# Storing tests-simple-study under the folder Unit-tests in the IDE

#----------------------------------------------------------
include(${CMAKE_SOURCE_DIR}/tests/macros.cmake)

add_boost_test(tests-simple-study
SRC simple-study.cpp
LIBS
antares-solver-hydro
antares-solver-variable
antares-solver-simulation
antares-solver-ts-generator
model_antares
Antares::tests::in-memory-study)
38 changes: 38 additions & 0 deletions src/tests/macros.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# The following function allows to add a test in a single line
# Arguments
# SRC path to the sources
# (optional) LIBS path to the libs to link
# (optional) INCLUDE include paths
# NOTE it's not necessary to add Boost::unit_test_framework

function(add_boost_test)
set(options "")
set(oneValueArgs)
set(multiValueArgs SRC LIBS INCLUDE)
cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}" "${oneValueArgs}" "${multiValueArgs}")
# Bypass cmake_parse_arguments for the 1st argument
set(TEST_NAME ${ARGV0})
add_executable(${TEST_NAME} ${arg_SRC})
# All tests use boost
target_link_libraries(${TEST_NAME} PRIVATE ${arg_LIBS} Boost::unit_test_framework)

# Optional: add private include directories
if (NOT "${arg_INCLUDE}" STREQUAL "")
target_include_directories(${TEST_NAME} PRIVATE ${arg_INCLUDE})
endif()

add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})

# Adding labels allows ctest filter what tests to run
set_property(TEST ${TEST_NAME} PROPERTY LABELS unit)

# Give the IDE some directions to display tests in a "Unit-tests" folder
set_target_properties(${TEST_NAME} PROPERTIES FOLDER Unit-tests)

# Linux only. TODO remove ?
if(UNIX AND NOT APPLE)
target_link_libraries(${TEST_NAME} PRIVATE stdc++fs)
endif()

endfunction()
39 changes: 11 additions & 28 deletions src/tests/src/api_internal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
set(EXECUTABLE_NAME test-api)
add_executable(${EXECUTABLE_NAME})

target_sources(${EXECUTABLE_NAME}
PRIVATE
test_api.cpp
)

target_link_libraries(${EXECUTABLE_NAME}
PRIVATE
Boost::unit_test_framework
Antares::solver_api
Antares::tests::in-memory-study
test_utils_unit
)

target_include_directories(${EXECUTABLE_NAME}
PRIVATE
#Allow to use the private headers
$<TARGET_PROPERTY:Antares::solver_api,INCLUDE_DIRECTORIES>
)

# Storing tests-ts-numbers under the folder Unit-tests in the IDE
set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests)

add_test(NAME test-api COMMAND ${EXECUTABLE_NAME})

set_property(TEST test-api PROPERTY LABELS unit)
include(${CMAKE_SOURCE_DIR}/tests/macros.cmake)

add_boost_test(test-api
SRC test_api.cpp
LIBS
Antares::solver_api
Antares::tests::in-memory-study
test_utils_unit
#Allow to use the private headers
INCLUDE
$<TARGET_PROPERTY:Antares::solver_api,INCLUDE_DIRECTORIES>)
23 changes: 4 additions & 19 deletions src/tests/src/api_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
set(EXECUTABLE_NAME test-client-api)
add_executable(${EXECUTABLE_NAME})
include(${CMAKE_SOURCE_DIR}/tests/macros.cmake)

target_sources(${EXECUTABLE_NAME}
PRIVATE
test_api.cpp
)

target_link_libraries(${EXECUTABLE_NAME}
PRIVATE
Boost::unit_test_framework
Antares::solver_api
)

# Storing tests-ts-numbers under the folder Unit-tests in the IDE
set_target_properties(${EXECUTABLE_NAME} PROPERTIES FOLDER Unit-tests)

add_test(NAME test-client-api COMMAND ${EXECUTABLE_NAME})

set_property(TEST test-client-api PROPERTY LABELS unit)
add_boost_test(test-client-api
SRC test_api.cpp
LIBS Antares::solver_api)
143 changes: 49 additions & 94 deletions src/tests/src/libs/antares/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,108 +18,63 @@ set(SRC_MATRIX_LIB

# Necessary cpp files
${src_libs_antares}/jit/jit.cpp
logs/logs.cpp
)
logs/logs.cpp)

add_library(matrix ${SRC_MATRIX_LIB})

target_link_libraries(matrix
PUBLIC
yuni-static-core
)
yuni-static-core)

target_include_directories(matrix
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/logs"
"${src_libs_antares}/jit/include"
)

# Storing lib-matrix under the folder Unit-tests in the IDE
set_target_properties(matrix PROPERTIES FOLDER Unit-tests)
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/logs"
"${src_libs_antares}/jit/include")

# Building tests on Matrix save operations
set(SRC_TEST_MATRIX_SAVE
logs/antares/logs/logs.h
array/fill-matrix.h
array/matrix-bypass-load.h
array/tests-matrix-save.h
array/tests-matrix-save.cpp
)

add_executable(tests-matrix-save ${SRC_TEST_MATRIX_SAVE})

target_include_directories(tests-matrix-save
PRIVATE
"${src_libs_antares}/array/include"
"${src_libs_antares}/io/include"
"${src_libs_antares}/jit/include"
"${src_libs_antares}/memory/include"
"${CMAKE_CURRENT_SOURCE_DIR}/logs"
"${CMAKE_CURRENT_SOURCE_DIR}/jit"
"${CMAKE_SOURCE_DIR}/tests/src/libs"

)

target_link_libraries(tests-matrix-save
PRIVATE
matrix
yuni-static-core
Boost::unit_test_framework
antares-core
)

# Storing tests-matrix-save under the folder Unit-tests in the IDE
set_target_properties(tests-matrix-save PROPERTIES FOLDER Unit-tests)

add_test(NAME save-matrix COMMAND tests-matrix-save)

set_property(TEST save-matrix PROPERTY LABELS unit)
add_boost_test(tests-matrix-save
SRC
logs/antares/logs/logs.h
array/fill-matrix.h
array/matrix-bypass-load.h
array/tests-matrix-save.h
array/tests-matrix-save.cpp
INCLUDE
"${src_libs_antares}/array/include"
"${src_libs_antares}/io/include"
"${src_libs_antares}/jit/include"
"${src_libs_antares}/memory/include"
"${CMAKE_CURRENT_SOURCE_DIR}/logs"
"${CMAKE_CURRENT_SOURCE_DIR}/jit"
"${CMAKE_SOURCE_DIR}/tests/src/libs"
LIBS
matrix
yuni-static-core
antares-core)

# Building tests on Matrix load operations
set(SRC_TEST_MATRIX_LOAD
array/fill-matrix.h
array/matrix-bypass-load.h
array/tests-matrix-load.h
array/tests-matrix-load.cpp
)

add_executable(tests-matrix-load ${SRC_TEST_MATRIX_LOAD})
target_include_directories(tests-matrix-load
PRIVATE
"${src_libs_antares}/array/include"
"${src_libs_antares}/io/include"
"${src_libs_antares}/jit/include"
"${src_libs_antares}/memory/include"
"${CMAKE_CURRENT_SOURCE_DIR}/logs"
"${CMAKE_CURRENT_SOURCE_DIR}/jit"
"${CMAKE_SOURCE_DIR}/tests/src/libs"
)


target_link_libraries(tests-matrix-load
PRIVATE
matrix
yuni-static-core
Boost::unit_test_framework
antares-core
)

# Storing tests-matrix-load under the folder Unit-tests in the IDE
set_target_properties(tests-matrix-load PROPERTIES FOLDER Unit-tests)

add_test(NAME load-matrix COMMAND tests-matrix-load)

set_property(TEST load-matrix PROPERTY LABELS unit)


add_executable(test-utils test_utils.cpp)
target_link_libraries(test-utils
PRIVATE
Boost::unit_test_framework
Antares::utils
yuni-static-core
)
set_target_properties(test-utils PROPERTIES FOLDER Unit-tests/test-utils)

add_test(NAME test-utils COMMAND test-utils)
set_property(TEST test-utils PROPERTY LABELS unit)
add_boost_test(tests-matrix-load
SRC
array/fill-matrix.h
array/matrix-bypass-load.h
array/tests-matrix-load.h
array/tests-matrix-load.cpp
INCLUDE
"${src_libs_antares}/array/include"
"${src_libs_antares}/io/include"
"${src_libs_antares}/jit/include"
"${src_libs_antares}/memory/include"
"${CMAKE_CURRENT_SOURCE_DIR}/logs"
"${CMAKE_CURRENT_SOURCE_DIR}/jit"
"${CMAKE_SOURCE_DIR}/tests/src/libs"
LIBS
matrix
yuni-static-core
antares-core)

# Test utilities
add_boost_test(test-utils
SRC test_utils.cpp
LIBS
Antares::utils
yuni-static-core)
25 changes: 7 additions & 18 deletions src/tests/src/libs/antares/antlr4-interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
include(${CMAKE_SOURCE_DIR}/tests/macros.cmake)

find_package(antlr4-runtime CONFIG REQUIRED)
Set(SRCS test_antlr_interface.cpp
)

set(execname "antlr-interface-test")
add_executable(${execname} ${SRCS})
target_link_libraries(${execname}
PRIVATE
antlr-interface
Boost::unit_test_framework
)


target_include_directories(${execname}
PRIVATE
${ANTLR4_INCLUDE_DIR})
add_test(NAME antlr-interface COMMAND ${execname})

set_tests_properties(antlr-interface PROPERTIES LABELS unit)
add_boost_test(antlr-interface-test
SRC test_antlr_interface.cpp
LIBS
antlr-interface
INCLUDE
${ANTLR4_INCLUDE_DIR})
18 changes: 4 additions & 14 deletions src/tests/src/libs/antares/benchmarking/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
set(PROJ test-duration-collector)
include(${CMAKE_SOURCE_DIR}/tests/macros.cmake)

add_executable(${PROJ})
target_sources(${PROJ} PRIVATE test_duration_collector.cpp)
target_link_libraries(${PROJ}
PRIVATE
Antares::benchmarking
Boost::unit_test_framework
)

set_target_properties(${PROJ} PROPERTIES FOLDER Unit-tests/${PROJ})

add_test(NAME ${PROJ} COMMAND ${PROJ})

set_property(TEST ${PROJ} PROPERTY LABELS unit)
add_boost_test(test-duration-collector
SRC test_duration_collector.cpp
LIBS Antares::benchmarking)
Loading

0 comments on commit 212167b

Please sign in to comment.