diff options
Diffstat (limited to 'uunit.h')
-rw-r--r-- | uunit.h | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -14,6 +14,7 @@ #include <fstream> #include <cmath> #include <exception> +#include <typeinfo> class uUnit { @@ -211,6 +212,36 @@ protected: #define uUNIT_ASSERT_EQUAL(expected, value) \ assert_equal(expected, value, __FILE__, __LINE__) + + template<typename T> + void assert_throws(const char* expected, std::function<void()> expr, + const char* file, std::size_t line) + { + try + { + expr(); + std::ostringstream ss; + ss << "throws assertion failed" << std::endl << + "- Expected: " << expected << " to be thrown" << std::endl << + "- Actual : no exception was thrown" << std::endl; + throw test_result{"", file, line, ss.str()}; + } + catch(const T& t) + { + // T was thrown as expected + } + catch(...) + { + std::ostringstream ss; + ss << "throws assertion failed" << std::endl << + "- Expected: " << expected << " to be thrown" << std::endl << + "- Actual : unexpected exception was thrown" << std::endl; + throw test_result{"", file, line, ss.str()}; + } + } + #define uUNIT_ASSERT_THROWS(expected, expr) \ + assert_throws<expected>(#expected, [&](){ expr; }, __FILE__, __LINE__) + private: static std::string sanitize(const std::string& input) { |