Moving classes to their own files (#1845)

This commit is contained in:
James Frowen 2020-05-04 10:59:38 +01:00 committed by GitHub
parent c9db7e3754
commit 64e52bf31e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 577 additions and 471 deletions

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
namespace Mirror.Tests
{
[TestFixture]
public abstract class ClientSceneTestsBase
{
protected const string NewAssetIdIgnoreMessage = "Ignoring this test till we know how to fix it, see https://github.com/vis2k/Mirror/issues/1831";
// use guid to find asset so that the path does not matter
protected const string ValidPrefabAssetGuid = "33169286da0313d45ab5bfccc6cf3775";
protected const string PrefabWithChildrenAssetGuid = "a78e009e3f2dee44e8859516974ede43";
protected const string InvalidPrefabAssetGuid = "78f0a3f755d35324e959f3ecdd993fb0";
// random guid, not used anywhere
protected const string AnotherGuidString = "5794128cdfda04542985151f82990d05";
protected GameObject validPrefab;
protected NetworkIdentity validPrefabNetId;
protected GameObject prefabWithChildren;
protected GameObject invalidPrefab;
protected Guid validPrefabGuid;
protected Guid anotherGuid;
protected Dictionary<Guid, GameObject> prefabs => ClientScene.prefabs;
protected Dictionary<Guid, SpawnHandlerDelegate> spawnHandlers => ClientScene.spawnHandlers;
protected Dictionary<Guid, UnSpawnDelegate> unspawnHandlers => ClientScene.unspawnHandlers;
static GameObject LoadPrefab(string guid)
{
return AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(guid));
}
[OneTimeSetUp]
public void OneTimeSetUp()
{
validPrefab = LoadPrefab(ValidPrefabAssetGuid);
validPrefabNetId = validPrefab.GetComponent<NetworkIdentity>();
prefabWithChildren = LoadPrefab(PrefabWithChildrenAssetGuid);
invalidPrefab = LoadPrefab(InvalidPrefabAssetGuid);
validPrefabGuid = new Guid(ValidPrefabAssetGuid);
anotherGuid = new Guid(AnotherGuidString);
}
[TearDown]
public void TearDown()
{
ClientScene.Shutdown();
// reset asset id incase they are changed by tests
validPrefabNetId.assetId = validPrefabGuid;
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
validPrefab = null;
prefabWithChildren = null;
invalidPrefab = null;
}
}
}

View File

@ -0,0 +1,66 @@
using System;
using NUnit.Framework;
namespace Mirror.Tests
{
public class ClientSceneTests_ClearSpawners : ClientSceneTestsBase
{
[Test]
public void RemovesAllPrefabsFromDictionary()
{
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(prefabs);
}
[Test]
public void RemovesAllSpawnHandlersFromDictionary()
{
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(spawnHandlers);
}
[Test]
public void RemovesAllUnspawnHandlersFromDictionary()
{
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(unspawnHandlers);
}
[Test]
public void ClearsAllDictionary()
{
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(prefabs);
Assert.IsEmpty(spawnHandlers);
Assert.IsEmpty(unspawnHandlers);
}
}
}

View File

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

View File

@ -0,0 +1,62 @@
using System;
using NUnit.Framework;
using UnityEngine;
namespace Mirror.Tests
{
public class ClientSceneTests_GetPrefab : ClientSceneTestsBase
{
[Test]
public void ReturnsFalseForEmptyGuid()
{
bool result = ClientScene.GetPrefab(new Guid(), out GameObject prefab);
Assert.IsFalse(result);
Assert.IsNull(prefab);
}
[Test]
public void ReturnsFalseForPrefabNotFound()
{
Guid guid = Guid.NewGuid();
bool result = ClientScene.GetPrefab(guid, out GameObject prefab);
Assert.IsFalse(result);
Assert.IsNull(prefab);
}
[Test]
public void ReturnsFalseForPrefabIsNull()
{
Guid guid = Guid.NewGuid();
prefabs.Add(guid, null);
bool result = ClientScene.GetPrefab(guid, out GameObject prefab);
Assert.IsFalse(result);
Assert.IsNull(prefab);
}
[Test]
public void ReturnsTrueWhenPrefabIsFound()
{
prefabs.Add(validPrefabGuid, validPrefab);
bool result = ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab);
Assert.IsTrue(result);
Assert.NotNull(prefab);
}
[Test]
public void HasOutPrefabWithCorrectGuid()
{
prefabs.Add(validPrefabGuid, validPrefab);
ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab);
Assert.NotNull(prefab);
NetworkIdentity networkID = prefab.GetComponent<NetworkIdentity>();
Assert.AreEqual(networkID.assetId, validPrefabGuid);
}
}
}

