Testing module

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-04-29 22:49:33 +03:00
1 parent 4fc129fb7e
commit e7186f17f0
42 files changed
+1816 -413

No files matched your search

+4
View File
@@ -1,6 +1,10 @@
include(FetchContent)
function(void_fetch_dependency DEP_NAME)
if(DEP_NAME STREQUAL "Boost")
find_package(Boost REQUIRED COMPONENTS stacktrace_backtrace)
return()
endif()
# FIXME: Add if needed external
# if(DEP_NAME STREQUAL "glaze")
# if(NOT glaze_POPULATED)
+7 -2
View File
@@ -22,9 +22,13 @@ function(void_add_module MODULE_NAME)
else()
set(LIB_TYPE "SHARED")
set(SCOPE "PUBLIC")
add_library(${TGT} SHARED)
if (MODULE_NAME STREQUAL "Testing")
add_library(${TGT} STATIC)
else()
add_library(${TGT} SHARED)
endif()
target_sources(${TGT} PRIVATE ${SRC})
target_compile_features(${TGT} PRIVATE cxx_std_23)
target_compile_features(${TGT} PUBLIC cxx_std_23)
endif()
add_library(${ALIAS} ALIAS ${TGT})
@@ -56,6 +60,7 @@ function(void_add_module MODULE_NAME)
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
EXPORT_NAME ${MODULE_NAME}
)
target_sources(${TGT} ${SCOPE} FILE_SET HEADERS BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${HDR})
target_include_directories(${TGT} ${SCOPE}
+9 -1
View File
@@ -4,11 +4,19 @@ include(CMakeFindDependencyMacro)
set(_void_external_deps "@VOID_EXTERNAL_DEPS_LIST@")
foreach(dep IN LISTS _void_external_deps)
if(NOT TARGET ${dep})
if(dep STREQUAL "Boost")
find_dependency(Boost REQUIRED COMPONENTS stacktrace_backtrace)
elseif(NOT TARGET ${dep})
find_dependency(${dep})
endif()
endforeach()
include("${CMAKE_CURRENT_LIST_DIR}/@EXPORT_SET_NAME@.cmake")
# Auto-load the testing helper so consumers get void_add_tests + the
# auto-scan sweep just by find_package(Void).
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/VoidTesting.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/VoidTesting.cmake")
endif()
check_required_components("@PROJECT_NAME@")
+147
View File
@@ -0,0 +1,147 @@
if(NOT INPUT_FILE OR NOT OUTPUT_FILE OR NOT SUITE_NAME)
message(FATAL_ERROR "INPUT_FILE, OUTPUT_FILE, and SUITE_NAME must be provided.")
endif()
file(STRINGS ${INPUT_FILE} lines)
set(registrations "")
set(in_fixture_class FALSE)
set(fixture_class_name "")
set(fixture_indent "")
set(line_number 0)
# Accumulators for preceding comments
set(current_tags "")
set(current_skip "")
foreach(line IN LISTS lines)
math(EXPR line_number "${line_number} + 1")
# Parse preceding comments for @tag and @skip
string(REGEX MATCH "//[ \t]*@tag[ \t]+(.*)" _tag_match "${line}")
if(_tag_match)
string(REGEX REPLACE ".*@tag[ \t]+" "" _tag_str "${line}")
string(REGEX REPLACE "//.*" "" _tag_str "${_tag_str}")
string(REPLACE " " "" _tag_str "${_tag_str}")
string(REPLACE "," ";" _tag_list "${_tag_str}")
foreach(t ${_tag_list})
list(APPEND current_tags "${t}")
endforeach()
endif()
string(REGEX MATCH "//[ \t]*@skip[ \t]+(.*)" _skip_match "${line}")
if(_skip_match)
string(REGEX REPLACE ".*@skip[ \t]+" "" current_skip "${line}")
string(STRIP "${current_skip}" current_skip)
# Wrap as a C++ string literal unless the user already quoted it.
if(NOT current_skip MATCHES "^\".*\"$")
string(REPLACE "\\" "\\\\" current_skip "${current_skip}")
string(REPLACE "\"" "\\\"" current_skip "${current_skip}")
set(current_skip "\"${current_skip}\"")
endif()
endif()
# Track fixture classes
string(REGEX MATCH "^[ \t]*class[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*:.*Void::Testing::Fixture" _match "${line}")
if(_match)
string(REGEX REPLACE "^[ \t]*class[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*:.*" "\\1" fixture_class_name "${line}")
set(in_fixture_class TRUE)
string(REGEX MATCH "^[ \t]*" fixture_indent "${line}")
endif()
if(in_fixture_class)
string(REGEX MATCH "^${fixture_indent}\\};" _close "${line}")
if(_close)
set(in_fixture_class FALSE)
set(fixture_class_name "")
endif()
endif()
# Match test functions
set(func_name "")
set(return_type "")
set(is_skipped_str "false")
if(in_fixture_class)
string(REGEX MATCH "(Void::Testing::)?(Test|DisabledTest)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\\(\\s*\\)[ \t]*(const)?" _func_match "${line}")
if(_func_match)
string(REGEX REPLACE ".*(Void::Testing::)?(Test|DisabledTest)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\\(\\s*\\).*" "\\3" func_name "${line}")
string(REGEX REPLACE ".*(Void::Testing::)?(Test|DisabledTest)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\\(\\s*\\).*" "\\2" return_type "${line}")
endif()
else()
string(REGEX MATCH "^(Void::Testing::)?(Test|DisabledTest)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\\(\\s*\\)" _func_match "${line}")
if(_func_match)
string(REGEX REPLACE "^(Void::Testing::)?(Test|DisabledTest)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\\(\\s*\\).*" "\\3" func_name "${line}")
string(REGEX REPLACE "^(Void::Testing::)?(Test|DisabledTest)[ \t]+([A-Za-z_][A-Za-z0-9_]*)[ \t]*\\(\\s*\\).*" "\\2" return_type "${line}")
endif()
endif()
if(func_name)
if(return_type STREQUAL "DisabledTest")
set(is_skipped_str "true")
if(NOT current_skip)
set(current_skip "\"disabled\"")
endif()
endif()
if(NOT current_skip)
set(current_skip "\"\"")
endif()
# Build tags C++
set(tags_cpp "{}")
list(LENGTH current_tags num_tags)
if(num_tags GREATER 0)
set(tags_list_cpp "")
foreach(t ${current_tags})
list(APPEND tags_list_cpp "\"${t}\"")
endforeach()
string(REPLACE ";" ", " tags_cpp "${tags_list_cpp}")
set(tags_cpp "{${tags_cpp}}")
endif()
set(safe_func_name "${func_name}")
if(in_fixture_class)
set(safe_func_name "${fixture_class_name}_${func_name}")
endif()
# Escape path for C++ string literals
string(REPLACE "\\" "/" INPUT_FILE_FORWARD "${INPUT_FILE}")
if(in_fixture_class)
string(APPEND registrations
"static Void::Testing::Detail::test_registrar<&${fixture_class_name}::${func_name}> _auto_reg_${safe_func_name}{\"${SUITE_NAME}\", \"${INPUT_FILE_FORWARD}\", ${line_number}, \"${func_name}\", std::vector<std::string>${tags_cpp}, ${is_skipped_str}, ${current_skip}};\n"
)
else()
string(APPEND registrations
"static Void::Testing::Detail::test_registrar<&${func_name}> _auto_reg_${safe_func_name}{\"${SUITE_NAME}\", \"${INPUT_FILE_FORWARD}\", ${line_number}, \"${func_name}\", std::vector<std::string>${tags_cpp}, ${is_skipped_str}, ${current_skip}};\n"
)
endif()
# Reset accumulators after consuming them
set(current_tags "")
set(current_skip "")
else()
# If line is code (not comment/empty), break the comment association
string(REGEX MATCH "^[ \t]*$" _is_empty "${line}")
string(REGEX MATCH "^[ \t]*//" _is_comment "${line}")
if(NOT _is_empty AND NOT _is_comment)
set(current_tags "")
set(current_skip "")
endif()
endif()
endforeach()
# The generated file MUST include the original file so function addresses can be resolved
file(WRITE ${OUTPUT_FILE}
"#include <Void/Testing/Detail/Registrar.hpp>\n"
"#include <vector>\n"
"#include <string>\n"
"\n"
"// Include the original test file to resolve function definitions\n"
"#include \"${INPUT_FILE_FORWARD}\"\n"
"\n"
"// Auto-generated by VoidTestScanner.cmake. Do not edit.\n"
"namespace {\n"
"${registrations}"
"}\n"
)
+142
View File
@@ -0,0 +1,142 @@
if(_VOID_TESTING_INCLUDED)
return()
endif()
set(_VOID_TESTING_INCLUDED TRUE)
set(VOID_TESTING_SCANNER "${CMAKE_CURRENT_LIST_DIR}/VoidTestScanner.cmake"
CACHE INTERNAL "Path to VoidTestScanner.cmake")
# Returns TRUE in <out_var> if <target> transitively links Void::Testing.
function(_void_testing_links_testing target out_var)
set(${out_var} FALSE PARENT_SCOPE)
set(_visited "")
set(_queue "${target}")
while(_queue)
list(POP_FRONT _queue _current)
if(_current IN_LIST _visited)
continue()
endif()
list(APPEND _visited "${_current}")
if(_current STREQUAL "Void::Testing" OR _current STREQUAL "Void_Testing")
set(${out_var} TRUE PARENT_SCOPE)
return()
endif()
if(NOT TARGET ${_current})
continue()
endif()
get_target_property(_libs ${_current} LINK_LIBRARIES)
if(_libs)
list(APPEND _queue ${_libs})
endif()
get_target_property(_iflibs ${_current} INTERFACE_LINK_LIBRARIES)
if(_iflibs)
list(APPEND _queue ${_iflibs})
endif()
endwhile()
endfunction()
# Apply the scan/HEADER_FILE_ONLY rewrite to <target>. Idempotent.
function(_void_testing_apply_scan target)
if(NOT TARGET ${target})
return()
endif()
get_target_property(_done ${target} VOID_TESTING_SCANNED)
if(_done)
return()
endif()
set_target_properties(${target} PROPERTIES VOID_TESTING_SCANNED TRUE)
get_target_property(_target_dir ${target} BINARY_DIR)
set(_gen_dir "${_target_dir}/void_testing_generated")
file(MAKE_DIRECTORY "${_gen_dir}")
get_target_property(_sources ${target} SOURCES)
set(_generated)
foreach(_src ${_sources})
if(NOT _src MATCHES "\\.cpp$")
continue()
endif()
if(_src MATCHES "\\.void_tests\\.cpp$")
continue()
endif()
get_filename_component(_abs ${_src} ABSOLUTE)
get_filename_component(_stem ${_src} NAME_WE)
set(_gen "${_gen_dir}/${_stem}.void_tests.cpp")
add_custom_command(
OUTPUT ${_gen}
COMMAND ${CMAKE_COMMAND}
-DINPUT_FILE=${_abs}
-DOUTPUT_FILE=${_gen}
-DSUITE_NAME=${_stem}
-P ${VOID_TESTING_SCANNER}
DEPENDS ${_abs} ${VOID_TESTING_SCANNER}
COMMENT "Scanning ${_src} for Void::Testing tests"
)
list(APPEND _generated ${_gen})
set_source_files_properties(${_src} PROPERTIES HEADER_FILE_ONLY TRUE)
set_source_files_properties(${_gen} PROPERTIES GENERATED TRUE)
endforeach()
if(_generated)
target_sources(${target} PRIVATE ${_generated})
endif()
if(NOT TEST ${target})
add_test(NAME ${target} COMMAND ${target})
endif()
endfunction()
# Public entry point — explicit opt-in. Idempotent with the auto-scan sweep.
function(void_add_tests target)
if(NOT TARGET ${target})
message(FATAL_ERROR "void_add_tests: '${target}' is not a target")
endif()
target_link_libraries(${target} PRIVATE Void::Testing)
_void_testing_apply_scan(${target})
endfunction()
# Sweep: visit every executable in <dir> and its subdirectories. If the
# executable transitively links Void::Testing and has not opted out via
# VOID_TESTING_NO_AUTO_SCAN, apply the scan rewrite.
function(_void_testing_sweep_directory dir)
get_property(_targets DIRECTORY "${dir}" PROPERTY BUILDSYSTEM_TARGETS)
foreach(_target IN LISTS _targets)
if(NOT TARGET ${_target})
continue()
endif()
get_target_property(_type ${_target} TYPE)
if(NOT _type STREQUAL "EXECUTABLE")
continue()
endif()
get_target_property(_optout ${_target} VOID_TESTING_NO_AUTO_SCAN)
if(_optout)
continue()
endif()
_void_testing_links_testing(${_target} _links)
if(_links)
_void_testing_apply_scan(${_target})
endif()
endforeach()
get_property(_subdirs DIRECTORY "${dir}" PROPERTY SUBDIRECTORIES)
foreach(_subdir IN LISTS _subdirs)
_void_testing_sweep_directory("${_subdir}")
endforeach()
endfunction()
# Schedule the sweep at the end of the include-site directory so the
# generated custom commands and target_sources() additions land in the
# same directory scope as the test targets that consume them.
if(NOT _VOID_TESTING_SWEEP_REGISTERED)
set(_VOID_TESTING_SWEEP_REGISTERED TRUE)
cmake_language(DEFER DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
CALL _void_testing_sweep_directory "${CMAKE_CURRENT_SOURCE_DIR}")
endif()