summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests')
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectBuilderTests.cs70
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectTests.cs88
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/BitArrayExtensionsTests.cs20
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentManagerTests.cs47
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentMapperTests.cs95
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentTypeTests.cs20
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/MonoGame.Extended.Entities.Tests.csproj7
-rw-r--r--Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/WorldManagerTests.cs71
8 files changed, 418 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectBuilderTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectBuilderTests.cs
new file mode 100644
index 0000000..1b3e971
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectBuilderTests.cs
@@ -0,0 +1,70 @@
+using System;
+using Microsoft.Xna.Framework.Graphics;
+using MonoGame.Extended.Sprites;
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests
+{
+ public class AspectBuilderTests
+ {
+ [Fact]
+ public void MatchAllTypes()
+ {
+ var builder = new AspectBuilder()
+ .All(typeof(Transform2), typeof(Sprite));
+
+ Assert.Equal(2, builder.AllTypes.Count);
+ Assert.Contains(typeof(Transform2), builder.AllTypes);
+ Assert.Contains(typeof(Sprite), builder.AllTypes);
+ }
+
+ [Fact]
+ public void MatchAllTypesIsEmpty()
+ {
+ var builder = new AspectBuilder()
+ .All();
+
+ Assert.Empty(builder.AllTypes);
+ Assert.Empty(builder.OneTypes);
+ Assert.Empty(builder.ExclusionTypes);
+ }
+
+ [Fact]
+ public void MatchOneOfType()
+ {
+ var builder = new AspectBuilder()
+ .One(typeof(Transform2), typeof(Sprite));
+
+ Assert.Equal(2, builder.OneTypes.Count);
+ Assert.Contains(typeof(Transform2), builder.OneTypes);
+ Assert.Contains(typeof(Sprite), builder.OneTypes);
+ }
+
+ [Fact]
+ public void ExcludeTypes()
+ {
+ var builder = new AspectBuilder()
+ .Exclude(typeof(Transform2), typeof(Sprite));
+
+ Assert.Equal(2, builder.ExclusionTypes.Count);
+ Assert.Contains(typeof(Transform2), builder.ExclusionTypes);
+ Assert.Contains(typeof(Sprite), builder.ExclusionTypes);
+ }
+
+ [Fact]
+ public void BuildAspect()
+ {
+ var componentManager = new ComponentManager();
+ var builder = new AspectBuilder()
+ .All(typeof(Transform2), typeof(Sprite))
+ .One(typeof(string))
+ .Exclude(typeof(Texture2D));
+
+ var aspect = builder.Build(componentManager);
+
+ Assert.True(aspect.AllSet.Data != 0);
+ Assert.True(aspect.OneSet.Data != 0);
+ Assert.True(aspect.ExclusionSet.Data != 0);
+ }
+ }
+} \ No newline at end of file
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectTests.cs
new file mode 100644
index 0000000..b06780b
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/AspectTests.cs
@@ -0,0 +1,88 @@
+using System.Collections.Specialized;
+using MonoGame.Extended.Sprites;
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests
+{
+ public class DummyComponent
+ {
+ }
+
+ public class AspectTests
+ {
+ private readonly ComponentManager _componentManager;
+ private readonly BitVector32 _entityA;
+ private readonly BitVector32 _entityB;
+
+ public AspectTests()
+ {
+ _componentManager = new ComponentManager();
+ _entityA = new BitVector32
+ {
+ [1 << _componentManager.GetComponentTypeId(typeof(Transform2))] = true,
+ [1 << _componentManager.GetComponentTypeId(typeof(Sprite))] = true,
+ [1 << _componentManager.GetComponentTypeId(typeof(DummyComponent))] = true
+ };
+ _entityB = new BitVector32
+ {
+ [1 << _componentManager.GetComponentTypeId(typeof(Transform2))] = true,
+ [1 << _componentManager.GetComponentTypeId(typeof(Sprite))] = true,
+ };
+ }
+
+ [Fact]
+ public void EmptyAspectMatchesAllComponents()
+ {
+ var componentManager = new ComponentManager();
+ var emptyAspect = Aspect.All()
+ .Build(componentManager);
+
+ Assert.True(emptyAspect.IsInterested(_entityA));
+ Assert.True(emptyAspect.IsInterested(_entityB));
+ }
+
+ [Fact]
+ public void IsInterestedInAllComponents()
+ {
+ var allAspect = Aspect
+ .All(typeof(Sprite), typeof(Transform2), typeof(DummyComponent))
+ .Build(_componentManager);
+
+ Assert.True(allAspect.IsInterested(_entityA));
+ Assert.False(allAspect.IsInterested(_entityB));
+ }
+
+ [Fact]
+ public void IsInterestedInEitherOneOfTheComponents()
+ {
+ var eitherOneAspect = Aspect
+ .One(typeof(Transform2), typeof(DummyComponent))
+ .Build(_componentManager);
+
+ Assert.True(eitherOneAspect.IsInterested(_entityA));
+ Assert.True(eitherOneAspect.IsInterested(_entityB));
+ }
+
+ [Fact]
+ public void IsInterestedInJustOneComponent()
+ {
+ var oneAspect = Aspect
+ .One(typeof(DummyComponent))
+ .Build(_componentManager);
+
+ Assert.True(oneAspect.IsInterested(_entityA));
+ Assert.False(oneAspect.IsInterested(_entityB));
+ }
+
+ [Fact]
+ public void IsInterestedInExcludingOneComponent()
+ {
+ var oneAspect = Aspect
+ .Exclude(typeof(DummyComponent))
+ .Build(_componentManager);
+
+ Assert.False(oneAspect.IsInterested(_entityA));
+ Assert.True(oneAspect.IsInterested(_entityB));
+ }
+ }
+} \ No newline at end of file
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/BitArrayExtensionsTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/BitArrayExtensionsTests.cs
new file mode 100644
index 0000000..44ec4af
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/BitArrayExtensionsTests.cs
@@ -0,0 +1,20 @@
+using System.Collections;
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests
+{
+ public class BitArrayExtensionsTests
+ {
+ [Fact]
+ public void BitArrayIsEmpty()
+ {
+ Assert.True(new BitArray(1).IsEmpty());
+ Assert.False(new BitArray(new[] { true }).IsEmpty());
+ Assert.True(new BitArray(new[] { false }).IsEmpty());
+
+ var bitArray = new BitArray(new[] { true });
+ bitArray.Set(0, false);
+ Assert.True(bitArray.IsEmpty());
+ }
+ }
+} \ No newline at end of file
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentManagerTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentManagerTests.cs
new file mode 100644
index 0000000..8d988e4
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentManagerTests.cs
@@ -0,0 +1,47 @@
+using MonoGame.Extended.Sprites;
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests
+{
+ public class ComponentManagerTests
+ {
+ [Fact]
+ public void GetMapperForType()
+ {
+ var componentManager = new ComponentManager();
+ var transformMapper = componentManager.GetMapper<Transform2>();
+ var spriteMapper = componentManager.GetMapper<Sprite>();
+
+ Assert.IsType<ComponentMapper<Transform2>>(transformMapper);
+ Assert.IsType<ComponentMapper<Sprite>>(spriteMapper);
+ Assert.Equal(0, transformMapper.Id);
+ Assert.Equal(1, spriteMapper.Id);
+ Assert.Same(spriteMapper, componentManager.GetMapper<Sprite>());
+ }
+
+ [Fact]
+ public void GetComponentTypeId()
+ {
+ var componentManager = new ComponentManager();
+
+ Assert.Equal(0, componentManager.GetComponentTypeId(typeof(Transform2)));
+ Assert.Equal(1, componentManager.GetComponentTypeId(typeof(Sprite)));
+ Assert.Equal(0, componentManager.GetComponentTypeId(typeof(Transform2)));
+ }
+
+ //[Fact]
+ //public void GetCompositionIdentity()
+ //{
+ // var compositionBits = new BitArray(3)
+ // {
+ // [0] = true,
+ // [1] = false,
+ // [2] = true
+ // };
+ // var componentManager = new ComponentManager();
+ // var identity = componentManager.GetCompositionIdentity(compositionBits);
+
+ // Assert.Equal(0b101, identity);
+ //}
+ }
+} \ No newline at end of file
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentMapperTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentMapperTests.cs
new file mode 100644
index 0000000..54fab04
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentMapperTests.cs
@@ -0,0 +1,95 @@
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests
+{
+ public class ComponentMapperTests
+ {
+ [Fact]
+ public void CreateComponentMapper()
+ {
+ var mapper = new ComponentMapper<object>(0, _ => {});
+
+ Assert.Equal(typeof(object), mapper.ComponentType);
+ Assert.Empty(mapper.Components);
+ }
+
+ [Fact]
+ public void OnPut()
+ {
+ const int entityId = 3;
+
+ var mapper = new ComponentMapper<Transform2>(1, _ => { });
+ var component = new Transform2();
+
+ mapper.OnPut += (entId) =>
+ {
+ Assert.Equal(entityId, entId);
+ Assert.Same(component, mapper.Get(entityId));
+ };
+
+ mapper.Put(entityId, component);
+ }
+
+ [Fact]
+ public void PutAndGetComponent()
+ {
+ const int entityId = 3;
+
+ var mapper = new ComponentMapper<Transform2>(1, _ => { });
+ var component = new Transform2();
+
+ mapper.Put(entityId, component);
+
+ Assert.Equal(typeof(Transform2), mapper.ComponentType);
+ Assert.True(mapper.Components.Count >= 1);
+ Assert.Same(component, mapper.Get(entityId));
+ }
+
+ [Fact]
+ public void OnDelete()
+ {
+ const int entityId = 1;
+
+ var mapper = new ComponentMapper<Transform2>(2, _ => { });
+ var component = new Transform2();
+
+ mapper.OnDelete += (entId) =>
+ {
+ Assert.Equal(entityId, entId);
+ Assert.False(mapper.Has(entityId));
+ };
+
+ mapper.Put(entityId, component);
+ mapper.Delete(entityId);
+ }
+
+ [Fact]
+ public void DeleteComponent()
+ {
+ const int entityId = 1;
+
+ var mapper = new ComponentMapper<Transform2>(2, _ => { });
+ var component = new Transform2();
+
+ mapper.Put(entityId, component);
+ mapper.Delete(entityId);
+
+ Assert.False(mapper.Has(entityId));
+ }
+
+ [Fact]
+ public void HasComponent()
+ {
+ const int entityId = 0;
+
+ var mapper = new ComponentMapper<Transform2>(3, _ => { });
+ var component = new Transform2();
+
+ Assert.False(mapper.Has(entityId));
+
+ mapper.Put(entityId, component);
+
+ Assert.True(mapper.Has(entityId));
+ }
+ }
+}
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentTypeTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentTypeTests.cs
new file mode 100644
index 0000000..8953804
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentTypeTests.cs
@@ -0,0 +1,20 @@
+using MonoGame.Extended.Sprites;
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests
+{
+ //public class ComponentTypeTests
+ //{
+ // [Fact]
+ // public void CreateComponentType()
+ // {
+ // var type = typeof(Sprite);
+ // var componentType = new ComponentType(type, 3);
+
+ // Assert.Same(type, componentType.Type);
+ // Assert.Equal(3, componentType.Id);
+ // Assert.Equal(new ComponentType(typeof(Sprite), 3), componentType);
+ // Assert.Equal(componentType.Id, componentType.GetHashCode());
+ // }
+ //}
+} \ No newline at end of file
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/MonoGame.Extended.Entities.Tests.csproj b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/MonoGame.Extended.Entities.Tests.csproj
new file mode 100644
index 0000000..9eac8c4
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/MonoGame.Extended.Entities.Tests.csproj
@@ -0,0 +1,7 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <ItemGroup>
+ <ProjectReference Include="..\..\source\MonoGame.Extended.Entities\MonoGame.Extended.Entities.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/WorldManagerTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/WorldManagerTests.cs
new file mode 100644
index 0000000..7620f1e
--- /dev/null
+++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/WorldManagerTests.cs
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Entities.Systems;
+using MonoGame.Extended.Sprites;
+using Xunit;
+
+namespace MonoGame.Extended.Entities.Tests;
+
+public class WorldManagerTests
+{
+ private GameTime _gameTime = new GameTime(TimeSpan.Zero, TimeSpan.FromMilliseconds(16));
+
+ [Fact]
+ public void CrudEntity()
+ {
+ var dummySystem = new DummySystem();
+ var worldBuilder = new WorldBuilder();
+ worldBuilder.AddSystem(dummySystem);
+ var world = worldBuilder.Build();
+
+ world.Initialize();
+
+ var entity = world.CreateEntity();
+ entity.Attach(new Transform2());
+ world.Update(_gameTime);
+ world.Draw(_gameTime);
+ var otherEntity = world.GetEntity(entity.Id);
+ Assert.Equal(entity, otherEntity);
+ Assert.True(otherEntity.Has<Transform2>());
+ Assert.Contains(entity.Id, dummySystem.AddedEntitiesId);
+
+ entity.Destroy();
+ world.Update(_gameTime);
+ world.Draw(_gameTime);
+ otherEntity = world.GetEntity(entity.Id);
+ Assert.Null(otherEntity);
+ Assert.Contains(entity.Id, dummySystem.RemovedEntitiesId);
+ }
+
+ private class DummyComponent { }
+
+ private class DummySystem : EntitySystem, IUpdateSystem, IDrawSystem
+ {
+ public List<int> AddedEntitiesId { get; } = new ();
+ public List<int> RemovedEntitiesId { get; } = new ();
+
+ public DummySystem() : base(Aspect.All(typeof(DummyComponent))) { }
+
+ public override void Initialize(IComponentMapperService mapperService)
+ {
+ // Do NOT initialize mapper in order to test: https://github.com/craftworkgames/MonoGame.Extended/issues/707
+ }
+
+ public void Draw(GameTime gameTime) { }
+
+ public void Update(GameTime gameTime) { }
+
+ protected override void OnEntityAdded(int entityId)
+ {
+ base.OnEntityAdded(entityId);
+ AddedEntitiesId.Add(entityId);
+ }
+
+ protected override void OnEntityRemoved(int entityId)
+ {
+ base.OnEntityRemoved(entityId);
+ RemovedEntitiesId.Add(entityId);
+ }
+ }
+}