From 840ddd121e75a31d67bf52bee5579b5510e34c65 Mon Sep 17 00:00:00 2001 From: Jimmy Cushnie Date: Sun, 29 Sep 2024 16:28:42 -0400 Subject: [PATCH] Expand tests --- .../SaveLoad_PolymorphismTests.cs | 43 ++++++++++++++++++- JECS.Tests/TestUtilities.cs | 27 +++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/JECS.Tests/SaveLoad tests/SaveLoad_PolymorphismTests.cs b/JECS.Tests/SaveLoad tests/SaveLoad_PolymorphismTests.cs index 13aa210..23371ab 100644 --- a/JECS.Tests/SaveLoad tests/SaveLoad_PolymorphismTests.cs +++ b/JECS.Tests/SaveLoad tests/SaveLoad_PolymorphismTests.cs @@ -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(value); + } + + [TestMethod] + public void SaveLoad_Interface() + { + IBaseClassInterface value = new ChildClass1() + { + StringValue = "test1", + IntValue = 532, + }; + + TestUtilities.PerformSaveLoadTest(value); + } + + [TestMethod] + public void SaveLoad_Polymorphism_Null() + { + TestUtilities.PerformSaveLoadTest(null); + } + + [TestMethod] + public void SaveLoad_Interface_Null() + { + TestUtilities.PerformSaveLoadTest(null); + } + + [TestMethod] public void SaveLoad_PolymorphismArray() { @@ -20,7 +57,8 @@ public void SaveLoad_PolymorphismArray() { StringValue = "test222", FloatValue = 101.0101f, - } + }, + null, }; TestUtilities.PerformSaveLoadTest(array, CollectionAssert.AreEqual); @@ -40,7 +78,8 @@ public void SaveLoad_InterfaceArray() { StringValue = "test222", FloatValue = 101.0101f, - } + }, + null, }; TestUtilities.PerformSaveLoadTest(array, CollectionAssert.AreEqual); diff --git a/JECS.Tests/TestUtilities.cs b/JECS.Tests/TestUtilities.cs index 40fb4a8..1284eb2 100644 --- a/JECS.Tests/TestUtilities.cs +++ b/JECS.Tests/TestUtilities.cs @@ -12,11 +12,12 @@ public static void PerformSaveLoadTest(T SAVED_VALUE) public delegate void AssertTwoItemsAreTheSameDelegate(T item1, T item2); public static void PerformSaveLoadTest(T SAVED_VALUE, AssertTwoItemsAreTheSameDelegate 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(SAVED_VALUE_KEY); @@ -24,11 +25,33 @@ public static void PerformSaveLoadTest(T SAVED_VALUE, AssertTwoItemsAreTheSam } 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(); + + assertTwoItemsAreTheSameDelegate.Invoke(SAVED_VALUE, loadedValue); + } + finally + { + Console.WriteLine("\n\nSave as whole file | Contents of file:"); + Console.WriteLine("```"); + Console.WriteLine(file2.GetRawText()); + Console.WriteLine("```"); + } }