encompass-cs/test/EntityTest.cs

173 lines
5.0 KiB
C#
Raw Normal View History

2019-06-13 02:51:36 +00:00
using NUnit.Framework;
using FluentAssertions;
2019-06-15 00:03:56 +00:00
using Encompass;
using System.Collections.Generic;
using System;
2019-06-15 00:03:56 +00:00
namespace Tests
{
struct MockComponent : IComponent
{
2019-06-15 00:03:56 +00:00
public string myString;
public int myInt;
}
2019-06-13 02:51:36 +00:00
2019-06-15 00:03:56 +00:00
public class EntityTest
{
[Test]
public void AddComponent()
2019-06-13 02:51:36 +00:00
{
2019-06-15 00:03:56 +00:00
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
2019-06-15 00:03:56 +00:00
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
2019-06-14 05:15:15 +00:00
2019-06-20 01:10:39 +00:00
entity.AddComponent(mockComponent);
2019-06-13 02:51:36 +00:00
2019-06-15 00:03:56 +00:00
var world = worldBuilder.Build();
2019-06-15 00:03:56 +00:00
Assert.IsTrue(entity.HasComponent<MockComponent>());
Assert.That(entity.GetComponent<MockComponent>().Item2, Is.EqualTo(mockComponent));
2019-06-15 00:03:56 +00:00
}
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
[Test]
public void GetComponents()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
MockComponent mockComponentA;
mockComponentA.myInt = 3;
mockComponentA.myString = "hello";
2019-06-14 05:15:15 +00:00
2019-06-15 00:03:56 +00:00
MockComponent mockComponentB;
mockComponentB.myInt = 5;
mockComponentB.myString = "wassup";
2019-06-14 05:15:15 +00:00
2019-06-15 00:03:56 +00:00
MockComponent mockComponentC;
mockComponentC.myInt = 1;
mockComponentC.myString = "howdy";
2019-06-14 05:15:15 +00:00
2019-06-20 01:10:39 +00:00
var componentAID = entity.AddComponent(mockComponentA);
var componentBID = entity.AddComponent(mockComponentB);
var componentCID = entity.AddComponent(mockComponentC);
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
var world = worldBuilder.Build();
2019-06-14 03:41:21 +00:00
var components = entity.GetComponents<MockComponent>();
components.Should().Contain(new ValueTuple<Guid, MockComponent>(componentAID, mockComponentA));
components.Should().Contain(new ValueTuple<Guid, MockComponent>(componentBID, mockComponentB));
components.Should().Contain(new ValueTuple<Guid, MockComponent>(componentCID, mockComponentC));
2019-06-15 00:03:56 +00:00
}
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
[Test]
public void GetComponent()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
2019-06-14 05:15:15 +00:00
var componentID = entity.AddComponent<MockComponent>(mockComponent);
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
var world = worldBuilder.Build();
2019-06-14 03:41:21 +00:00
Assert.AreEqual(new ValueTuple<Guid, MockComponent>(componentID, mockComponent), entity.GetComponent<MockComponent>());
2019-06-15 00:03:56 +00:00
}
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
[Test]
public void HasComponent()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
2019-06-14 05:15:15 +00:00
entity.AddComponent(mockComponent);
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
var world = worldBuilder.Build();
2019-06-14 03:41:21 +00:00
2019-06-15 00:03:56 +00:00
Assert.IsTrue(entity.HasComponent<MockComponent>());
2019-06-13 02:51:36 +00:00
}
[Test]
public void HasComponentWhenInactive()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
var componentID = entity.AddComponent(mockComponent);
var world = worldBuilder.Build();
entity.DeactivateComponent(componentID);
world.Update(0.01f);
Assert.IsFalse(entity.HasComponent<MockComponent>());
}
2019-06-20 01:10:39 +00:00
[Test]
public void RemoveComponent()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
var componentID = entity.AddComponent(mockComponent);
var world = worldBuilder.Build();
entity.RemoveComponent(componentID);
world.Update(0.01f);
Assert.IsFalse(entity.HasComponent<MockComponent>());
Assert.IsEmpty(entity.GetComponents<MockComponent>());
}
[Test]
public void ReactivateComponent()
{
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 3;
mockComponent.myString = "hello";
var componentID = entity.AddComponent(mockComponent);
var world = worldBuilder.Build();
entity.DeactivateComponent(componentID);
world.Update(0.01f);
Assert.IsFalse(entity.HasComponent<MockComponent>());
Assert.IsEmpty(entity.GetComponents<MockComponent>());
entity.ActivateComponent(componentID);
world.Update(0.01f);
Assert.IsTrue(entity.HasComponent<MockComponent>());
Assert.IsNotEmpty(entity.GetComponents<MockComponent>());
}
2019-06-13 02:51:36 +00:00
}
}