encompass-cs/test/OrderedRendererTest.cs

102 lines
3.1 KiB
C#

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, IDrawComponent { }
class TestRenderer : OrderedRenderer<TestDrawComponent>
{
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<Guid, TestDrawComponent> resultComponent;
class CalledRenderer : OrderedRenderer<TestDrawComponent>
{
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<TestDrawComponent>())
{
Destroy(GetEntityIDByComponentID<TestDrawComponent>(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);
}
}
}