fix static initialisation order issues causing the tests vector to get reset

This commit is contained in:
Crzyrndm 2018-07-10 13:37:34 +12:00
parent 51545734e8
commit bf5105f0a3
2 changed files with 51 additions and 3 deletions

View File

@ -0,0 +1,48 @@
#include "test_suite.hpp"
#include <iostream>
std::vector<std::pair<std::function<void(void)>, std::string>> &test_suite::tests()
{
static std::vector<std::pair<std::function<void(void)>, 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;
}

View File

@ -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<void()> 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::pair<std::function<void(void)>, std::string>> tests;
static std::vector<std::pair<std::function<void(void)>, std::string>> &tests();
};