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; [Reads(typeof(EntityMessage), typeof(MockComponent))] class GetMockComponentsEngine : Engine { public override void Update(double dt) { gottenMockComponentIDPairs = Enumerable.Empty<(Guid, MockComponent)>(); foreach (var entityMessage in ReadMessages()) { gottenMockComponentIDPairs = GetComponents(entityMessage.entity); } } } [Reads(typeof(EntityMessage), 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; } [Reads(typeof(AddComponentTestMessage), typeof(MockComponent))] class AddComponentEngine : 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)); } } } [Test] public void AddComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new AddComponentEngine()); worldBuilder.AddEngine(new GetMockComponentEngine()); 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.EmitMessage(addComponentTestMessage); var world = worldBuilder.Build(); world.Update(0.01); } [Test] public void GetComponents() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new GetMockComponentsEngine()); 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); EntityMessage entityMessage; entityMessage.entity = entity; worldBuilder.EmitMessage(entityMessage); var world = worldBuilder.Build(); world.Update(0.01); gottenMockComponentIDPairs.Should().Contain((componentAID, mockComponentA)); gottenMockComponentIDPairs.Should().Contain((componentBID, mockComponentB)); gottenMockComponentIDPairs.Should().Contain((componentCID, mockComponentC)); } [Test] public void GetComponent() { 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.EmitMessage(entityMessage); var world = worldBuilder.Build(); world.Update(0.01); Assert.AreEqual((componentID, mockComponent), gottenMockComponentIDPair); } struct HasComponentTestMessage : IMessage { public Entity entity; } [Reads(typeof(HasComponentTestMessage), typeof(MockComponent))] class HasComponentTestEngine : Engine { public override void Update(double dt) { foreach (var hasComponentTestEngine in ReadMessages()) { Assert.IsTrue(HasComponent(hasComponentTestEngine.entity)); } } } [Test] public void HasComponent() { 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.EmitMessage(hasComponentTestMessage); var world = worldBuilder.Build(); world.Update(0.01); } struct HasComponentWhenInactiveTestMessage : IMessage { public Entity entity; } [Reads(typeof(HasComponentWhenInactiveTestMessage), typeof(MockComponent))] class HasComponentWhenInactiveTestEngine : Engine { public override void Update(double dt) { foreach (var hasComponentTestEngine in ReadMessages()) { Assert.IsFalse(HasComponent(hasComponentTestEngine.entity)); } } } [Test] public void HasComponentWhenInactive() { 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.EmitMessage(testMessage); worldBuilder.DeactivateComponent(componentID); var world = worldBuilder.Build(); world.Update(0.01f); } struct RemoveComponentTestMessage : IMessage { public Entity entity; public Guid componentID; } [Reads(typeof(RemoveComponentTestMessage), typeof(MockComponent))] [Writes(typeof(MockComponent))] class RemoveComponentTestEngine : Engine { public override void Update(double dt) { foreach (var removeComponentMessage in ReadMessages()) { RemoveComponent(removeComponentMessage.componentID); Assert.IsFalse(HasComponent(removeComponentMessage.entity)); Assert.IsEmpty(GetComponents(removeComponentMessage.entity)); } } } [Test] public void RemoveComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new RemoveComponentTestEngine()); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); RemoveComponentTestMessage removeComponentMessage; removeComponentMessage.entity = entity; removeComponentMessage.componentID = componentID; worldBuilder.EmitMessage(removeComponentMessage); var world = worldBuilder.Build(); world.Update(0.01f); } struct ActivateComponentMessage : IMessage { public Entity entity; public Guid componentID; } [Reads(typeof(ActivateComponentMessage), typeof(MockComponent))] [Writes(typeof(MockComponent))] class ActivateComponentEngine : Engine { public override void Update(double dt) { foreach (var activateComponentMessage in ReadMessages()) { ActivateComponent(activateComponentMessage.componentID); Assert.IsTrue(HasComponent(activateComponentMessage.entity)); } } } [Test] public void ActivateComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new ActivateComponentEngine()); var entity = worldBuilder.CreateEntity(); 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.EmitMessage(activateMessage); var world = worldBuilder.Build(); world.Update(0.01f); } struct DeactivateComponentMessage : IMessage { public Entity entity; public Guid componentID; } [Reads(typeof(DeactivateComponentMessage), typeof(MockComponent))] [Writes(typeof(MockComponent))] class DeactivateComponentEngine : Engine { public override void Update(double dt) { foreach (var deactivateComponentMessage in ReadMessages()) { DeactivateComponent(deactivateComponentMessage.componentID); Assert.IsFalse(HasComponent(deactivateComponentMessage.entity)); } } } [Test] public void DeactivateComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new DeactivateComponentEngine()); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 3; mockComponent.myString = "hello"; var componentID = worldBuilder.AddComponent(entity, mockComponent); DeactivateComponentMessage deactivateComponentMessage; deactivateComponentMessage.entity = entity; deactivateComponentMessage.componentID = componentID; worldBuilder.EmitMessage(deactivateComponentMessage); var world = worldBuilder.Build(); world.Update(0.01); } } }