#include #include "Void/Testing/Config.hpp" #include #include #include #include #ifdef VOID_HAS_STACKTRACE #include #endif #include #include #include #include #include #ifdef VOID_HAS_ASYNC #include #include #endif #ifdef VOID_HAS_CONFIGURATION #include #else #include #endif #ifdef VOID_HAS_LOG #include #include #include #include #endif #ifdef VOID_HAS_TEXT #include #endif #include #include #include #include #include #include #include #include #include namespace Void::Testing { RunConfig Runner::parse_args(int argc, char** argv) { RunConfig config; for (int i = 1; i < argc; ++i) { std::string arg = argv[i]; if ((arg == "--filter" || arg == "--suite" || arg == "--reporter" || arg == "--output" || arg == "--tag" || arg == "--seed" || arg == "--repeat" || arg == "--log-file" || arg == "--parallel") && i + 1 < argc) { std::string val = argv[++i]; if (arg == "--filter") { config.filter_pattern = val; } else if (arg == "--suite") { config.suite_filter = val; } else if (arg == "--reporter") { config.reporter = val; } else if (arg == "--output") { config.output_file = val; } else if (arg == "--tag") { config.tag_filter = val; } else if (arg == "--log-file") { config.log_file = val; } else if (arg == "--seed") { config.shuffle = true; config.random_seed = static_cast(std::stoul(val)); } else if (arg == "--repeat") { config.repeat = static_cast(std::stoul(val)); } else if (arg == "--parallel") { config.parallel_threads = static_cast(std::stoul(val)); } } else if (arg == "--help") { config.show_help = true; } else if (arg == "--list") { config.list_only = true; } else if (arg == "--verbose") { config.verbose = true; } else if (arg == "--no-color") { config.no_color = true; } else if (arg == "--shuffle") { config.shuffle = true; std::random_device rd; config.random_seed = rd(); } } return config; } std::vector Runner::filter_tests(const std::vector& tests, const RunConfig& config) { std::vector result; std::regex filter_regex{".*"}; if (!config.filter_pattern.empty()) { try { filter_regex = std::regex{config.filter_pattern}; } catch (...) { return {}; } } for (const auto& test : tests) { if (!config.suite_filter.empty() && test.suite != config.suite_filter) { continue; } if (!std::regex_search(test.name, filter_regex) && !std::regex_search(test.suite + "." + test.name, filter_regex)) { continue; } if (!config.tag_filter.empty()) { bool has = false; for (const auto& t : test.tags) { if (t == config.tag_filter) { has = true; break; } } if (!has) { continue; } } result.push_back(test); } return result; } std::unique_ptr Runner::create_reporter(const RunConfig& config) { if (config.reporter == "junit") { static std::ofstream file{config.output_file.empty() ? "test_results.xml" : config.output_file}; return std::make_unique(file); } return std::make_unique(std::cout, !config.no_color); } TestResult Runner::execute_test(TestCase& test_case) { TestResult result; result.name = test_case.name; result.suite = test_case.suite; result.tags = test_case.tags; if (test_case.disabled) { result.state = TestState::Skipped; result.skip_reason = test_case.skip_reason.empty() ? "disabled" : test_case.skip_reason; return result; } Detail::TestContext::current().current_result = &result; Detail::TestContext::current().should_abort = false; int crash_signal = 0; std::string crash_stacktrace; Detail::install_signal_handlers( [&](int sig, const std::string& trace) { crash_signal = sig; crash_stacktrace = trace; }); auto start = std::chrono::high_resolution_clock::now(); try { #ifndef _WIN32 if (sigsetjmp(Detail::get_jump_buffer(), 1) == 0) { Detail::set_jump_ready(true); run_test_body(test_case, result); } else { result.state = TestState::Crashed; result.crash_info = std::format( "Signal: {}", Detail::signal_name(crash_signal)); result.stacktrace = crash_stacktrace; } Detail::set_jump_ready(false); #else run_test_body(test_case, result); #endif } catch (const Detail::RequireFailure&) { if (result.state != TestState::Skipped) { result.state = TestState::Failed; } } catch (const std::exception& e) { result.state = TestState::Failed; result.crash_info = std::format("Unhandled exception: {}", e.what()); #ifdef VOID_HAS_STACKTRACE result.stacktrace = Void::Stacktrace::format_stacktrace( Void::Stacktrace::capture_stacktrace(1, 8)); #endif } catch (...) { result.state = TestState::Failed; result.crash_info = "Unhandled unknown exception"; #ifdef VOID_HAS_STACKTRACE result.stacktrace = Void::Stacktrace::format_stacktrace( Void::Stacktrace::capture_stacktrace(1, 8)); #endif } auto end = std::chrono::high_resolution_clock::now(); result.duration = end - start; Detail::uninstall_signal_handlers(); Detail::TestContext::current().current_result = nullptr; if (result.state != TestState::Skipped && result.state != TestState::Crashed) { bool any = std::any_of( result.assertions.begin(), result.assertions.end(), [](const auto& a) { return !a.passed; }); result.state = any ? TestState::Failed : TestState::Passed; } return result; } // Like execute_test() but without sigsetjmp/signal-handler installation, // safe to call from multiple threads simultaneously. TestResult Runner::execute_test_parallel(TestCase& test_case) { TestResult result; result.name = test_case.name; result.suite = test_case.suite; result.tags = test_case.tags; if (test_case.disabled) { result.state = TestState::Skipped; result.skip_reason = test_case.skip_reason.empty() ? "disabled" : test_case.skip_reason; return result; } Detail::TestContext::current().current_result = &result; Detail::TestContext::current().should_abort = false; auto start = std::chrono::high_resolution_clock::now(); try { run_test_body(test_case, result); } catch (const Detail::RequireFailure&) { if (result.state != TestState::Skipped) { result.state = TestState::Failed; } } catch (const std::exception& e) { result.state = TestState::Failed; result.crash_info = std::format("Unhandled exception: {}", e.what()); } catch (...) { result.state = TestState::Failed; result.crash_info = "Unhandled unknown exception"; } auto end = std::chrono::high_resolution_clock::now(); result.duration = end - start; Detail::TestContext::current().current_result = nullptr; if (result.state != TestState::Skipped && result.state != TestState::Crashed) { bool any = std::any_of( result.assertions.begin(), result.assertions.end(), [](const auto& a) { return !a.passed; }); result.state = any ? TestState::Failed : TestState::Passed; } return result; } void Runner::run_test_body(TestCase& test_case, TestResult&) { if (test_case.fixture_create) { void* fixture = test_case.fixture_create(); Detail::TestContext::current().fixture = fixture; try { test_case.fixture_setup(fixture); test_case.run(); test_case.fixture_teardown(fixture); } catch (...) { test_case.fixture_teardown(fixture); test_case.fixture_destroy(fixture); Detail::TestContext::current().fixture = nullptr; throw; } test_case.fixture_destroy(fixture); Detail::TestContext::current().fixture = nullptr; } else { test_case.run(); } } RunResult Runner::run(const RunConfig& config) { Void::Testing::Detail::Color::g_color_enabled = !config.no_color; if (config.show_help) { using namespace Void::Testing::Detail::Color; std::cout << "\n" << apply(bold_blue, "\xe2\x95\xad\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x95\xae") << "\n" << apply(bold_blue, "\xe2\x94\x82") << apply(bold_white, " Void Testing") << apply(dim, " \xe2\x80\x94 Command Line Interface") << std::string(28, ' ') << apply(bold_blue, "\xe2\x94\x82") << "\n" << apply(bold_blue, "\xe2\x95\xb0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80" "\xe2\x94\x80\xe2\x94\x80\xe2\x95\xaf") << "\n" << "\n" << " " << apply(bold_white, "USAGE:") << "\n" << " " << apply(cyan, "") << " " << apply(dim, "[options]") << "\n" << "\n" << " " << apply(bold_white, "OPTIONS:") << "\n"; constexpr std::size_t OPT_WIDTH = 23; auto opt = [&](std::string_view name) { #ifdef VOID_HAS_TEXT return apply(green, Void::Str::pad_right(name, OPT_WIDTH)); #else std::string s{name}; if (s.size() < OPT_WIDTH) { s.append(OPT_WIDTH - s.size(), ' '); } return apply(green, s); #endif }; std::cout << " " << opt("--filter ") << apply(dim, "Run tests matching the regex pattern") << "\n" << " " << opt("--suite ") << apply(dim, "Run only tests in the specified suite") << "\n" << " " << opt("--tag ") << apply(dim, "Run only tests with the specified tag") << "\n" << " " << opt("--list") << apply(dim, "List discovered tests without running them") << "\n" << " " << opt("--shuffle") << apply(dim, "Randomize the order of test execution") << "\n" << " " << opt("--seed ") << apply(dim, "Set the random seed (implies --shuffle)") << "\n" << " " << opt("--repeat ") << apply(dim, "Repeat the test run n times") << "\n" << " " << opt("--reporter ") << apply(dim, "Choose the output reporter: console, junit") << "\n" << " " << opt("--output ") << apply(dim, "Write reporter output to a file") << "\n" #ifdef VOID_HAS_LOG << " " << opt("--log-file ") << apply(dim, "Mirror test events to via Void::Logging") << "\n" #endif #ifdef VOID_HAS_ASYNC << " " << opt("--parallel ") << apply(dim, "Run tests across n threads (no crash detection)") << "\n" #endif << " " << opt("--verbose") << apply(dim, "Enable verbose output") << "\n" << " " << opt("--no-color") << apply(dim, "Disable colored console output") << "\n" << " " << opt("--help") << apply(dim, "Show this help message") << "\n" << "\n" << " " << apply(bold_white, "EXAMPLES:") << "\n" << " " << apply(cyan, "./run_tests") << " " << apply(yellow, "--filter") << " \"math_tests.*\"" << "\n" << " " << apply(cyan, "./run_tests") << " " << apply(yellow, "--tag") << " integration " << apply(yellow, "--shuffle") << "\n" << " " << apply(cyan, "./run_tests") << " " << apply(yellow, "--reporter") << " junit " << apply(yellow, "--output") << " results.xml" << "\n" << "\n"; return {}; } auto all_tests = Detail::Registry::instance().tests(); auto tests = filter_tests(all_tests, config); auto reporter = create_reporter(config); // ── Optional: file logger sink (requires Void::Logging) ────────────────── #ifdef VOID_HAS_LOG std::unique_ptr log_sink; if (!config.log_file.empty()) { try { log_sink = std::make_unique( config.log_file); } catch (const std::exception& e) { std::cerr << "warning: --log-file failed: " << e.what() << "\n"; } } #else if (!config.log_file.empty()) { std::cerr << "warning: --log-file requires Void::Logging (not linked)\n"; } #endif // ── Optional: VOID_LOG_LEVEL auto-sink ─────────────────────────────────── #ifdef VOID_HAS_LOG std::unique_ptr env_log; { #ifdef VOID_HAS_CONFIGURATION const auto lvl_str = Void::Configuration::get_env_or("VOID_LOG_LEVEL"); #else const char* raw_env = std::getenv("VOID_LOG_LEVEL"); // NOLINT(concurrency-mt-unsafe) const std::string lvl_str = raw_env ? raw_env : ""; #endif if (!lvl_str.empty()) { #ifdef VOID_HAS_ENUM_REFLECTION auto level = Void::Detail::enum_parse(lvl_str) .value_or(Void::Logging::Level::Info); #else Void::Logging::Level level = Void::Logging::Level::Info; if (lvl_str == "Trace") level = Void::Logging::Level::Trace; else if (lvl_str == "Debug") level = Void::Logging::Level::Debug; else if (lvl_str == "Warning") level = Void::Logging::Level::Warning; else if (lvl_str == "Error") level = Void::Logging::Level::Error; else if (lvl_str == "Fatal") level = Void::Logging::Level::Fatal; #endif env_log = std::make_unique(std::cerr); env_log->set_level(level); Void::Log::set_sink(Void::Log::make_sink(*env_log)); } } #endif // VOID_HAS_LOG if (config.list_only) { for (const auto& t : tests) { std::cout << t.suite << "." << t.name << (t.disabled ? " [DISABLED]" : "") << "\n"; } return {}; } RunResult run_result; std::vector suite_results; std::unordered_map suite_index; for (auto& test : tests) { if (suite_index.find(test.suite) == suite_index.end()) { suite_index[test.suite] = suite_results.size(); suite_results.push_back({.name = test.suite}); } } auto log_result = [&](const TestResult& r, const std::string& q) { #ifdef VOID_HAS_LOG if (!log_sink) { return; } const auto ts = Void::Time::utc_now_iso8601_ms(); const auto dur = Void::Time::format_duration(r.duration); switch (r.state) { case TestState::Passed: log_sink->info(std::format("[{}] test pass: {} ({})", ts, q, dur)); break; case TestState::Failed: log_sink->error(std::format( "[{}] test fail: {} ({}) \xe2\x80\x94 {}", ts, q, dur, r.crash_info.empty() ? "assertion failed" : r.crash_info)); break; case TestState::Skipped: log_sink->warning(std::format( "[{}] test skip: {} \xe2\x80\x94 {}", ts, q, r.skip_reason.empty() ? "no reason" : r.skip_reason)); break; case TestState::Crashed: log_sink->fatal(std::format( "[{}] test crash: {} \xe2\x80\x94 {}", ts, q, r.crash_info)); break; } #else (void)r; (void)q; #endif }; auto start_time = std::chrono::high_resolution_clock::now(); // ── Optional: parallel execution (requires Void::Concurrency) ──────────── bool run_parallel = config.parallel_threads > 0; #ifndef VOID_HAS_ASYNC if (run_parallel) { std::cerr << "warning: --parallel requires Void::Concurrency " "(not linked); running sequentially\n"; run_parallel = false; } #endif if (run_parallel) { #ifdef VOID_HAS_ASYNC reporter->on_run_start(config, static_cast(tests.size())); #ifdef VOID_HAS_LOG if (log_sink) { log_sink->info(std::format( "run start (parallel, {} threads): {} test(s)", config.parallel_threads, tests.size())); } #endif Void::Concurrency::ThreadPool pool{config.parallel_threads}; std::vector> futures; futures.reserve(tests.size()); for (auto& test : tests) { futures.push_back(pool.submit( [this, &test] { return execute_test_parallel(test); })); } std::vector par_results; par_results.reserve(tests.size()); for (auto& fut : futures) { par_results.push_back(fut.get()); } std::unordered_map suite_started; for (std::size_t i = 0; i < tests.size(); ++i) { auto& test = tests[i]; auto& result = par_results[i]; if (!suite_started[test.suite]) { suite_started[test.suite] = true; reporter->on_suite_start( test.suite, static_cast( std::count_if(tests.begin(), tests.end(), [&](const auto& t) { return t.suite == test.suite; }))); } reporter->on_test_start(test); reporter->on_test_end(result); log_result(result, std::format("{}.{}", result.suite, result.name)); suite_results[suite_index[test.suite]].tests.push_back( std::move(result)); } #endif // VOID_HAS_ASYNC } else { reporter->on_run_start(config, static_cast(tests.size())); #ifdef VOID_HAS_LOG if (log_sink) { log_sink->info(std::format("run start: {} test(s) discovered", tests.size())); } #endif if (config.shuffle) { std::shuffle(tests.begin(), tests.end(), std::mt19937{config.random_seed}); #ifdef VOID_HAS_LOG if (log_sink) { log_sink->info(std::format("shuffle enabled (seed = {})", config.random_seed)); } #endif } for (unsigned rep = 0; rep < config.repeat; ++rep) { std::string current_suite; for (auto& test : tests) { auto& suite = suite_results[suite_index[test.suite]]; // Emit suite header only when the suite name changes. if (test.suite != current_suite) { current_suite = test.suite; reporter->on_suite_start( suite.name, std::count_if( tests.begin(), tests.end(), [&](const auto& t) { return t.suite == suite.name; })); #ifdef VOID_HAS_LOG if (log_sink) { log_sink->info(std::format("suite start: {}", suite.name)); } #endif } reporter->on_test_start(test); auto result = execute_test(test); reporter->on_test_end(result); log_result(result, std::format("{}.{}", result.suite, result.name)); if (rep == config.repeat - 1) { suite.tests.push_back(std::move(result)); } } } } auto end_time = std::chrono::high_resolution_clock::now(); for (auto& suite : suite_results) { for (const auto& test : suite.tests) { switch (test.state) { case TestState::Passed: run_result.passed++; break; case TestState::Failed: run_result.failed++; break; case TestState::Skipped: run_result.skipped++; break; case TestState::Crashed: run_result.crashed++; break; } run_result.total++; } reporter->on_suite_end(suite); run_result.suites.push_back(std::move(suite)); } run_result.total_duration = end_time - start_time; reporter->on_run_end(run_result); #ifdef VOID_HAS_LOG if (log_sink) { log_sink->info( std::format("run end: {} total / {} passed / {} failed / {} " "skipped / {} crashed", run_result.total, run_result.passed, run_result.failed, run_result.skipped, run_result.crashed)); } if (env_log) { Void::Log::set_sink({}); } #endif return run_result; } int Runner::main(int argc, char** argv) { auto config = parse_args(argc, argv); auto result = run(config); return (result.failed + result.crashed > 0) ? 1 : 0; } } // namespace Void::Testing