using NUnit.Framework; using System.Linq; using Encompass; using System.Collections.Generic; using System; namespace Tests { public class EngineTest { static IEnumerable components; static MockComponent component; public class ReadComponentsTestEngine : Engine { public override void Update(float dt) { components = this.ReadComponents(); } } public class ReadComponentTestEngine : Engine { public override void Update(float dt) { component = 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, components.ToList()); Assert.Contains(mockComponentB, components.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, component); } [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 originalComponent = this.ReadComponent(); var newComponent = this.ReadComponent(); newComponent.myInt = 420; newComponent.myString = "blaze it"; this.UpdateComponent(originalComponent, newComponent); component = 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, component.myInt); Assert.AreEqual("blaze it", component.myString); } public class UndeclaredUpdateComponentTestEngine : Engine { public override void Update(float dt) { var originalComponent = this.ReadComponent(); var newComponent = this.ReadComponent(); newComponent.myInt = 420; newComponent.myString = "blaze it"; this.UpdateComponent(originalComponent, newComponent); 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(); Assert.Throws(() => world.Update(0.01f)); } } }