encompass-cs/test/EntityTest.cs

104 lines
3.1 KiB
C#
Raw Normal View History

2019-06-13 02:51:36 +00:00
using NUnit.Framework;
using FluentAssertions;
2019-06-14 03:41:21 +00:00
using System.Linq;
2019-06-13 02:51:36 +00:00
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-15 00:03:56 +00:00
entity.AddComponent<MockComponent>(mockComponent);
2019-06-13 02:51:36 +00:00
2019-06-15 00:03:56 +00:00
var world = worldBuilder.Build();
// world.Update();
2019-06-15 00:03:56 +00:00
Assert.IsTrue(entity.HasComponent<MockComponent>());
Assert.That(entity.GetComponent<MockComponent>().Value, 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
var componentAID = entity.AddComponent<MockComponent>(mockComponentA);
var componentBID = entity.AddComponent<MockComponent>(mockComponentB);
var componentCID = entity.AddComponent<MockComponent>(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 KeyValuePair<Guid, MockComponent>(componentAID, mockComponentA));
components.Should().Contain(new KeyValuePair<Guid, MockComponent>(componentBID, mockComponentB));
components.Should().Contain(new KeyValuePair<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 KeyValuePair<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
2019-06-15 00:03:56 +00:00
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
2019-06-15 00:03:56 +00:00
Assert.IsTrue(entity.HasComponent<MockComponent>());
2019-06-13 02:51:36 +00:00
}
}
}