View File

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

View File

@ -1,123 +1,11 @@
using System; using System;
using System.Collections.Generic;
using NUnit.Framework; using NUnit.Framework;
using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.TestTools; using UnityEngine.TestTools;
namespace Mirror.Tests namespace Mirror.Tests
{ {
[TestFixture] public class ClientSceneTests_RegisterPrefab : ClientSceneTestsBase
public abstract class BaseClientSceneTests
{
protected const string NewAssetIdIgnoreMessage = "Ignoring this test till we know how to fix it, see https://github.com/vis2k/Mirror/issues/1831";
// use guid to find asset so that the path does not matter
protected const string ValidPrefabAssetGuid = "33169286da0313d45ab5bfccc6cf3775";
protected const string PrefabWithChildrenAssetGuid = "a78e009e3f2dee44e8859516974ede43";
protected const string InvalidPrefabAssetGuid = "78f0a3f755d35324e959f3ecdd993fb0";
// random guid, not used anywhere
protected const string AnotherGuidString = "5794128cdfda04542985151f82990d05";
protected GameObject validPrefab;
protected NetworkIdentity validPrefabNetId;
protected GameObject prefabWithChildren;
protected GameObject invalidPrefab;
protected Guid validPrefabGuid;
protected Guid anotherGuid;
protected Dictionary<Guid, GameObject> prefabs => ClientScene.prefabs;
protected Dictionary<Guid, SpawnHandlerDelegate> spawnHandlers => ClientScene.spawnHandlers;
protected Dictionary<Guid, UnSpawnDelegate> unspawnHandlers => ClientScene.unspawnHandlers;
static GameObject LoadPrefab(string guid)
{
return AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(guid));
}
[OneTimeSetUp]
public void OneTimeSetUp()
{
validPrefab = LoadPrefab(ValidPrefabAssetGuid);
validPrefabNetId = validPrefab.GetComponent<NetworkIdentity>();
prefabWithChildren = LoadPrefab(PrefabWithChildrenAssetGuid);
invalidPrefab = LoadPrefab(InvalidPrefabAssetGuid);
validPrefabGuid = new Guid(ValidPrefabAssetGuid);
anotherGuid = new Guid(AnotherGuidString);
}
[TearDown]
public void TearDown()
{
ClientScene.Shutdown();
// reset asset id incase they are changed by tests
validPrefabNetId.assetId = validPrefabGuid;
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
validPrefab = null;
prefabWithChildren = null;
invalidPrefab = null;
}
}
public class ClientSceneTests_GetPrefab : BaseClientSceneTests
{
[Test]
public void ReturnsFalseForEmptyGuid()
{
bool result = ClientScene.GetPrefab(new Guid(), out GameObject prefab);
Assert.IsFalse(result);
Assert.IsNull(prefab);
}
[Test]
public void ReturnsFalseForPrefabNotFound()
{
Guid guid = Guid.NewGuid();
bool result = ClientScene.GetPrefab(guid, out GameObject prefab);
Assert.IsFalse(result);
Assert.IsNull(prefab);
}
[Test]
public void ReturnsFalseForPrefabIsNull()
{
Guid guid = Guid.NewGuid();
prefabs.Add(guid, null);
bool result = ClientScene.GetPrefab(guid, out GameObject prefab);
Assert.IsFalse(result);
Assert.IsNull(prefab);
}
[Test]
public void ReturnsTrueWhenPrefabIsFound()
{
prefabs.Add(validPrefabGuid, validPrefab);
bool result = ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab);
Assert.IsTrue(result);
Assert.NotNull(prefab);
}
[Test]
public void HasOutPrefabWithCorrectGuid()
{
prefabs.Add(validPrefabGuid, validPrefab);
ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab);
Assert.NotNull(prefab);
NetworkIdentity networkID = prefab.GetComponent<NetworkIdentity>();
Assert.AreEqual(networkID.assetId, validPrefabGuid);
}
}
public class ClientSceneTests_RegisterPrefab : BaseClientSceneTests
{ {
[Test] [Test]
[TestCase(RegisterPrefabOverload.Prefab, false)] [TestCase(RegisterPrefabOverload.Prefab, false)]
@ -663,361 +551,4 @@ public void Handler_ErrorWhenUnSpawnHandlerIsNull(RegisterPrefabOverload overloa
} }
} }
public class ClientSceneTests_UnregisterPrefab : BaseClientSceneTests
{
[Test]
public void RemovesPrefabFromDictionary()
{
prefabs.Add(validPrefabGuid, validPrefab);
ClientScene.UnregisterPrefab(validPrefab);
Assert.IsFalse(prefabs.ContainsKey(validPrefabGuid));
}
[Test]
public void RemovesSpawnHandlerFromDictionary()
{
spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null));
ClientScene.UnregisterPrefab(validPrefab);
Assert.IsFalse(spawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void RemovesUnSpawnHandlerFromDictionary()
{
unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { }));
ClientScene.UnregisterPrefab(validPrefab);
Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void ErrorWhenPrefabIsNull()
{
LogAssert.Expect(LogType.Error, "Could not unregister prefab because it was null");
ClientScene.UnregisterPrefab(null);
}
[Test]
public void ErrorWhenPrefabHasNoNetworkIdentity()
{
LogAssert.Expect(LogType.Error, $"Could not unregister '{invalidPrefab.name}' since it contains no NetworkIdentity component");
ClientScene.UnregisterPrefab(invalidPrefab);
}
}
public class ClientSceneTests_RegisterSpawnHandler : BaseClientSceneTests
{
[Test]
public void SpawnDelegate_AddsHandlerToSpawnHandlers()
{
int handlerCalled = 0;
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((pos, rot) =>
{
handlerCalled++;
return null;
});
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(spawnHandlers.ContainsKey(guid));
// check spawnHandler above is called
SpawnHandlerDelegate handler = spawnHandlers[guid];
handler.Invoke(default);
Assert.That(handlerCalled, Is.EqualTo(1));
}
[Test]
public void SpawnDelegate_AddsHandlerToSpawnHandlersWithCorrectArguments()
{
int handlerCalled = 0;
Vector3 somePosition = new Vector3(10, 20, 3);
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((pos, assetId) =>
{
handlerCalled++;
Assert.That(pos, Is.EqualTo(somePosition));
Assert.That(assetId, Is.EqualTo(guid));
return null;
});
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(spawnHandlers.ContainsKey(guid));
// check spawnHandler above is called
SpawnHandlerDelegate handler = spawnHandlers[guid];
handler.Invoke(new SpawnMessage { position = somePosition, assetId = guid });
Assert.That(handlerCalled, Is.EqualTo(1));
}
[Test]
public void SpawnDelegate_AddsHandlerToUnSpawnHandlers()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(unspawnHandlers.ContainsKey(guid));
Assert.AreEqual(unspawnHandlers[guid], unspawnHandler);
}
[Test]
public void SpawnDelegate_ErrorWhenSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = null;
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnDelegate_ErrorWhenUnSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = null;
LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnDelegate_ErrorWhenAssetIdIsEmpty()
{
Guid guid = new Guid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
SpawnDelegate spawnHandler2 = new SpawnDelegate((x, y) => new GameObject());
UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x));
LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2);
}
[Test]
public void SpawnDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary()
{
Guid guid = Guid.NewGuid();
prefabs.Add(guid, validPrefab);
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_AddsHandlerToSpawnHandlers()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(spawnHandlers.ContainsKey(guid));
Assert.AreEqual(spawnHandlers[guid], spawnHandler);
}
[Test]
public void SpawnHandlerDelegate_AddsHandlerToUnSpawnHandlers()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(unspawnHandlers.ContainsKey(guid));
Assert.AreEqual(unspawnHandlers[guid], unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = null;
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenUnSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = null;
LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenAssetIdIsEmpty()
{
Guid guid = new Guid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
SpawnHandlerDelegate spawnHandler2 = new SpawnHandlerDelegate(x => new GameObject());
UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x));
LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary()
{
Guid guid = Guid.NewGuid();
prefabs.Add(guid, validPrefab);
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => new GameObject());
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x));
LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
}
public class ClientSceneTests_UnregisterSpawnHandler : BaseClientSceneTests
{
[Test]
public void RemovesSpawnHandlersFromDictionary()
{
spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null));
ClientScene.UnregisterSpawnHandler(validPrefabGuid);
Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void RemovesUnSpawnHandlersFromDictionary()
{
unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { }));
ClientScene.UnregisterSpawnHandler(validPrefabGuid);
Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void DoesNotRemovePrefabDictionary()
{
prefabs.Add(validPrefabGuid, validPrefab);
ClientScene.UnregisterSpawnHandler(validPrefabGuid);
// Should not be removed
Assert.IsTrue(prefabs.ContainsKey(validPrefabGuid));
}
}
public class ClientSceneTests_ClearSpawners : BaseClientSceneTests
{
[Test]
public void RemovesAllPrefabsFromDictionary()
{
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(prefabs);
}
[Test]
public void RemovesAllSpawnHandlersFromDictionary()
{
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(spawnHandlers);
}
[Test]
public void RemovesAllUnspawnHandlersFromDictionary()
{
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(unspawnHandlers);
}
[Test]
public void ClearsAllDictionary()
{
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
prefabs.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
spawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
unspawnHandlers.Add(Guid.NewGuid(), null);
ClientScene.ClearSpawners();
Assert.IsEmpty(prefabs);
Assert.IsEmpty(spawnHandlers);
Assert.IsEmpty(unspawnHandlers);
}
}
} }

