test: add relationship and reordering tests

This commit is contained in:
hypercross 2026-07-22 08:07:01 +08:00
parent 68eeeeb7a6
commit 2a3feb556f
2 changed files with 101 additions and 0 deletions

View File

@ -9,6 +9,8 @@ public class ReactivityTests
private struct Position { public float X; public float Y; }
private struct Health { public int Value; }
private struct Frozen { }
private struct ChildOf { }
private struct ParentOf { }
/// <summary>
/// Helper that collects changes into a list and exposes an R3 Observer.
@ -264,6 +266,31 @@ public class ReactivityTests
collector.Changes.Should().Contain(c => c.Kind == ChangeKind.EntityAdded);
}
[Fact]
public void ReorderSources_PostsRelationshipReordered()
{
var world = new World();
var parent = world.CreateEntity();
var a = world.CreateEntity();
var b = world.CreateEntity();
world.AddComponent(a, new Relationship<ChildOf, ParentOf> { Target = parent });
world.AddComponent(b, new Relationship<ChildOf, ParentOf> { Target = parent });
world.PostChanges(); // Clear pending
var collector = new ChangeCollector();
using var sub = world.ObserveEntityChanges().Subscribe(collector.ToObserver());
world.ReorderSources<Relationship<ChildOf, ParentOf>>(parent, [b, a]);
world.PostChanges();
collector.Changes.Should().ContainSingle()
.Which.Should().Match<EntityChange>(c =>
c.Entity == parent &&
c.Kind == ChangeKind.RelationshipReordered &&
c.ComponentType == typeof(Relationship<ChildOf, ParentOf>));
}
private class EntityCreatorSystem : ISystem
{
private readonly World _world;

View File

@ -197,6 +197,80 @@ public class RelationshipTests
results.Should().BeEquivalentTo([child]);
}
[Fact]
public void GetSources_PreservesInsertionOrder()
{
var world = new World();
var parent = world.CreateEntity();
// Add sources in a specific order.
var sources = new Entity[5];
for (int i = 0; i < 5; i++)
{
sources[i] = world.CreateEntity();
world.AddComponent(sources[i], new Relationship<ChildOf, ParentOf>
{
Target = parent
});
}
var result = world.GetSources<Relationship<ChildOf, ParentOf>>(parent);
result.Should().Equal(sources);
}
[Fact]
public void GetSources_OrderIsStableAfterMiddleRemove()
{
var world = new World();
var parent = world.CreateEntity();
var a = world.CreateEntity();
var b = world.CreateEntity();
var c = world.CreateEntity();
world.AddComponent(a, new Relationship<ChildOf, ParentOf> { Target = parent });
world.AddComponent(b, new Relationship<ChildOf, ParentOf> { Target = parent });
world.AddComponent(c, new Relationship<ChildOf, ParentOf> { Target = parent });
// Remove the middle source.
world.RemoveComponent<Relationship<ChildOf, ParentOf>>(b);
// Remaining sources should preserve insertion order.
var result = world.GetSources<Relationship<ChildOf, ParentOf>>(parent);
result.Should().Equal([a, c]);
}
[Fact]
public void ReorderSources_ChangesIterationOrder()
{
var world = new World();
var parent = world.CreateEntity();
var a = world.CreateEntity();
var b = world.CreateEntity();
var c = world.CreateEntity();
world.AddComponent(a, new Relationship<ChildOf, ParentOf> { Target = parent });
world.AddComponent(b, new Relationship<ChildOf, ParentOf> { Target = parent });
world.AddComponent(c, new Relationship<ChildOf, ParentOf> { Target = parent });
// Reverse the order.
world.ReorderSources<Relationship<ChildOf, ParentOf>>(parent, [c, b, a]);
var result = world.GetSources<Relationship<ChildOf, ParentOf>>(parent);
result.Should().Equal([c, b, a]);
}
[Fact]
public void ReorderSources_NoopsWhenTargetHasNoRelationships()
{
var world = new World();
var entity = world.CreateEntity();
// Should not throw.
world.ReorderSources<Relationship<ChildOf, ParentOf>>(entity, []);
}
[Fact]
public void DestroyEntity_WithBothIncomingAndOutgoingRelationships()
{