Files
unity-application/Assets/Accounts/Tests/ProgressTest.cs
Dries Van Schuylenbergh 7b6eb4db69 Test accounts
2023-03-25 15:35:26 +00:00

214 lines
5.5 KiB
C#

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