Files
Build-System/src/SelfHost/TestRegistryGen.cpp
T
faumaray ab64147f9d
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
2026-11-26 20:45:54 +03:00

90 lines
3.2 KiB
C++

#include <Void/Build/SelfHost/TestRegistryGen.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Util/Json.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/Types.hpp>
#include <format>
#include <regex>
namespace Void::Build::SelfHost {
Void::Result::Result<std::vector<DiscoveredTestCase>, 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<DiscoveredTestCase> 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<GeneratedRegistration, Void::Result::Error>
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 <Void/Testing/Detail/Registrar.hpp>\n"
"#include <Void/Testing/Test.hpp>\n"
"#include <string>\n"
"#include <vector>\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<std::string>{{}}, 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