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;
|
2023-01-16 18:16:14 +00:00
|
|
|
|
2023-01-25 16:51:36 +00:00
|
|
|
try
|
|
|
|
{
|
2023-01-16 18:16:14 +00:00
|
|
|
test_case->run();
|
|
|
|
}
|
2023-01-25 16:51:36 +00:00
|
|
|
catch (const std::runtime_error& re)
|
|
|
|
{
|
|
|
|
std::cout << "Failed with runtime error: " << re.what() << std::endl;
|
|
|
|
mFailingTests.push_back(test_case->getName());
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
std::cout << "Failed with exception: " << e.what() << std::endl;
|
|
|
|
mFailingTests.push_back(test_case->getName());
|
|
|
|
}
|
2023-01-16 18:16:14 +00:00
|
|
|
catch(...)
|
|
|
|
{
|
2023-01-25 16:51:36 +00:00
|
|
|
std::cout << "Failed with unknown exception" << std::endl;
|
2023-01-16 18:16:14 +00:00
|
|
|
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
|
|
|
}
|