diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2023-01-26 20:38:52 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2023-01-26 20:38:52 +0100 |
commit | 8c54d41a063a595b8663490b2bbd1d06bac209f7 (patch) | |
tree | 96bba4965486d4ed798a6648894e24cd2a61c937 /uunit.h | |
parent | 33b40f71fcea36987cc30b0f57f85aff1b8e54ff (diff) |
Use std::source_location instead of __FILE__ and __LINE__ and add new functions for use instead of macros.source_location
Diffstat (limited to 'uunit.h')
-rw-r--r-- | uunit.h | 63 |
1 files changed, 47 insertions, 16 deletions
@@ -15,6 +15,7 @@ #include <cmath> #include <exception> #include <typeinfo> +#include <source_location> class uUnit { @@ -168,8 +169,8 @@ public: static std::function<void(const char*, const char*, bool)> 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<typename T, typename U> + 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<typename T> static void assert_throws(const char* expected, std::function<void()> expr, @@ -241,9 +255,17 @@ public: throw test_result{"", file, line, ss.str(), {}, {}}; } } - #define uUNIT_ASSERT_THROWS(expected, expr) \ - uUnit::assert_throws<expected>(#expected, [&](){expr;}, __FILE__, __LINE__) - #define uASSERT_THROWS(...) uUNIT_ASSERT_THROWS(__VA_ARGS__) + + template<typename T> + static void assert_throws(std::function<void()> expr, + const std::source_location location = + std::source_location::current()) + { + assert_throws<T>(typeid(T).name(), expr, location.file_name(), location.line()); + } + +#define uUNIT_ASSERT_THROWS(Exc, Expr) assert_throws<Exc>([](){Expr;}) +#define uASSERT_THROWS(Exc, Expr) assert_throws<Exc>([](){Expr;}) protected: template<typename O, typename F> @@ -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<typename F> + 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) |