mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-15 23:02:34 +02:00
* Added new ComponentEventBus, combined it with IEventBus. * Removed all traces of IEntity from ComponentDependencies. Removed IEntityManager dependency from ComponentManager. * Added entity create/delete events to IEntityManager. * ComponentEvents now use EntitySystemMessages instead of their custom ComponentEvent class. * Component events are now just overloads of entity events. * Removed obsolete EntitySystemMessage, now everything uses the base EntityEventArgs. * Add a bool argument for if the message should be broadcast as well as directed. Fix ordering and init issues of events in EntityManager. * Changed names from Component/Entity events to Directed/Broadcast. * Fix bugs and unit tests.
116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
using System;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
using Robust.Shared.GameObjects;
|
|
|
|
namespace Robust.UnitTesting.Shared.GameObjects
|
|
{
|
|
public partial class EntityEventBusTests
|
|
{
|
|
[Test]
|
|
public void SubscribeCompEvent()
|
|
{
|
|
// Arrange
|
|
var entUid = new EntityUid(7);
|
|
var compInstance = new MetaDataComponent();
|
|
|
|
var entManMock = new Mock<IEntityManager>();
|
|
|
|
|
|
var compManMock = new Mock<IComponentManager>();
|
|
|
|
IComponent? outIComponent = compInstance;
|
|
compManMock.Setup(m => m.TryGetComponent(entUid, typeof(MetaDataComponent), out outIComponent))
|
|
.Returns(true);
|
|
|
|
compManMock.Setup(m => m.GetComponent(entUid, typeof(MetaDataComponent)))
|
|
.Returns(compInstance);
|
|
|
|
entManMock.Setup(m => m.ComponentManager).Returns(compManMock.Object);
|
|
var bus = new EntityEventBus(entManMock.Object);
|
|
|
|
// Subscribe
|
|
int calledCount = 0;
|
|
bus.SubscribeLocalEvent<MetaDataComponent, TestEvent>(HandleTestEvent);
|
|
|
|
// add a component to the system
|
|
entManMock.Raise(m=>m.EntityAdded += null, entManMock.Object, entUid);
|
|
compManMock.Raise(m => m.ComponentAdded += null, new AddedComponentEventArgs(compInstance, entUid));
|
|
|
|
// Raise
|
|
var evntArgs = new TestEvent(5);
|
|
bus.RaiseLocalEvent(entUid, evntArgs, true);
|
|
|
|
// Assert
|
|
Assert.That(calledCount, Is.EqualTo(1));
|
|
void HandleTestEvent(EntityUid uid, MetaDataComponent component, TestEvent args)
|
|
{
|
|
calledCount++;
|
|
Assert.That(uid, Is.EqualTo(entUid));
|
|
Assert.That(component, Is.EqualTo(compInstance));
|
|
Assert.That(args.TestNumber, Is.EqualTo(5));
|
|
}
|
|
|
|
}
|
|
|
|
[Test]
|
|
public void UnsubscribeCompEvent()
|
|
{
|
|
// Arrange
|
|
var entUid = new EntityUid(7);
|
|
var compInstance = new MetaDataComponent();
|
|
|
|
var entManMock = new Mock<IEntityManager>();
|
|
|
|
|
|
var compManMock = new Mock<IComponentManager>();
|
|
|
|
IComponent? outIComponent = compInstance;
|
|
compManMock.Setup(m => m.TryGetComponent(entUid, typeof(MetaDataComponent), out outIComponent))
|
|
.Returns(true);
|
|
|
|
compManMock.Setup(m => m.GetComponent(entUid, typeof(MetaDataComponent)))
|
|
.Returns(compInstance);
|
|
|
|
entManMock.Setup(m => m.ComponentManager).Returns(compManMock.Object);
|
|
var bus = new EntityEventBus(entManMock.Object);
|
|
|
|
// Subscribe
|
|
int calledCount = 0;
|
|
bus.SubscribeLocalEvent<MetaDataComponent, TestEvent>(HandleTestEvent);
|
|
bus.UnsubscribeLocalEvent<MetaDataComponent, TestEvent>(HandleTestEvent);
|
|
|
|
// add a component to the system
|
|
entManMock.Raise(m => m.EntityAdded += null, entManMock.Object, entUid);
|
|
compManMock.Raise(m => m.ComponentAdded += null, new AddedComponentEventArgs(compInstance, entUid));
|
|
|
|
// Raise
|
|
var evntArgs = new TestEvent(5);
|
|
bus.RaiseLocalEvent(entUid, evntArgs, true);
|
|
|
|
// Assert
|
|
Assert.That(calledCount, Is.EqualTo(0));
|
|
void HandleTestEvent(EntityUid uid, MetaDataComponent component, TestEvent args)
|
|
{
|
|
calledCount++;
|
|
}
|
|
|
|
}
|
|
|
|
private class DummyComponent : Component
|
|
{
|
|
public override string Name => "Dummy";
|
|
}
|
|
|
|
private class TestEvent : EntityEventArgs
|
|
{
|
|
public int TestNumber { get; }
|
|
|
|
public TestEvent(int testNumber)
|
|
{
|
|
TestNumber = testNumber;
|
|
}
|
|
}
|
|
}
|
|
}
|