Skip to content

Commit

Permalink
Test_StructuralChange_OldBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Feb 13, 2025
1 parent d24d0c5 commit 2df4aeb
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ECS/Archetype/EntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ All methods calling MoveEntityTo() have a runtime exception upfront before calli
}
*/
internal static StructuralChangeException StructuralChangeWithinQueryLoop() {
return new StructuralChangeException("within a query loop");
return new StructuralChangeException("within query loop. See: https://friflo.gitbook.io/friflo.engine.ecs/documentation/query#structuralchangeexception");
}

/*
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/ECS/Arch/Test_StructuralChangeException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ namespace Tests.ECS.Arch {

public static class Test_StructuralChangeException
{
[Test]
public static void Test_StructuralChangeException_Message()
{
var store = new EntityStore();
store.CreateEntity();
foreach (var entity in store.Entities)
{
var e = Assert.Throws<StructuralChangeException>(() => {
entity.AddTag<TestTag>();
});
Assert.AreEqual("within query loop. See: https://friflo.gitbook.io/friflo.engine.ecs/documentation/query#structuralchangeexception", e!.Message);
}
}

[Test]
public static void Test_StructuralChangeException_Entities()
{
Expand Down
116 changes: 116 additions & 0 deletions src/Tests/ECS/Arch/Test_StructuralChange_OldBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Friflo.Engine.ECS;
using Friflo.Engine.ECS.Serialize;
using NUnit.Framework;


// ReSharper disable ConditionalTernaryEqualBranch
// ReSharper disable CompareOfFloatsByEqualityOperator
// ReSharper disable StringLiteralTypo
// ReSharper disable InconsistentNaming
namespace Tests.ECS.Arch {

public static class Test_StructuralChange_OldBehavior
{
[Test]
public static void Test_StructuralChange_OldBehavior_no_throw()
{
var store = new EntityStore();
store.CreateEntity(new MyComponent1(), new MyComponent2(), new MyComponent3(), new MyComponent4(), new MyComponent4());
{
var query = store.Query();
query.ThrowOnStructuralChange = false;
foreach (var entity in query.Entities)
{
TestNoExceptions(store, entity);
}
} {
var query = store.Query<MyComponent1>();
query.ThrowOnStructuralChange = false;
query.ForEachEntity((ref MyComponent1 _, Entity entity) =>
{
TestNoExceptions(store, entity);
});
}{
var query = store.Query<MyComponent1, MyComponent2>();
query.ThrowOnStructuralChange = false;
query.ForEachEntity((ref MyComponent1 _, ref MyComponent2 _, Entity entity) =>
{
TestNoExceptions(store, entity);
});
}{
var query = store.Query<MyComponent1, MyComponent2, MyComponent3>();
query.ThrowOnStructuralChange = false;
query.ForEachEntity((ref MyComponent1 _, ref MyComponent2 _, ref MyComponent3 _, Entity entity) =>
{
TestNoExceptions(store, entity);
});
}{
var query = store.Query<MyComponent1, MyComponent2, MyComponent3, MyComponent4>();
query.ThrowOnStructuralChange = false;
query.ForEachEntity((ref MyComponent1 _, ref MyComponent2 _, ref MyComponent3 _, ref MyComponent4 _, Entity entity) =>
{
TestNoExceptions(store, entity);
});
}{
var query = store.Query<MyComponent1, MyComponent2, MyComponent3, MyComponent4, MyComponent5>();
query.ThrowOnStructuralChange = false;
query.ForEachEntity((ref MyComponent1 _, ref MyComponent2 _, ref MyComponent3 _, ref MyComponent4 _, ref MyComponent5 _, Entity entity) =>
{
TestNoExceptions(store, entity);
});
}
}

private static void TestNoExceptions(EntityStore store, Entity entity)
{
entity.AddTag<TestTag>();
entity.RemoveTag<TestTag>();

entity.AddComponent<Position>();
entity.RemoveComponent<Position>();

var buffer = store.GetCommandBuffer();
buffer.Playback();

var entityBatch = new EntityBatch();
entityBatch.ApplyTo(entity);

TestMultiAddRemoveNoExceptions(entity);

var converter = EntityConverter.Default;
var dataEntity = new DataEntity { pid = 1 };
converter.DataEntityToEntity(dataEntity, store, out _);
}


private static void TestMultiAddRemoveNoExceptions(Entity entity)
{
// --- add multiple components
entity.Add(new Position());
entity.Add(new Position(), new Scale3());
entity.Add(new Position(), new Scale3(), new Rotation());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1(), new MyComponent2());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1(), new MyComponent2(), new MyComponent3());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1(), new MyComponent2(), new MyComponent3(), new MyComponent4());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1(), new MyComponent2(), new MyComponent3(), new MyComponent4(), new MyComponent5());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1(), new MyComponent2(), new MyComponent3(), new MyComponent4(), new MyComponent5(), new MyComponent6());
entity.Add(new Position(), new Scale3(), new Rotation(), new MyComponent1(), new MyComponent2(), new MyComponent3(), new MyComponent4(), new MyComponent5(), new MyComponent6(), new MyComponent7());

// --- remove multiple components
entity.Remove<Position>();
entity.Remove<Position, Scale3>();
entity.Remove<Position, Scale3, Rotation>();
entity.Remove<Position, Scale3, Rotation, MyComponent1>();
entity.Remove<Position, Scale3, Rotation, MyComponent1, MyComponent2>();
entity.Remove<Position, Scale3, Rotation, MyComponent1, MyComponent2, MyComponent3>();
entity.Remove<Position, Scale3, Rotation, MyComponent1, MyComponent2, MyComponent3, MyComponent4>();
entity.Remove<Position, Scale3, Rotation, MyComponent1, MyComponent2, MyComponent3, MyComponent4, MyComponent5>();
entity.Remove<Position, Scale3, Rotation, MyComponent1, MyComponent2, MyComponent3, MyComponent4, MyComponent5, MyComponent6>();
entity.Remove<Position, Scale3, Rotation, MyComponent1, MyComponent2, MyComponent3, MyComponent4, MyComponent5, MyComponent6, MyComponent7>();
}

}

}

0 comments on commit 2df4aeb

Please sign in to comment.