A null event handler delegate cannot be subscribed to an event type.

Resolved https://github.com/space-wizards/RobustToolbox/issues/575.
This commit is contained in:
Acruid
2019-09-03 13:30:53 -07:00
parent 58aac5ea2b
commit 5b3aaea5ec
2 changed files with 56 additions and 0 deletions

View File

@@ -348,6 +348,10 @@ namespace Robust.Shared.GameObjects
public void SubscribeEvent<T>(EntityEventHandler<T> eventHandler, IEntityEventSubscriber s)
where T : EntityEventArgs
{
// adding a null handler delegate should do nothing, since trying to invoke it would throw a NullRefException
if(eventHandler == null)
return;
var eventType = typeof(T);
if (!_eventSubscriptions.TryGetValue(eventType, out var subscriptions))
{

View File

@@ -0,0 +1,52 @@
using System;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Map;
namespace Robust.UnitTesting.Shared.GameObjects
{
[TestFixture, Parallelizable, TestOf(typeof(EntityManager))]
public class EntityManager_Tests
{
/// <summary>
/// Raising a null C# delegate does not generate a NullReferenceException.
/// </summary>
[Test]
public void SubscribeEvent_NullEvent_NoNullException()
{
// Arrange
var manager = new TestEntityManager();
var subscriber = new TestEventSubscriber();
manager.SubscribeEvent((EntityEventHandler<TestEventArgs>) null, subscriber);
// Act
manager.RaiseEvent(null, new TestEventArgs());
//Assert: this should do nothing
}
}
internal class TestEventSubscriber : IEntityEventSubscriber { }
internal class TestEntityManager : EntityManager
{
public override IEntity SpawnEntity(string protoName)
{
throw new NotImplementedException();
}
public override IEntity SpawnEntityNoMapInit(string protoName)
{
throw new NotImplementedException();
}
public override IEntity SpawnEntityAt(string entityType, GridCoordinates coordinates)
{
throw new NotImplementedException();
}
}
internal class TestEventArgs : EntityEventArgs { }
}