From bf5105f0a3bf7928b153da2f4cd768869a63d51d Mon Sep 17 00:00:00 2001 From: Crzyrndm Date: Tue, 10 Jul 2018 13:37:34 +1200 Subject: [PATCH] fix static initialisation order issues causing the tests vector to get reset --- tests/helpers/test_suite.cpp | 48 ++++++++++++++++++++++++++++++++++++ tests/helpers/test_suite.hpp | 6 ++--- 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 tests/helpers/test_suite.cpp diff --git a/tests/helpers/test_suite.cpp b/tests/helpers/test_suite.cpp new file mode 100644 index 00000000..336f48b3 --- /dev/null +++ b/tests/helpers/test_suite.cpp @@ -0,0 +1,48 @@ +#include "test_suite.hpp" +#include + +std::vector, std::string>> &test_suite::tests() +{ + static std::vector, std::string>> all_tests; + return all_tests; +} + +std::string build_name(const std::string &pretty, const std::string &method) +{ + return pretty.substr(0, pretty.find("::") + 2) + method; +} + +test_status test_suite::go() +{ + test_status status; + + for (auto test : tests()) + { + try + { + test.first(); + std::cout << '.'; + status.tests_passed++; + } + catch (std::exception &ex) + { + std::string fail_msg = test.second + " failed with:\n" + std::string(ex.what()); + std::cout << "*\n" + << fail_msg << '\n'; + status.tests_failed++; + status.failures.push_back(fail_msg); + } + catch (...) + { + std::cout << "*\n" + << test.second << " failed\n"; + status.tests_failed++; + status.failures.push_back(test.second); + } + + std::cout.flush(); + status.tests_run++; + } + + return status; +} \ No newline at end of file diff --git a/tests/helpers/test_suite.hpp b/tests/helpers/test_suite.hpp index 36010c10..7d0a116d 100644 --- a/tests/helpers/test_suite.hpp +++ b/tests/helpers/test_suite.hpp @@ -36,7 +36,7 @@ public: { test_status status; - for (auto test : tests) + for (auto test : tests()) { try { @@ -69,9 +69,9 @@ public: protected: static void register_test_internal(std::function t, const std::string &function) { - tests.push_back(std::make_pair(t, function)); + tests().push_back(std::make_pair(t, function)); } private: - static std::vector, std::string>> tests; + static std::vector, std::string>> &tests(); };