HyriseSQLParser/test/lib/test.cpp

59 lines
1.5 KiB
C++
Raw Normal View History

2014-12-03 16:32:56 +01:00
#include "test.h"
class TestsManager {
2016-02-27 15:01:06 +01:00
// Note: static initialization fiasco
// http://www.parashift.com/c++-faq-lite/static-init-order.html
// http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html
2014-12-03 16:32:56 +01:00
public:
2016-02-27 15:22:22 +01:00
static std::vector<std::string>& testNames() {
static std::vector<std::string>* _testNames = new std::vector<std::string>;
return *_testNames;
2016-02-27 15:01:06 +01:00
}
static std::vector<void (*)(void)>& tests() {
static std::vector<void (*)(void)>* tests = new std::vector<void (*)(void)>;
return *tests;
}
2014-12-03 16:32:56 +01:00
};
int AddTest(void (*foo)(void), std::string name) {
2016-02-27 15:01:06 +01:00
TestsManager::tests().push_back(foo);
2016-02-27 15:22:22 +01:00
TestsManager::testNames().push_back(name);
2016-02-27 15:01:06 +01:00
return 0;
2014-12-03 16:32:56 +01:00
}
2016-02-27 15:48:20 +01:00
size_t RunTests() {
2016-02-27 15:22:22 +01:00
size_t numFailed = 0;
2016-02-27 15:01:06 +01:00
for (size_t i = 0; i < TestsManager::tests().size(); ++i) {
2016-02-27 15:22:22 +01:00
printf("\033[0;32m{ running}\033[0m %s\n", TestsManager::testNames()[i].c_str());
2016-02-27 15:01:06 +01:00
try {
// Run test
(*TestsManager::tests()[i])();
2016-02-27 15:22:22 +01:00
printf("\033[0;32m{ ok}\033[0m %s\n", TestsManager::testNames()[i].c_str());
2016-02-27 15:01:06 +01:00
} catch (AssertionFailedException& e) {
2016-02-27 15:22:22 +01:00
printf("\033[1;31m{ failed} %s\n", TestsManager::testNames()[i].c_str());
2016-02-27 15:01:06 +01:00
printf("\tAssertion failed: %s\n\033[0m", e.what());
2016-02-27 15:22:22 +01:00
numFailed++;
2016-02-27 15:01:06 +01:00
}
}
2016-02-27 15:48:20 +01:00
return numFailed;
2014-12-03 16:32:56 +01:00
}
int main() {
2016-02-27 15:48:20 +01:00
size_t numFailed = RunTests();
if (numFailed == 0) {
return 0;
} else {
return -1;
}
2014-12-03 16:32:56 +01:00
}