Test accounts

This commit is contained in:
Dries Van Schuylenbergh
2023-03-25 15:35:26 +00:00
committed by Jelle De Geest
parent fee989006c
commit 7b6eb4db69
29 changed files with 691 additions and 1269 deletions

View File

@@ -42,6 +42,7 @@ public class ChangeUserScreen : MonoBehaviour
/// </summary>
void Start()
{
userList.Load();
error.SetActive(false);
DisplayUsers();
}

View File

@@ -101,4 +101,14 @@ public class Progress
// Raise an exception when key is not found
throw new KeyNotFoundException();
}
/// <summary>
/// Check whether a key is present
/// </summary>
/// <param name="key">The key to check</param>
/// <returns>true if a item can be found with the specified key</returns>
public bool Has(string key)
{
return entries.Find(x => x.key == key) != null;
}
}

View File

@@ -19,7 +19,7 @@ public class UserList : ScriptableObject
/// <summary>
/// The index of the current/last logged in user in the <c>storedUsers</c> list
/// </summary>
public int currentUserIndex;
public int currentUserIndex = -1;
/// <summary>
/// A list containing all users (which can be serialized)
@@ -43,8 +43,13 @@ public class UserList : ScriptableObject
/// </summary>
void OnEnable()
{
PATH = $"{Application.persistentDataPath}/users.json";
Load();
// The PATH variable can be set by the testing framework,
// so we don't overwrite the actual userlist with test data
if (PATH == null)
{
PATH = $"{Application.persistentDataPath}/users.json";
Load();
}
}
/// <summary>
@@ -71,6 +76,10 @@ public class UserList : ScriptableObject
{
User user = CreateNewUser(name, avatar);
storedUserList.storedUsers.Add(user);
if (storedUserList.storedUsers.Count == 1)
{
storedUserList.currentUserIndex = 0;
}
Save();
return user;
}
@@ -103,6 +112,10 @@ public class UserList : ScriptableObject
/// <returns>The current logged in user</returns>
public User GetCurrentUser()
{
if (storedUserList.storedUsers.Count == 0)
{
return null;
}
return storedUserList.storedUsers[storedUserList.currentUserIndex];
}
@@ -119,28 +132,46 @@ public class UserList : ScriptableObject
/// Change the current user
/// </summary>
/// <param name="index">Index of the user in the userlist</param>
/// <exception cref="IndexOutOfRangeException"></exception>
public void ChangeCurrentUser(int index)
{
storedUserList.currentUserIndex = index;
if (0 <= index && index < storedUserList.storedUsers.Count)
{
storedUserList.currentUserIndex = index;
}
else
{
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Change the current user
/// </summary>
/// <param name="user">Reference to the user in the userlist</param>
/// <exception cref="KeyNotFoundException"></exception>
public void ChangeCurrentUser(User user)
{
storedUserList.currentUserIndex = storedUserList.storedUsers.IndexOf(user);
int idx = storedUserList.storedUsers.IndexOf(user);
if (idx < 0)
{
throw new KeyNotFoundException();
}
storedUserList.currentUserIndex = idx;
}
/// <summary>
/// Remove the user
/// </summary>
/// <param name="index">The index of the user in the userlist</param>
/// <returns>true if user was successful removed, false otherwise</returns>
/// <returns>true if user was successful removed, false otherwise</returns
public bool DeleteUser(int index)
{
return DeleteUser(storedUserList.storedUsers[index]);
if (0 <= index && index < storedUserList.storedUsers.Count)
{
return DeleteUser(storedUserList.storedUsers[index]);
}
return false;
}
/// <summary>
@@ -177,13 +208,14 @@ public class UserList : ScriptableObject
/// </summary>
public void Load()
{
try
{
storedUserList.storedUsers.Clear();
storedUserList.storedUsers.Clear();
storedUserList.currentUserIndex = -1;
string text = File.ReadAllText(PATH);
storedUserList = JsonUtility.FromJson<StoredUserList>(text);
if (!File.Exists(PATH))
{
Save();
}
catch (FileNotFoundException) { Debug.Log($"Path '{PATH}' not found"); }
string text = File.ReadAllText(PATH);
storedUserList = JsonUtility.FromJson<StoredUserList>(text);
}
}

View File

@@ -112,6 +112,7 @@ public class UserProgressScreen : MonoBehaviour
void Start()
{
// Assign the current user
userList.Load();
user = userList.GetCurrentUser();
// Set correct displayed items

View File

@@ -7,8 +7,14 @@ using UnityEngine;
/// <summary>
/// Test the Progress class
/// </summary>
public class TestProgress
[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>
@@ -29,205 +35,179 @@ public class TestProgress
}
/// <summary>
/// Helper method
/// Setup the tests
/// </summary>
/// <returns><c>true</c> if <c>Progress.AddOrUpdate(...)</c> throws a <c>SerializationException</c></returns>
private bool AddNonSerializableStruct()
[SetUp]
public void Setup_Progress()
{
Progress progress = new Progress();
NonSerializableStruct nss = new NonSerializableStruct();
try { progress.AddOrUpdate<NonSerializableStruct>("key", nss); }
catch (SerializationException) { return true; }
return false;
}
/// <summary>
/// Helper method
/// </summary>
/// <returns><c>true</c> if <c>Progress.Get(...)</c> throws a <c>KeyNotFoundException</c></returns>
private bool AccessInvalidKey()
{
Progress progress = new Progress();
try { progress.Get<int>("non-existing key"); }
catch (KeyNotFoundException) { return true; }
return false;
}
/// <summary>
/// Helper method
/// </summary>
/// <returns><c>true</c> if <c>Progress.Get(...)</c> throws a <c>InvalidCastException</c></returns>
private bool AccessInvalidType()
{
Progress progress = new Progress();
progress.AddOrUpdate<int>("key", 123456789);
try { progress.Get<double>("key"); }
catch (InvalidCastException) { return true; }
return false;
progress = new Progress();
}
/// <summary>
/// Test for creation of a new progress
/// </summary>
[Test]
public void TestNewProgress()
public void Test_New_Progress()
{
Progress progress = new Progress();
Debug.Assert(progress != null);
Assert.IsNotNull(progress);
}
/// <summary>
/// Test whether invalid data will not be added
/// </summary>
[Test]
public void TestProgressAddInvalidData()
public void Test_Progress_Add_InvalidData()
{
Progress progress = new Progress();
Debug.Assert(!progress.AddOrUpdate<GameObject>("key", null));
Assert.IsFalse(progress.AddOrUpdate<GameObject>("key", null));
}
/// <summary>
/// Test whether a duplicated key will be added
/// </summary>
[Test]
public void TestProgressAddDuplicateKey()
public void Test_Progress_Add_DuplicateKey()
{
Progress progress = new Progress();
progress.AddOrUpdate<int>("key 1", 0);
Debug.Assert(progress.AddOrUpdate<int>("key 1", 1));
Assert.IsTrue(progress.AddOrUpdate<int>("key 1", 1));
}
/// <summary>
/// Test whether a <c>int</c> value can be added
/// </summary>
[Test]
public void TestProgressAddInt()
public void Test_Progress_Add_Int()
{
Progress progress = new Progress();
Debug.Assert(progress.AddOrUpdate<int>("key", 1));
Assert.IsTrue(progress.AddOrUpdate<int>("key", 1));
}
/// <summary>
/// Test whether a <c>double</c> value can be added
/// </summary>
[Test]
public void TestProgressAddDouble()
public void Test_Progress_Add_Double()
{
Progress progress = new Progress();
Debug.Assert(progress.AddOrUpdate<double>("key", 1.0));
Assert.IsTrue(progress.AddOrUpdate<double>("key", 1.0));
}
/// <summary>
/// Test whether a <c>string</c> value can be added
/// </summary>
[Test]
public void TestProgressAddString()
public void Test_Progress_Add_String()
{
Progress progress = new Progress();
Debug.Assert(progress.AddOrUpdate<string>("key", "Hello World!"));
Assert.IsTrue(progress.AddOrUpdate<string>("key", "Hello World!"));
}
/// <summary>
/// Test whether a serializable struct can be added
/// </summary>
[Test]
public void TestProgressAddSerializableStruct()
public void Test_Progress_Add_SerializableStruct()
{
Progress progress = new Progress();
Debug.Assert(progress.AddOrUpdate<SerializableStruct>("key", new SerializableStruct()));
Assert.IsTrue(progress.AddOrUpdate<SerializableStruct>("key", new SerializableStruct()));
}
/// <summary>
/// Test whether a non-serializable struct will throw an error
/// </summary>
[Test]
public void TestProgressAddNonSerializableStruct()
public void Test_Progress_Add_NonSerializableStruct()
{
Debug.Assert(AddNonSerializableStruct());
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 TestProgressGetInvalidKey()
public void Test_Progress_Get_InvalidKey()
{
Debug.Assert(AccessInvalidKey());
Assert.Throws<KeyNotFoundException>(delegate { progress.Get<int>("non-existing key"); });
}
/// <summary>
/// Test whether an invalid type will throw an error
/// </summary>
[Test]
public void TestProgressGetInvalidType()
public void Test_Progress_Get_InvalidType()
{
Debug.Assert(AccessInvalidType());
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 TestProgressUpdate()
public void Test_Progress_Update()
{
Progress progress = new Progress();
progress.AddOrUpdate<int>("key", 1);
Debug.Assert(progress.Get<int>("key") == 1);
Assert.AreEqual(progress.Get<int>("key"), 1);
progress.AddOrUpdate<int>("key", 2);
Debug.Assert(progress.Get<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 TestProgressGetInt()
public void Test_Progress_Get_Int()
{
Progress progress = new Progress();
progress.AddOrUpdate<int>("key", 1);
Debug.Assert(progress.Get<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 TestProgressGetDouble()
public void Test_Progress_Get_Double()
{
Progress progress = new Progress();
progress.AddOrUpdate<double>("key", 1.0);
Debug.Assert(progress.Get<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 TestProgressGetString()
public void Test_Progress_Get_String()
{
Progress progress = new Progress();
progress.AddOrUpdate<string>("key", "Hello World!");
Debug.Assert(progress.Get<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 TestProgressGetStruct()
public void Test_Progress_Get_Struct()
{
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<SerializableStruct>("key", data);
SerializableStruct result = progress.Get<SerializableStruct>("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);
Assert.AreEqual(result, data);
}
}

View File

@@ -1,30 +1,33 @@
using UnityEngine;
using NUnit.Framework;
/// <summary>
/// Test the UserCreationScreen class
/// </summary>
public class TestUserCreationScreen
[TestFixture]
public class UserCreationScreenTest
{
/// <summary>
/// Tets IsValidUsername will return <c>true</c> for an valid username
/// </summary>
public void TestIsValidUsernameTrue()
[Test]
public void Test_UserCreationScreen_IsValidUsername_True()
{
foreach (char c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
Debug.Assert(UserCreationScreen.IsValidUsername(c.ToString()));
Assert.IsTrue(UserCreationScreen.IsValidUsername(c.ToString()));
Debug.Assert(UserCreationScreen.IsValidUsername("123456789AbC"));
Assert.IsTrue(UserCreationScreen.IsValidUsername("123456789AbC"));
}
/// <summary>
/// Tets IsValidUsername will return <c>false</c> for an invalid username
/// </summary>
public void TestIsValidUsernameFalse()
[Test]
public void Test_UserCreationScreen_IsValidUsername_False()
{
Debug.Assert(!UserCreationScreen.IsValidUsername(string.Empty));
Assert.IsFalse(UserCreationScreen.IsValidUsername(string.Empty));
foreach (char c in " \n\t+-*/%_.,;:!?(){}[]\\'\"|&~^$")
Debug.Assert(!UserCreationScreen.IsValidUsername(c.ToString()));
Assert.IsFalse(UserCreationScreen.IsValidUsername(c.ToString()));
Debug.Assert(!UserCreationScreen.IsValidUsername("123456789_10_11_12_13"));
Assert.IsFalse(UserCreationScreen.IsValidUsername("123456789_10_11_12_13"));
}
}

View File

@@ -0,0 +1,470 @@
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
/// <summary>
/// Test the UserList class
/// </summary>
[TestFixture]
public class UserListTest
{
/// <summary>
/// Create a new path so the existing users.json file will not be overwritten
/// </summary>
private static string PATH = $"{Application.persistentDataPath}/unit_test_users.json";
/// <summary>
/// NUnit test magic (for skipping the setup)
/// </summary>
public const string SKIP_SETUP = "SKIP_SETUP";
/// <summary>
/// Reference to the userlist to be tested
/// </summary>
private UserList userList;
/// <summary>
/// Helper variable for quick user creation
/// </summary>
private string username = "u5erNam3";
/// <summary>
/// Helper variable for quick user creation
/// </summary>
private Sprite avatar = Sprite.Create(
Texture2D.blackTexture,
new Rect(0, 0, Texture2D.blackTexture.width, Texture2D.blackTexture.height),
new Vector2(0.5f, 0.5f)
);
/// <summary>
/// Setup the tests
/// </summary>
[SetUp]
public void Setup_UserList()
{
// Check whether the current test needs to skip the setup
ArrayList cat = TestContext.CurrentContext.Test.Properties["_CATEGORIES"] as ArrayList;
bool skip = cat != null && cat.Contains(SKIP_SETUP);
if (!skip)
{
// The actual setup code
UserList.PATH = UserListTest.PATH;
userList = ScriptableObject.CreateInstance<UserList>();
}
}
/// <summary>
/// Test whether the UserList.PATH is correctly set
/// </summary>
[Test]
[Category(SKIP_SETUP)]
public void Test_UserList_OnEnable()
{
UserList.PATH = null;
userList = ScriptableObject.CreateInstance<UserList>();
Assert.AreEqual($"{Application.persistentDataPath}/users.json", UserList.PATH);
}
/// <summary>
/// Test for creation of a new UserList
/// </summary>
[Test]
public void Test_New_UserList()
{
Assert.IsNotNull(userList);
Assert.Zero(userList.GetUsers().Count);
}
/// <summary>
/// Test for creation of new user (without adding the user to the list)
/// </summary>
[Test]
public void Test_UserList_CreateNewUser()
{
User user = userList.CreateNewUser(username, avatar);
Assert.IsNotNull(user);
Assert.Zero(userList.GetUsers().Count);
Assert.AreEqual(username, user.username);
Assert.AreEqual(avatar, user.avatar);
}
/// <summary>
/// Test for creating a new user and adding the user to the list
/// </summary>
public void Test_UserList_CreateAndAddNewUser()
{
Assert.AreEqual(-1, userList.GetCurrentUserIndex());
User user = userList.CreateAndAddNewUser(username, avatar);
Assert.IsNotNull(user);
Assert.AreEqual(1, userList.GetUsers().Count);
Assert.AreEqual(user, userList.GetUsers()[0]);
Assert.Zero(userList.GetCurrentUserIndex());
}
/// <summary>
/// Test whether an existing user can be found by its username
/// </summary>
[Test]
public void Test_UserList_GetUserByUsername_Valid()
{
User u = userList.CreateAndAddNewUser(username, avatar);
User v = userList.GetUserByUsername(username);
Assert.AreEqual(u, v);
}
/// <summary>
/// Test whether a non-existing user can not be found
/// </summary>
[Test]
public void Test_UserList_GetUserByUsername_Null()
{
User user = userList.GetUserByUsername("not-a-user");
Assert.IsNull(user);
}
/// <summary>
/// Test whether the correct current user is returned
/// </summary>
[Test]
public void Test_UserList_GetCurrentUser()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
userList.ChangeCurrentUser(2);
User W = userList.GetCurrentUser();
Assert.AreEqual(w, W);
}
/// <summary>
/// Test whether a null user is returned when the userlist is empty
/// </summary>
[Test]
public void Test_UserList_GetCurrent_Empty()
{
User user = userList.GetCurrentUser();
Assert.IsNull(user);
}
/// <summary>
/// Test whether the correct index is returned for the current user
/// </summary>
[Test]
public void Test_UserList_GetCurrentUserIndex()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
userList.ChangeCurrentUser(2);
int idx = userList.GetCurrentUserIndex();
Assert.AreEqual(2, idx);
}
/// <summary>
/// Test whether a bad index is returned when the userlist is empty
/// </summary>
[Test]
public void Test_UserList_GetCurrentUserIndex_Empty()
{
Assert.AreEqual(-1, userList.GetCurrentUserIndex());
}
/// <summary>
/// Test whether the current user (referenced by index) is correctly changed
/// </summary>
[Test]
public void Test_UserList_ChangeCurrentUser_ValidIndex()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
userList.ChangeCurrentUser(2);
User W = userList.GetCurrentUser();
Assert.AreEqual(w, W);
}
/// <summary>
/// Test whether the current user is not changed when a bad index is given
/// </summary>
[Test]
public void Test_UserList_ChangeCurrentUser_InvalidIndex()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
Assert.Throws<IndexOutOfRangeException>(delegate { userList.ChangeCurrentUser(-1); });
Assert.Throws<IndexOutOfRangeException>(delegate { userList.ChangeCurrentUser(5); });
}
/// <summary>
/// Test whether the current user is not changed when a bad index is given
/// </summary>
[Test]
public void Test_UserList_ChangeCurrentUser_IndexEmpty()
{
Assert.Throws<IndexOutOfRangeException>(delegate { userList.ChangeCurrentUser(0); });
}
/// <summary>
/// Test whether the current user is correctly changed
/// </summary>
[Test]
public void Test_UserList_ChangeCurrentUser_ValidUser()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
userList.ChangeCurrentUser(v);
User V = userList.GetCurrentUser();
Assert.AreEqual(v, V);
}
/// <summary>
/// Test whether the current user is not changed when a non-existing user is given
/// </summary>
[Test]
public void Test_UserList_ChangeCurrentUser_InvalidUser()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateNewUser($"{username}_{'v'}", avatar);
Assert.Throws<KeyNotFoundException>(delegate { userList.ChangeCurrentUser(v); });
}
/// <summary>
/// Test whether the current user is not changed when a null user is given
/// </summary>
[Test]
public void Test_UserList_ChangeCurrentUser_NullUser()
{
Assert.Throws<KeyNotFoundException>(delegate { userList.ChangeCurrentUser(null); });
}
/// <summary>
/// Test whether deleting a existing user (referenced by index) will correctly be removed
/// </summary>
[Test]
public void Test_UserList_DeleteUser_ValidIndex()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
Assert.IsTrue(userList.DeleteUser(1));
}
/// <summary>
/// Test whether deleting a non-existing user (referenced by wrong index) will fail
/// </summary>
[Test]
public void Test_UserList_DeleteUser_InValidIndex()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
Assert.IsFalse(userList.DeleteUser(-1));
Assert.IsFalse(userList.DeleteUser(5));
}
/// <summary>
/// Test whether deleting any user from an empty userlist will fail
/// </summary>
[Test]
public void Test_UserList_DeleteUser_IndexEmpty()
{
Assert.IsFalse(userList.DeleteUser(0));
}
/// <summary>
/// Test whether deleting an existing user will correctly remove the user and set the currentUserIndex correctly
/// </summary>
[Test]
public void Test_UserList_DeleteUser_LastValid()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
userList.ChangeCurrentUser(2);
Assert.AreEqual(3, userList.GetUsers().Count);
Assert.AreEqual(2, userList.GetCurrentUserIndex());
Assert.IsTrue(userList.DeleteUser(w));
Assert.AreEqual(2, userList.GetUsers().Count);
Assert.AreEqual(1, userList.GetCurrentUserIndex());
}
/// <summary>
/// Test whether deleting an existing user will remove the user correctly
/// </summary>
/// <remarks>This will change the currentUserIndex to point to another user</remarks>
[Test]
public void Test_UserList_DeleteUser_FirstValid()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateAndAddNewUser($"{username}_{'v'}", avatar);
User w = userList.CreateAndAddNewUser($"{username}_{'w'}", avatar);
userList.ChangeCurrentUser(0);
Assert.AreEqual(3, userList.GetUsers().Count);
Assert.AreEqual(0, userList.GetCurrentUserIndex());
Assert.IsTrue(userList.DeleteUser(u));
Assert.AreEqual(2, userList.GetUsers().Count);
Assert.AreEqual(0, userList.GetCurrentUserIndex());
}
/// <summary>
/// Test whether deleting a non-existing user will not affect the userlist
/// </summary>
[Test]
public void Test_UserList_DeleteUser_Invalid()
{
User u = userList.CreateAndAddNewUser($"{username}_{'u'}", avatar);
User v = userList.CreateNewUser($"{username}_{'v'}", avatar);
Assert.AreEqual(1, userList.GetUsers().Count);
Assert.IsFalse(userList.DeleteUser(v));
Assert.AreEqual(1, userList.GetUsers().Count);
}
/// <summary>
/// Test whether calling the DeleteUser function on an empty list will not affect the userlist
/// </summary>
[Test]
public void Test_UserList_DeleteUser_Empty()
{
User user = userList.CreateNewUser(username, avatar);
Assert.Zero(userList.GetUsers().Count);
Assert.IsFalse(userList.DeleteUser(user));
Assert.Zero(userList.GetUsers().Count);
}
/// <summary>
/// Test whether a savefile is correctly constructed when no savefile is present
/// </summary>
[Test]
public void Test_UserList_Save_New()
{
if (File.Exists(PATH))
{
File.Delete(PATH);
}
List<User> u = new List<User>();
for (int i = 0; i < 5; i++)
{
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
}
userList.ChangeCurrentUser(3);
userList.Save();
FileAssert.Exists(PATH);
string content = File.ReadAllText(PATH);
int id = avatar.GetInstanceID();
string expected = $"{{\"currentUserIndex\":3,\"storedUsers\":[{{\"username\":\"u5erNam3_0\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_1\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_2\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_3\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_4\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}}]}}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether a savefile is correctly constructed when a savefile already exists
/// </summary>
[Test]
public void Test_UserList_Save_Existing()
{
if (!File.Exists(PATH))
{
File.CreateText(PATH).Close();
File.WriteAllText(PATH, "https://www.youtube.com/watch?v=dQw4w9WgXcQ");
}
List<User> u = new List<User>();
for (int i = 0; i < 5; i++)
{
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
}
userList.ChangeCurrentUser(3);
userList.Save();
FileAssert.Exists(PATH);
string content = File.ReadAllText(PATH);
int id = avatar.GetInstanceID();
string expected = $"{{\"currentUserIndex\":3,\"storedUsers\":[{{\"username\":\"u5erNam3_0\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_1\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_2\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_3\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}},{{\"username\":\"u5erNam3_4\",\"avatar\":{{\"instanceID\":{id}}},\"playtime\":0.0,\"courses\":[],\"minigames\":[]}}]}}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether a save file is correctly constructed from an empty userlist
/// </summary>
[Test]
public void Test_UserList_Save_Empty()
{
userList.Save();
FileAssert.Exists(PATH);
string content = File.ReadAllText(PATH);
string expected = "{\"currentUserIndex\":-1,\"storedUsers\":[]}";
Assert.AreEqual(expected, content);
}
/// <summary>
/// Test whether a userlist (containing some users) is correrctly loaded
/// </summary>
[Test]
public void Test_UserList_Load()
{
List<User> u = new List<User>();
for (int i = 0; i < 5; i++)
{
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
}
userList.ChangeCurrentUser(3);
userList.Save();
userList.Load();
Assert.AreEqual(userList.GetUsers().Count, u.Count);
Assert.AreEqual(userList.GetCurrentUserIndex(), 3);
List<User> v = userList.GetUsers();
for (int i = 0; i < 5; i++)
{
Assert.AreEqual(u[i].username, v[i].username);
Assert.AreEqual(u[i].avatar, v[i].avatar);
}
}
/// <summary>
/// Test whether an empty userlist is correctly loaded
/// </summary>
[Test]
public void Test_UserList_Load_Empty()
{
userList.Save();
userList.Load();
Assert.Zero(userList.GetUsers().Count);
Assert.AreEqual(-1, userList.GetCurrentUserIndex());
}
/// <summary>
/// Test if the user save file is not found, a new one will be created and an empty userlist will be loaded
/// </summary>
[Test]
public void Test_UserList_Load_NotFound()
{
List<User> u = new List<User>();
for (int i = 0; i < 5; i++)
{
u.Add(userList.CreateAndAddNewUser($"{username}_{i}", avatar));
}
userList.ChangeCurrentUser(3);
userList.Save();
File.Delete(PATH);
userList.Load();
Assert.Zero(userList.GetUsers().Count);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cc44c73b32b9af7469b76bd6071f0cf5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,150 +1,166 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Test the User class
/// </summary>
public class TestUser
[TestFixture]
public class UserTest
{
/// <summary>
/// Reference to the user to be tested
/// </summary>
private User user;
/// <summary>
/// Setup the tests
/// </summary>
[SetUp]
public void Setup_User()
{
user = new User();
}
/// <summary>
/// Test for the creation of a new user
/// </summary>
public void TestNewUser()
[Test]
public void Test_New_User()
{
User user = new User();
Debug.Assert(user != null);
Debug.Assert(user.courses.Count == 0);
Debug.Assert(user.minigames.Count == 0);
Assert.NotNull(user);
Assert.Zero(user.courses.Count);
Assert.Zero(user.minigames.Count);
}
/// <summary>
/// Test whether progress on a new course can be added
/// </summary>
public void TestUserAddCourse()
[Test]
public void Test_User_AddCourse()
{
User user = new User();
Progress p = new Progress();
user.courses.Add(p);
Debug.Assert(user.courses.Count == 1);
Debug.Assert(user.minigames.Count == 0);
Assert.AreEqual(user.courses.Count, 1);
Assert.Zero(user.minigames.Count);
}
/// <summary>
/// Test whether progress on a new minigame can be added
/// </summary>
public void TestUserAddMinigame()
[Test]
public void Test_User_AddMinigame()
{
User user = new User();
Progress p = new Progress();
user.minigames.Add(p);
Debug.Assert(user.courses.Count == 0);
Debug.Assert(user.minigames.Count == 1);
Assert.Zero(user.courses.Count);
Assert.AreEqual(user.minigames.Count, 1);
}
/// <summary>
/// Test GetRecentCourses will return empty when no progress is stored
/// </summary>
public void TestGetRecentCoursesEmpty()
[Test]
public void Test_User_GetRecentCourses_Empty()
{
User user = new User();
Debug.Assert(user.GetRecentCourses().Count == 0);
Assert.Zero(user.GetRecentCourses().Count);
}
/// <summary>
/// Temporary test for GetRecentCourses will return all progress that is stored
/// TEMPORARY test for GetRecentCourses will return all progress that is stored
/// </summary>
public void TestGetRecentCoursesAll()
[Test]
public void Test_User_GetRecentCourses_All()
{
User user = new User();
Progress p = new Progress();
p.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
p.AddOrUpdate<float>("courseProgress", 0.5f);
user.courses.Add(p);
List<Tuple<CourseIndex, float>> list = user.GetRecentCourses();
Debug.Assert(list.Count == 1);
Debug.Assert(list[0].Item1 == CourseIndex.FINGERSPELLING);
Debug.Assert(list[0].Item2 == 0.5f);
Assert.AreEqual(list.Count, 1);
Assert.AreEqual(list[0].Item1, CourseIndex.FINGERSPELLING);
Assert.AreEqual(list[0].Item2, 0.5f);
}
/// <summary>
/// Test GetRecommendedCourses will return <c>Tuple<CourseIndex.FINGERSPELLING, 0.0></c> when no progress is stored
/// </summary>
public void TestGetRecommendedCoursesEmpty()
[Test]
public void Test_User_GetRecommendedCourses_Empty()
{
User user = new User();
List<Tuple<CourseIndex, float>> list = user.GetRecommendedCourses();
Debug.Assert(list.Count == 1);
Debug.Assert(list[0].Item1 == CourseIndex.FINGERSPELLING);
Debug.Assert(list[0].Item2 == 0.0f);
Assert.AreEqual(list.Count, 1);
Assert.AreEqual(list[0].Item1, CourseIndex.FINGERSPELLING);
Assert.AreEqual(list[0].Item2, 0.0f);
}
/// <summary>
/// Temporary test for GetRecommenedCourses will return all progress that is stored
/// TEMPORARY test for GetRecommenedCourses will return all progress that is stored
/// </summary>
public void TestGetRecommendedCoursesAll()
[Test]
public void Test_User_GetRecommendedCourses_All()
{
User user = new User();
Progress p = new Progress();
p.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
p.AddOrUpdate<float>("courseProgress", 0.5f);
user.courses.Add(p);
List<Tuple<CourseIndex, float>> list = user.GetRecommendedCourses();
Debug.Assert(list.Count == 1);
Debug.Assert(list[0].Item1 == CourseIndex.FINGERSPELLING);
Debug.Assert(list[0].Item2 == 0.5f);
Assert.AreEqual(list.Count, 1);
Assert.AreEqual(list[0].Item1, CourseIndex.FINGERSPELLING);
Assert.AreEqual(list[0].Item2, 0.5f);
}
/// <summary>
/// Test GetCourseProgress returns null when course cannot be found
/// </summary>
public void TestGetCourseProgressNull()
[Test]
public void Test_User_GetCourseProgress_Null()
{
User user = new User();
Debug.Assert(user.GetCourseProgress(CourseIndex.FINGERSPELLING) == null);
Debug.Assert(user.GetCourseProgress((CourseIndex)100) == null);
Assert.Null(user.GetCourseProgress(CourseIndex.FINGERSPELLING));
Assert.Null(user.GetCourseProgress((CourseIndex)100));
}
/// <summary>
/// Test GetCourseProgress returns correct progress object
/// </summary>
public void TestGetCourseProgressValid()
[Test]
public void Test_User_GetCourseProgress_Valid()
{
User user = new User();
Progress p = new Progress();
p.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
p.AddOrUpdate<float>("courseProgress", 3.14159265f);
user.courses.Add(p);
Progress q = user.GetCourseProgress(CourseIndex.FINGERSPELLING);
Debug.Assert(q.Get<CourseIndex>("courseIndex") == CourseIndex.FINGERSPELLING);
Debug.Assert(q.Get<float>("courseProgress") == 3.14159265f);
Assert.AreEqual(q.Get<CourseIndex>("courseIndex"), CourseIndex.FINGERSPELLING);
Assert.AreEqual(q.Get<float>("courseProgress"), 3.14159265f);
}
/// <summary>
/// Test GetMinigameProgress returns null when minigame cannot be found
/// </summary>
public void TestGetMinigameProgressNull()
[Test]
public void Test_User_GetMinigameProgress_Null()
{
User user = new User();
Debug.Assert(user.GetMinigameProgress(MinigameIndex.SPELLING_BEE) == null);
Debug.Assert(user.GetMinigameProgress((MinigameIndex)100) == null);
Assert.Null(user.GetMinigameProgress(MinigameIndex.SPELLING_BEE));
Assert.Null(user.GetMinigameProgress((MinigameIndex)100));
Progress p = new Progress();
p.AddOrUpdate<MinigameIndex>("minigameIndex", MinigameIndex.SPELLING_BEE);
user.minigames.Add(p);
Debug.Assert(user.GetMinigameProgress(MinigameIndex.HANGMAN) == null);
Assert.Null(user.GetMinigameProgress(MinigameIndex.HANGMAN));
}
/// <summary>
/// Test GetMinigameProgress returns correct progress object
/// </summary>
public void TestGetMinigameProgressValid()
[Test]
public void Test_User_GetMinigameProgress_Valid()
{
User user = new User();
Progress p = new Progress();
p.AddOrUpdate<MinigameIndex>("minigameIndex", MinigameIndex.SPELLING_BEE);
user.minigames.Add(p);
Progress q = user.GetMinigameProgress(MinigameIndex.SPELLING_BEE);
Debug.Assert(q.Get<CourseIndex>("minigameIndex") == CourseIndex.FINGERSPELLING);
Assert.AreEqual(q.Get<CourseIndex>("minigameIndex"), CourseIndex.FINGERSPELLING);
}
}