5#include <source_location>
14namespace RST::RSTester {
16 constexpr const char* COLOR_GREEN =
"\033[32m";
17 constexpr const char* COLOR_RED =
"\033[31m";
18 constexpr const char* COLOR_YELLOW =
"\033[33m";
20 constexpr const char* COLOR_BOLD_GREEN =
"\033[1;32m";
21 constexpr const char* COLOR_BOLD_RED =
"\033[1;31m";
22 constexpr const char* COLOR_BOLD_YELLOW =
"\033[1;33m";
23 constexpr const char* COLOR_BOLD_CYAN =
"\033[1;36m";
24 constexpr const char* COLOR_BOLD_WHITE =
"\033[1;37m";
25 constexpr const char* COLOR_RESET =
"\033[0m";
27 struct TestAbortException :
public std::exception {};
30 constexpr std::string tryToStr(
const T& t){
31 if constexpr(
requires{ (std::ostringstream() << t).str(); }){
32 return (std::ostringstream() << std::boolalpha << t).str();
39 std::string_view expr =
"";
40 std::source_location sl = std::source_location::current();
42 std::string opt_lhs =
"";
43 std::string opt_rhs =
"";
50 #define CHECK_T_COMP_OP(OP) \
52 info_t operator OP(B&& b) && { \
53 i.opt_lhs = tryToStr(a); \
54 i.opt_rhs = tryToStr(b); \
55 i.result = (a OP b); \
56 return std::move(i); \
64 #undef CHECK_T_COMP_OP
65 operator info_t() && {
66 i.opt_lhs = tryToStr(a);
75 check_t<A> operator<(A&& a) && {
76 return check_t<A>{std::forward<A>(a), i};
80 #define DECOMPOSE_CMP_OP(expr, lit) RST::RSTester::destruct_t{RST::RSTester::info_t{lit ""}} < expr
82 inline void checkInternal(info_t i, std::string_view additional_info =
""){
83 (void)additional_info;
85 std::cerr <<
" " << i.sl.file_name() <<
":" << i.sl.line() <<
" -> "
86 << COLOR_RED <<
"FAILED\n" << COLOR_RESET
87 <<
" Expression: " << i.expr <<
"\n"
88 <<
" Values: " << i.opt_lhs <<
" and " << i.opt_rhs <<
"\n";
89 throw TestAbortException();
94 #define CHECK(e, ...) RST::RSTester::checkInternal(DECOMPOSE_CMP_OP(e, #e) __VA_OPT__(,) __VA_ARGS__)
97 using function_sig = void();
98 function_sig& function;
99 std::string_view name;
104 static TestRunner& getInstance() {
105 static TestRunner instance;
109 int addTest(testfunction test) {
110 _tests.push_back(test);
115 auto time_start_global = std::chrono::high_resolution_clock::now();
117 std::cout << COLOR_BOLD_YELLOW <<
"[==========] " << COLOR_RESET <<
"Running " << _tests.size() <<
" tests.\n";
118 int failed_count = 0;
121 for (
const auto& t : _tests) {
122 std::cout << COLOR_BOLD_CYAN <<
"\n[ RUN ] " << COLOR_RESET << COLOR_BOLD_WHITE << t.name <<
"\n" << COLOR_RESET;
124 auto time_start_test = std::chrono::high_resolution_clock::now();
129 }
catch (
const TestAbortException&) {
130 std::cout <<
" -> Test aborted due to fatal failure.\n";
133 }
catch (
const std::exception& e) {
134 std::cerr <<
" -> Unhandled exception: " << e.what() <<
"\n";
139 auto time_end_test = std::chrono::high_resolution_clock::now();
140 auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_end_test - time_start_test).count();
143 std::cout << COLOR_GREEN <<
"[ OK ] " << COLOR_RESET << t.name <<
" (" << duration_ms <<
" ms)\n";
145 std::cout << COLOR_RED <<
"[ TEST FAILED ] " << COLOR_RESET << t.name <<
" (" << duration_ms <<
" ms)\n";
148 auto time_end_global = std::chrono::high_resolution_clock::now();
149 auto total_duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_end_global - time_start_global).count();
151 std::cout << COLOR_BOLD_YELLOW <<
"\n[==========] " << COLOR_RESET <<
"Test execution finished. (" << total_duration_ms <<
" ms total)\n";
152 std::cout << COLOR_BOLD_CYAN <<
"[ RESULT ] " << COLOR_RESET << total_tests - failed_count <<
"/" << total_tests <<
" tests passed (" << ((total_tests > 0) ? ((total_tests - failed_count) * 100 / total_tests) : 0) <<
"%)\n\n";
155 if (failed_count > 0) {
156 std::cout << COLOR_BOLD_RED <<
"[ FAILED ] " << COLOR_RESET << COLOR_BOLD_WHITE << failed_count <<
" tests failed.\n" << COLOR_RESET;
158 std::cout << COLOR_BOLD_GREEN <<
"[ PASSED ] " << COLOR_RESET << COLOR_BOLD_WHITE <<
"All tests passed!\n" << COLOR_RESET;
163 TestRunner() =
default;
164 std::vector<testfunction> _tests = {};
167 #define CONCAT_IMPL(x, y) x##y
168 #define CONCAT(x, y) CONCAT_IMPL(x, y)
170 #define TEST_CASE(name) \
171 static void name(); \
172 static int CONCAT(impl, __LINE__) = RST::RSTester::TestRunner::getInstance().addTest({name, #name}); \
176 #define RUN_ALL_TESTS() RST::RSTester::TestRunner::getInstance().runAllTests()