#include #include #include #include #include #include #include #include namespace Void::Build::SelfHost { Void::Result::Result, Void::Result::Error> scan_void_tests(const std::filesystem::path &source_file) { auto text = Void::FileSystem::read_file(source_file); if (!text) { return std::unexpected{text.error()}; } const std::string suite = source_file.stem().string(); const std::regex pattern{ R"((?:^|[\r\n])\s*(?:Void::Testing::)?Test\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(\s*\))"}; std::vector found; std::size_t line_no = 1; std::size_t pos = 0; while (pos < text->size()) { const auto nl = text->find('\n', pos); const auto line = std::string{text->substr(pos, nl == std::string::npos ? std::string::npos : nl - pos)}; std::smatch m; if (std::regex_search(line, m, pattern)) { found.push_back(DiscoveredTestCase{suite, m[1].str(), source_file, line_no}); } if (nl == std::string::npos) { break; } pos = nl + 1; ++line_no; } return found; } Void::Result::Result generate_test_registration_unit(const std::filesystem::path &source_file, const std::filesystem::path &output_dir) { auto cases_res = scan_void_tests(source_file); if (!cases_res) { return std::unexpected{cases_res.error()}; } std::error_code ec; const auto abs_src = std::filesystem::weakly_canonical(source_file, ec); const std::string path_hash = Util::sha256_hex(abs_src.string()).substr(0, 12); const auto out_cpp = output_dir / (source_file.stem().string() + "_" + path_hash + ".void_tests.cpp"); std::string code = "// Auto-generated by VoidBuildSystem native Void::Testing registration generator\n" "#include \n" "#include \n" "#include \n" "#include \n\n"; for (const auto &c : *cases_res) { code += "Void::Testing::Test " + c.function_name + "();\n"; } code += "\nnamespace {\n"; for (std::size_t i = 0; i < cases_res->size(); ++i) { const auto &c = (*cases_res)[i]; code += std::format("static Void::Testing::Detail::test_registrar<&{}> _vbs_reg_{}_{}{{\n" " {}, {}, {}, {}, std::vector{{}}, false, \"\"\n" "}};\n", c.function_name, path_hash, i, Util::json_quote(c.suite_name), Util::json_quote(abs_src.string()), c.line_number, Util::json_quote(c.function_name)); } code += "} // namespace\n"; auto wrote = Platform::atomic_write_file(out_cpp, code, false); if (!wrote) { return std::unexpected{wrote.error()}; } return GeneratedRegistration{out_cpp, std::move(*cases_res)}; } } // namespace Void::Build::SelfHost