Skip to content
Snippets Groups Projects
Commit a66da626 authored by Sergey Kosukhin's avatar Sergey Kosukhin Committed by Yen-Chen Chen
Browse files

Update gtest_helper.cmake (!38)


This updates the `add_icon_c_test` macro in `cmake/gtest_helper.cmake`:
1. The macro is turned into a function to avoid namespace pollution.
2. The function is renamed to `fs_add_c_test`.
3. The function now accepts named options `SOURCES` (followed by a list of source files of the test) and `ARGS` (followed by a list of command-line arguments for the test).
4. The old signature of the macro is still supported, i.e. it is possible to call something like `add_c_test(UtilHashTest ctest_util_hash.cpp)`, but it made sense to me to switch to the new one.
5. The `UtilFileTest` test is now provided with an extra command-line argument and it runs on `${CMAKE_CURRENT_BINARY_DIR}` instead of `.` as it was before !28.

Co-authored-by: default avatarYen-Chen Chen <yen-chen.chen@kit.edu>
Approved-by: default avatarTerry Cojean <terry.cojean@kit.edu>
Merged-by: default avatarYen-Chen Chen <yen-chen.chen@kit.edu>
Changelog: other
parent 2ba50116
No related branches found
No related tags found
1 merge request!38Update gtest_helper.cmake
Pipeline #74114 passed
......@@ -10,23 +10,31 @@
# ---------------------------------------------------------------
# cmake-format: off
# add_icon_c_test(<test>
# <source>)
# fs_add_c_test(<test_name>
# [SOURCES <sources>]
# [ARGS <args>])
# cmake-format: on
# -----------------------------------------------------------------------------
# Compiles a test executable with the name <test> using the source code
# <source>. The googletest and fortran-support libraries will be linked
# automatically.
# Compiles a test executable with the name <test_name> using the source code
# <source>. Specify ctest arguments in <args> if necessary. The googletest and
# libfortran-support libraries will be linked automatically.
#
# The C++ standard is set to C++17.
#
macro(add_icon_c_test test_name file_name)
add_executable("CTest_${test_name}" ${file_name})
function(fs_add_c_test test_name)
cmake_parse_arguments(PARSE_ARGV 1 ARG "" "" "SOURCES;ARGS")
if(NOT ARG_SOURCES)
set(ARG_SOURCES "${ARG_UNPARSED_ARGUMENTS}")
endif()
add_executable("CTest_${test_name}" ${ARG_SOURCES})
target_link_libraries(
"CTest_${test_name}" PRIVATE fortran-support::fortran-support
GTest::gtest_main)
add_test(NAME "CTest_${test_name}" COMMAND "CTest_${test_name}")
GTest::gtest_main stdc++fs)
add_test(NAME "CTest_${test_name}" COMMAND "CTest_${test_name}" ${ARG_ARGS})
set_property(TEST "CTest_${test_name}" PROPERTY LABELS C)
set_target_properties("CTest_${test_name}"
PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
endmacro()
endfunction()
......@@ -44,7 +44,7 @@ endif()
include("${PROJECT_SOURCE_DIR}/cmake/gtest_helper.cmake")
if(FS_ENABLE_BACKTRACE_TEST)
add_icon_c_test(UtilBacktraceTest ctest_util_backtrace.cpp)
fs_add_c_test(UtilBacktraceTest SOURCES ctest_util_backtrace.cpp)
endif()
file(
......@@ -71,17 +71,18 @@ file(
" floating_points -7.1, 1.0, 3.7, 8.9\n"
" booleans .TRUE., .FALSE., .true.\n"
" chars 'unit', 'test'\n")
add_icon_c_test(UtilNmlAnnotateTest ctest_nml_annotate.cpp)
add_icon_c_test(UtilArithmeticExprTest ctest_util_arithmetic_expr.cpp)
add_icon_c_test(UtilHashTest ctest_util_hash.cpp)
add_icon_c_test(UtilStrideTest ctest_util_stride.cpp)
add_icon_c_test(UtilStringParseTest ctest_util_string_parse.cpp)
add_icon_c_test(UtilSysinfoTest ctest_util_sysinfo.cpp)
add_icon_c_test(UtilSystemTest ctest_util_system.cpp)
add_icon_c_test(UtilTimerTest ctest_util_timer.cpp)
fs_add_c_test(UtilNmlAnnotateTest SOURCES ctest_nml_annotate.cpp)
fs_add_c_test(UtilArithmeticExprTest SOURCES ctest_util_arithmetic_expr.cpp)
fs_add_c_test(UtilHashTest SOURCES ctest_util_hash.cpp)
fs_add_c_test(UtilStrideTest SOURCES ctest_util_stride.cpp)
fs_add_c_test(UtilStringParseTest SOURCES ctest_util_string_parse.cpp)
fs_add_c_test(UtilSysinfoTest SOURCES ctest_util_sysinfo.cpp)
fs_add_c_test(UtilSystemTest SOURCES ctest_util_system.cpp)
fs_add_c_test(UtilTimerTest SOURCES ctest_util_timer.cpp)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/util_file_test.txt"
"This is a test file for unit tests for util_file.c\n")
file(CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/util_file_test.txt"
"${CMAKE_CURRENT_BINARY_DIR}/util_file_link.txt" SYMBOLIC)
add_icon_c_test(UtilFileTest ctest_util_file.cpp)
fs_add_c_test(UtilFileTest SOURCES ctest_util_file.cpp ARGS
"${CMAKE_CURRENT_BINARY_DIR}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment