using System; using NUnit.Framework; using FluentAssertions; using Encompass; using System.Collections.Generic; namespace Tests { public class OrderedRendererTest { struct AComponent : IComponent { } struct BComponent : IComponent { } struct CComponent : IComponent { } struct TestDrawComponent : IComponent { } class TestRenderer : OrderedRenderer { public override void Render(Guid drawComponentID, TestDrawComponent testDrawComponent) { } } static bool called = false; class DeactivatedRenderer : TestRenderer { public override void Render(Guid drawComponentID, TestDrawComponent testDrawComponent) { called = true; } } static bool calledOnDraw = false; static ValueTuple resultComponent; class CalledRenderer : OrderedRenderer { public override void Render(Guid drawComponentID, TestDrawComponent testDrawComponent) { resultComponent = (drawComponentID, testDrawComponent); calledOnDraw = true; } } [Test] public void RenderMethodCalledOnWorldDraw() { var worldBuilder = new WorldBuilder(); var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer()); AComponent aComponent; CComponent cComponent; TestDrawComponent testDrawComponent; var entity = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entity, aComponent); worldBuilder.SetComponent(entity, cComponent); var testDrawComponentID = worldBuilder.SetDrawComponent(entity, testDrawComponent, 2); var world = worldBuilder.Build(); world.Update(0.01f); world.Draw(); Assert.IsTrue(calledOnDraw); resultComponent.Should().BeEquivalentTo((testDrawComponentID, testDrawComponent)); } [Reads(typeof(TestDrawComponent))] class DestroyerEngine : Engine { public override void Update(double dt) { foreach (var (componentID, component) in ReadComponents()) { Destroy(GetEntityIDByComponentID(componentID)); } } } [Test] public void RenderMethodNotCalledAfterDestroy() { calledOnDraw = false; var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new DestroyerEngine()); var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer()); TestDrawComponent testDrawComponent; var entity = worldBuilder.CreateEntity(); var testDrawComponentID = worldBuilder.SetDrawComponent(entity, testDrawComponent, 1); var world = worldBuilder.Build(); world.Update(0.01); world.Draw(); Assert.IsFalse(calledOnDraw); } } }