Skip to content

Commit

Permalink
Basic tests for RW and R and Modified events. Missing actual value co…
Browse files Browse the repository at this point in the history
…mparison tests.
  • Loading branch information
thygrrr committed Nov 7, 2024
1 parent 9850d03 commit 511d078
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 20 deletions.
6 changes: 3 additions & 3 deletions fennecs.tests/Conceptual/EventConceptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ static void Added(Entity entity, T value)
}
}

public interface IValue<T>
private interface IValue<T>
{
T Value { get; set; }
}

public interface IModified<in C>
private interface IModified<in C>
{
static void Modified(C value)
{
Expand All @@ -38,7 +38,7 @@ static void Removed(Entity entity, T value)
}
}

public record struct TestComponent(int Value) :
private record struct TestComponent(int Value) :
IValue<int>,
IAdded<TestComponent>,
IModified<TestComponent>,
Expand Down
9 changes: 9 additions & 0 deletions fennecs.tests/Storage/R.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ public void Can_Read()

Assert.Equal(1, rw.read);
}

[Fact]
public void Implicitly_Casts_to_Value()
{
var x = 1;
var r = new R<int>(ref x);

Assert.Equal(1, r);
}
}
100 changes: 99 additions & 1 deletion fennecs.tests/Storage/RW.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using fennecs.storage;
using fennecs.events;
using fennecs.storage;

namespace fennecs.tests.Storage;

Expand All @@ -17,6 +18,19 @@ public void Can_Read()
Assert.Equal(1, rw.read);
}

[Fact]
public void Implicitly_Casts_to_Value()
{
using var world = new World();
var entity = world.Spawn();

var x = 1;
var match = default(Match);
var rw = new RW<int>(ref x, ref entity, ref match);

Assert.Equal(1, rw);
}

[Fact]
public void Can_Write()
{
Expand Down Expand Up @@ -101,4 +115,88 @@ public void Can_Remove_Relation()
entity.RW<int>(match).Remove();
Assert.False(entity.Has<int>(match));
}


private struct Type69 : Modified<Type69>;

[Fact]
public void Triggers_Entities_on_Modified_Value()
{
using var world = new World();
var entity = world.Spawn();
entity.Add(new Type69());

var match = default(Match);
var rw = entity.RW<Type69>(match);

var modified = false;

Modified<Type69>.Entities += entities =>
{
Assert.Equal(1, entities.Length);
Assert.Equal(entity, entities[0]);
modified = true;
};

rw.write = new();
Assert.True(modified);

Modified<Type69>.Clear();
}

private class Type42 : Modified<Type42>;

[Fact]
public void Triggers_Entities_on_Modified_Reference()
{
using var world = new World();
var entity = world.Spawn();
entity.Add(new Type42());

var match = default(Match);
var rw = entity.RW<Type42>(match);

var modified = false;

Modified<Type42>.Entities += entities =>
{
Assert.Equal(1, entities.Length);
Assert.Equal(entity, entities[0]);
modified = true;
};

rw.write = new();
Assert.True(modified);

Modified<Type42>.Clear();
}

[Fact]
public void Triggers_Values_on_Modified_Reference()
{
using var world = new World();
var entity = world.Spawn();
var original = new Type42();
entity.Add(original);

var match = default(Match);
var rw = entity.RW<Type42>(match);

var modified = false;
var updated = new Type42();

Modified<Type42>.Values += (entities, originals, updateds) =>
{
Assert.Equal(1, entities.Length);
Assert.Equal(entity, entities[0]);
Assert.Equal(original, originals[0]);
Assert.Equal(updated, updateds[0]);
modified = true;
};

rw.write = updated;
Assert.True(modified);

Modified<Type42>.Clear();
}
}
61 changes: 61 additions & 0 deletions fennecs.tests/Stream/Stream.2.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,67 @@ namespace fennecs.tests.Stream;
// ReSharper disable once ClassNeverInstantiated.Global
public class Stream2Tests(ITestOutputHelper output)
{
[Fact] public void Can_Use_RW_Inferred()
{
using var world = new World();
var entity = world.Spawn();
entity.Add(123).Add(890f);

var stream = world.Stream<int, float>();

stream.For(static (a, b) => {
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
b.write = 456f;
});
}

[Fact] public void Can_Use_WR_Inferred()
{
using var world = new World();
var entity = world.Spawn();
entity.Add(123).Add(890f);

var stream = world.Stream<int, float>();

stream.For(static (a, b) => {
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
});
}

[Fact] public void Can_Use_WW_Inferred()
{
using var world = new World();
var entity = world.Spawn();
entity.Add(123).Add(890f);

var stream = world.Stream<int, float>();

stream.For(static (a, b) =>
{
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
a.write = 456;
b.write = 789f;
});
}

[Fact] public void Can_Use_RR_Inferred()
{
using var world = new World();
var entity = world.Spawn();
entity.Add(123).Add(890f);

var stream = world.Stream<int, float>();

stream.For(static (a, b) =>
{
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
});
}

[Fact]
public void Can_Enumerate_Stream()
{
Expand Down
6 changes: 4 additions & 2 deletions fennecs/Archetype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public sealed class Archetype : IEnumerable<Entity>, IComparable<Archetype>
/// Does this Archetype currently contain no Entities?
/// </summary>
public bool IsEmpty => Count == 0;



public ReadOnlySpan<Identity> Span => IdentityStorage.Span;

Check warning on line 42 in fennecs/Archetype.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'Archetype.Span'

Check warning on line 42 in fennecs/Archetype.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'Archetype.Span'


/// <summary>
/// The World this Archetype is a part of.
/// </summary>
Expand Down
14 changes: 9 additions & 5 deletions fennecs/Delegates.2.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using fennecs.storage;

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

namespace fennecs;

public delegate void ComponentActionWW<C0, C1>(RW<C0> comp0, RW<C1> comp1) where C0 : notnull where C1 : notnull;
public delegate void ComponentActionWR<C0, C1>(RW<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
public delegate void ComponentActionRW<C0, C1>(R<C0> comp0, RW<C1> comp1) where C0 : notnull where C1 : notnull;
public delegate void ComponentActionRR<C0, C1>(R<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;

public delegate void ComponentActionR<C0>(R<C0> comp0) where C0 : notnull;
public delegate void ComponentActionW<C0>(RW<C0> comp0) where C0 : notnull;

public delegate void ComponentActionER<C0>(EntityRef entity, R<C0> comp0) where C0 : notnull;
public delegate void ComponentActionEW<C0>(EntityRef entity, RW<C0> comp0) where C0 : notnull;

public delegate void EntityComponentActionWW<C0, C1>(EntityRef entity, R<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
public delegate void EntityComponentActionRR<C0, C1>(EntityRef entity, R<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
public delegate void EntityComponentActionWR<C0, C1>(EntityRef entity, RW<C0> comp0, R<C1> comp1) where C0 : notnull where C1 : notnull;
public delegate void EntityComponentActionRW<C0, C1>(EntityRef entity, R<C0> comp0, RW<C1> comp1) where C0 : notnull where C1 : notnull;
Loading

0 comments on commit 511d078

Please sign in to comment.