View File

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

View File

@ -0,0 +1,224 @@
using System;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Mirror.Tests
{
public class ClientSceneTests_RegisterSpawnHandler : ClientSceneTestsBase
{
[Test]
public void SpawnDelegate_AddsHandlerToSpawnHandlers()
{
int handlerCalled = 0;
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((pos, rot) =>
{
handlerCalled++;
return null;
});
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(spawnHandlers.ContainsKey(guid));
// check spawnHandler above is called
SpawnHandlerDelegate handler = spawnHandlers[guid];
handler.Invoke(default);
Assert.That(handlerCalled, Is.EqualTo(1));
}
[Test]
public void SpawnDelegate_AddsHandlerToSpawnHandlersWithCorrectArguments()
{
int handlerCalled = 0;
Vector3 somePosition = new Vector3(10, 20, 3);
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((pos, assetId) =>
{
handlerCalled++;
Assert.That(pos, Is.EqualTo(somePosition));
Assert.That(assetId, Is.EqualTo(guid));
return null;
});
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(spawnHandlers.ContainsKey(guid));
// check spawnHandler above is called
SpawnHandlerDelegate handler = spawnHandlers[guid];
handler.Invoke(new SpawnMessage { position = somePosition, assetId = guid });
Assert.That(handlerCalled, Is.EqualTo(1));
}
[Test]
public void SpawnDelegate_AddsHandlerToUnSpawnHandlers()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(unspawnHandlers.ContainsKey(guid));
Assert.AreEqual(unspawnHandlers[guid], unspawnHandler);
}
[Test]
public void SpawnDelegate_ErrorWhenSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = null;
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnDelegate_ErrorWhenUnSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = null;
LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnDelegate_ErrorWhenAssetIdIsEmpty()
{
Guid guid = new Guid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary()
{
Guid guid = Guid.NewGuid();
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
SpawnDelegate spawnHandler2 = new SpawnDelegate((x, y) => new GameObject());
UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x));
LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2);
}
[Test]
public void SpawnDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary()
{
Guid guid = Guid.NewGuid();
prefabs.Add(guid, validPrefab);
SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_AddsHandlerToSpawnHandlers()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(spawnHandlers.ContainsKey(guid));
Assert.AreEqual(spawnHandlers[guid], spawnHandler);
}
[Test]
public void SpawnHandlerDelegate_AddsHandlerToUnSpawnHandlers()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
Assert.IsTrue(unspawnHandlers.ContainsKey(guid));
Assert.AreEqual(unspawnHandlers[guid], unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = null;
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenUnSpawnHandlerIsNull()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = null;
LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenAssetIdIsEmpty()
{
Guid guid = new Guid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
[Test]
public void SpawnHandlerDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary()
{
Guid guid = Guid.NewGuid();
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null);
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { });
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
SpawnHandlerDelegate spawnHandler2 = new SpawnHandlerDelegate(x => new GameObject());
UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x));
LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}");
ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2);
}
[Test]
public void SpawnHandlerDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary()
{
Guid guid = Guid.NewGuid();
prefabs.Add(guid, validPrefab);
SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => new GameObject());
UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x));
LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'");
ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);
}
}
}

