Skip to content

Commit

Permalink
Enforcing constructor creation of QueryDescription by making attribut…
Browse files Browse the repository at this point in the history
…es private and updating tests to .Net8 and new API.
  • Loading branch information
genaray committed Aug 12, 2024
1 parent 70f9f27 commit d1fdb23
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/Arch.Tests/Arch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Configurations>Debug;Release;Release-Pure;Release-PureECS;Release-Events;Debug-PureECS;Debug-Events</Configurations>
<LangVersion>12</LangVersion>
<!--<TreatWarningsAsErrors>true</TreatWarningsAsErrors>-->
</PropertyGroup>

Expand Down
10 changes: 5 additions & 5 deletions src/Arch.Tests/BitSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public sealed class BitSetTest
[Test]
public void ComponentHashOrder()
{
ComponentType[] array1 = { typeof(int), typeof(byte) };
ComponentType[] array2 = { typeof(int), typeof(byte) };
ComponentType[] array3 = { typeof(byte), typeof(int) };
ComponentType[] array1 = [typeof(int), typeof(byte)];
ComponentType[] array2 = [typeof(int), typeof(byte)];
ComponentType[] array3 = [typeof(byte), typeof(int)];

That(Component.GetHashCode(array2), Is.EqualTo(Component.GetHashCode(array1)));
That(Component.GetHashCode(array3), Is.EqualTo(Component.GetHashCode(array1)));
Expand Down Expand Up @@ -243,8 +243,8 @@ public void Component32Hash(int borderComponentId)
var componentTypeNotOnBorder = new ComponentType(borderComponentId - 10, 0);
var componentTypeOnBorder = new ComponentType(borderComponentId, 0);

ComponentType[] array1 = { componentTypeNotOnBorder, componentTypeOnBorder };
ComponentType[] array2 = { componentTypeNotOnBorder };
ComponentType[] array1 = [componentTypeNotOnBorder, componentTypeOnBorder];
ComponentType[] array2 = [componentTypeNotOnBorder];

That(Component.GetHashCode(array1), Is.Not.EqualTo(Component.GetHashCode(array2)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Arch.Tests/ChunkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Arch.Tests;
public sealed class ChunkTest
{
private Chunk _chunk;
private readonly ComponentType[] _types = { typeof(Transform), typeof(Rotation) };
private readonly ComponentType[] _types = [typeof(Transform), typeof(Rotation)];

/// <summary>
/// Checks if data inside the chunk is being set correctly.
Expand Down
32 changes: 16 additions & 16 deletions src/Arch.Tests/CommandBufferTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Arch.Tests;
public sealed partial class CommandBufferTest
{

private static readonly ComponentType[] _group = { typeof(Transform), typeof(Rotation) };
private static readonly ComponentType[] _secondGroup = { typeof(Transform), typeof(Rotation), typeof(Ai), typeof(int) };
private readonly QueryDescription _queryDescription = new() { All = _group };
private static readonly ComponentType[] _group = [typeof(Transform), typeof(Rotation)];
private static readonly ComponentType[] _secondGroup = [typeof(Transform), typeof(Rotation), typeof(Ai), typeof(int)];
private readonly QueryDescription _queryDescription = new(all: _group);

[Test]
public void CommandBufferSparseSet()
Expand All @@ -38,7 +38,7 @@ public void CommandBufferForExistingEntity()
var world = World.Create();
var commandBuffer = new CommandBuffer();

var entity = world.Create(new ComponentType[] { typeof(Transform), typeof(Rotation), typeof(int) });
var entity = world.Create([typeof(Transform), typeof(Rotation), typeof(int)]);
commandBuffer.Set(in entity, new Transform { X = 20, Y = 20 });
commandBuffer.Add(in entity, new Ai());
commandBuffer.Remove<int>(in entity);
Expand All @@ -58,7 +58,7 @@ public void CommandBuffer()
var world = World.Create();
var commandBuffer = new CommandBuffer();

var entity = commandBuffer.Create(new ComponentType[] { typeof(Transform), typeof(Rotation), typeof(int) });
var entity = commandBuffer.Create([typeof(Transform), typeof(Rotation), typeof(int)]);
commandBuffer.Set(in entity, new Transform { X = 20, Y = 20 });
commandBuffer.Add(in entity, new Ai());
commandBuffer.Remove<int>(in entity);
Expand All @@ -82,9 +82,9 @@ public void CommandBufferCreateMultipleEntities()
var entities = new List<Entity>();
using (var commandBuffer = new CommandBuffer())
{
entities.Add(commandBuffer.Create(new ComponentType[] { typeof(Transform) }));
entities.Add(commandBuffer.Create(new ComponentType[] { typeof(Transform) }));
entities.Add(commandBuffer.Create(new ComponentType[] { typeof(Transform) }));
entities.Add(commandBuffer.Create([typeof(Transform)]));
entities.Add(commandBuffer.Create([typeof(Transform)]));
entities.Add(commandBuffer.Create([typeof(Transform)]));
commandBuffer.Playback(world);
}

Expand All @@ -100,16 +100,16 @@ public void CommandBufferCreateAndDestroy()

using (var commandBuffer = new CommandBuffer())
{
commandBuffer.Create(new ComponentType[] { typeof(Transform) });
commandBuffer.Create(new ComponentType[] { typeof(Transform) });
var e = commandBuffer.Create(new ComponentType[] { typeof(Transform) });
commandBuffer.Create([typeof(Transform)]);
commandBuffer.Create([typeof(Transform)]);
var e = commandBuffer.Create([typeof(Transform)]);
commandBuffer.Destroy(e);
commandBuffer.Playback(world);
}

That(world.Size, Is.EqualTo(2));

var query = new QueryDescription { All = new ComponentType[] { typeof(Transform) } };
var query = new QueryDescription(all: [typeof(Transform)]);
var entities = new Entity[world.CountEntities(query)];
world.GetEntities(query, entities);

Expand All @@ -132,14 +132,14 @@ public void CommandBufferModify()
// Create an entity
using (var commandBuffer = new CommandBuffer())
{
commandBuffer.Create( new ComponentType[] { typeof(int) });
commandBuffer.Create([typeof(int)]);
commandBuffer.Playback(world);
}

That(world.Size, Is.EqualTo(1));

// Retrieve the entity we just created
var query = new QueryDescription { All = new ComponentType[] { typeof(int) } };
var query = new QueryDescription(all: [typeof(int)]);
var entities = new Entity[world.CountEntities(query)];
world.GetEntities(query, entities);

Expand Down Expand Up @@ -190,8 +190,8 @@ public void CommandBufferCombined()
var world = World.Create();
var commandBuffer = new CommandBuffer();

var entity = world.Create(new ComponentType[] { typeof(Transform), typeof(Rotation), typeof(int) });
var bufferedEntity = commandBuffer.Create(new ComponentType[] { typeof(Transform), typeof(Rotation), typeof(int) });
var entity = world.Create([typeof(Transform), typeof(Rotation), typeof(int)]);
var bufferedEntity = commandBuffer.Create([typeof(Transform), typeof(Rotation), typeof(int)]);

commandBuffer.Set(in entity, new Transform { X = 20, Y = 20 });
commandBuffer.Add(in entity, new Ai());
Expand Down
2 changes: 1 addition & 1 deletion src/Arch.Tests/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Setup()
}

private World _world;
private readonly ComponentType[] _group = { typeof(Transform), typeof(Rotation) };
private readonly ComponentType[] _group = [typeof(Transform), typeof(Rotation)];
private Entity _entity;

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Arch.Tests/EnumeratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace Arch.Tests;
[TestFixture]
public sealed class EnumeratorTest
{
private static readonly ComponentType[] _group = { typeof(Transform), typeof(Rotation) };
private static readonly ComponentType[] _otherGroup = { typeof(Transform), typeof(Rotation), typeof(Ai) };
private readonly QueryDescription _description = new() { All = _group };
private static readonly ComponentType[] _group = [typeof(Transform), typeof(Rotation)];
private static readonly ComponentType[] _otherGroup = [typeof(Transform), typeof(Rotation), typeof(Ai)];
private readonly QueryDescription _description = new(all: _group);

private World _world;

Expand Down
24 changes: 12 additions & 12 deletions src/Arch.Tests/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public sealed partial class QueryTest
private JobScheduler _jobScheduler;
private World? _world;

private static readonly ComponentType[] _entityGroup = { typeof(Transform), typeof(Rotation) };
private static readonly ComponentType[] _entityAiGroup = { typeof(Transform), typeof(Rotation), typeof(Ai) };
private static readonly ComponentType[] _entityGroup = [typeof(Transform), typeof(Rotation)];
private static readonly ComponentType[] _entityAiGroup = [typeof(Transform), typeof(Rotation), typeof(Ai)];

private readonly QueryDescription _withoutAiQuery = new() { All = new ComponentType[] { typeof(Transform) }, Any = new ComponentType[] { typeof(Rotation) }, None = new ComponentType[] { typeof(Ai) } };
private readonly QueryDescription _allQuery = new() { All = new ComponentType[] { typeof(Transform), typeof(Rotation) }, Any = new ComponentType[] { typeof(Ai) } };
private readonly QueryDescription _withoutAiQuery = new( all: [typeof(Transform)], any: [typeof(Rotation)], none: [typeof(Ai)]);
private readonly QueryDescription _allQuery = new(all: [typeof(Transform), typeof(Rotation)], any: [typeof(Ai)]);

[OneTimeSetUp]
public void Setup()
Expand All @@ -39,7 +39,7 @@ public void Teardown()
[Test]
public void AllQuery()
{
var query = new QueryDescription { All = new ComponentType[] { typeof(Transform) } };
var query = new QueryDescription(all: [typeof(Transform)]);

_world = World.Create();
for (var index = 0; index < 100; index++)
Expand All @@ -48,14 +48,14 @@ public void AllQuery()
}

var count = 0;
_world.Query(query, (Entity entity) => count++);
_world.Query(query, _ => count++);
That(count, Is.EqualTo(100));
}

[Test]
public void AnyQuery()
{
var query = new QueryDescription { Any = new ComponentType[] { typeof(Transform) } };
var query = new QueryDescription(any: [typeof(Transform)]);

_world = World.Create();
for (var index = 0; index < 100; index++)
Expand All @@ -64,14 +64,14 @@ public void AnyQuery()
}

var count = 0;
_world.Query(query, (Entity entity) => count++);
_world.Query(query, _ => count++);
That(count, Is.EqualTo(100));
}

[Test]
public void NoneQuery()
{
var query = new QueryDescription { None = new ComponentType[] { typeof(Transform) } };
var query = new QueryDescription(none: [typeof(Transform)]);

_world = World.Create();
for (var index = 0; index < 100; index++)
Expand All @@ -80,14 +80,14 @@ public void NoneQuery()
}

var count = 0;
_world.Query(query, (Entity entity) => count++);
_world.Query(query, _ => count++);
That(count, Is.EqualTo(0));
}

[Test]
public void EmptyQuery()
{
var query = new QueryDescription { None = new ComponentType[] { typeof(int) } };
var query = new QueryDescription(none: [typeof(int)]);

_world = World.Create();
_world.Create();
Expand All @@ -101,7 +101,7 @@ public void EmptyQuery()
public void ExclusiveQuery()
{
var exclusiveGroup = new ComponentType[] { typeof(Transform), typeof(Rotation) };
var query = new QueryDescription { Exclusive = exclusiveGroup };
var query = new QueryDescription(exclusive: exclusiveGroup);

_world = World.Create();
for (var index = 0; index < 100; index++)
Expand Down
10 changes: 5 additions & 5 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public sealed partial class WorldTest
{
private World _world;

private readonly ComponentType[] _entityGroup = { typeof(Transform), typeof(Rotation) };
private readonly ComponentType[] _entityAiGroup = { typeof(Transform), typeof(Rotation), typeof(Ai) };
private readonly ComponentType[] _entityGroup = [typeof(Transform), typeof(Rotation)];
private readonly ComponentType[] _entityAiGroup = [typeof(Transform), typeof(Rotation), typeof(Ai)];

[SetUp]
public void Setup()
Expand Down Expand Up @@ -90,7 +90,7 @@ public void Destroy()
[Test]
public void DestroyAll()
{
var query = new QueryDescription { All = new ComponentType[] { typeof(Transform) } };
var query = new QueryDescription(all: [typeof(Transform)]);

var entities = new Entity[_world.CountEntities(query)];
_world.GetEntities(query, entities.AsSpan());
Expand Down Expand Up @@ -357,7 +357,7 @@ public void GetEntitesTest()
{
// Query
var archTypes = new ComponentType[] { typeof(Transform) };
var query = new QueryDescription { All = archTypes };
var query = new QueryDescription(all: archTypes);

// World
using var world = World.Create();
Expand Down Expand Up @@ -385,7 +385,7 @@ public void CountEntitiesTest()
{
// Query
var archTypes = new ComponentType[] { typeof(Transform) };
var query = new QueryDescription { All = archTypes };
var query = new QueryDescription(all: archTypes);

// World
using var world = World.Create();
Expand Down
13 changes: 9 additions & 4 deletions src/Arch/Core/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,26 +226,26 @@ public partial struct QueryDescription : IEquatable<QueryDescription>
/// An <see cref="Signature"/> of all components that an <see cref="Entity"/> should have mandatory.
/// <remarks>If the content of the array is subsequently changed, a <see cref="Rebuild"/> should be carried out.</remarks>
/// </summary>
public Signature All { get; set; } = new();
public Signature All { get; private set; } = new();

/// <summary>
/// An array of all components of which an <see cref="Entity"/> should have at least one.
/// <remarks>If the content of the array is subsequently changed, a <see cref="Rebuild"/> should be carried out.</remarks>
/// </summary>
public Signature Any { get; set; } = new();
public Signature Any { get; private set; } = new();

/// <summary>
/// An array of all components of which an <see cref="Entity"/> should not have any.
/// <remarks>If the content of the array is subsequently changed, a <see cref="Rebuild"/> should be carried out.</remarks>
/// </summary>
public Signature None { get; set; } = new();
public Signature None { get; private set; } = new();

/// <summary>
/// An array of all components that exactly match the structure of an <see cref="Entity"/>.
/// <see cref="Entity"/>'s with more or less components than those defined in the array are not addressed.
/// <remarks>If the content of the array is subsequently changed, a <see cref="Rebuild"/> should be carried out.</remarks>
/// </summary>
public Signature Exclusive { get; set; } = new();
public Signature Exclusive { get; private set; } = new();

/// <summary>
/// Initializes a new instance of the <see cref="QueryDescription"/> struct.
Expand Down Expand Up @@ -273,6 +273,11 @@ public QueryDescription(ComponentType[]? all = null, ComponentType[]? any = null
_hashCode = GetHashCode();
}

public QueryDescription(Signature all) : this()
{
All = all;
}

/// <summary>
/// Recreates this instance by calculating a new <see cref="_hashCode"/>.
/// Is actually only needed if the passed arrays are changed afterwards.
Expand Down

0 comments on commit d1fdb23

Please sign in to comment.