Skip to content

Commit

Permalink
Add registration of TEST_METHOD
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Aug 2, 2024
1 parent 3c27e0c commit c17b9e7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
25 changes: 22 additions & 3 deletions Source/UEST/Private/UEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,32 @@ uint32 FUESTTestBase::GetTestFlags() const

void FUESTTestBase::GetTests(TArray<FString>& OutBeautifiedNames, TArray<FString>& OutTestCommands) const
{
OutBeautifiedNames.Add(GetBeautifiedTestName());
OutTestCommands.Add(FString());
if (TestMethods.Num())
{
TestMethods.GenerateKeyArray(OutBeautifiedNames);
TestMethods.GenerateKeyArray(OutTestCommands);
}
else
{
OutBeautifiedNames.Add(GetBeautifiedTestName());
OutTestCommands.AddDefaulted(1);
}
}

bool FUESTTestBase::RunTest(const FString& Parameters)
{
DoTest(Parameters);
if (TestMethods.Num())
{
if (const auto* Method = TestMethods.Find(Parameters); ensure(Method))
{
Method->Execute();
}
}
else
{
DoTest(Parameters);
}

return true;
}

Expand Down
39 changes: 28 additions & 11 deletions Source/UEST/Public/UEST.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "Misc/AutomationTest.h"

// These asserts are just a stub

#define AssertThatMsgf(Expression, Message, ...) \
Expand Down Expand Up @@ -29,6 +31,18 @@ class UEST_API FUESTTestBase : public FAutomationTestBase
virtual void DoTest(const FString& Parameters)
{
}

public:
// TODO: Can we do this without delegates, just using method pointers?
TMap<FString, FSimpleDelegate> TestMethods;
};

struct FUESTMethodRegistrar
{
FUESTMethodRegistrar(FString Name, FUESTTestBase& Test, FSimpleDelegate Delegate)
{
Test.TestMethods.Add(Name, Delegate);
}
};

template<typename TClass>
Expand All @@ -51,30 +65,33 @@ struct TUESTInstantiator
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>

#define UEST_CONCAT_SEQ_1(seq, op) BOOST_PP_SEQ_HEAD(seq)
#define UEST_CONCAT_SEQ_N(seq, op) BOOST_PP_SEQ_FOLD_LEFT(op, BOOST_PP_SEQ_HEAD(seq), BOOST_PP_SEQ_TAIL(seq))
#define UEST_CONCAT_SEQ(seq, op) BOOST_PP_IF(BOOST_PP_GREATER(BOOST_PP_SEQ_SIZE(seq), 1), UEST_CONCAT_SEQ_N, UEST_CONCAT_SEQ_1)(seq, op)
#define UEST_CONCAT_SEQ_1(seq, fold_op, elem_op) elem_op(BOOST_PP_SEQ_HEAD(seq))
#define UEST_CONCAT_SEQ_N(seq, fold_op, elem_op) BOOST_PP_SEQ_FOLD_LEFT(fold_op, elem_op(BOOST_PP_SEQ_HEAD(seq)), BOOST_PP_SEQ_TAIL(seq))
#define UEST_CONCAT_SEQ(seq, fold_op, elem_op) BOOST_PP_IF(BOOST_PP_GREATER(BOOST_PP_SEQ_SIZE(seq), 1), UEST_CONCAT_SEQ_N, UEST_CONCAT_SEQ_1)(seq, fold_op, elem_op)

#define UEST_PRETTY_NAME_OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT(., x))
#define UEST_PRETTY_NAME(...) UEST_CONCAT_SEQ(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__), UEST_PRETTY_NAME_OP)
#define UEST_PRETTY_NAME_ELEM_OP(x) BOOST_PP_STRINGIZE(x)
#define UEST_PRETTY_NAME_FOLD_OP(s, state, x) state "." UEST_PRETTY_NAME_ELEM_OP(x)
#define UEST_PRETTY_NAME(...) UEST_CONCAT_SEQ(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__), UEST_PRETTY_NAME_FOLD_OP, UEST_PRETTY_NAME_ELEM_OP)

#define UEST_CLASS_NAME_OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT(_, x))
#define UEST_CLASS_NAME(...) UEST_CONCAT_SEQ(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__), UEST_CLASS_NAME_OP)
#define UEST_CLASS_NAME_ELEM_OP(x) x
#define UEST_CLASS_NAME_FOLD_OP(s, state, x) BOOST_PP_CAT(state, BOOST_PP_CAT(_, x))
#define UEST_CLASS_NAME(...) UEST_CONCAT_SEQ(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__), UEST_CLASS_NAME_FOLD_OP, UEST_CLASS_NAME_ELEM_OP)

#define TEST_CLASS_WITH_BASE_IMPL(BaseClass, ClassName, PrettyName) \
struct BOOST_PP_CAT(F, BOOST_PP_CAT(ClassName, Impl)); \
struct BOOST_PP_CAT(F, ClassName) : public BaseClass \
{ \
typedef BOOST_PP_CAT(F, BOOST_PP_CAT(ClassName, Impl)) ThisClass; \
typedef BaseClass Super; \
BOOST_PP_CAT(F, ClassName)() \
: Super(UE_MODULE_NAME "." BOOST_PP_STRINGIZE(PrettyName)) \
: Super(TEXT(UE_MODULE_NAME "." PrettyName)) \
{ \
} \
/* This using is needed so Rider understands that we are a runnable test */ \
using Super::RunTest; \
virtual FString GetBeautifiedTestName() const override \
{ \
return UE_MODULE_NAME "." BOOST_PP_STRINGIZE(PrettyName); \
return TEXT(UE_MODULE_NAME "." PrettyName); \
} \
virtual FString GetTestSourceFileName() const override \
{ \
Expand Down Expand Up @@ -132,5 +149,5 @@ struct TUESTInstantiator
*/
#define TEST_CLASS(...) TEST_CLASS_WITH_BASE(FUESTTestBase, __VA_ARGS__)

// TODO: This is just a stub, we need to register method in test class
#define TEST_METHOD(MethodName) void MethodName()
#define TEST_METHOD(MethodName) FUESTMethodRegistrar reg##MethodName{TEXT(#MethodName), *this, FSimpleDelegate::CreateRaw(this, &ThisClass::MethodName)}; \
void MethodName()

0 comments on commit c17b9e7

Please sign in to comment.