using NUnit.Framework; using FluentAssertions; using Encompass; using System.Collections.Generic; using System; using System.Linq; namespace Tests { public class ComponentTests { struct MockComponent : IComponent { public string myString; public int myInt; } struct EntityMessage : IMessage { public Entity entity; } static IEnumerable<(Guid, MockComponent)> gottenMockComponentIDPairs = Enumerable.Empty<(Guid, MockComponent)>(); static (Guid, MockComponent) gottenMockComponentIDPair; [Receives(typeof(EntityMessage))] [Reads(typeof(MockComponent))] class GetMockComponentEngine : Engine { public override void Update(double dt) { foreach (var entityMessage in ReadMessages()) { gottenMockComponentIDPair = GetComponent(entityMessage.entity); } } } struct AddComponentTestMessage : IMessage { public Entity entity; public MockComponent mockComponent; } [Receives(typeof(AddComponentTestMessage))] [Reads(typeof(MockComponent))] class AddComponentTestEngine : Engine { public override void Update(double dt) { foreach (var addComponentTestMessage in ReadMessages()) { Assert.IsTrue(HasComponent(addComponentTestMessage.entity)); Assert.That(GetComponent(addComponentTestMessage.entity).Item2, Is.EqualTo(addComponentTestMessage.mockComponent)); } } } [TestCase(true)] [TestCase(false)] public void AddComponent(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new AddComponentTestEngine()); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; worldBuilder.AddComponent(entity, mockComponent); AddComponentTestMessage addComponentTestMessage; addComponentTestMessage.entity = entity; addComponentTestMessage.mockComponent = mockComponent; worldBuilder.SendMessage(addComponentTestMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } struct AddMockComponentMessage : IMessage { public Entity entity; public MockComponent mockComponent; } [Sends(typeof(AddMockComponentMessage))] class EmitMockComponentMessageEngine : Engine { private Entity entity; public EmitMockComponentMessageEngine(Entity entity) { this.entity = entity; } public override void Update(double dt) { MockComponent mockComponent; mockComponent.myInt = 10; mockComponent.myString = "four"; AddMockComponentMessage addMockComponentMessage; addMockComponentMessage.entity = entity; addMockComponentMessage.mockComponent = mockComponent; SendMessage(addMockComponentMessage); } } [Activates(typeof(MockComponent))] [Receives(typeof(AddMockComponentMessage))] class AddMockComponentEngine : Engine { public override void Update(double dt) { foreach (var message in ReadMessages()) { AddComponent(message.entity, message.mockComponent); } } } static bool hasComponentResult = false; [ReadsPending(typeof(MockComponent))] class HasMockComponentEngine : Engine { private Entity entity; public HasMockComponentEngine(Entity entity) { this.entity = entity; } public override void Update(double dt) { hasComponentResult = HasComponent(entity); } } [TestCase(true)] [TestCase(false)] public void AddComponentAndReadSameFrame(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); var entity = worldBuilder.CreateEntity(); worldBuilder.AddEngine(new EmitMockComponentMessageEngine(entity)); worldBuilder.AddEngine(new AddMockComponentEngine()); worldBuilder.AddEngine(new HasMockComponentEngine(entity)); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } Assert.IsTrue(hasComponentResult); } [Receives(typeof(EntityMessage))] [Reads(typeof(MockComponent))] class GetMockComponentsEngine : Engine { private Entity entity; private Guid componentAID; private Guid componentBID; private Guid componentCID; private MockComponent componentA; private MockComponent componentB; private MockComponent componentC; public GetMockComponentsEngine( Entity entity, Guid componentAID, MockComponent componentA, Guid componentBID, MockComponent componentB, Guid componentCID, MockComponent componentC ) { this.entity = entity; this.componentAID = componentAID; this.componentA = componentA; this.componentBID = componentBID; this.componentB = componentB; this.componentCID = componentCID; this.componentC = componentC; } public override void Update(double dt) { foreach (var entityMessage in ReadMessages()) { var results = GetComponents(entityMessage.entity); results.Should().Contain((componentAID, componentA)); results.Should().Contain((componentBID, componentB)); results.Should().Contain((componentCID, componentC)); } } } [TestCase(true)] [TestCase(false)] public void GetComponents(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); var entity = worldBuilder.CreateEntity(); MockComponent mockComponentA; mockComponentA.myInt = 3; mockComponentA.myString = "hello"; MockComponent mockComponentB; mockComponentB.myInt = 5; mockComponentB.myString = "wassup"; MockComponent mockComponentC; mockComponentC.myInt = 1; mockComponentC.myString = "howdy"; var componentAID = worldBuilder.AddComponent(entity, mockComponentA); var componentBID = worldBuilder.AddComponent(entity, mockComponentB); var componentCID = worldBuilder.AddComponent(entity, mockComponentC); worldBuilder.AddEngine(new GetMockComponentsEngine( entity, componentAID, mockComponentA, componentBID, mockComponentB, componentCID, mockComponentC )); EntityMessage entityMessage; entityMessage.entity = entity; worldBuilder.SendMessage(entityMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } [TestCase(true)] [TestCase(false)] public void GetComponent(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new GetMockComponentEngine()); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); EntityMessage entityMessage; entityMessage.entity = entity; worldBuilder.SendMessage(entityMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } Assert.AreEqual((componentID, mockComponent), gottenMockComponentIDPair); } struct HasComponentTestMessage : IMessage { public Entity entity; } [Receives(typeof(HasComponentTestMessage))] [Reads(typeof(MockComponent))] class HasComponentTestEngine : Engine { public override void Update(double dt) { foreach (var hasComponentTestEngine in ReadMessages()) { Assert.IsTrue(HasComponent(hasComponentTestEngine.entity)); } } } [TestCase(true)] [TestCase(false)] public void HasComponent(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new HasComponentTestEngine()); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; worldBuilder.AddComponent(entity, mockComponent); HasComponentTestMessage hasComponentTestMessage; hasComponentTestMessage.entity = entity; worldBuilder.SendMessage(hasComponentTestMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } struct HasComponentWhenInactiveTestMessage : IMessage { public Entity entity; } [Receives(typeof(HasComponentWhenInactiveTestMessage))] [Reads(typeof(MockComponent))] class HasComponentWhenInactiveTestEngine : Engine { public override void Update(double dt) { foreach (var hasComponentTestEngine in ReadMessages()) { Assert.IsFalse(HasComponent(hasComponentTestEngine.entity)); } } } [TestCase(true)] [TestCase(false)] public void HasComponentWhenInactive(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new HasComponentWhenInactiveTestEngine()); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); HasComponentWhenInactiveTestMessage testMessage; testMessage.entity = entity; worldBuilder.SendMessage(testMessage); worldBuilder.DeactivateComponent(componentID); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } struct RemoveComponentTestMessage : IMessage { public Entity entity; public Guid componentID; } [Receives(typeof(RemoveComponentTestMessage))] class RemoveComponentTestEngine : Engine { public override void Update(double dt) { foreach (var removeComponentMessage in ReadMessages()) { RemoveComponent(removeComponentMessage.componentID); } } } [Receives(typeof(RemoveComponentTestMessage))] [Sends(typeof(CheckHasMockComponentMessage))] class DoRemoveCheckEngine : Engine { private Entity entity; public DoRemoveCheckEngine(Entity entity) { this.entity = entity; } public override void Update(double dt) { if (SomeMessage()) { CheckHasMockComponentMessage checkHasMockComponentMessage; checkHasMockComponentMessage.entity = entity; checkHasMockComponentMessage.shouldHaveComponent = true; SendMessage(checkHasMockComponentMessage); } else { CheckHasMockComponentMessage checkHasMockComponentMessage; checkHasMockComponentMessage.entity = entity; checkHasMockComponentMessage.shouldHaveComponent = false; SendMessage(checkHasMockComponentMessage); } } } [Receives(typeof(CheckHasMockComponentMessage))] [Reads(typeof(MockComponent))] class CheckHasMockComponentEngine : Engine { public override void Update(double dt) { foreach (var checkHasMockComponentMessage in ReadMessages()) { Assert.IsTrue(HasComponent(checkHasMockComponentMessage.entity)); } } } [TestCase(true)] [TestCase(false)] public void RemoveComponent(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); var entity = worldBuilder.CreateEntity(); worldBuilder.AddEngine(new RemoveComponentTestEngine()); worldBuilder.AddEngine(new CheckHasMockComponentEngine()); worldBuilder.AddEngine(new DoRemoveCheckEngine(entity)); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); RemoveComponentTestMessage removeComponentMessage; removeComponentMessage.entity = entity; removeComponentMessage.componentID = componentID; worldBuilder.SendMessage(removeComponentMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } struct ActivateComponentMessage : IMessage { public Entity entity; public Guid componentID; } [Activates(typeof(MockComponent))] [Receives(typeof(ActivateComponentMessage))] class ActivateComponentEngine : Engine { public override void Update(double dt) { foreach (var activateComponentMessage in ReadMessages()) { ActivateComponent(activateComponentMessage.componentID); } } } struct CheckHasMockComponentMessage : IMessage { public Entity entity; public bool shouldHaveComponent; } [Receives(typeof(ActivateComponentMessage))] [Sends(typeof(CheckHasMockComponentMessage))] class DoActivateCheckEngine : Engine { private Entity entity; public DoActivateCheckEngine(Entity entity) { this.entity = entity; } public override void Update(double dt) { if (SomeMessage()) { CheckHasMockComponentMessage checkHasMockComponentMessage; checkHasMockComponentMessage.entity = entity; checkHasMockComponentMessage.shouldHaveComponent = true; SendMessage(checkHasMockComponentMessage); } } } [Receives(typeof(CheckHasMockComponentMessage))] [ReadsPending(typeof(MockComponent))] class CheckHasPendingMockComponentEngine : Engine { public override void Update(double dt) { foreach (var checkHasMockComponentMessage in ReadMessages()) { Assert.IsTrue(HasComponent(checkHasMockComponentMessage.entity)); } } } [TestCase(true)] [TestCase(false)] public void ActivateComponent(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); var entity = worldBuilder.CreateEntity(); worldBuilder.AddEngine(new ActivateComponentEngine()); worldBuilder.AddEngine(new CheckHasPendingMockComponentEngine()); worldBuilder.AddEngine(new DoActivateCheckEngine(entity)); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); worldBuilder.DeactivateComponent(componentID); ActivateComponentMessage activateMessage; activateMessage.entity = entity; activateMessage.componentID = componentID; worldBuilder.SendMessage(activateMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } struct DeactivateComponentMessage : IMessage { public Entity entity; public Guid componentID; } [Receives(typeof(DeactivateComponentMessage))] class DeactivateComponentEngine : Engine { public override void Update(double dt) { foreach (var deactivateComponentMessage in ReadMessages()) { DeactivateComponent(deactivateComponentMessage.componentID); } } } [Receives(typeof(DeactivateComponentMessage))] [Sends(typeof(CheckHasMockComponentMessage))] class DoDeactivateCheckEngine : Engine { private Entity entity; public DoDeactivateCheckEngine(Entity entity) { this.entity = entity; } public override void Update(double dt) { if (SomeMessage()) { CheckHasMockComponentMessage checkHasMockComponentMessage; checkHasMockComponentMessage.entity = entity; checkHasMockComponentMessage.shouldHaveComponent = true; SendMessage(checkHasMockComponentMessage); } else { CheckHasMockComponentMessage checkHasMockComponentMessage; checkHasMockComponentMessage.entity = entity; checkHasMockComponentMessage.shouldHaveComponent = false; SendMessage(checkHasMockComponentMessage); } } } [TestCase(true)] [TestCase(false)] public void DeactivateComponent(bool parallelUpdate) { var worldBuilder = new WorldBuilder(); var entity = worldBuilder.CreateEntity(); worldBuilder.AddEngine(new DeactivateComponentEngine()); worldBuilder.AddEngine(new CheckHasMockComponentEngine()); worldBuilder.AddEngine(new DoDeactivateCheckEngine(entity)); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); DeactivateComponentMessage deactivateComponentMessage; deactivateComponentMessage.entity = entity; deactivateComponentMessage.componentID = componentID; worldBuilder.SendMessage(deactivateComponentMessage); var world = worldBuilder.Build(); if (parallelUpdate) { world.ParallelUpdate(0.01); } else { world.Update(0.01); } } } }