Add test fixture.

This commit is contained in:
James Grogan 2022-11-29 18:00:19 +00:00
parent af6fad72eb
commit d6d4319e21
37 changed files with 421 additions and 279 deletions

View file

@ -1,13 +1,45 @@
#pragma once
#include <memory>
#include <string>
#include <functional>
class TestCase
{
public:
TestCase(){};
using TestCaseFunction = std::function<void()>;
TestCase(const std::string& name, const std::string& tags, TestCaseFunction func)
: mName(name),
mTags(tags),
mTestFunction(func)
{
};
virtual ~TestCase() = default;
TestCase(const TestCase&) = delete;
virtual bool Run() {return false;};
const std::string& getName() const
{
return mName;
}
const std::string& getTags() const
{
return mTags;
}
virtual void run()
{
mTestFunction();
};
public:
TestCaseFunction mTestFunction{nullptr};
private:
std::string mName;
std::string mTags;
};
using TestCasePtr = std::unique_ptr<TestCase>;

View file

@ -3,24 +3,42 @@
#include <vector>
#include <iostream>
void TestCaseRunner::AddTestCase(const std::string& label, TestCasePtr testCase)
TestCaseRunner::~TestCaseRunner()
{
mCases.push_back({label, std::move(testCase)});
for (auto testCase : mCases)
{
//delete testCase;
}
}
bool TestCaseRunner::Run()
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);
}
bool TestCaseRunner::run()
{
for (auto test_case : mCases)
{
std::cout << "TestFramework: Running Test - " << test_case->getName() << std::endl;
test_case->run();
}
bool testsPassed = true;
/*
for(const auto& test : mCases)
{
std::cout << "Running " << test.first << std::endl;
const auto result = test.second->Run();
std::cout << "Running " << test->getName() << std::endl;
const auto result = test->Run();
if (!result)
{
std::cout << test.first << " Failed" << std::endl;
std::cout << test->getName() << " Failed" << std::endl;
testsPassed = false;
break;
}
}
return testsPassed;
*/
return true;
}

View file

@ -8,12 +8,20 @@
class TestCaseRunner
{
public:
TestCaseRunner() = default;
void AddTestCase(const std::string& label, TestCasePtr testCase);
static TestCaseRunner& getInstance()
{
static TestCaseRunner instance;
return instance;
}
bool Run();
~TestCaseRunner();
void addTestCase(const std::string& label, const std::string& tag, TestCase::TestCaseFunction func);
bool run();
private:
using TestInstance = std::pair<std::string, TestCasePtr>;
std::vector<TestInstance> mCases;
std::vector<TestCase*> mCases;
};

View file

@ -0,0 +1,18 @@
#pragma once
#include "TestCaseRunner.h"
struct Holder
{
Holder(const std::string& name, const std::string& tags, std::function<void()> func)
{
TestCaseRunner::getInstance().addTestCase(name, tags, func);
}
};
#define TEST_CASE(NAME, TAGS) \
static void Test##NAME(); \
namespace{Holder holder##NAME( #NAME, #TAGS, &Test##NAME );} \
static void Test##NAME() \