Resolve WES-187 "Unit tests account and system"
This commit is contained in:
committed by
Jerome Coudron
parent
c4b6c60288
commit
b9bbef8dcf
26
Assets/Accounts/Tests/EditMode/AccountEditMode.asmdef
Normal file
26
Assets/Accounts/Tests/EditMode/AccountEditMode.asmdef
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "AccountEditMode",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"UnityEngine.TestRunner",
|
||||
"UnityEditor.TestRunner",
|
||||
"AccountsScripts",
|
||||
"InterfacesScripts",
|
||||
"ArchitectureScripts"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"nunit.framework.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8500f5aede7627729bd8c97b15e541c4
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
27
Assets/Accounts/Tests/EditMode/UserAvatarListTests.cs
Normal file
27
Assets/Accounts/Tests/EditMode/UserAvatarListTests.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Test the UserAvatarList class
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class UserAvatarListTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test the UserAvatarList class correctly initializes the UserList class
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_Awake()
|
||||
{
|
||||
UserList.AVATARS = new List<Sprite>();
|
||||
|
||||
var scriptableObject = AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset");
|
||||
scriptableObject.Awake();
|
||||
|
||||
for (int i = 0; i < scriptableObject.avatars.Count; i++)
|
||||
Assert.AreEqual(scriptableObject.avatars[i], UserList.AVATARS[i]);
|
||||
}
|
||||
}
|
||||
|
||||
11
Assets/Accounts/Tests/EditMode/UserAvatarListTests.cs.meta
Normal file
11
Assets/Accounts/Tests/EditMode/UserAvatarListTests.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c26e155ab597954a8c65fafe61a8e22
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
33
Assets/Accounts/Tests/EditMode/UserCreationScreenTests.cs
Normal file
33
Assets/Accounts/Tests/EditMode/UserCreationScreenTests.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using NUnit.Framework;
|
||||
|
||||
/// <summary>
|
||||
/// Test the UserCreationScreen class
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class UserCreationScreenTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Tets IsValidUsername will return <c>true</c> for an valid username
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserCreationScreen_IsValidUsername_True()
|
||||
{
|
||||
foreach (char c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||
Assert.IsTrue(UserCreationScreen.IsValidUsername(c.ToString()));
|
||||
|
||||
Assert.IsTrue(UserCreationScreen.IsValidUsername("123456789AbC"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tets IsValidUsername will return <c>false</c> for an invalid username
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserCreationScreen_IsValidUsername_False()
|
||||
{
|
||||
Assert.IsFalse(UserCreationScreen.IsValidUsername(string.Empty));
|
||||
foreach (char c in " \n\t+-*/%_.,;:!?(){}[]\\'\"|&~^$")
|
||||
Assert.IsFalse(UserCreationScreen.IsValidUsername(c.ToString()));
|
||||
|
||||
Assert.IsFalse(UserCreationScreen.IsValidUsername("123456789_10_11_12_13"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c44016c2b2f4405eb7a902b6781e4da
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
448
Assets/Accounts/Tests/EditMode/UserListTests.cs
Normal file
448
Assets/Accounts/Tests/EditMode/UserListTests.cs
Normal file
@@ -0,0 +1,448 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Test the UserList class
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class UserListTests
|
||||
{
|
||||
/// <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>
|
||||
/// Helper variable for quick user creation
|
||||
/// </summary>
|
||||
private string username = "u5erNam3";
|
||||
|
||||
/// <summary>
|
||||
/// Helper variable for quick user creation
|
||||
/// </summary>
|
||||
private Sprite avatar = null;
|
||||
|
||||
private string cachedPath;
|
||||
|
||||
/// <summary>
|
||||
/// Setup the tests
|
||||
/// </summary>
|
||||
[SetUp]
|
||||
public void Setup_UserList()
|
||||
{
|
||||
PersistentDataController.PATH = UserListTests.PATH;
|
||||
|
||||
if (File.Exists(PATH))
|
||||
File.Delete(PATH);
|
||||
|
||||
PersistentDataController.GetInstance().Load();
|
||||
AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
|
||||
avatar = UserList.AVATARS[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleanup after testing
|
||||
/// </summary>
|
||||
[TearDown]
|
||||
public void TearDown_UserList()
|
||||
{
|
||||
PersistentDataController.PATH = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test for creation of a new UserList
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_New_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_AddUser()
|
||||
{
|
||||
Assert.Zero(UserList.GetUsers().Count);
|
||||
User user = UserList.AddUser(username, avatar);
|
||||
|
||||
Assert.IsNotNull(user);
|
||||
Assert.AreEqual(1, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(username, user.GetUsername());
|
||||
Assert.AreEqual(avatar, user.GetAvatar());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether an existing user can be found by its username
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByUsername_Valid()
|
||||
{
|
||||
User u = UserList.AddUser(username, avatar);
|
||||
User v = UserList.GetUserByUsername(username);
|
||||
Assert.AreEqual(u.GetUsername(), v.GetUsername());
|
||||
}
|
||||
|
||||
/// <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 an existing user can be found by its username
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByIndex_Valid()
|
||||
{
|
||||
User u = UserList.AddUser(username, avatar);
|
||||
User v = UserList.GetUserByIndex(0);
|
||||
Assert.AreEqual(u.GetUsername(), v.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a non-existing user can not be found
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUserByIndex_Exception()
|
||||
{
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.GetUserByIndex(0); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a userlist is correctly returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetUsers()
|
||||
{
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
u.Add(UserList.AddUser($"{username}_{i}", avatar));
|
||||
|
||||
List<User> v = UserList.GetUsers();
|
||||
Assert.AreEqual(v.Count, u.Count);
|
||||
for (int i = 0; i < 5; i++)
|
||||
Assert.AreEqual(u[i].GetUsername(), v[i].GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the correct current user is returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetCurrentUser()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(2);
|
||||
|
||||
User W = UserList.GetCurrentUser();
|
||||
Assert.AreEqual(w.GetUsername(), W.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether a null user is returned when the userlist is empty
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_GetCurrentUser_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_IndexOf_Valid()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(2);
|
||||
|
||||
Assert.AreEqual(0, UserList.IndexOf(u.GetUsername()));
|
||||
Assert.AreEqual(1, UserList.IndexOf(v.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.IndexOf(w.GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the correct index is returned for the current user
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_IndexOf_Exception()
|
||||
{
|
||||
User u = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = username,
|
||||
avatarIndex = 0
|
||||
});
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.IndexOf(u.GetUsername()); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the current user (referenced by index) is correctly changed
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_ValidIndex()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(2);
|
||||
|
||||
User W = UserList.GetCurrentUser();
|
||||
Assert.AreEqual(w.GetUsername(), W.GetUsername());
|
||||
}
|
||||
|
||||
/// <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.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{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_ValidUsername()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(v.GetUsername());
|
||||
User V = UserList.GetCurrentUser();
|
||||
Assert.AreEqual(v.GetUsername(), V.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the current user is not changed when a non-existing user is given
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_InvalidUsername()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = $"{username}_{'v'}",
|
||||
avatarIndex = 0
|
||||
});
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.ChangeCurrentUser(v.GetUsername()); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the current user is not changed when a null user is given
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_ChangeCurrentUser_NullUsername()
|
||||
{
|
||||
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.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{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.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.DeleteUser(-1); });
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { UserList.DeleteUser(5); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting any user from an empty userlist will fail
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_IndexEmpty()
|
||||
{
|
||||
Assert.Throws<IndexOutOfRangeException>(delegate { 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_BeforeCurrentUser()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(1);
|
||||
|
||||
Assert.AreEqual(3, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
Assert.IsTrue(UserList.DeleteUser(u.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(0, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting an existing user will correctly remove the user and set the currentUserIndex correctly
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_CurrentUser()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(1);
|
||||
|
||||
Assert.AreEqual(3, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
Assert.IsTrue(UserList.DeleteUser(v.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(0, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting an existing user will correctly remove the user and set the currentUserIndex correctly
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_AfterCurrentUser()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = UserList.AddUser($"{username}_{'v'}", avatar);
|
||||
User w = UserList.AddUser($"{username}_{'w'}", avatar);
|
||||
UserList.ChangeCurrentUser(1);
|
||||
|
||||
Assert.AreEqual(3, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
Assert.IsTrue(UserList.DeleteUser(w.GetUsername()));
|
||||
Assert.AreEqual(2, UserList.GetUsers().Count);
|
||||
Assert.AreEqual(1, UserList.IndexOf(UserList.GetCurrentUser().GetUsername()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether deleting a non-existing user will not affect the userlist
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_UserList_DeleteUser_Invalid()
|
||||
{
|
||||
User u = UserList.AddUser($"{username}_{'u'}", avatar);
|
||||
User v = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = $"{username}_{'v'}",
|
||||
avatarIndex = 0
|
||||
});
|
||||
|
||||
Assert.AreEqual(1, UserList.GetUsers().Count);
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.DeleteUser(v.GetUsername()); });
|
||||
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 = new User(new PersistentDataController.SavedUserData()
|
||||
{
|
||||
username = username,
|
||||
avatarIndex = 0
|
||||
});
|
||||
Assert.Zero(UserList.GetUsers().Count);
|
||||
Assert.Throws<KeyNotFoundException>(delegate { UserList.DeleteUser(user.GetUsername()); });
|
||||
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()
|
||||
{
|
||||
List<User> u = new List<User>();
|
||||
for (int i = 0; i < 5; i++)
|
||||
u.Add(UserList.AddUser($"{username}_{i}", avatar));
|
||||
|
||||
UserList.ChangeCurrentUser(3);
|
||||
UserList.Save();
|
||||
FileAssert.Exists(PATH);
|
||||
|
||||
string content = File.ReadAllText(PATH);
|
||||
string expected = $"{{\"version\":{PersistentDataController.VERSION},\"users\":[{{\"entries\":[],\"username\":\"u5erNam3_0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_1\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_2\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_3\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_4\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}],\"currentUser\":3,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
||||
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.AddUser($"{username}_{i}", avatar));
|
||||
|
||||
UserList.ChangeCurrentUser(3);
|
||||
UserList.Save();
|
||||
FileAssert.Exists(PATH);
|
||||
|
||||
string content = File.ReadAllText(PATH);
|
||||
string expected = $"{{\"version\":{PersistentDataController.VERSION},\"users\":[{{\"entries\":[],\"username\":\"u5erNam3_0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_1\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_2\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_3\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}},{{\"entries\":[],\"username\":\"u5erNam3_4\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}],\"currentUser\":3,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
||||
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 = $"{{\"version\":{PersistentDataController.VERSION},\"users\":[],\"currentUser\":-1,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
||||
Assert.AreEqual(expected, content);
|
||||
}
|
||||
}
|
||||
11
Assets/Accounts/Tests/EditMode/UserListTests.cs.meta
Normal file
11
Assets/Accounts/Tests/EditMode/UserListTests.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc44c73b32b9af7469b76bd6071f0cf5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
226
Assets/Accounts/Tests/EditMode/UserTests.cs
Normal file
226
Assets/Accounts/Tests/EditMode/UserTests.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Test the User class
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class UserTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference to the user to be tested
|
||||
/// </summary>
|
||||
private User user;
|
||||
|
||||
/// <summary>
|
||||
/// Setup the tests
|
||||
/// </summary>
|
||||
[SetUp]
|
||||
public void Setup_User()
|
||||
{
|
||||
PersistentDataController.SavedUserData data = new PersistentDataController.SavedUserData();
|
||||
data.username = "username";
|
||||
data.avatarIndex = 0;
|
||||
user = new User(data);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Test for the creation of a new user
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_New_User()
|
||||
{
|
||||
Assert.NotNull(user);
|
||||
Assert.Zero(user.GetCourses().Count);
|
||||
Assert.Zero(user.GetMinigames().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the correct username is returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetUsername()
|
||||
{
|
||||
Assert.AreEqual("username", user.GetUsername());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the correct avatar is returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetAvatar()
|
||||
{
|
||||
Assert.AreEqual(UserList.AVATARS[0], user.GetAvatar());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether the correct total playtime is returned
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetPlaytime()
|
||||
{
|
||||
Assert.AreEqual(0.0, user.GetPlaytime());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether progress on a new course can be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_AddCourse()
|
||||
{
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
user.AddCourseProgress(p);
|
||||
Assert.AreEqual(user.GetCourses().Count, 1);
|
||||
Assert.Zero(user.GetMinigames().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test whether progress on a new minigame can be added
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_AddMinigame()
|
||||
{
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
user.AddMinigameProgress(p);
|
||||
Assert.Zero(user.GetCourses().Count);
|
||||
Assert.AreEqual(user.GetMinigames().Count, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetRecentCourses will return empty when no progress is stored
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetRecentCourses_Empty()
|
||||
{
|
||||
Assert.Zero(user.GetRecentCourses().Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TEMPORARY test for GetRecentCourses will return all progress that is stored
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetRecentCourses_All()
|
||||
{
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
p.progress = 0.5f;
|
||||
user.AddCourseProgress(p);
|
||||
List<Tuple<CourseIndex, float>> list = user.GetRecentCourses();
|
||||
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>
|
||||
[Test]
|
||||
public void Test_User_GetRecommendedCourses_Empty()
|
||||
{
|
||||
List<Tuple<CourseIndex, float>> list = user.GetRecommendedCourses();
|
||||
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
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetRecommendedCourses_All()
|
||||
{
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
p.progress = 0.5f;
|
||||
user.AddCourseProgress(p);
|
||||
List<Tuple<CourseIndex, float>> list = user.GetRecommendedCourses();
|
||||
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>
|
||||
[Test]
|
||||
public void Test_User_GetCourseProgress_Null()
|
||||
{
|
||||
Assert.Null(user.GetCourseProgress(CourseIndex.FINGERSPELLING));
|
||||
Assert.Null(user.GetCourseProgress((CourseIndex)100));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetCourseProgress returns correct progress object
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetCourseProgress_Valid()
|
||||
{
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
p.progress = 3.14159265f;
|
||||
user.AddCourseProgress(p);
|
||||
var q = user.GetCourseProgress(CourseIndex.FINGERSPELLING);
|
||||
Assert.AreEqual(q.courseIndex, CourseIndex.FINGERSPELLING);
|
||||
Assert.AreEqual(q.progress, 3.14159265f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test progress of a course is correctly reset (aka removed)
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_ResetCourseProgres()
|
||||
{
|
||||
var p = new PersistentDataController.SavedCourseProgress();
|
||||
p.courseIndex = CourseIndex.FINGERSPELLING;
|
||||
user.AddCourseProgress(p);
|
||||
Assert.IsNotNull(user.GetCourseProgress(CourseIndex.FINGERSPELLING));
|
||||
user.ResetCourseProgress(CourseIndex.FINGERSPELLING);
|
||||
Assert.IsNull(user.GetCourseProgress(CourseIndex.FINGERSPELLING));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetMinigameProgress returns null when minigame cannot be found
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetMinigameProgress_Null()
|
||||
{
|
||||
Assert.Null(user.GetMinigameProgress(MinigameIndex.SPELLING_BEE));
|
||||
Assert.Null(user.GetMinigameProgress((MinigameIndex)100));
|
||||
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
p.minigameIndex = MinigameIndex.SPELLING_BEE;
|
||||
user.AddMinigameProgress(p);
|
||||
Assert.Null(user.GetMinigameProgress(MinigameIndex.HANGMAN));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test GetMinigameProgress returns correct progress object
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_GetMinigameProgress_Valid()
|
||||
{
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
p.minigameIndex = MinigameIndex.SPELLING_BEE;
|
||||
user.AddMinigameProgress(p);
|
||||
var q = user.GetMinigameProgress(MinigameIndex.SPELLING_BEE);
|
||||
Assert.AreEqual(q.minigameIndex, MinigameIndex.SPELLING_BEE);
|
||||
Assert.Zero(q.latestScores.Count);
|
||||
Assert.Zero(q.highestScores.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test progress of a minigame is correctly reset (aka removed)
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void Test_User_ResetMinigameProgres()
|
||||
{
|
||||
var p = new PersistentDataController.SavedMinigameProgress();
|
||||
p.minigameIndex = MinigameIndex.SPELLING_BEE;
|
||||
user.AddMinigameProgress(p);
|
||||
Assert.IsNotNull(user.GetMinigameProgress(MinigameIndex.SPELLING_BEE));
|
||||
user.ResetMinigameProgress(MinigameIndex.SPELLING_BEE);
|
||||
Assert.IsNull(user.GetMinigameProgress(MinigameIndex.SPELLING_BEE));
|
||||
}
|
||||
}
|
||||
11
Assets/Accounts/Tests/EditMode/UserTests.cs.meta
Normal file
11
Assets/Accounts/Tests/EditMode/UserTests.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6775c39e189fa9e9bbd85516fc4a50cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user