summaryrefslogtreecommitdiff
path: root/asl/testing
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-02-20 23:13:37 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-02-20 23:13:37 +0100
commit409ef997e2ff99b2bbea89ea61d10fc8e26dac96 (patch)
tree103225ce6af074120731ce9bb3c16a3778687d6c /asl/testing
parent6fd19d6dfe2c9780ce268de4205300ede4a16b89 (diff)
Add customizable assert failure handler
Diffstat (limited to 'asl/testing')
-rw-r--r--asl/testing/testing.cpp14
-rw-r--r--asl/testing/testing.hpp6
2 files changed, 15 insertions, 5 deletions
diff --git a/asl/testing/testing.cpp b/asl/testing/testing.cpp
index 623d87b..a4eecd7 100644
--- a/asl/testing/testing.cpp
+++ b/asl/testing/testing.cpp
@@ -21,21 +21,31 @@ void asl::testing::register_test(Test* test)
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("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");
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 RED(S) "\x1b[0;31m" S RESET
#define GREEN(S) "\x1b[0;32m" S RESET
int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
{
+ asl::set_assert_failure_handler(report_assert_failure, nullptr);
+
int fail = 0;
int pass = 0;
diff --git a/asl/testing/testing.hpp b/asl/testing/testing.hpp
index b47c416..c3214a5 100644
--- a/asl/testing/testing.hpp
+++ b/asl/testing/testing.hpp
@@ -9,7 +9,7 @@ struct 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();
@@ -39,8 +39,8 @@ struct Test
#define ASL_TEST_ASSERT(EXPR) \
if (EXPR) {} \
- else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); return; }
+ else { ::asl::testing::report_failure(#EXPR); return; }
#define ASL_TEST_EXPECT(EXPR) \
if (EXPR) {} \
- else { ::asl::testing::report_failure(#EXPR, __FILE__, __LINE__); }
+ else { ::asl::testing::report_failure(#EXPR); }