Graphle 1.0.0
A C++20 header-only graph library
Loading...
Searching...
No Matches
test_framework.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <string_view>
5#include <iostream>
6#include <source_location>
7#include <format>
8#include <functional>
9
10
11#define GRAPHLE_CAT_1(A, B) A##B
12#define GRAPHLE_CAT(A, B) GRAPHLE_CAT_1(A, B)
13
14
20#ifdef GRAPHLE_DOXYGEN
21 #define GRAPHLE_TEST_DONT_CATCH_ERRORS "Not Defined"
22#endif
23
24
29#define TEST(Suite, Name) \
30struct test_##Suite_##Name { \
31 static void main(void); \
32 \
33 static inline auto autoregister = [] { \
34 graphle::test::test_registry::instance() \
35 .register_test( \
36 #Suite, \
37 #Name, \
38 &test_##Suite_##Name::main); \
39 \
40 return 0; \
41 } (); \
42}; \
43 \
44void test_##Suite_##Name::main(void)
45
46
51#define SUBTEST_SCOPE(Name) \
52if (auto impl_scope_object = graphle::test::subtest_scope_handler { Name }; true)
53
54
55
60#define FAIL(...) \
61throw graphle::test::assertion_failure { __VA_ARGS__ }
62
67#define ASSERT_TRUE(...) \
68if (!bool(__VA_ARGS__)) FAIL(#__VA_ARGS__)
69
74#define ASSERT_FALSE(...) \
75if (bool(__VA_ARGS__)) FAIL(#__VA_ARGS__)
76
77
78namespace graphle::test {
79 template <typename R, typename... A> using fn = R(*)(A...);
80
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";
84
85
87 struct assertion_failure : std::exception {
88 explicit assertion_failure(std::string_view condition, std::source_location where = std::source_location::current()) :
90 where(where),
91 message(std::format("Assertion failure at line {} in {} ({}): {}", where.line(), where.file_name(), where.function_name(), condition))
92 {}
93
94 const char* what(void) const noexcept override {
95 return message.c_str();
96 }
97
98 std::string_view condition;
99 std::source_location where;
100 std::string message;
101 };
102
103
105 inline void emit_test_success(std::string_view name) {
106 printf(color_green);
107 std::cout << "[Test " << name << "]: SUCCESS\n";
108 printf(color_restore);
109 }
110
111
113 inline void emit_test_failure(std::string_view name, const auto& failure) {
114 printf(color_red);
115 std::cout << "[Test " << name << "]: FAILED\n\t" << failure.what() << "\n";
116 printf(color_restore);
117 }
118
119
122 public:
123 static test_registry& instance(void) {
124 static test_registry i { };
125 return i;
126 }
127
128
129 void register_test(std::string_view suite, std::string_view name, fn<void> test) {
130 tests.emplace_back(test, std::format("{}::{}", suite, name));
131 }
132
133
134 int run_tests(void) {
135 bool has_failures = false;
136
137
138 for (auto& test_data : tests) {
139 auto& [test, name, scope] = test_data;
140 current = std::addressof(test_data);
141
142
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;
146 #else
147 using exception_t = std::exception;
148 #endif
149
150
151 try {
152 std::invoke(test);
153 emit_test_success(name);
154 scope.clear();
155 } catch (const exception_t& e) {
156 has_failures = true;
157
158 while (!scope.empty()) {
159 emit_test_failure(test_data.scoped_name(), e);
160 scope.pop_back();
161 }
162
163 emit_test_failure(name, e);
164 }
165 }
166
167 return has_failures ? EXIT_FAILURE : EXIT_SUCCESS;
168 }
169
170
171 void begin_subtest(std::string name) {
172 current->subtest_scope.push_back(std::move(name));
173 }
174
175
176 void end_subtest(void) {
177 emit_test_success(current->scoped_name());
178 current->subtest_scope.pop_back();
179 }
180
181
182 const auto& current_test(void) const {
183 return *current;
184 }
185 private:
186 struct test_data {
187 fn<void> test;
188 std::string name;
189 std::vector<std::string> subtest_scope;
190
191
192 [[nodiscard]] std::string scoped_name(void) const {
193 std::string result = name;
194
195 for (const auto& scope : subtest_scope) {
196 result += "::";
197 result += scope;
198 }
199
200 return result;
201 }
202 };
203
204
205 std::vector<test_data> tests;
206 test_data* current = nullptr;
207 };
208
209
212 explicit subtest_scope_handler(std::string name) {
213 test_registry::instance().begin_subtest(std::move(name));
214 }
215
217 if (!std::uncaught_exceptions()) test_registry::instance().end_subtest();
218 }
219 };
220}
221
222
224int main(void) {
226}
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)
const char * what(void) const noexcept override
assertion_failure(std::string_view condition, std::source_location where=std::source_location::current())
int main(void)