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${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${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 \n" "#include \n" "#include \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" )