summaryrefslogtreecommitdiff
path: root/uunit.h
diff options
context:
space:
mode:
Diffstat (limited to 'uunit.h')
-rw-r--r--uunit.h63
1 files changed, 47 insertions, 16 deletions
diff --git a/uunit.h b/uunit.h
index ec88873..ec182a8 100644
--- a/uunit.h
+++ b/uunit.h
@@ -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)