Skip to content

Commit

Permalink
added and changed some tests for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Aug 25, 2019
1 parent e8661ad commit ee72a90
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 22 deletions.
14 changes: 10 additions & 4 deletions source/DefaultEcs.Test/EntitySetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
54 changes: 54 additions & 0 deletions source/DefaultEcs.Test/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>(1);
Entity entity = world.CreateEntity();

entity.Set<bool>();
entity.Set<short>();
entity.Set<ushort>();
entity.Set<long>();
entity.Set<ulong>();
entity.Set<int>();
entity.Set<uint>();
entity.Set<double>();
entity.Set<float>();
entity.Set<decimal>();
entity.Set<object>();
entity.Set<string>();
entity.Set<char>();
entity.Set<byte>();
entity.Set<World>();
entity.Set<Entity>();
entity.Set<Delegate>();
entity.Set<Action>();
entity.Set<Action<bool>>();
entity.Set<Action<short>>();
entity.Set<Action<ushort>>();
entity.Set<Action<long>>();
entity.Set<Action<ulong>>();
entity.Set<Action<int>>();
entity.Set<Action<uint>>();
entity.Set<Action<double>>();
entity.Set<Action<float>>();
entity.Set<Action<decimal>>();
entity.Set<Action<object>>();
entity.Set<Action<string>>();
entity.Set<Action<char>>();
entity.Set<Action<byte>>();
}
}

[Fact]
public void Set_Should_only_produce_one_component_for_flag_type()
{
Expand Down Expand Up @@ -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<bool>();
}
}

[Fact]
public void Remove_Should_remove_component()
{
Expand Down
20 changes: 14 additions & 6 deletions source/DefaultEcs.Test/Resource/AResourceManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected override void OnResourceLoaded(in Entity entity, string info, IDisposa
entity.Set(0);
}

entity.Set(info);
++entity.Get<int>();
}
}
Expand All @@ -35,16 +36,23 @@ protected override void OnResourceLoaded(in Entity entity, string info, IDisposa
[Fact]
public void Should_load_resource()
{
IDisposable value = Substitute.For<IDisposable>();

using (ResourceManagerTest manager = new ResourceManagerTest(value))
using (World world = new World(1))
{
manager.Manage(world);
Entity entity = world.CreateEntity();
entity.Set(new ManagedResource<string, IDisposable>("dummy"));

Check.That(entity.Get<int>()).IsEqualTo(1);
using (ResourceManagerTest manager = new ResourceManagerTest(null))
{
manager.Manage(world);

Check.That(entity.Get<string>()).IsEqualTo("dummy");
Check.That(entity.Get<int>()).IsEqualTo(1);

entity.Set(new ManagedResource<string, IDisposable>("dummy2"));

Check.That(entity.Get<string>()).IsEqualTo("dummy2");
Check.That(entity.Get<int>()).IsEqualTo(2);
}
}
}

Expand All @@ -53,8 +61,8 @@ public void Should_load_multiple_resource()
{
IDisposable value = Substitute.For<IDisposable>();

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();
Expand Down
9 changes: 8 additions & 1 deletion source/DefaultEcs.Test/Serialization/ISerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public sealed class ISerializerTest
{
#region Types

private struct Int32
{ }

private struct Test
{
#pragma warning disable IDE0052 // Remove unread private members
Expand Down Expand Up @@ -122,6 +125,7 @@ public void Serialize_Should_serialize_World(Type serializerType)
world.CreateEntity(),
world.CreateEntity()
};
entities[0].Set<Int32>();
entities[0].Set<bool>(true);
entities[0].Set<sbyte>(13);
entities[0].Set<byte>(7);
Expand Down Expand Up @@ -176,6 +180,7 @@ public void Serialize_Should_serialize_World(Type serializerType)

Entity[] entitiesCopy = copyWorld.GetAllEntities().ToArray();

Check.That(entitiesCopy[0].Has<Int32>());
Check.That(entitiesCopy[0].Get<bool>()).IsEqualTo(entities[0].Get<bool>());
Check.That(entitiesCopy[0].Get<sbyte>()).IsEqualTo(entities[0].Get<sbyte>());
Check.That(entitiesCopy[0].Get<byte>()).IsEqualTo(entities[0].Get<byte>());
Expand Down Expand Up @@ -238,6 +243,7 @@ public void Serialize_Should_serialize_Entities(Type serializerType)
world.CreateEntity(),
world.CreateEntity()
};
entities[0].Set<Int32>();
entities[0].Set<bool>(true);
entities[0].Set<sbyte>(13);
entities[0].Set<byte>(7);
Expand Down Expand Up @@ -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))
Expand All @@ -289,6 +295,7 @@ public void Serialize_Should_serialize_Entities(Type serializerType)
entitiesCopy = serializer.Deserialize(stream, copyWorld).ToArray();
}

