Skip to content

Instantly share code, notes, and snippets.

@doctor-g
Created July 23, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctor-g/3624bb15b85a6bc660253ef7413b3719 to your computer and use it in GitHub Desktop.
Save doctor-g/3624bb15b85a6bc660253ef7413b3719 to your computer and use it in GitHub Desktop.
#include "AutomationTest.h"
#include "HighScoreTable.h"
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FHighScoreTableTest, "UnitTests.HighScoreTable", EAutomationTestFlags::EditorContext | EAutomationTestFlags::EngineFilter)
#define Describe(Desc) Description = Desc;
#define GivenAnEmptyTable() Table = NewObject<UHighScoreTable>(); Scores.Empty();
#define GivenATableContaining(...) GivenAnEmptyTable() Scores.Append(TArray<int32> { __VA_ARGS__}); for (int32 Value : Scores) Table->AddPotentialHighScore(Value);
#define WhenScoreIsAdded(X) Result=Table->AddPotentialHighScore(X);
#define ThenIndexOfMostRecentEntryIs(Expected) TestEqual(TEXT("Most recent index"), Result, Expected);
#define ThenTheSizeOfTheTableIs(Expected) TestEqual(TEXT("Size of the table"), Table->GetTable().Num(), Expected);
bool FHighScoreTableTest::RunTest(const FString& Parameters)
{
FString Description;
UHighScoreTable* Table;
int32 Result;
TArray<int32> Scores;
Describe("First entry is in first position");
GivenAnEmptyTable();
WhenScoreIsAdded(1);
ThenIndexOfMostRecentEntryIs(0);
Describe("Second entry in single-element table");
GivenATableContaining(2);
WhenScoreIsAdded(1);
ThenIndexOfMostRecentEntryIs(1);
Describe("New first entry in single-element table")
GivenATableContaining(1);
WhenScoreIsAdded(2);
ThenIndexOfMostRecentEntryIs(0);
Describe("Lowest item is last in the list");
GivenATableContaining(5,4,3,2);
WhenScoreIsAdded(1);
ThenIndexOfMostRecentEntryIs(4);
Describe("Low score is rejected");
GivenATableContaining(5, 4, 3, 2, 1);
WhenScoreIsAdded(0);
ThenIndexOfMostRecentEntryIs(-1);
Describe("New high score");
GivenATableContaining(5, 4, 3, 2, 1);
WhenScoreIsAdded(6);
ThenIndexOfMostRecentEntryIs(0);
Describe("The high score table should never have more than five entries when the score is added to the top.");
GivenATableContaining(5, 4, 3, 2, 1);
WhenScoreIsAdded(6);
ThenTheSizeOfTheTableIs(5);
Describe("The high score table should never have more than five entries when the score would be added to the bottom");
GivenATableContaining(6, 5, 4, 3, 2);
WhenScoreIsAdded(1);
ThenTheSizeOfTheTableIs(5);
return true;
}
#undef Describe
#undef GivenAnEmptyTable
#undef GivenATableContaining
#undef WhenScoreIsAdded
#undef ThenIndexOfMostRecentEntryIs
#undef ThenTheSizeOfTheTableIs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment