Skip to content

Commit

Permalink
Initial matchers support
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Aug 2, 2024
1 parent c17b9e7 commit 5f9ded2
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 17 deletions.
36 changes: 25 additions & 11 deletions Source/UEST/Private/UESTTests.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,51 @@
#include "UEST.h"

TEST_CLASS(TestClass)
TEST(SimpleTest)
{
ASSERT_THAT(true, Is::True);

bool t = true;
ASSERT_THAT(t, Is::True);
ASSERT_THAT(t, Is::Not::False);

bool f = false;
ASSERT_THAT(f, Is::False);
ASSERT_THAT(f, Is::Not::True);

const int* v1 = nullptr;
ASSERT_THAT(v1, Is::Null);

const int v2 = 42;
ASSERT_THAT(&v2, Is::Not::Null);
}

TEST_CLASS(SimpleTestClass)
{
TEST_METHOD(Test1)
{
ASSERT_THAT(true);
ASSERT_THAT(true, Is::True);
}

TEST_METHOD(Test2)
{
ASSERT_THAT(true);
ASSERT_THAT(true, Is::True);
}
};

TEST(Test)
{
ASSERT_THAT(true);
}

TEST(Test, With, Deep, Naming)
{
ASSERT_THAT(true);
ASSERT_THAT(true, Is::True);
}

TEST_CLASS(TestClass, With, Deep, Naming)
{
TEST_METHOD(Test1)
{
ASSERT_THAT(true);
ASSERT_THAT(true, Is::True);
}

TEST_METHOD(Test2)
{
ASSERT_THAT(true);
ASSERT_THAT(true, Is::True);
}
};
97 changes: 91 additions & 6 deletions Source/UEST/Public/UEST.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,105 @@
#pragma once

#include "Assert/CQTestConvert.h"
#include "Misc/AutomationTest.h"

// These asserts are just a stub
template<typename T>
struct IMatcher
{
virtual ~IMatcher() = default;

virtual bool Matches(const T& Value) const = 0;

virtual FString Describe() const = 0;
};

#define AssertThatMsgf(Expression, Message, ...) \
namespace Is
{
template<typename T>
requires std::is_pointer_v<T>
struct Null final : IMatcher<T>
{
virtual bool Matches(const T& Value) const override
{
return Value != nullptr;
}

virtual FString Describe() const override
{
return TEXT("be nullptr");
}
};

template<typename T, typename M>
// TODO: Add requires
struct NotMatcher final : IMatcher<T>
{
M Matcher;

virtual bool Matches(const T& Value) const override
{
return !Matcher.Matches(Value);
}

virtual FString Describe() const override
{
return FString::Printf(TEXT("not %s"), *Matcher.Describe());
}
};

template<typename T>
requires std::same_as<T, bool>
struct True final : IMatcher<T>
{
virtual bool Matches(const T& Value) const override
{
return Value;
}

virtual FString Describe() const override
{
return TEXT("be true");
}
};

template<typename T>
requires std::same_as<T, bool>
struct False final : IMatcher<T>
{
virtual bool Matches(const T& Value) const override
{
return !Value;
}

virtual FString Describe() const override
{
return TEXT("be false");
}
};

namespace Not
{
template<typename T>
using Null = NotMatcher<T, Null<T>>;

template<typename T>
using True = False<T>;

template<typename T>
using False = True<T>;
}
}

#define ASSERT_THAT(Value, Matcher) \
do \
{ \
if (!ensureAlwaysMsgf((Expression), TEXT(Message), ##__VA_ARGS__)) \
Matcher<decltype(Value)> _Matcher; \
if (!ensureAlwaysMsgf(_Matcher.Matches(Value), TEXT("%s must %s"), *CQTestConvert::ToString(Value), *_Matcher.Describe())) \
{ \
return; \
} \
} while (false)

#define ASSERT_THAT(Expression) AssertThatMsgf(Expression, "expected expression to be true")

class UEST_API FUESTTestBase : public FAutomationTestBase
{
protected:
Expand All @@ -41,7 +126,7 @@ struct FUESTMethodRegistrar
{
FUESTMethodRegistrar(FString Name, FUESTTestBase& Test, FSimpleDelegate Delegate)
{
Test.TestMethods.Add(Name, Delegate);
Test.TestMethods.Add(MoveTemp(Name), MoveTemp(Delegate));
}
};

Expand Down
1 change: 1 addition & 0 deletions Source/UEST/UEST.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public UEST(ReadOnlyTargetRules Target) : base(Target)
"Boost",
"Core",
"CoreUObject",
"CQTest",
"Engine",
}
);
Expand Down
6 changes: 6 additions & 0 deletions UEST.uplugin
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"Type": "Runtime",
"LoadingPhase": "Default"
}
],
"Plugins": [
{
"Name": "CQTest",
"Enabled": true
}
]
}

0 comments on commit 5f9ded2

Please sign in to comment.