stuff-from-scratch/test/test_utils/TestCaseRunner.cpp

65 lines
1.4 KiB
C++
Raw Normal View History

2021-05-23 20:02:38 +00:00
#include "TestCaseRunner.h"
2022-11-30 18:28:50 +00:00
#include "FileLogger.h"
2021-05-23 20:02:38 +00:00
#include <vector>
#include <iostream>
2022-11-30 18:28:50 +00:00
bool TestCaseRunner::sLastTestFailed = false;
std::string TestCaseRunner::sFailureLine = {};
TestCaseRunner::TestCaseRunner()
{
}
2022-11-29 18:00:19 +00:00
TestCaseRunner::~TestCaseRunner()
2021-05-23 20:02:38 +00:00
{
2022-11-30 18:28:50 +00:00
}
TestCaseRunner& TestCaseRunner::getInstance()
{
static TestCaseRunner instance;
return instance;
2022-11-29 18:00:19 +00:00
}
void TestCaseRunner::addTestCase(const std::string& label, const std::string& tag, TestCase::TestCaseFunction func)
{
auto test_case = new TestCase(label, tag, func);
mCases.push_back(test_case);
2021-05-23 20:02:38 +00:00
}
2022-11-30 18:28:50 +00:00
void TestCaseRunner::markTestFailure(const std::string& line)
{
sLastTestFailed = true;
sFailureLine = line;
}
2022-11-29 18:00:19 +00:00
bool TestCaseRunner::run()
2021-05-23 20:02:38 +00:00
{
2022-11-30 18:28:50 +00:00
FileLogger::GetInstance().disable();
2022-11-29 18:00:19 +00:00
for (auto test_case : mCases)
{
2022-11-30 18:28:50 +00:00
sLastTestFailed = false;
2022-11-29 18:00:19 +00:00
std::cout << "TestFramework: Running Test - " << test_case->getName() << std::endl;
test_case->run();
2022-11-30 18:28:50 +00:00
if (sLastTestFailed)
{
std::cout << "Failed at line: " << sLastTestFailed << std::endl;
mFailingTests.push_back(test_case->getName());
}
2022-11-29 18:00:19 +00:00
}
2022-11-30 18:28:50 +00:00
if (mFailingTests.size() > 0)
2021-05-23 20:02:38 +00:00
{
2022-11-30 18:28:50 +00:00
std::cout << mFailingTests.size() << " failing tests: " << std::endl;
for(const auto& name : mFailingTests)
2021-05-23 20:02:38 +00:00
{
2022-11-30 18:28:50 +00:00
std::cout << name << std::endl;
2021-05-23 20:02:38 +00:00
}
}
2022-11-30 18:28:50 +00:00
2022-11-29 18:00:19 +00:00
return true;
2021-05-23 20:02:38 +00:00
}