2019-06-15 00:51:06 +00:00
|
|
|
using NUnit.Framework;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using Encompass;
|
|
|
|
using System.Collections.Generic;
|
2019-06-15 01:13:24 +00:00
|
|
|
using System;
|
2019-06-15 00:51:06 +00:00
|
|
|
|
|
|
|
namespace Tests {
|
|
|
|
public class EngineTest
|
|
|
|
{
|
|
|
|
static IEnumerable<MockComponent> components;
|
2019-06-15 01:13:24 +00:00
|
|
|
static MockComponent component;
|
2019-06-15 00:51:06 +00:00
|
|
|
|
2019-06-15 01:13:24 +00:00
|
|
|
public class ReadComponentsTestEngine : Engine
|
2019-06-15 00:51:06 +00:00
|
|
|
{
|
|
|
|
public override void Update(float dt)
|
|
|
|
{
|
|
|
|
components = this.ReadComponents<MockComponent>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 01:13:24 +00:00
|
|
|
public class ReadComponentTestEngine : Engine
|
|
|
|
{
|
|
|
|
public override void Update(float dt)
|
|
|
|
{
|
|
|
|
component = this.ReadComponent<MockComponent>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:51:06 +00:00
|
|
|
[Test]
|
|
|
|
public void ReadComponents()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
2019-06-15 01:13:24 +00:00
|
|
|
worldBuilder.AddEngine<ReadComponentsTestEngine>();
|
2019-06-15 00:51:06 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2019-06-15 01:13:24 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void ReadComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
worldBuilder.AddEngine<ReadComponentTestEngine>();
|
|
|
|
|
|
|
|
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<ReadComponentTestEngine>();
|
|
|
|
|
|
|
|
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<InvalidOperationException>(() => world.Update(0.01f));
|
|
|
|
}
|
2019-06-15 07:39:08 +00:00
|
|
|
|
|
|
|
[Mutates(typeof(MockComponent))]
|
|
|
|
public class UpdateComponentTestEngine : Engine
|
|
|
|
{
|
|
|
|
public override void Update(float dt)
|
|
|
|
{
|
|
|
|
var originalComponent = this.ReadComponent<MockComponent>();
|
|
|
|
var newComponent = this.ReadComponent<MockComponent>();
|
|
|
|
newComponent.myInt = 420;
|
|
|
|
newComponent.myString = "blaze it";
|
|
|
|
this.UpdateComponent(originalComponent, newComponent);
|
|
|
|
component = this.ReadComponent<MockComponent>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
worldBuilder.AddEngine<UpdateComponentTestEngine>();
|
|
|
|
|
|
|
|
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<MockComponent>();
|
|
|
|
var newComponent = this.ReadComponent<MockComponent>();
|
|
|
|
newComponent.myInt = 420;
|
|
|
|
newComponent.myString = "blaze it";
|
|
|
|
this.UpdateComponent(originalComponent, newComponent);
|
|
|
|
component = this.ReadComponent<MockComponent>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void UpdateUndeclaredComponent()
|
|
|
|
{
|
|
|
|
var worldBuilder = new WorldBuilder();
|
|
|
|
worldBuilder.AddEngine<UndeclaredUpdateComponentTestEngine>();
|
|
|
|
|
|
|
|
var entity = worldBuilder.CreateEntity();
|
|
|
|
|
|
|
|
MockComponent mockComponent;
|
|
|
|
mockComponent.myInt = 0;
|
|
|
|
mockComponent.myString = "hello";
|
|
|
|
|
|
|
|
entity.AddComponent(mockComponent);
|
|
|
|
|
|
|
|
var world = worldBuilder.Build();
|
|
|
|
|
|
|
|
Assert.Throws<IllegalComponentMutationException>(() => world.Update(0.01f));
|
|
|
|
}
|
2019-06-15 00:51:06 +00:00
|
|
|
}
|
|
|
|
}
|