Add customizable assert failure handler
This commit is contained in:
@ -1,12 +1,18 @@
|
|||||||
#include "asl/base/assert.hpp"
|
#include "asl/base/assert.hpp"
|
||||||
// #include "asl/io/print.hpp"
|
|
||||||
|
|
||||||
|
static asl::AssertFailureHandler* s_handler = nullptr;
|
||||||
|
static void* s_user = nullptr;
|
||||||
|
|
||||||
|
void asl::set_assert_failure_handler(AssertFailureHandler handler, void* user)
|
||||||
|
{
|
||||||
|
s_handler = handler;
|
||||||
|
s_user = user;
|
||||||
|
}
|
||||||
|
|
||||||
void asl::report_assert_failure(const char* msg, const source_location& sl)
|
void asl::report_assert_failure(const char* msg, const source_location& sl)
|
||||||
{
|
{
|
||||||
// @Todo(org)
|
if (s_handler != nullptr)
|
||||||
// eprint("------------------------------------------------------------\n");
|
{
|
||||||
// eprint("Assertion failure at {}, line {}:\n", sl.file, sl.line);
|
s_handler(msg, sl, s_user);
|
||||||
// eprint("{}\n", msg);
|
}
|
||||||
// eprint("------------------------------------------------------------\n");
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,10 @@
|
|||||||
namespace asl
|
namespace asl
|
||||||
{
|
{
|
||||||
|
|
||||||
|
using AssertFailureHandler = void (const char* msg, const source_location&, void* user);
|
||||||
|
|
||||||
|
void set_assert_failure_handler(AssertFailureHandler handler, void* user);
|
||||||
|
|
||||||
void report_assert_failure(const char* msg, const source_location& sl = source_location{});
|
void report_assert_failure(const char* msg, const source_location& sl = source_location{});
|
||||||
|
|
||||||
} // namespace asl
|
} // namespace asl
|
||||||
@ -16,6 +20,8 @@ void report_assert_failure(const char* msg, const source_location& sl = source_l
|
|||||||
#define ASL_DEBUG_BREAK() __builtin_debugtrap()
|
#define ASL_DEBUG_BREAK() __builtin_debugtrap()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// @Todo Configure asserts at build time
|
||||||
|
|
||||||
#define ASL_ASSERT(...) \
|
#define ASL_ASSERT(...) \
|
||||||
if (__VA_ARGS__) {} \
|
if (__VA_ARGS__) {} \
|
||||||
else \
|
else \
|
||||||
|
@ -21,21 +21,31 @@ void asl::testing::register_test(Test* test)
|
|||||||
|
|
||||||
static bool g_current_test_fail = false;
|
static bool g_current_test_fail = false;
|
||||||
|
|
||||||
void asl::testing::report_failure(const char* msg, const char* file, int line)
|
void asl::testing::report_failure(const char* msg, const asl::source_location& sl)
|
||||||
{
|
{
|
||||||
asl::eprint("--------------------------------------------------------------\n");
|
asl::eprint("--------------------------------------------------------------\n");
|
||||||
asl::eprint("Test assertion failed at {}, line {}:\n", file, line);
|
asl::eprint("Test assertion failed at {}, line {}:\n", sl.file, sl.line);
|
||||||
asl::eprint(" {}\n", msg);
|
asl::eprint(" {}\n", msg);
|
||||||
asl::eprint("--------------------------------------------------------------\n");
|
asl::eprint("--------------------------------------------------------------\n");
|
||||||
g_current_test_fail = true;
|
g_current_test_fail = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void report_assert_failure(const char* msg, const asl::source_location& sl, void*)
|
||||||
|
{
|
||||||
|
asl::eprint("------------------------------------------------------------\n");
|
||||||
|
asl::eprint("Assertion failure at {}, line {}:\n", sl.file, sl.line);
|
||||||
|
asl::eprint("{}\n", msg);
|
||||||
|
asl::eprint("------------------------------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
#define RESET "\x1b[0m"
|
#define RESET "\x1b[0m"
|
||||||
#define RED(S) "\x1b[0;31m" S RESET
|
#define RED(S) "\x1b[0;31m" S RESET
|
||||||
#define GREEN(S) "\x1b[0;32m" S RESET
|
#define GREEN(S) "\x1b[0;32m" S RESET
|
||||||
|
|
||||||
int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
|
int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
|
||||||
{
|
{
|
||||||
|
asl::set_assert_failure_handler(report_assert_failure, nullptr);
|
||||||
|
|
||||||
int fail = 0;
|
int fail = 0;
|
||||||
int pass = 0;
|
int pass = 0;
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ struct Test;
|
|||||||
|
|
||||||
void register_test(Test*);
|
void register_test(Test*);
|
||||||
|
|
||||||
void report_failure(const char* msg, const char* file, int line);
|
void report_failure(const char* msg, const asl::source_location& = asl::source_location{});
|
||||||
|
|
||||||
using TestFunction = void();
|
using TestFunction = void();
|
||||||
|
|
||||||
@ -39,8 +39,8 @@ struct Test
|
|||||||
|
|
||||||
#define ASL_TEST_ASSERT(EXPR) \
|
#define ASL_TEST_ASSERT(EXPR) \
|
||||||
if (EXPR) {} \
|
if (EXPR) {} \
|
||||||
else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); return; }
|
else { ::asl::testing::report_failure(#EXPR); return; }
|
||||||
|
|
||||||
#define ASL_TEST_EXPECT(EXPR) \
|
#define ASL_TEST_EXPECT(EXPR) \
|
||||||
if (EXPR) {} \
|
if (EXPR) {} \
|
||||||
else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); }
|
else { ::asl::testing::report_failure(#EXPR); }
|
||||||
|
Reference in New Issue
Block a user