using NUnit.Framework; using System; using System.Collections.Generic; using System.Runtime.Serialization; using UnityEngine; /// /// Test the Progress class /// [TestFixture] public class ProgressTest { /// /// Reference to the progress object to be tested /// private Progress progress; /// /// A dummy serializable struct to perform test operations on /// [Serializable] private struct SerializableStruct { public int r, g, b; public float x, y, z; } /// /// A dummy non-serializable struct to perform test operations on /// private struct NonSerializableStruct { public int r, g, b; public float x, y, z; } /// /// Setup the tests /// [SetUp] public void Setup_Progress() { progress = new Progress(); } /// /// Test for creation of a new progress /// [Test] public void Test_New_Progress() { Assert.IsNotNull(progress); } /// /// Test whether invalid data will not be added /// [Test] public void Test_Progress_Add_InvalidData() { Assert.IsFalse(progress.AddOrUpdate("key", null)); } /// /// Test whether a duplicated key will be added /// [Test] public void Test_Progress_Add_DuplicateKey() { progress.AddOrUpdate("key 1", 0); Assert.IsTrue(progress.AddOrUpdate("key 1", 1)); } /// /// Test whether a int value can be added /// [Test] public void Test_Progress_Add_Int() { Assert.IsTrue(progress.AddOrUpdate("key", 1)); } /// /// Test whether a double value can be added /// [Test] public void Test_Progress_Add_Double() { Assert.IsTrue(progress.AddOrUpdate("key", 1.0)); } /// /// Test whether a string value can be added /// [Test] public void Test_Progress_Add_String() { Assert.IsTrue(progress.AddOrUpdate("key", "Hello World!")); } /// /// Test whether a serializable struct can be added /// [Test] public void Test_Progress_Add_SerializableStruct() { Assert.IsTrue(progress.AddOrUpdate("key", new SerializableStruct())); } /// /// Test whether a non-serializable struct will throw an error /// [Test] public void Test_Progress_Add_NonSerializableStruct() { NonSerializableStruct nss = new NonSerializableStruct(); Assert.Throws(delegate { progress.AddOrUpdate("key", nss); }); } /// /// Test whether a key is present /// [Test] public void Test_Progress_Has_ValidKey() { progress.AddOrUpdate("key", 1); Assert.IsTrue(progress.Has("key")); } /// /// Test whether a key is not present /// [Test] public void Test_Progress_Has_InvalidKey() { Assert.IsFalse(progress.Has("non-existing key")); } /// /// Test whether an invalid key will throw an error /// [Test] public void Test_Progress_Get_InvalidKey() { Assert.Throws(delegate { progress.Get("non-existing key"); }); } /// /// Test whether an invalid type will throw an error /// [Test] public void Test_Progress_Get_InvalidType() { progress.AddOrUpdate("key", 123456789); Assert.Throws(delegate { progress.Get("key"); }); } /// /// Test whether a value is correctly updated /// [Test] public void Test_Progress_Update() { progress.AddOrUpdate("key", 1); Assert.AreEqual(progress.Get("key"), 1); progress.AddOrUpdate("key", 2); Assert.AreEqual(progress.Get("key"), 2); } /// /// Test whether a int value can be read /// [Test] public void Test_Progress_Get_Int() { progress.AddOrUpdate("key", 1); Assert.AreEqual(progress.Get("key"), 1); } /// /// Test whether a double value can be read /// [Test] public void Test_Progress_Get_Double() { progress.AddOrUpdate("key", 1.0); Assert.AreEqual(progress.Get("key"), 1.0); } /// /// Test whether a string value can be read /// [Test] public void Test_Progress_Get_String() { progress.AddOrUpdate("key", "Hello World!"); Assert.AreEqual(progress.Get("key"), "Hello World!"); } /// /// Test whether a serializable struct can be read /// [Test] public void Test_Progress_Get_Struct() { int R = 1, G = 10, B = 100; float X = 0.1f, Y = 0.01f, Z = 0.001f; SerializableStruct data = new SerializableStruct { r = R, g = G, b = B, x = X, y = Y, z = Z }; progress.AddOrUpdate("key", data); SerializableStruct result = progress.Get("key"); Assert.AreEqual(result, data); } }