From 8c54d41a063a595b8663490b2bbd1d06bac209f7 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 26 Jan 2023 20:38:52 +0100 Subject: Use std::source_location instead of __FILE__ and __LINE__ and add new functions for use instead of macros. --- uunit.h | 63 +++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 16 deletions(-) (limited to 'uunit.h') diff --git a/uunit.h b/uunit.h index ec88873..ec182a8 100644 --- a/uunit.h +++ b/uunit.h @@ -15,6 +15,7 @@ #include #include #include +#include class uUnit { @@ -168,8 +169,8 @@ public: static std::function status_cb; - static void u_assert(bool value, const char* expr, - const char* file, std::size_t line) + static void assert(bool value, const char* expr, + const char* file, std::size_t line) { if(!value) { @@ -179,10 +180,16 @@ public: throw test_result{"", file, line, ss.str(), {}, {}}; } } - //! Convenience macro to pass along filename and linenumber - #define uUNIT_ASSERT(value) \ - uUnit::u_assert(value, #value, __FILE__, __LINE__) - #define uASSERT(...) uUNIT_ASSERT(__VA_ARGS__) + + static void assert(bool value, const char* expr, + const std::source_location location = + std::source_location::current()) + { + assert(value, expr, location.file_name(), location.line()); + } + +#define uUNIT_ASSERT assert +#define uASSERT assert static void assert_equal(double expected, double value, const char* file, std::size_t line) @@ -210,10 +217,17 @@ public: throw test_result{"", file, line, ss.str(), {}, {}}; } } - //! Convenience macro to pass along filename and linenumber - #define uUNIT_ASSERT_EQUAL(expected, value) \ - uUnit::assert_equal(expected, value, __FILE__, __LINE__) - #define uASSERT_EQUAL(...) uUNIT_ASSERT_EQUAL(__VA_ARGS__) + + template + static void assert_equal(const T& expected, const U& value, + const std::source_location location = + std::source_location::current()) + { + assert_equal(expected, value, location.file_name(), location.line()); + } + +#define uUNIT_ASSERT_EQUAL assert_equal +#define uASSERT_EQUAL assert_equal template static void assert_throws(const char* expected, std::function expr, @@ -241,9 +255,17 @@ public: throw test_result{"", file, line, ss.str(), {}, {}}; } } - #define uUNIT_ASSERT_THROWS(expected, expr) \ - uUnit::assert_throws(#expected, [&](){expr;}, __FILE__, __LINE__) - #define uASSERT_THROWS(...) uUNIT_ASSERT_THROWS(__VA_ARGS__) + + template + static void assert_throws(std::function expr, + const std::source_location location = + std::source_location::current()) + { + assert_throws(typeid(T).name(), expr, location.file_name(), location.line()); + } + +#define uUNIT_ASSERT_THROWS(Exc, Expr) assert_throws([](){Expr;}) +#define uASSERT_THROWS(Exc, Expr) assert_throws([](){Expr;}) protected: template @@ -251,9 +273,18 @@ protected: { tests.emplace_back(std::bind(fn, obj), name, file); } - #define uUNIT_TEST(func) \ - registerTest(this, &func, #func, __FILE__) - #define uTEST(...) uUNIT_TEST(__VA_ARGS__) + + template + void test(void (F::*fn)(), const char* name, + const std::source_location location = + std::source_location::current()) + { + registerTest((F*)(this), fn, name, location.file_name()); + } + +#define uUNIT_TEST(func) \ + registerTest(this, &func, #func, __FILE__) +#define uTEST(...) uUNIT_TEST(__VA_ARGS__) private: static std::string sanitize(const std::string& input) -- cgit v1.2.3