From ee72a908336b036561930edf2b8fd75a7abbfec9 Mon Sep 17 00:00:00 2001 From: Laszlo Paillat Date: Sun, 25 Aug 2019 13:35:48 +0200 Subject: [PATCH] added and changed some tests for better coverage --- source/DefaultEcs.Test/EntitySetTest.cs | 14 +++-- source/DefaultEcs.Test/EntityTest.cs | 54 +++++++++++++++++++ .../Resource/AResourceManagerTest.cs | 20 ++++--- .../Serialization/ISerializerTest.cs | 9 +++- .../System/AComponentSystemTest.cs | 2 + .../System/AEntitySystemTest.cs | 27 +++++++++- source/DefaultEcs.Test/WorldTest.cs | 6 +++ source/DefaultEcs/System/ASystem.cs | 7 --- source/DefaultEcs/Technical/ComponentEnum.cs | 2 +- source/DefaultEcs/World.cs | 2 +- 10 files changed, 121 insertions(+), 22 deletions(-) diff --git a/source/DefaultEcs.Test/EntitySetTest.cs b/source/DefaultEcs.Test/EntitySetTest.cs index d493fdc1..8bb3636d 100644 --- a/source/DefaultEcs.Test/EntitySetTest.cs +++ b/source/DefaultEcs.Test/EntitySetTest.cs @@ -41,12 +41,18 @@ public void GetEntities_Should_not_return_disabled_Entity() world.CreateEntity(), world.CreateEntity() }; - - entities[0].Disable(); - + using (EntitySet set = world.GetEntities().Build()) { - Check.That(set.GetEntities().ToArray()).ContainsExactly(entities.Skip(1)); + Check.That(set.GetEntities().ToArray()).ContainsExactly(entities); + + entities[3].Disable(); + + Check.That(set.GetEntities().ToArray()).ContainsExactly(entities.Take(3)); + + entities[3].Enable(); + + Check.That(set.GetEntities().ToArray()).ContainsExactly(entities); } } } diff --git a/source/DefaultEcs.Test/EntityTest.cs b/source/DefaultEcs.Test/EntityTest.cs index b2b809d3..065ee254 100644 --- a/source/DefaultEcs.Test/EntityTest.cs +++ b/source/DefaultEcs.Test/EntityTest.cs @@ -208,6 +208,49 @@ public void Set_Should_set_component_value() } } + [Fact] + public void Set_Should_not_thow_When_more_than_32_components() + { + using (World world = new World(1)) + { + world.SetMaximumComponentCount(1); + Entity entity = world.CreateEntity(); + + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + entity.Set>(); + } + } + [Fact] public void Set_Should_only_produce_one_component_for_flag_type() { @@ -309,6 +352,17 @@ public void SetSameAs_Should_set_component_to_reference() } } + [Fact] + public void Remove_Should_not_throw_When_no_component() + { + using (World world = new World(1)) + { + Entity entity = world.CreateEntity(); + + entity.Remove(); + } + } + [Fact] public void Remove_Should_remove_component() { diff --git a/source/DefaultEcs.Test/Resource/AResourceManagerTest.cs b/source/DefaultEcs.Test/Resource/AResourceManagerTest.cs index 0432413e..f580580a 100644 --- a/source/DefaultEcs.Test/Resource/AResourceManagerTest.cs +++ b/source/DefaultEcs.Test/Resource/AResourceManagerTest.cs @@ -26,6 +26,7 @@ protected override void OnResourceLoaded(in Entity entity, string info, IDisposa entity.Set(0); } + entity.Set(info); ++entity.Get(); } } @@ -35,16 +36,23 @@ protected override void OnResourceLoaded(in Entity entity, string info, IDisposa [Fact] public void Should_load_resource() { - IDisposable value = Substitute.For(); - - using (ResourceManagerTest manager = new ResourceManagerTest(value)) using (World world = new World(1)) { - manager.Manage(world); Entity entity = world.CreateEntity(); entity.Set(new ManagedResource("dummy")); - Check.That(entity.Get()).IsEqualTo(1); + using (ResourceManagerTest manager = new ResourceManagerTest(null)) + { + manager.Manage(world); + + Check.That(entity.Get()).IsEqualTo("dummy"); + Check.That(entity.Get()).IsEqualTo(1); + + entity.Set(new ManagedResource("dummy2")); + + Check.That(entity.Get()).IsEqualTo("dummy2"); + Check.That(entity.Get()).IsEqualTo(2); + } } } @@ -53,8 +61,8 @@ public void Should_load_multiple_resource() { IDisposable value = Substitute.For(); - using (ResourceManagerTest manager = new ResourceManagerTest(value)) using (World world = new World(1)) + using (ResourceManagerTest manager = new ResourceManagerTest(value)) { manager.Manage(world); Entity entity = world.CreateEntity(); diff --git a/source/DefaultEcs.Test/Serialization/ISerializerTest.cs b/source/DefaultEcs.Test/Serialization/ISerializerTest.cs index 717da4eb..c2fd1a03 100644 --- a/source/DefaultEcs.Test/Serialization/ISerializerTest.cs +++ b/source/DefaultEcs.Test/Serialization/ISerializerTest.cs @@ -12,6 +12,9 @@ public sealed class ISerializerTest { #region Types + private struct Int32 + { } + private struct Test { #pragma warning disable IDE0052 // Remove unread private members @@ -122,6 +125,7 @@ public void Serialize_Should_serialize_World(Type serializerType) world.CreateEntity(), world.CreateEntity() }; + entities[0].Set(); entities[0].Set(true); entities[0].Set(13); entities[0].Set(7); @@ -176,6 +180,7 @@ public void Serialize_Should_serialize_World(Type serializerType) Entity[] entitiesCopy = copyWorld.GetAllEntities().ToArray(); + Check.That(entitiesCopy[0].Has()); Check.That(entitiesCopy[0].Get()).IsEqualTo(entities[0].Get()); Check.That(entitiesCopy[0].Get()).IsEqualTo(entities[0].Get()); Check.That(entitiesCopy[0].Get()).IsEqualTo(entities[0].Get()); @@ -238,6 +243,7 @@ public void Serialize_Should_serialize_Entities(Type serializerType) world.CreateEntity(), world.CreateEntity() }; + entities[0].Set(); entities[0].Set(true); entities[0].Set(13); entities[0].Set(7); @@ -277,7 +283,7 @@ public void Serialize_Should_serialize_Entities(Type serializerType) { using (Stream stream = File.Create(filePath)) { - serializer.Serialize(stream, entities); + serializer.Serialize(stream, entities[0], entities[1], entities[2]); } using (World copyWorld = new World(42)) @@ -289,6 +295,7 @@ public void Serialize_Should_serialize_Entities(Type serializerType) entitiesCopy = serializer.Deserialize(stream, copyWorld).ToArray(); } + Check.That(entitiesCopy[0].Has()); Check.That(entitiesCopy[0].Get()).IsEqualTo(entities[0].Get()); Check.That(entitiesCopy[0].Get()).IsEqualTo(entities[0].Get()); Check.That(entitiesCopy[0].Get()).IsEqualTo(entities[0].Get()); diff --git a/source/DefaultEcs.Test/System/AComponentSystemTest.cs b/source/DefaultEcs.Test/System/AComponentSystemTest.cs index 914ff078..24eb9fc7 100644 --- a/source/DefaultEcs.Test/System/AComponentSystemTest.cs +++ b/source/DefaultEcs.Test/System/AComponentSystemTest.cs @@ -19,6 +19,8 @@ public System(World world, SystemRunner runner) protected override void Update(int state, ref bool component) { + base.Update(state, ref component); + component = true; } } diff --git a/source/DefaultEcs.Test/System/AEntitySystemTest.cs b/source/DefaultEcs.Test/System/AEntitySystemTest.cs index ce868c06..13349867 100644 --- a/source/DefaultEcs.Test/System/AEntitySystemTest.cs +++ b/source/DefaultEcs.Test/System/AEntitySystemTest.cs @@ -30,6 +30,8 @@ public System(World world, SystemRunner runner) protected override void Update(int state, in Entity entity) { + base.Update(state, entity); + entity.Get() = true; } } @@ -184,12 +186,22 @@ public void Should_call_EntityAdded_When_entity_added() using (System system = new System(world.GetEntities().With().Build())) { Entity addedEntity = default; - system.EntityAdded += (in Entity e) => addedEntity = e; + + ActionIn callback = (in Entity e) => addedEntity = e; + + system.EntityAdded += callback; Entity entity = world.CreateEntity(); entity.Set(); Check.That(addedEntity).IsEqualTo(entity); + + system.EntityAdded -= callback; + addedEntity = default; + entity.Remove(); + entity.Set(); + + Check.That(addedEntity).IsEqualTo(default(Entity)); } } @@ -204,6 +216,7 @@ public void Should_call_EntityAdded_When_entity_already_present() using (System system = new System(world.GetEntities().With().Build())) { Entity addedEntity = default; + system.EntityAdded += (in Entity e) => addedEntity = e; Check.That(addedEntity).IsEqualTo(entity); @@ -218,13 +231,23 @@ public void Should_call_EntityRemoved_When_entity_removed() using (System system = new System(world.GetEntities().With().Build())) { Entity removedEntity = default; - system.EntityRemoved += (in Entity e) => removedEntity = e; + + ActionIn callback = (in Entity e) => removedEntity = e; + + system.EntityRemoved += callback; Entity entity = world.CreateEntity(); entity.Set(); entity.Remove(); Check.That(removedEntity).IsEqualTo(entity); + + system.EntityRemoved -= callback; + removedEntity = default; + entity.Set(); + entity.Remove(); + + Check.That(removedEntity).IsEqualTo(default(Entity)); } } diff --git a/source/DefaultEcs.Test/WorldTest.cs b/source/DefaultEcs.Test/WorldTest.cs index e69d5837..f49d350d 100644 --- a/source/DefaultEcs.Test/WorldTest.cs +++ b/source/DefaultEcs.Test/WorldTest.cs @@ -13,6 +13,12 @@ private struct FlagType { } #region Tests + [Fact] + public void New_Should_throw_When_maxEntityCount_is_inferior_to_0() + { + Check.ThatCode(() => new World(-1)).Throws(); + } + [Fact] public void Subscribe_Should_subscribe() { diff --git a/source/DefaultEcs/System/ASystem.cs b/source/DefaultEcs/System/ASystem.cs index 7e9fbfcc..80272d84 100644 --- a/source/DefaultEcs/System/ASystem.cs +++ b/source/DefaultEcs/System/ASystem.cs @@ -26,13 +26,6 @@ protected ASystem(SystemRunner runner) IsEnabled = true; } - /// - /// Initialise a new instance of the class. - /// - protected ASystem() - : this(null) - { } - #endregion #region Methods diff --git a/source/DefaultEcs/Technical/ComponentEnum.cs b/source/DefaultEcs/Technical/ComponentEnum.cs index 4f4d3d16..25e48176 100644 --- a/source/DefaultEcs/Technical/ComponentEnum.cs +++ b/source/DefaultEcs/Technical/ComponentEnum.cs @@ -94,7 +94,7 @@ public ComponentEnum Copy() } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Clear() => _bitArray?.Fill(0u); + public void Clear() => _bitArray.Fill(0u); public override unsafe string ToString() { diff --git a/source/DefaultEcs/World.cs b/source/DefaultEcs/World.cs index 997350fb..142974ef 100644 --- a/source/DefaultEcs/World.cs +++ b/source/DefaultEcs/World.cs @@ -105,8 +105,8 @@ public World() [Subscribe] private void On(in EntityDisposingMessage message) { - Info.EntityInfos[message.EntityId].Components.Clear(); EntityDisposed?.Invoke(new Entity(WorldId, message.EntityId)); + Info.EntityInfos[message.EntityId].Components.Clear(); } [Subscribe]