#pragma once #include #include #include #include #include #include #include #include namespace vn::test { class Failure : public std::runtime_error { public: using std::runtime_error::runtime_error; }; inline std::string location_text(const std::source_location& location) { std::ostringstream out; out << location.file_name() << ":" << location.line(); return out.str(); } template std::string display(const T& value) { if constexpr (std::is_enum_v) { std::ostringstream out; out << static_cast(value); return out.str(); } else if constexpr (std::is_same_v) { return value ? "true" : "false"; } else { std::ostringstream out; out << value; return out.str(); } } struct Context { [[noreturn]] void fail(std::string message, const std::source_location& location = std::source_location::current()) const { throw Failure(location_text(location) + ": " + std::move(message)); } void expect(bool condition, const std::source_location& location = std::source_location::current()) const { if (!condition) fail("expected condition to be true", location); } template void expect_eq(const A& actual, const B& expected, const std::source_location& location = std::source_location::current()) const { if (!(actual == expected)) fail("expected " + display(actual) + " == " + display(expected), location); } template void expect_ne(const A& actual, const B& expected, const std::source_location& location = std::source_location::current()) const { if (actual == expected) fail("expected " + display(actual) + " != " + display(expected), location); } void expect_contains(std::string_view text, std::string_view needle, const std::source_location& location = std::source_location::current()) const { if (text.find(needle) == std::string_view::npos) fail("expected text to contain \"" + std::string(needle) + "\", got:\n" + std::string(text), location); } void expect_not_contains( std::string_view text, std::string_view needle, const std::source_location& location = std::source_location::current()) const { if (text.find(needle) != std::string_view::npos) fail("expected text not to contain \"" + std::string(needle) + "\", got:\n" + std::string(text), location); } void expect_starts_with( std::string_view text, std::string_view prefix, const std::source_location& location = std::source_location::current()) const { if (!text.starts_with(prefix)) fail("expected text to start with \"" + std::string(prefix) + "\", got:\n" + std::string(text), location); } }; struct TestCase { std::string name; void (*run)(Context&) = nullptr; }; struct Suite { std::string name; std::vector tests; }; inline int run_tests(const std::vector& suites, std::string_view only = {}) { int failures = 0; int count = 0; for (const Suite& suite : suites) { if (!only.empty() && suite.name != only) continue; for (const TestCase& test : suite.tests) { ++count; Context context; try { test.run(context); std::cout << "[ok] " << suite.name << "." << test.name << "\n"; } catch (const Failure& failure) { ++failures; std::cerr << "[FAIL] " << suite.name << "." << test.name << ": " << failure.what() << "\n"; } catch (const std::exception& error) { ++failures; std::cerr << "[FAIL] " << suite.name << "." << test.name << ": unexpected exception: " << error.what() << "\n"; } } } if (count == 0) { std::cerr << "no tests selected\n"; return 1; } std::cout << count - failures << "/" << count << " tests passed\n"; return failures == 0 ? 0 : 1; } } // namespace vn::test