using NUnit.Framework; using System.Linq; using Encompass; using System.Collections.Generic; using System; namespace Tests { public class EngineTest { static IEnumerable resultComponents; static MockComponent resultComponent; public class ReadComponentsTestEngine : Engine { public override void Update(float dt) { resultComponents = this.ReadComponents(); } } public class ReadComponentTestEngine : Engine { public override void Update(float dt) { resultComponent = this.ReadComponent(); } } [Test] public void ReadComponents() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 0; mockComponent.myString = "hello"; MockComponent mockComponentB; mockComponentB.myInt = 1; mockComponentB.myString = "howdy"; entity.AddComponent(mockComponent); entity.AddComponent(mockComponentB); var world = worldBuilder.Build(); world.Update(0.01f); Assert.Contains(mockComponent, resultComponents.ToList()); Assert.Contains(mockComponentB, resultComponents.ToList()); } [Test] public void ReadComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 0; mockComponent.myString = "hello"; entity.AddComponent(mockComponent); var world = worldBuilder.Build(); world.Update(0.01f); Assert.AreEqual(mockComponent, resultComponent); } [Test] public void ReadComponentWhenMultipleComponents() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 0; mockComponent.myString = "hello"; MockComponent mockComponentB; mockComponentB.myInt = 1; mockComponentB.myString = "howdy"; entity.AddComponent(mockComponent); entity.AddComponent(mockComponentB); var world = worldBuilder.Build(); Assert.Throws(() => world.Update(0.01f)); } [Mutates(typeof(MockComponent))] public class UpdateComponentTestEngine : Engine { public override void Update(float dt) { var component = this.ReadComponent(); this.UpdateComponent(component, (MockComponent comp) => { comp.myInt = 420; comp.myString = "blaze it"; return comp; }); resultComponent = this.ReadComponent(); } } [Test] public void UpdateComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 0; mockComponent.myString = "hello"; entity.AddComponent(mockComponent); var world = worldBuilder.Build(); world.Update(0.01f); Assert.AreEqual(420, resultComponent.myInt); Assert.AreEqual("blaze it", resultComponent.myString); } public class UndeclaredUpdateComponentTestEngine : Engine { public override void Update(float dt) { var component = this.ReadComponent(); this.UpdateComponent(component, (MockComponent comp) => { comp.myInt = 420; comp.myString = "blaze it"; return comp; }); component = this.ReadComponent(); } } [Test] public void UpdateUndeclaredComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(); var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; mockComponent.myInt = 0; mockComponent.myString = "hello"; entity.AddComponent(mockComponent); var world = worldBuilder.Build(); var ex = Assert.Throws(() => world.Update(0.01f)); Assert.That(ex.Message, Is.EqualTo("Engine UndeclaredUpdateComponentTestEngine tried to mutate undeclared Component MockComponent")); } } }