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

90 lines
1.8 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-12-18 10:16:31 +00:00
#include "ConsoleLogger.h"
//#include "TestUiApplication.h"
2021-05-23 20:02:38 +00:00
2022-11-30 18:28:50 +00:00
bool TestCaseRunner::sLastTestFailed = false;
2023-12-18 10:16:31 +00:00
String TestCaseRunner::sFailureLine = {};
2022-11-30 18:28:50 +00:00
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-12-18 10:16:31 +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;
}
2023-12-18 10:16:31 +00:00
*/
2023-01-05 13:16:52 +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
}
2023-12-18 10:16:31 +00:00
void TestCaseRunner::addTestCase(const String& label, const String& tag, TestCase::TestCaseFunction func)
2022-11-29 18:00:19 +00:00
{
auto test_case = new TestCase(label, tag, func);
mCases.push_back(test_case);
2021-05-23 20:02:38 +00:00
}
2023-12-18 10:16:31 +00:00
void TestCaseRunner::markTestFailure(const String& line)
2022-11-30 18:28:50 +00:00
{
sLastTestFailed = true;
sFailureLine = line;
}
2023-12-18 10:16:31 +00:00
bool TestCaseRunner::run(const Vector<String>& args)
2021-05-23 20:02:38 +00:00
{
2023-12-18 10:16:31 +00:00
String test_to_run;
2022-12-06 18:02:43 +00:00
if (args.size() > 0 )
{
test_to_run = args[0];
}
2023-12-18 10:16:31 +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;
2023-12-18 10:16:31 +00:00
ConsoleLogger::logLine("TestFramework: Running Test - %s", test_case->getName().raw());
test_case->run();
2022-11-30 18:28:50 +00:00
if (sLastTestFailed)
{
2023-12-18 10:16:31 +00:00
ConsoleLogger::logLine("Failed at line %s", sFailureLine.raw());
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
{
2023-12-18 10:16:31 +00:00
ConsoleLogger::logLine("%d failing tests", mFailingTests.size());
2022-11-30 18:28:50 +00:00
for(const auto& name : mFailingTests)
2021-05-23 20:02:38 +00:00
{
2023-12-18 10:16:31 +00:00
ConsoleLogger::logLine(name);
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
}