Files
Library/cmake/VoidTestScanner.cmake

177 lines
7.2 KiB
CMake

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(READ "${INPUT_FILE}" _raw_content)
string(REPLACE "\r\n" "\n" _raw_content "${_raw_content}")
string(REPLACE "\r" "\n" _raw_content "${_raw_content}")
string(REPLACE "\\" "__VOID_SCAN_BS__" _raw_content "${_raw_content}")
string(REPLACE ";" "__VOID_SCAN_SC__" _raw_content "${_raw_content}")
string(REPLACE "[" "__VOID_SCAN_LB__" _raw_content "${_raw_content}")
string(REPLACE "]" "__VOID_SCAN_RB__" _raw_content "${_raw_content}")
string(REPLACE "\n" ";" lines "${_raw_content}")
# Forward-slash path for C++ string literals / #include (always set, so files
# whose tests are all reflection-registered Suite<> structs still emit a valid
# include of the original .cpp).
string(REPLACE "\\" "/" INPUT_FILE_FORWARD "${INPUT_FILE}")
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")
string(REPLACE "__VOID_SCAN_RB__" "]" line "${line}")
string(REPLACE "__VOID_SCAN_LB__" "[" line "${line}")
string(REPLACE "__VOID_SCAN_SC__" ";" line "${line}")
string(REPLACE "__VOID_SCAN_BS__" "\\" line "${line}")
# 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)
# Derive indentation without an empty-match-capable regex. CMake 4
# rejects such expressions even when the current input is non-empty.
string(STRIP "${line}" _trimmed_fixture_line)
string(FIND "${line}" "${_trimmed_fixture_line}" _indent_length)
string(SUBSTRING "${line}" 0 ${_indent_length} fixture_indent)
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(STRIP "${line}" _stripped_line)
if(_stripped_line STREQUAL "")
set(_is_empty TRUE)
else()
set(_is_empty FALSE)
endif()
if(line MATCHES "^[ \t]*//")
set(_is_comment TRUE)
else()
set(_is_comment FALSE)
endif()
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"
)