View File

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

View File

@ -0,0 +1,54 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Mirror.Tests
{
public class ClientSceneTests_UnregisterPrefab : ClientSceneTestsBase
{
[Test]
public void RemovesPrefabFromDictionary()
{
prefabs.Add(validPrefabGuid, validPrefab);
ClientScene.UnregisterPrefab(validPrefab);
Assert.IsFalse(prefabs.ContainsKey(validPrefabGuid));
}
[Test]
public void RemovesSpawnHandlerFromDictionary()
{
spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null));
ClientScene.UnregisterPrefab(validPrefab);
Assert.IsFalse(spawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void RemovesUnSpawnHandlerFromDictionary()
{
unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { }));
ClientScene.UnregisterPrefab(validPrefab);
Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void ErrorWhenPrefabIsNull()
{
LogAssert.Expect(LogType.Error, "Could not unregister prefab because it was null");
ClientScene.UnregisterPrefab(null);
}
[Test]
public void ErrorWhenPrefabHasNoNetworkIdentity()
{
LogAssert.Expect(LogType.Error, $"Could not unregister '{invalidPrefab.name}' since it contains no NetworkIdentity component");
ClientScene.UnregisterPrefab(invalidPrefab);
}
}
}

View File

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

View File

@ -0,0 +1,39 @@
using NUnit.Framework;
namespace Mirror.Tests
{
public class ClientSceneTests_UnregisterSpawnHandler : ClientSceneTestsBase
{
[Test]
public void RemovesSpawnHandlersFromDictionary()
{
spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null));
ClientScene.UnregisterSpawnHandler(validPrefabGuid);
Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void RemovesUnSpawnHandlersFromDictionary()
{
unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { }));
ClientScene.UnregisterSpawnHandler(validPrefabGuid);
Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid));
}
[Test]
public void DoesNotRemovePrefabDictionary()
{
prefabs.Add(validPrefabGuid, validPrefab);
ClientScene.UnregisterSpawnHandler(validPrefabGuid);
// Should not be removed
Assert.IsTrue(prefabs.ContainsKey(validPrefabGuid));
}
}
}

View File

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