Check.That(entitiesCopy[0].Has<Int32>());
Check.That(entitiesCopy[0].Get<bool>()).IsEqualTo(entities[0].Get<bool>());
Check.That(entitiesCopy[0].Get<sbyte>()).IsEqualTo(entities[0].Get<sbyte>());
Check.That(entitiesCopy[0].Get<byte>()).IsEqualTo(entities[0].Get<byte>());
Expand Down
2 changes: 2 additions & 0 deletions source/DefaultEcs.Test/System/AComponentSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public System(World world, SystemRunner<int> runner)

protected override void Update(int state, ref bool component)
{
base.Update(state, ref component);

component = true;
}
}
Expand Down
27 changes: 25 additions & 2 deletions source/DefaultEcs.Test/System/AEntitySystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public System(World world, SystemRunner<int> runner)

protected override void Update(int state, in Entity entity)
{
base.Update(state, entity);

entity.Get<bool>() = true;
}
}
Expand Down Expand Up @@ -184,12 +186,22 @@ public void Should_call_EntityAdded_When_entity_added()
using (System system = new System(world.GetEntities().With<bool>().Build()))
{
Entity addedEntity = default;
system.EntityAdded += (in Entity e) => addedEntity = e;

ActionIn<Entity> callback = (in Entity e) => addedEntity = e;

system.EntityAdded += callback;

Entity entity = world.CreateEntity();
entity.Set<bool>();

Check.That(addedEntity).IsEqualTo(entity);

system.EntityAdded -= callback;
addedEntity = default;
entity.Remove<bool>();
entity.Set<bool>();

Check.That(addedEntity).IsEqualTo(default(Entity));
}
}

Expand All @@ -204,6 +216,7 @@ public void Should_call_EntityAdded_When_entity_already_present()
using (System system = new System(world.GetEntities().With<bool>().Build()))
{
Entity addedEntity = default;

system.EntityAdded += (in Entity e) => addedEntity = e;

Check.That(addedEntity).IsEqualTo(entity);
Expand All @@ -218,13 +231,23 @@ public void Should_call_EntityRemoved_When_entity_removed()
using (System system = new System(world.GetEntities().With<bool>().Build()))
{
Entity removedEntity = default;
system.EntityRemoved += (in Entity e) => removedEntity = e;

ActionIn<Entity> callback = (in Entity e) => removedEntity = e;

system.EntityRemoved += callback;

Entity entity = world.CreateEntity();
entity.Set<bool>();
entity.Remove<bool>();

Check.That(removedEntity).IsEqualTo(entity);

system.EntityRemoved -= callback;
removedEntity = default;
entity.Set<bool>();
entity.Remove<bool>();

Check.That(removedEntity).IsEqualTo(default(Entity));
}
}

Expand Down
6 changes: 6 additions & 0 deletions source/DefaultEcs.Test/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>();
}

[Fact]
public void Subscribe_Should_subscribe()
{
Expand Down
7 changes: 0 additions & 7 deletions source/DefaultEcs/System/ASystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ protected ASystem(SystemRunner<T> runner)
IsEnabled = true;
}

/// <summary>
/// Initialise a new instance of the <see cref="ASystem{T}"/> class.
/// </summary>
protected ASystem()
: this(null)
{ }

#endregion

#region Methods
Expand Down
2 changes: 1 addition & 1 deletion source/DefaultEcs/Technical/ComponentEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion source/DefaultEcs/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ee72a90

Please sign in to comment.