#pragma once
#include "./CommonSources/commondefinitions.h"
#include <string>
#include <memory>
namespace TestToolBox
{
inline std::string ParamDescription()
{
return "";
}
template <typename T>
std::string ParamDescription(T t)
{
std::ostringstream oss;
oss << t;
return oss.str();
}
template <typename T, typename... Rest>
std::string ParamDescription(T t, Rest... rest)
{
return ParamDescription(t) + ", " + ParamDescription(rest...);
}
class ConditionalFixtureBase
{
public:
virtual ~ConditionalFixtureBase()
{
}
};
using ConditionalFixtureBaseUp = std::unique_ptr<ConditionalFixtureBase>;
class ConditionalFixture
{
public:
USE_AS_SIMPLE_SINGLETON(ConditionalFixture);
~ConditionalFixture(void);
template <typename FixtureClassType, typename... Params>
bool CreateIfNotExisting(Params... params)
{
std::string paramsString = ParamDescription(params...);
std::string delimiter = paramsString.empty() ? "" : " with args ";
std::string idFixture = typeid(FixtureClassType).name() + delimiter + paramsString;
if (!Exists(idFixture))
{
RemoveCurrent();
Add(idFixture, ConditionalFixtureBaseUp(new FixtureClassType(params...)));
return true;
}
return false;
}
bool RemoveCurrent();
private:
struct ConditionalFixtureEntry;
using ConditionalFixtureEntryUp = std::unique_ptr<ConditionalFixtureEntry>;
struct ConditionalFixtureEntry
{
ConditionalFixtureEntry(
std::string const & in_idFixture,
ConditionalFixtureBaseUp in_upConditionalFixtureBase)
: idFixture(in_idFixture)
, upConditionalFixtureBase(std::move(in_upConditionalFixtureBase))
{
}
std::string idFixture;
ConditionalFixtureBaseUp upConditionalFixtureBase;
};
ConditionalFixtureEntryUp m_upConditionalFixtureEntry;
private:
explicit ConditionalFixture(void) {};
bool Exists(std::string const & in_idFixture, int in_level = 1);
void Add(std::string const & in_idFixture, ConditionalFixtureBaseUp in_upFixture);
void WriteLine(std::string const & info);
};
DEFINE_SIMPLE_SINGLETON_POINTER(ConditionalFixture);
}
#define TTB_COND_FIXTURE(FixtureClassType) \
TestToolBox::TheConditionalFixture()->CreateIfNotExisting<FixtureClassType>();
#define TTB_COND_FIXTURE_1(FixtureClassType, p1) \
TestToolBox::TheConditionalFixture()->CreateIfNotExisting<FixtureClassType>(p1);
#define TTB_COND_FIXTURE_2(FixtureClassType, p1, p2) \
TestToolBox::TheConditionalFixture()->CreateIfNotExisting<FixtureClassType>(p1,p2);
#define TTB_COND_FIXTURE_3(FixtureClassType, p1, p2, p3) \
TestToolBox::TheConditionalFixture()->CreateIfNotExisting<FixtureClassType>(p1,p2,p3);
#define TTB_COND_FIXTURE_4(FixtureClassType, p1, p2, p3, p4) \
TestToolBox::TheConditionalFixture()->CreateIfNotExisting<FixtureClassType>(p1,p2,p3,p4);
#define TTB_COND_FIXTURE_CLEANUP() \
TestToolBox::TheConditionalFixture()->RemoveCurrent();