6#include <source_location>
11#define GRAPHLE_CAT_1(A, B) A##B
12#define GRAPHLE_CAT(A, B) GRAPHLE_CAT_1(A, B)
21 #define GRAPHLE_TEST_DONT_CATCH_ERRORS "Not Defined"
29#define TEST(Suite, Name) \
30struct test_##Suite_##Name { \
31 static void main(void); \
33 static inline auto autoregister = [] { \
34 graphle::test::test_registry::instance() \
38 &test_##Suite_##Name::main); \
44void test_##Suite_##Name::main(void)
51#define SUBTEST_SCOPE(Name) \
52if (auto impl_scope_object = graphle::test::subtest_scope_handler { Name }; true)
61throw graphle::test::assertion_failure { __VA_ARGS__ }
67#define ASSERT_TRUE(...) \
68if (!bool(__VA_ARGS__)) FAIL(#__VA_ARGS__)
74#define ASSERT_FALSE(...) \
75if (bool(__VA_ARGS__)) FAIL(#__VA_ARGS__)
79 template <
typename R,
typename... A>
using fn = R(*)(A...);
81 constexpr static inline const char* color_red =
"\x1B[31m";
82 constexpr static inline const char* color_green =
"\x1B[32m";
83 constexpr static inline const char* color_restore =
"\033[0m";
94 const char*
what(
void)
const noexcept override {
107 std::cout <<
"[Test " << name <<
"]: SUCCESS\n";
108 printf(color_restore);
115 std::cout <<
"[Test " << name <<
"]: FAILED\n\t" << failure.what() <<
"\n";
116 printf(color_restore);
130 tests.emplace_back(test, std::format(
"{}::{}", suite, name));
135 bool has_failures =
false;
138 for (
auto& test_data : tests) {
139 auto& [test, name, scope] = test_data;
140 current = std::addressof(test_data);
143 #ifdef GRAPHLE_TEST_DONT_CATCH_ERRORS
144 struct never_thrown_t {
const char* what(
void)
const {
return nullptr; } };
145 using exception_t = never_thrown_t;
147 using exception_t = std::exception;
155 }
catch (
const exception_t& e) {
158 while (!scope.empty()) {
167 return has_failures ? EXIT_FAILURE : EXIT_SUCCESS;
172 current->subtest_scope.push_back(std::move(name));
178 current->subtest_scope.pop_back();
189 std::vector<std::string> subtest_scope;
192 [[nodiscard]] std::string scoped_name(
void)
const {
193 std::string result = name;
195 for (
const auto& scope : subtest_scope) {
205 std::vector<test_data> tests;
206 test_data* current =
nullptr;
static test_registry & instance(void)
void begin_subtest(std::string name)
void register_test(std::string_view suite, std::string_view name, fn< void > test)
const auto & current_test(void) const
void emit_test_success(std::string_view name)
void emit_test_failure(std::string_view name, const auto &failure)
std::source_location where
std::string_view condition
const char * what(void) const noexcept override
assertion_failure(std::string_view condition, std::source_location where=std::source_location::current())
~subtest_scope_handler(void)
subtest_scope_handler(std::string name)