From 1bd908531600b1392d98f2e3bfa21e3227df670b Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 26 Oct 2020 15:33:28 +0100 Subject: Add exception catching to make sure, a test that results in an exception being thrown, will result in a test case failure. --- uunit.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/uunit.h b/uunit.h index de9c93e..c4c53ee 100644 --- a/uunit.h +++ b/uunit.h @@ -13,6 +13,7 @@ #include #include #include +#include class uUnit { @@ -47,6 +48,7 @@ public: std::string file; std::size_t line; std::string msg; + std::string failure_type; int id; }; @@ -70,7 +72,7 @@ public: try { suite->setup(); - test.first(); + test.func(); suite->teardown(); } catch(test_result& result) @@ -78,18 +80,41 @@ public: std::cout << "F"; fflush(stdout); result.id = test_num; - result.func = test.second; + result.func = test.name; + result.failure_type = "Assertion"; failed_tests.push_back(result); ++failed; continue; } catch(...) { - break; // Uncaught exception. Do not proceed with this test. + test_result result; + std::cout << "F"; + fflush(stdout); + result.id = test_num; + result.func = test.name; + result.file = test.file; + result.line = 0; + try + { + throw; + } + catch(std::exception& e) + { + result.msg = std::string("Uncaught exception: ") + e.what(); + } + catch(...) + { + result.msg = "Uncaught exception without std::exception type"; + } + result.failure_type = "Exception"; + failed_tests.push_back(result); + ++failed; + continue; } std::cout << "."; fflush(stdout); - test_result result{test.second}; + test_result result{test.name}; result.id = test_num; successful_tests.push_back(result); } @@ -99,11 +124,12 @@ public: std::endl; out << "" << std::endl; out << " " << std::endl; - for(auto test : failed_tests) + for(const auto& test : failed_tests) { - out << " " << std::endl; + out << " " << std::endl; // constexpr newline cross-platform specifik out << " " << sanitize(test.func) << "" << std::endl; - out << " Assertion" << std::endl; + out << " " << sanitize(test.failure_type) << + "" << std::endl; out << " " << std::endl; out << " " << sanitize(test.file) << "" << std::endl; out << " " << test.line << "" << std::endl; @@ -114,7 +140,7 @@ public: } out << " " << std::endl; out << " " << std::endl; - for(auto test : successful_tests) + for(const auto& test : successful_tests) { out << " " << std::endl; out << " " << sanitize(test.func) << "" << std::endl; @@ -138,12 +164,12 @@ public: protected: template - void registerTest(O* obj, const F& fn, const char* name) + void registerTest(O* obj, const F& fn, const char* name, const char* file) { - tests.emplace_back(std::make_pair(std::bind(fn, obj), name)); + tests.push_back({std::bind(fn, obj), name, file}); } #define uUNIT_TEST(func) \ - registerTest(this, &func, #func) + registerTest(this, &func, #func, __FILE__) void u_assert(bool value, const char* expr, const char* file, std::size_t line) @@ -219,7 +245,15 @@ private: static uUnit* suite_list; uUnit* next_unit{nullptr}; - std::vector, const char*>> tests; + + struct test_t + { + std::function func; + const char* name; + const char* file; + }; + + std::vector tests; }; #ifdef uUNIT_MAIN -- cgit v1.2.3