Files
unity-application/Assets/Architecture/Tests/EditMode/PersistentDataControllerTests.cs
2023-05-14 20:18:29 +00:00

1005 lines
33 KiB
C#

using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
/// <summary>
/// Test the PersistentDataController class
/// </summary>
[TestFixture]
public class PersistentDataControllerTests
{
/// <summary>
/// Create a new path so the existing .json file will not be overwritten
/// </summary>
private static string PATH = $"{Application.persistentDataPath}/wesign_unit_test.json";
/// <summary>
/// Reference to the pdc to perform tests on
/// </summary>
private PersistentDataController pdc = null;
/// <summary>
/// A dummy serializable struct to perform test operations on
/// </summary>
[Serializable]
private struct Struct
{
public int r, g, b;
public float x, y, z;
}
/// <summary>
/// A dummy serializable enum to perform test operations on
/// </summary>
private enum Enum
{
SQUARE,
TRIANBLE,
CIRCLE
}
/// <summary>
/// Setup the PersistentDataController tests
/// </summary>
[SetUp]
public void Setup_PersistentDataController()
{
Assert.IsNull(PersistentDataController.PATH);
PersistentDataController.PATH = PersistentDataControllerTests.PATH;
pdc = PersistentDataController.GetInstance();
}
/// <summary>
/// Cleaning up the tests
/// </summary>
[TearDown]
public void TearDown_PersistentDataController()
{
PersistentDataController.PATH = null;
}
/// <summary>
/// Test whether the singleton instance is correctly returned
/// </summary>
[Test]
public void Test_PersistentDataController_GetInstance()
{
PersistentDataController.PATH = null;
Assert.IsNotNull(PersistentDataController.GetInstance());
Assert.AreEqual(0x04_03, PersistentDataController.VERSION);
Assert.AreEqual($"{Application.persistentDataPath}/wesign_saved_data.json", PersistentDataController.PATH);
}
/// <summary>
/// Test whether all data is correctly cleared
/// </summary>
[Test]
public void Test_PersistentDataController_Clear()
{
pdc.Load();
pdc.Clear();
Assert.Zero(pdc.GetUsers().Count);
Assert.AreEqual(-1, pdc.GetCurrentUser());
}
/// <summary>
/// Test whether an empty savefile can be saved correctly
/// </summary>
[Test]
public void Test_PersistentDataController_Save_Empty()
{
pdc.Load();
pdc.Clear();
pdc.Save();
FileAssert.Exists(PATH);
string content = File.ReadAllText(PATH);
string expected = "{\"version\":1027,\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether a savefile can be created when non already exists
/// </summary>
[Test]
public void Test_PersistentDataController_Save_New()
{
pdc.Load();
pdc.Clear();
if (File.Exists(PATH))
File.Delete(PATH);
pdc.Save();
FileAssert.Exists(PATH);
string content = File.ReadAllText(PATH);
string expected = "{\"version\":1027,\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether an existing savefile can be loaded
/// </summary>
[Test]
public void Test_PersistentDataController_Load_Existing()
{
string content = "{\"version\":1027,\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
File.WriteAllText(PATH, content);
Assert.IsTrue(pdc.Load(false));
}
/// <summary>
/// Test whether an exisiting older savefile will create a new correct savefile
/// </summary>
[Test]
public void Test_PersistentDataController_Load_OlderVersion()
{
string content = "{\"version\":1026,\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
File.WriteAllText(PATH, content);
Assert.IsFalse(pdc.Load(false));
}
/// <summary>
/// Test whether an exisiting newer savefile will create a new correct savefile
/// </summary>
[Test]
public void Test_PersistentDataController_Load_NewerVersion()
{
string content = "{\"version\":1028,\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
File.WriteAllText(PATH, content);
Assert.IsFalse(pdc.Load(false));
}
/// <summary>
/// Test whether the PersistentDataController will fail loading a savefile when no savefile is present
/// </summary>
[Test]
public void Test_PersistentDataController_Load_New()
{
if (File.Exists(PATH))
File.Delete(PATH);
Assert.IsFalse(pdc.Load(false));
FileAssert.DoesNotExist(PATH);
}
/// <summary>
/// Test whether a corrupted savefile will throw an error
/// </summary>
[Test]
public void Test_PersistentDataController_Load_Exception()
{
File.WriteAllText(PATH, "https://www.youtube.com/watch?v=dQw4w9WgXcQ");
Assert.IsFalse(pdc.Load(false));
Assert.AreEqual("https://www.youtube.com/watch?v=dQw4w9WgXcQ", File.ReadAllText(PATH));
}
/// <summary>
/// Test whether a corrupted savefile will be overriden
/// </summary>
[Test]
public void Test_PersistentDataController_Load_Override()
{
File.WriteAllText(PATH, "https://www.youtube.com/watch?v=dQw4w9WgXcQ");
Assert.IsFalse(pdc.Load(true));
string content = File.ReadAllText(PATH);
string expected = "{\"version\":1027,\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether the current version is correct
/// </summary>
[Test]
public void Test_PersistentDataController_Version()
{
const int VERSION = 0x04_03;
Assert.AreEqual(VERSION, PersistentDataController.VERSION);
}
/// <summary>
/// Test whether a new user can be added
/// </summary>
[Test]
public void Test_PersistentDataController_AddUser()
{
pdc.Load();
pdc.Clear();
var d = new PersistentDataController.SavedUserData()
{
username = "username",
avatarIndex = 0
};
pdc.AddUser(d);
string content = File.ReadAllText(PATH);
string expected = "{\"version\":1027,\"users\":[{\"entries\":[],\"username\":\"username\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether all users are returned
/// </summary>
[Test]
public void Test_PersistentDataController_GetUsers()
{
pdc.Load();
pdc.Clear();
var d = new PersistentDataController.SavedUserData()
{
username = "username",
avatarIndex = 0
};
pdc.AddUser(d);
var users = pdc.GetUsers();
Assert.AreEqual(1, users.Count);
Assert.AreEqual("username", users[0].username);
Assert.AreEqual(0, users[0].avatarIndex);
}
/// <summary>
/// Test whether the current user is returned
/// </summary>
[Test]
public void Test_PersistentDataController_GetCurrentUser()
{
pdc.Load();
pdc.Clear();
Assert.AreEqual(-1, pdc.GetCurrentUser());
pdc.AddUser(new PersistentDataController.SavedUserData()
{
username = "username",
avatarIndex = 0
});
Assert.AreEqual(0, pdc.GetCurrentUser());
}
/// <summary>
/// Test whether the current user is corrctly changed
/// </summary>
[Test]
public void Test_PersistentDataController_SetCurrentUser()
{
pdc.Load();
pdc.Clear();
for (int i = 0; i < 5; i++)
pdc.AddUser(new PersistentDataController.SavedUserData()
{
username = $"username_{i}",
avatarIndex = i
});
pdc.SetCurrentUser(3);
Assert.AreEqual(3, pdc.GetCurrentUser());
}
/// <summary>
/// Test whether setting an invalid current user throws an error
/// </summary>
[Test]
public void Test_PersistentDataController_SetCurrentUser_Invalid()
{
pdc.Load();
pdc.Clear();
pdc.AddUser(new PersistentDataController.SavedUserData()
{
username = $"username",
avatarIndex = 0
});
Assert.Throws<IndexOutOfRangeException>(delegate { pdc.SetCurrentUser(3); });
}
/// <summary>
/// Test whether setting the current user for an empty userlist will throw an error
/// </summary>
[Test]
public void Test_PersistentDataController_SetCurrentUser_Empty()
{
pdc.Load();
pdc.Clear();
Assert.Throws<IndexOutOfRangeException>(delegate { pdc.SetCurrentUser(0); });
}
/// <summary>
/// Test whether a user is correctly removed and the current user is correctly updated
/// </summary>
[Test]
public void Test_PersistentDataController_DeleteUser_BeforeCurrent()
{
pdc.Load();
pdc.Clear();
var users = new List<PersistentDataController.SavedUserData>();
for (int i = 0; i < 5; i++)
{
var d = new PersistentDataController.SavedUserData()
{
username = $"username_{i}",
avatarIndex = i
};
pdc.AddUser(d);
users.Add(d);
}
pdc.SetCurrentUser(0);
users.RemoveAt(2);
pdc.DeleteUser(2);
var vsers = pdc.GetUsers();
Assert.AreEqual(0, pdc.GetCurrentUser());
Assert.AreEqual(users.Count, vsers.Count);
for (int i = 0; i < 4; i++)
{
Assert.AreEqual(users[i].username, vsers[i].username);
Assert.AreEqual(users[i].avatarIndex, vsers[i].avatarIndex);
}
}
/// <summary>
/// Test whether a user is correctly removed and the current user is correctly updated
/// </summary>
[Test]
public void Test_PersistentDataController_DeleteUser_Current()
{
pdc.Load();
pdc.Clear();
var users = new List<PersistentDataController.SavedUserData>();
for (int i = 0; i < 5; i++)
{
var d = new PersistentDataController.SavedUserData()
{
username = $"username_{i}",
avatarIndex = i
};
pdc.AddUser(d);
users.Add(d);
}
pdc.SetCurrentUser(2);
users.RemoveAt(2);
pdc.DeleteUser(2);
var vsers = pdc.GetUsers();
Assert.AreEqual(1, pdc.GetCurrentUser());
Assert.AreEqual(users.Count, vsers.Count);
for (int i = 0; i < 4; i++)
{
Assert.AreEqual(users[i].username, vsers[i].username);
Assert.AreEqual(users[i].avatarIndex, vsers[i].avatarIndex);
}
}
/// <summary>
/// Test whether a user is correctly removed and the current user is correctly updated
/// </summary>
[Test]
public void Test_PersistentDataController_DeleteUser_AfterCurrent()
{
pdc.Load();
pdc.Clear();
var users = new List<PersistentDataController.SavedUserData>();
for (int i = 0; i < 5; i++)
{
var d = new PersistentDataController.SavedUserData()
{
username = $"username_{i}",
avatarIndex = i
};
pdc.AddUser(d);
users.Add(d);
}
pdc.SetCurrentUser(4);
users.RemoveAt(2);
pdc.DeleteUser(2);
var vsers = pdc.GetUsers();
Assert.AreEqual(3, pdc.GetCurrentUser());
Assert.AreEqual(users.Count, vsers.Count);
for (int i = 0; i < 4; i++)
{
Assert.AreEqual(users[i].username, vsers[i].username);
Assert.AreEqual(users[i].avatarIndex, vsers[i].avatarIndex);
}
}
/// <summary>
/// Test whether deleting an invalid user will throw an error
/// </summary>
[Test]
public void Test_PersistentDataController_DeleteUser_Invalid()
{
pdc.Load();
pdc.Clear();
pdc.AddUser(new PersistentDataController.SavedUserData()
{
username = $"username",
avatarIndex = 0
});
Assert.Throws<IndexOutOfRangeException>(delegate { pdc.SetCurrentUser(3); });
}
/// <summary>
/// Test whether deleting a user from an empty userlist will throw an error
/// </summary>
[Test]
public void Test_PersistentDataController_DeleteUser_Empty()
{
pdc.Load();
pdc.Clear();
Assert.Throws<IndexOutOfRangeException>(delegate { pdc.DeleteUser(0); });
}
/// <summary>
/// Test whether the correct current course is returned
/// </summary>
[Test]
public void Test_PersistentDataController_CurrentCourse()
{
pdc.Load();
pdc.Clear();
pdc.SetCurrentCourse(CourseIndex.FINGERSPELLING);
Assert.AreEqual(CourseIndex.FINGERSPELLING, pdc.GetCurrentCourse());
}
/// <summary>
/// Test whether the correct current minigame is returned
/// </summary>
[Test]
public void Test_PersistentDataController_CurrentMinigame()
{
pdc.Load();
pdc.Clear();
pdc.SetCurrentMinigame(MinigameIndex.SPELLING_BEE);
Assert.AreEqual(MinigameIndex.SPELLING_BEE, pdc.GetCurrentMinigame());
}
/// <summary>
/// Test whether the correct current theme is returned
/// </summary>
[Test]
public void Test_PersistentDataController_CurrentTheme()
{
pdc.Load();
pdc.Clear();
pdc.SetCurrentTheme(ThemeIndex.SIGN_ALPHABET);
Assert.AreEqual(ThemeIndex.SIGN_ALPHABET, pdc.GetCurrentTheme());
}
/// <summary>
/// Test whether hardware acceleration preference is saved correctly
/// </summary>
[Test]
public void Test_PersistentDataController_UseGPU()
{
pdc.Load();
pdc.Clear();
Assert.IsFalse(pdc.IsUsingGPU());
pdc.SetGPUUsage(true);
Assert.IsTrue(pdc.IsUsingGPU());
pdc.SetGPUUsage(false);
Assert.IsFalse(pdc.IsUsingGPU());
}
/// <summary>
/// Test whether restoring user preference is done correctly
/// </summary>
[Test]
public void Test_PersistentDataController_RestoreSettings()
{
pdc.Load();
pdc.Clear();
pdc.SetGPUUsage(true);
pdc.RestoreSettings();
Assert.IsFalse(pdc.IsUsingGPU());
}
/// <summary>
/// Test the creation of a new PersistentDataContainer
/// </summary>
[Test]
public void Test_New_PersistentDataContainer()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsNotNull(c);
Assert.Zero(c.entries.Count);
}
/// <summary>
/// Test whether setting an invalid object on a key in a PersitentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Set_Invalid()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsFalse(c.Set<object>("key", null));
}
/// <summary>
/// Test whether setting a valid object on duplicate key in a PersitentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Set_DuplicateKey()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsTrue(c.Set<int>("key", 123));
Assert.IsTrue(c.Set<int>("key", 321));
}
/// <summary>
/// Test whether setting an int in a PersitentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Set_Int()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsTrue(c.Set<int>("key", 123));
}
/// <summary>
/// Test whether setting a string in a PersitentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Set_String()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsTrue(c.Set<string>("key", "abc"));
}
/// <summary>
/// Test whether setting a struct in a PersitentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Set_Struct()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsTrue(c.Set<Struct>("key", new Struct()));
}
/// <summary>
/// Test whether setting an enum in a PersitentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Set_Enum()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsTrue(c.Set<Enum>("key", new Enum()));
}
/// <summary>
/// Test whether retrieving the wrong type from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_InvalidType()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<Struct>("key", new Struct() { r = 255, g = 127, b = 63, x = 31, y = 15, z = 7 });
Assert.Throws<InvalidCastException>(delegate { c.Get<int>("key"); });
c.Set<int>("key", 123);
Assert.Throws<InvalidCastException>(delegate { c.Get<Struct>("key"); });
}
/// <summary>
/// Test whether retrieving the an wrong key from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_KeyNotFound()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<int>("key", 123);
Assert.Throws<KeyNotFoundException>(delegate { c.Get<int>("KEY"); });
}
/// <summary>
/// Test whether retrieving the unknown key from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_Empty()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.Throws<KeyNotFoundException>(delegate { c.Get<int>("key"); });
}
/// <summary>
/// Test whether retrieving an int from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_Int()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<int>("key", 123);
Assert.AreEqual(123, c.Get<int>("key"));
}
/// <summary>
/// Test whether retrieving a string from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_String()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<string>("key", "value");
Assert.AreEqual("value", c.Get<string>("key"));
}
/// <summary>
/// Test whether retrieving a struct from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_Struct()
{
var c = new PersistentDataController.PersistentDataContainer();
var s = new Struct() { r = 255, g = 127, b = 63, x = 31, y = 15, z = 7 };
c.Set<Struct>("key", s);
Assert.AreEqual(s, c.Get<Struct>("key"));
}
/// <summary>
/// Test whether retrieving an enum from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Get_Enum()
{
var c = new PersistentDataController.PersistentDataContainer();
var e = Enum.CIRCLE;
c.Set<Enum>("key", e);
Assert.AreEqual(e, c.Get<Enum>("key"));
}
/// <summary>
/// Test whether removing an invalid key from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Remove_Invalid()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<int>("key", 123);
Assert.Throws<KeyNotFoundException>(delegate { c.Remove("KEY"); });
}
/// <summary>
/// Test whether removing a non-existing key from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Remove_Empty()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.Throws<KeyNotFoundException>(delegate { c.Remove("key"); });
}
/// <summary>
/// Test whether removing an int from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Remove_Int()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<int>("key", 123);
c.Remove("key");
Assert.Throws<KeyNotFoundException>(delegate { c.Get<int>("key"); });
}
/// <summary>
/// Test whether removing a string from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Remove_String()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<string>("key", "value");
c.Remove("key");
Assert.Throws<KeyNotFoundException>(delegate { c.Get<string>("key"); });
}
/// <summary>
/// Test whether removing a struct from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Remove_Struct()
{
var c = new PersistentDataController.PersistentDataContainer();
var s = new Struct() { r = 255, g = 127, b = 63, x = 31, y = 15, z = 7 };
c.Set<Struct>("key", s);
c.Remove("key");
Assert.Throws<KeyNotFoundException>(delegate { c.Get<Struct>("key"); });
}
/// <summary>
/// Test whether removing an enum from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Remove_Enum()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<Enum>("key", Enum.CIRCLE);
c.Remove("key");
Assert.Throws<KeyNotFoundException>(delegate { c.Get<Enum>("key"); });
}
/// <summary>
/// Test whether popping an invalid key from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Pop_Invalid()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<int>("key", 123);
Assert.Throws<KeyNotFoundException>(delegate { c.Remove("KEY"); });
}
/// <summary>
/// Test whether popping a non-existing key from a PersistentDataContainer will throw an error
/// </summary>
[Test]
public void Test_PersistentDataContainer_Pop_Empty()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.Throws<KeyNotFoundException>(delegate { c.Remove("KEY"); });
}
/// <summary>
/// Test whether popping an int from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Pop_Int()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<int>("key", 123);
Assert.AreEqual(123, c.Pop<int>("key"));
Assert.Throws<KeyNotFoundException>(delegate { c.Get<int>("key"); });
}
/// <summary>
/// Test whether popping a string from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Pop_String()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<string>("key", "value");
Assert.AreEqual("value", c.Pop<string>("key"));
Assert.Throws<KeyNotFoundException>(delegate { c.Get<string>("key"); });
}
/// <summary>
/// Test whether popping a struct from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Pop_Struct()
{
var c = new PersistentDataController.PersistentDataContainer();
var s = new Struct() { r = 255, g = 127, b = 63, x = 31, y = 15, z = 7 };
c.Set<Struct>("key", s);
Assert.AreEqual(s, c.Pop<Struct>("key"));
Assert.Throws<KeyNotFoundException>(delegate { c.Get<Struct>("key"); });
}
/// <summary>
/// Test whether popping an enum from a PersistentDataContainer can be done correctly
/// </summary>
[Test]
public void Test_PersistentDataContainer_Pop_Enum()
{
var c = new PersistentDataController.PersistentDataContainer();
c.Set<Enum>("key", Enum.CIRCLE);
Assert.AreEqual(Enum.CIRCLE, c.Pop<Enum>("key"));
Assert.Throws<KeyNotFoundException>(delegate { c.Get<Enum>("key"); });
}
/// <summary>
/// Test whether checking for a valid key will return true
/// </summary>
[Test]
public void Test_PersistentDataContainer_Has_ValidKey()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsFalse(c.Has("key"));
c.Set<int>("key", 123);
Assert.IsTrue(c.Has("key"));
}
/// <summary>
/// Test whether checking for a invalid key will return false
/// </summary>
[Test]
public void Test_PersistentDataContainer_Has_InvalidKey()
{
var c = new PersistentDataController.PersistentDataContainer();
Assert.IsFalse(c.Has("KEY"));
c.Set<int>("key", 123);
Assert.IsFalse(c.Has("KEY"));
}
/// <summary>
/// Test whether a Learnable can be saved correctly
/// </summary>
[Test]
public void Test_SavedCourseProgress_AddLearnable_Valid()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
Assert.Zero(progress.learnables.Count);
bool added = progress.AddLearnable("learnable", 0);
Assert.IsTrue(added);
Assert.AreEqual(1, progress.learnables.Count);
Assert.AreEqual("learnable", progress.learnables[0].name);
Assert.AreEqual(0, progress.FindLearnable("learnable").index);
}
/// <summary>
/// Test whether a duplicate name in a Learnable, will not save this Learnable
/// </summary>
[Test]
public void Test_SavedCourseProgress_AddLearnable_DuplicateName()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
Assert.AreEqual(1, progress.learnables.Count);
Assert.IsFalse(progress.AddLearnable("learnable", 1));
Assert.AreEqual(1, progress.learnables.Count);
}
/// <summary>
/// Test whether a duplicate index in a Learnable, will not save this Learnable
/// </summary>
[Test]
public void Test_SavedCourseProgress_AddLearnable_DuplicateIndex()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
Assert.AreEqual(1, progress.learnables.Count);
Assert.IsFalse(progress.AddLearnable("LEARNABLE", 0));
Assert.AreEqual(1, progress.learnables.Count);
}
/// <summary>
/// Test whether when updating a invalid named Learnable will throw an error
/// </summary>
[Test]
public void Test_SavedCourseProgress_UpdateLearnable_InvalidName()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
Assert.AreEqual(0.0f, progress.FindLearnable("learnable").progress);
Assert.Throws<KeyNotFoundException>(delegate { progress.UpdateLearnable("LEARNABLE", 3.0f); });
Assert.AreEqual(0.0f, progress.FindLearnable("learnable").progress);
}
/// <summary>
/// Test whether when updating a valid named Learnable will be correctly saved
/// </summary>
[Test]
public void Test_SavedCourseProgress_UpdateLearnable_UpdatesProgress()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
Assert.AreEqual(0.0f, progress.FindLearnable("learnable").progress);
progress.UpdateLearnable("learnable", 3.0f);
Assert.AreEqual(3.0f, progress.FindLearnable("learnable").progress);
}
/// <summary>
/// Test whether when updating a Learnable, the progress is capped at 5
/// </summary>
[Test]
public void Test_SavedCourseProgress_UpdateLearnable_CropsProgressAtFive()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
Assert.AreEqual(0, progress.completedLearnables);
progress.UpdateLearnable("learnable", 10.0f);
Assert.AreEqual(5.0f, progress.FindLearnable("learnable").progress);
Assert.AreEqual(1, progress.completedLearnables);
}
/// <summary>
/// Test whether when updating a Learnable, the progress is capped at -5
/// </summary>
[Test]
public void Test_SavedCourseProgress_UpdateLearnable_CropsProgressAtNegativeFive()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
Assert.AreEqual(0, progress.completedLearnables);
progress.UpdateLearnable("learnable", -10.0f);
Assert.AreEqual(-5.0f, progress.FindLearnable("learnable").progress);
Assert.AreEqual(0, progress.completedLearnables);
}
/// <summary>
/// Test whether a Learnable can be fountd correctly
/// </summary>
[Test]
public void Test_SavedCourseProgress_FindLearnable()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable 1", 0);
Assert.IsNull(progress.FindLearnable("learnable 2"));
progress.AddLearnable("learnable 2", 1);
Assert.IsNotNull(progress.FindLearnable("learnable 2"));
}
/// <summary>
/// Test whether no Learnable is returned when there are no learnables
/// </summary>
[Test]
public void Test_SavedCourseProgress_GetRandomLearnable_NoLearnables()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
Assert.IsNull(progress.GetRandomLearnable());
}
/// <summary>
/// Test whether no Learnable is returned when there are no unused learnables
/// </summary>
[Test]
public void Test_SavedCourseProgress_GetRandomLearnable_NoUnusedLearnables()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
progress.AddLearnable("learnable", 0);
progress.FindLearnable("learnable").inUse = true;
progress.completedLearnables = 1;
progress.inUseLearnables = 0;
Assert.IsNull(progress.GetRandomLearnable());
}
/// <summary>
/// Test whether no Learnable is returned when there are only completed learnables
/// </summary>
[Test]
public void Test_SavedCourseProgress_GetRandomLearnable_OnlyCompletedLearnables()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
for (int i = 0; i < 2; i++)
{
progress.AddLearnable($"learnable {i}", i);
var learnable = progress.FindLearnable($"learnable {i}");
learnable.progress = 4.0f;
learnable.inUse = true;
}
progress.completedLearnables = 2;
progress.inUseLearnables = 0;
Assert.IsNull(progress.GetRandomLearnable());
}
/// <summary>
/// Test whether a random Learnable is returned
/// </summary>
[Test]
public void Test_SavedCourseProgress_GetRandomLearnable_Valid()
{
PersistentDataController.SavedCourseProgress progress = new PersistentDataController.SavedCourseProgress();
for (int i = 0; i < 10; i++)
{
progress.AddLearnable($"learnable {i}", i);
progress.FindLearnable($"learnable {i}").inUse = true;
}
progress.completedLearnables = 0;
progress.inUseLearnables = 10;
Assert.AreEqual(10, progress.inUseLearnables);
Assert.AreEqual(0, progress.completedLearnables);
Assert.IsNotNull(progress.GetRandomLearnable());
Assert.AreEqual(10, progress.inUseLearnables);
Assert.AreEqual(0, progress.completedLearnables);
}
}