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

98 lines
2.1 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"
2023-01-05 16:40:27 +00:00
#include "TestUiApplication.h"
2022-11-30 18:28:50 +00:00
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
}
2023-01-05 16:40:27 +00:00
TestUiApplication* TestCaseRunner::getTestApplication()
2023-01-05 13:16:52 +00:00
{
return mTestApplication;
}
2023-01-05 16:40:27 +00:00
void TestCaseRunner::setTestApplication(TestUiApplication* app)
2023-01-05 13:16:52 +00:00
{
mTestApplication = app;
}
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-12-06 18:02:43 +00:00
bool TestCaseRunner::run(const std::vector<std::string>& args)
2021-05-23 20:02:38 +00:00
{
2022-12-06 18:02:43 +00:00
std::string test_to_run;
if (args.size() > 0 )
{
test_to_run = args[0];
}
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-12-06 18:02:43 +00:00
if (!test_to_run.empty())
{
if (test_case->getName() != test_to_run)
{
continue;
}
}
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;
try{
test_case->run();
}
catch(...)
{
std::cout << "Failed with exception" << std::endl;
mFailingTests.push_back(test_case->getName());
};
2022-11-30 18:28:50 +00:00
if (sLastTestFailed)
{
2022-12-06 18:02:43 +00:00
std::cout << "Failed at line: " << sFailureLine << std::endl;
2022-11-30 18:28:50 +00:00
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
}