encompass-cs/test/WorldBuilderTest.cs

279 lines
7.9 KiB
C#
Raw Normal View History

2019-06-17 00:56:36 +00:00
using NUnit.Framework;
using Encompass;
using System.Collections.Generic;
using Encompass.Exceptions;
2019-06-17 00:56:36 +00:00
namespace Tests
{
public class WorldBuilderTest
{
public class EngineCycleSimple
{
struct AMessage : IMessage { }
struct BMessage : IMessage { }
[Reads(typeof(AMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(BMessage))]
2019-06-17 00:56:36 +00:00
class AEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
BMessage message;
this.SendMessage(message);
2019-06-17 00:56:36 +00:00
}
}
[Reads(typeof(BMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(AMessage))]
2019-06-17 00:56:36 +00:00
class BEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
AMessage message;
this.SendMessage(message);
2019-06-17 00:56:36 +00:00
}
}
[Test]
public void EngineCycle()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine(new BEngine());
2019-06-17 00:56:36 +00:00
Assert.Throws<EngineCycleException>(() => worldBuilder.Build());
}
}
public class EngineCycleComplex
{
struct AMessage : IMessage { }
struct BMessage : IMessage { }
struct CMessage : IMessage { }
struct DMessage : IMessage { }
[Reads(typeof(AMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(BMessage))]
2019-06-17 00:56:36 +00:00
class AEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
BMessage message;
this.SendMessage(message);
2019-06-17 00:56:36 +00:00
}
}
[Reads(typeof(BMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(CMessage))]
2019-06-17 00:56:36 +00:00
class BEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
CMessage message;
this.SendMessage(message);
2019-06-17 00:56:36 +00:00
}
}
[Reads(typeof(CMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(DMessage))]
2019-06-17 00:56:36 +00:00
class CEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
DMessage message;
this.SendMessage(message);
2019-06-17 00:56:36 +00:00
}
}
[Reads(typeof(DMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(AMessage))]
2019-06-17 00:56:36 +00:00
class DEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
AMessage message;
this.SendMessage(message);
2019-06-17 00:56:36 +00:00
}
}
[Test]
public void EngineCycle()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine(new BEngine());
worldBuilder.AddEngine(new CEngine());
worldBuilder.AddEngine(new DEngine());
2019-06-17 00:56:36 +00:00
Assert.Throws<EngineCycleException>(() => worldBuilder.Build());
}
}
public class MutationConflict
{
struct AComponent : IComponent { }
2019-07-19 01:20:38 +00:00
[Updates(typeof(AComponent))]
2019-06-17 00:56:36 +00:00
class AEngine : Engine
{
public override void Update(double dt) { }
2019-06-17 00:56:36 +00:00
}
2019-07-19 01:20:38 +00:00
[Updates(typeof(AComponent))]
2019-06-17 00:56:36 +00:00
class BEngine : Engine
{
public override void Update(double dt) { }
2019-06-17 00:56:36 +00:00
}
[Test]
public void MutationConflictException()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AEngine());
worldBuilder.AddEngine(new BEngine());
2019-06-17 00:56:36 +00:00
Assert.Throws<EngineWriteConflictException>(() => worldBuilder.Build());
}
}
public class EngineMessageSelfCycle
{
struct AMessage : IMessage { }
[Reads(typeof(AMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(AMessage))]
class AEngine : Engine
{
public override void Update(double dt)
{
}
}
[Test]
public void ThrowsError()
{
var worldBuilder = new WorldBuilder();
Assert.Throws<EngineMessageSelfCycleException>(() => worldBuilder.AddEngine(new AEngine()), "Engine both reads and writes Message AMessage");
}
}
public class IllegalReadType
{
struct ANonMessage { }
[Reads(typeof(ANonMessage))]
class MyEngine : Engine
{
public override void Update(double dt)
{
}
}
[Test]
public void ThrowsError()
{
var worldBuilder = new WorldBuilder();
Assert.Throws<IllegalReadTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
}
}
public class IllegalWriteType
{
struct ANonMessage { }
2019-07-19 01:20:38 +00:00
[Sends(typeof(ANonMessage))]
class MyEngine : Engine
{
public override void Update(double dt)
{
}
}
[Test]
public void ThrowsError()
{
var worldBuilder = new WorldBuilder();
Assert.Throws<IllegalWriteTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
2019-06-17 00:56:36 +00:00
}
}
public class LegalEngines
{
static List<Engine> order = new List<Engine>();
struct AComponent : IComponent { }
struct BComponent : IComponent { }
struct AMessage : IMessage { }
struct BMessage : IMessage { }
struct CMessage : IMessage { }
struct DMessage : IMessage { }
2019-07-19 01:20:38 +00:00
[Sends(typeof(AMessage))]
2019-06-17 00:56:36 +00:00
class AEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
order.Add(this);
}
}
2019-07-19 01:20:38 +00:00
[Sends(typeof(BMessage))]
2019-06-17 00:56:36 +00:00
class BEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
order.Add(this);
}
}
[Reads(typeof(AMessage), typeof(BMessage))]
2019-07-19 01:20:38 +00:00
[Sends(typeof(DMessage))]
2019-06-17 00:56:36 +00:00
class CEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
order.Add(this);
}
}
[Reads(typeof(DMessage))]
class DEngine : Engine
{
public override void Update(double dt)
2019-06-17 00:56:36 +00:00
{
order.Add(this);
}
}
[Test]
public void EngineOrder()
{
var worldBuilder = new WorldBuilder();
var engineA = worldBuilder.AddEngine(new AEngine());
var engineB = worldBuilder.AddEngine(new BEngine());
var engineC = worldBuilder.AddEngine(new CEngine());
var engineD = worldBuilder.AddEngine(new DEngine());
2019-06-17 00:56:36 +00:00
Assert.DoesNotThrow(() => worldBuilder.Build());
var world = worldBuilder.Build();
world.Update(0.01f);
Assert.That(order.IndexOf(engineA), Is.LessThan(order.IndexOf(engineC)));
Assert.That(order.IndexOf(engineB), Is.LessThan(order.IndexOf(engineC)));
Assert.That(order.IndexOf(engineC), Is.LessThan(order.IndexOf(engineD)));
}
}
}
}