using System; using System.Collections.Generic; using System.Runtime.Serialization; using UnityEngine; /// /// Test the Progress class /// public class TestProgress : MonoBehaviour { /// /// 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; } /// /// Helper method /// /// true if Progress.AddOrUpdate(...) throws a SerializationException private bool AddNonSerializableStruct() { Progress progress = new Progress(); NonSerializableStruct nss = new NonSerializableStruct(); try { progress.AddOrUpdate("key", nss); } catch (SerializationException) { return true; } return false; } /// /// Helper method /// /// true if Progress.Get(...) throws a KeyNotFoundException private bool AccessInvalidKey() { Progress progress = new Progress(); try { progress.Get("non-existing key"); } catch (KeyNotFoundException) { return true; } return false; } /// /// Helper method /// /// true if Progress.Get(...) throws a InvalidCastException private bool AccessInvalidType() { Progress progress = new Progress(); progress.AddOrUpdate("key", 123456789); try { progress.Get("key"); } catch (InvalidCastException) { return true; } return false; } /// /// Start is called before the first frame update /// void Start() { TestNewProgress(); TestProgressAddInvalidData(); TestProgressAddDuplicateKey(); TestProgressAddInt(); TestProgressAddDouble(); TestProgressAddString(); TestProgressAddSerializableStruct(); TestProgressAddNonSerializableStruct(); TestProgressGetInvalidKey(); TestProgressGetInvalidType(); TestProgressUpdate(); TestProgressGetInt(); TestProgressGetDouble(); TestProgressGetString(); TestProgressGetStruct(); } /// /// Test for creation of a new progress /// public void TestNewProgress() { Progress progress = new Progress(); Debug.Assert(progress != null); } /// /// Test whether invalid data will not be added /// public void TestProgressAddInvalidData() { Progress progress = new Progress(); Debug.Assert(!progress.AddOrUpdate("key", null)); } /// /// Test whether a duplicated key will be added /// public void TestProgressAddDuplicateKey() { Progress progress = new Progress(); progress.AddOrUpdate("key 1", 0); Debug.Assert(progress.AddOrUpdate("key 1", 1)); } /// /// Test whether a int value can be added /// public void TestProgressAddInt() { Progress progress = new Progress(); Debug.Assert(progress.AddOrUpdate("key", 1)); } /// /// Test whether a double value can be added /// public void TestProgressAddDouble() { Progress progress = new Progress(); Debug.Assert(progress.AddOrUpdate("key", 1.0)); } /// /// Test whether a string value can be added /// public void TestProgressAddString() { Progress progress = new Progress(); Debug.Assert(progress.AddOrUpdate("key", "Hello World!")); } /// /// Test whether a serializable struct can be added /// public void TestProgressAddSerializableStruct() { Progress progress = new Progress(); Debug.Assert(progress.AddOrUpdate("key", new SerializableStruct())); } /// /// Test whether a non-serializable struct will throw an error /// public void TestProgressAddNonSerializableStruct() { Debug.Assert(AddNonSerializableStruct()); } /// /// Test whether an invalid key will throw an error /// public void TestProgressGetInvalidKey() { Debug.Assert(AccessInvalidKey()); } /// /// Test whether an invalid type will throw an error /// public void TestProgressGetInvalidType() { Debug.Assert(AccessInvalidType()); } /// /// Test whether a value is correctly updated /// public void TestProgressUpdate() { Progress progress = new Progress(); progress.AddOrUpdate("key", 1); Debug.Assert(progress.Get("key") == 1); progress.AddOrUpdate("key", 2); Debug.Assert(progress.Get("key") == 2); } /// /// Test whether a int value can be read /// public void TestProgressGetInt() { Progress progress = new Progress(); progress.AddOrUpdate("key", 1); Debug.Assert(progress.Get("key") == 1); } /// /// Test whether a double value can be read /// public void TestProgressGetDouble() { Progress progress = new Progress(); progress.AddOrUpdate("key", 1.0); Debug.Assert(progress.Get("key") == 1.0); } /// /// Test whether a string value can be read /// public void TestProgressGetString() { Progress progress = new Progress(); progress.AddOrUpdate("key", "Hello World!"); Debug.Assert(progress.Get("key") == "Hello World!"); } /// /// Test whether a serializable struct can be read /// public void TestProgressGetStruct() { Progress progress = new Progress(); 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"); Debug.Assert(result.r == R); Debug.Assert(result.g == G); Debug.Assert(result.b == B); Debug.Assert(result.x == X); Debug.Assert(result.y == Y); Debug.Assert(result.z == Z); } }