Skip to content

Commit

Permalink
Port C++ observer remove override tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Nov 15, 2024
1 parent 7d070d6 commit daa1220
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/Flecs.NET.Tests/Cpp/ObserverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,4 +1468,120 @@ private void OnRemoveInherited()
Assert.Equal(1, count);
Assert.True(i == matched);
}

[Fact]
private void OnSetAfterRemoveOverride()
{
using World world = World.Create();

world.Component<Position>().Entity.Add(Ecs.OnInstantiate, Ecs.Inherit);

Entity @base = world.Prefab()
.Set(new Position(1, 2));

Entity e1 = world.Entity().IsA(@base)
.Set(new Position(10, 20));

int count = 0;

world.Observer<Position>()
.Event(Ecs.OnSet)
.Each((Iter it, int row, ref Position p) =>
{
Assert.True(it.Entity(row) == e1);
Assert.True(it.Src(0) == @base);
Assert.Equal(1, p.X);
Assert.Equal(2, p.Y);
count++;
});

e1.Remove<Position>();

Assert.Equal(1, count);
}

[Fact]
private void OnSetAfterRemoveOverrideCreateObserverBefore()
{
using World world = World.Create();

world.Component<Position>().Entity.Add(Ecs.OnInstantiate, Ecs.Inherit);

int count = 0;

Entity @base = world.Prefab();
Entity e1 = world.Entity();

world.Observer<Position>()
.Event(Ecs.OnSet)
.Each((Iter it, int row, ref Position _) =>
{
Assert.True(it.Entity(row) == e1);
Assert.True(it.Src(0) == @base);
count++;
});

@base.Set(new Position(1, 2));
e1.Add<Position>().IsA(@base);

Assert.Equal(0, count);

e1.Remove<Position>();

Assert.Equal(1, count);
}

[Fact]
private void OnSetWithOverrideAfterDelete()
{
using World world = World.Create();

world.Component<Position>().Entity.Add(Ecs.OnInstantiate, Ecs.Inherit);

Entity @base = world.Prefab()
.Set(new Position(1, 2));

Entity e1 = world.Entity().IsA(@base)
.Set(new Position(10, 20));

int count = 0;

world.Observer<Position>()
.Event(Ecs.OnSet)
.Each((Iter _, int _, ref Position _) =>
{
count++;
});

e1.Destruct();

Assert.Equal(0, count);
}

[Fact]
private void OnSetWithOverrideAfterClear()
{
using World world = World.Create();

world.Component<Position>().Entity.Add(Ecs.OnInstantiate, Ecs.Inherit);

Entity @base = world.Prefab()
.Set(new Position(1, 2));

Entity e1 = world.Entity().IsA(@base)
.Set(new Position(10, 20));

int count = 0;

world.Observer<Position>()
.Event(Ecs.OnSet)
.Each((Iter _, int _, ref Position _) =>
{
count++;
});

e1.Clear();

Assert.Equal(0, count);
}
}

0 comments on commit daa1220

Please sign in to comment.