NetworkBehaviourTests: RegisterDelegateDoesntOverwrite

This commit is contained in:
vis2k 2020-03-07 13:43:35 +01:00
parent 7071794e46
commit f22188ff0b

View File

@ -93,6 +93,13 @@ public void CallSendEventInternal()
} }
} }
// we need to inherit from networkbehaviour to test protected functions
public class NetworkBehaviourDelegateComponent : NetworkBehaviour
{
public static void Delegate(NetworkBehaviour comp, NetworkReader reader) {}
public static void Delegate2(NetworkBehaviour comp, NetworkReader reader) {}
}
public class NetworkBehaviourTests public class NetworkBehaviourTests
{ {
GameObject gameObject; GameObject gameObject;
@ -611,6 +618,25 @@ public void InvokeSyncEvent()
// clean up // clean up
NetworkBehaviour.ClearDelegates(); NetworkBehaviour.ClearDelegates();
} }
[Test]
public void RegisterDelegateDoesntOverwrite()
{
// registerdelegate is protected, but we can use
// RegisterCommandDelegate which calls RegisterDelegate
NetworkBehaviour.RegisterCommandDelegate(
typeof(NetworkBehaviourDelegateComponent),
nameof(NetworkBehaviourDelegateComponent.Delegate),
NetworkBehaviourDelegateComponent.Delegate);
// registering the same name with a different callback shouldn't
// work
LogAssert.Expect(LogType.Error, "Function " + typeof(NetworkBehaviourDelegateComponent) + "." + nameof(NetworkBehaviourDelegateComponent.Delegate) + " and " + typeof(NetworkBehaviourDelegateComponent) + "." + nameof(NetworkBehaviourDelegateComponent.Delegate2) + " have the same hash. Please rename one of them");
NetworkBehaviour.RegisterCommandDelegate(
typeof(NetworkBehaviourDelegateComponent),
nameof(NetworkBehaviourDelegateComponent.Delegate),
NetworkBehaviourDelegateComponent.Delegate2);
}
} }
// we need to inherit from networkbehaviour to test protected functions // we need to inherit from networkbehaviour to test protected functions