mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
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:
@@ -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))
|
||||
{
|
||||
|
||||
52
Robust.UnitTesting/Shared/GameObjects/EntityManager_Tests.cs
Normal file
52
Robust.UnitTesting/Shared/GameObjects/EntityManager_Tests.cs
Normal 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 { }
|
||||
}
|
||||
Reference in New Issue
Block a user