Skip to content

Commit

Permalink
Expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyCushnie committed Sep 29, 2024
1 parent 3f57e7e commit 840ddd1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
43 changes: 41 additions & 2 deletions JECS.Tests/SaveLoad tests/SaveLoad_PolymorphismTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@ namespace JECS.Tests
[TestClass]
public class SaveLoad_PolymorphismTests
{
[TestMethod]
public void SaveLoad_Polymorphism()
{
AbstractBaseClass value = new ChildClass1()
{
StringValue = "test1",
IntValue = 532,
};

TestUtilities.PerformSaveLoadTest<AbstractBaseClass>(value);
}

[TestMethod]
public void SaveLoad_Interface()
{
IBaseClassInterface value = new ChildClass1()
{
StringValue = "test1",
IntValue = 532,
};

TestUtilities.PerformSaveLoadTest<IBaseClassInterface>(value);
}

[TestMethod]
public void SaveLoad_Polymorphism_Null()
{
TestUtilities.PerformSaveLoadTest<AbstractBaseClass>(null);
}

[TestMethod]
public void SaveLoad_Interface_Null()
{
TestUtilities.PerformSaveLoadTest<IBaseClassInterface>(null);
}


[TestMethod]
public void SaveLoad_PolymorphismArray()
{
Expand All @@ -20,7 +57,8 @@ public void SaveLoad_PolymorphismArray()
{
StringValue = "test222",
FloatValue = 101.0101f,
}
},
null,
};

TestUtilities.PerformSaveLoadTest(array, CollectionAssert.AreEqual);
Expand All @@ -40,7 +78,8 @@ public void SaveLoad_InterfaceArray()
{
StringValue = "test222",
FloatValue = 101.0101f,
}
},
null,
};

TestUtilities.PerformSaveLoadTest(array, CollectionAssert.AreEqual);
Expand Down
27 changes: 25 additions & 2 deletions JECS.Tests/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,46 @@ public static void PerformSaveLoadTest<T>(T SAVED_VALUE)
public delegate void AssertTwoItemsAreTheSameDelegate<T>(T item1, T item2);
public static void PerformSaveLoadTest<T>(T SAVED_VALUE, AssertTwoItemsAreTheSameDelegate<T> assertTwoItemsAreTheSameDelegate)
{
const string SAVED_VALUE_KEY = "save/load test key";
var file = new MemoryDataFile();

try
{
const string SAVED_VALUE_KEY = "JECS_TEST_KEY";

file.Set(SAVED_VALUE_KEY, SAVED_VALUE);
var loadedValue = file.Get<T>(SAVED_VALUE_KEY);

assertTwoItemsAreTheSameDelegate.Invoke(SAVED_VALUE, loadedValue);
}
finally
{
Console.WriteLine("Contents of file:");
Console.WriteLine("Save under a top-level key | Contents of file:");
Console.WriteLine("```");
Console.WriteLine(file.GetRawText());
Console.WriteLine("```");
}


// There are two ways a value can be saved: under a key, and as a whole file.
// I want all SaveLoad tests to test both methods of saving.
// Now ideally these would be separate tests so that it's easy to see which way of saving went wrong in the case that it's just one of them.
// Unfortunately I can't see an easy way to do that.

var file2 = new MemoryDataFile();
try
{
file2.SaveAsObject(SAVED_VALUE);
var loadedValue = file2.GetAsObject<T>();

assertTwoItemsAreTheSameDelegate.Invoke(SAVED_VALUE, loadedValue);
}
finally
{
Console.WriteLine("\n\nSave as whole file | Contents of file:");
Console.WriteLine("```");
Console.WriteLine(file2.GetRawText());
Console.WriteLine("```");
}
}


Expand Down

0 comments on commit 840ddd1

Please sign in to comment.