Skip to content

Improve query generation and add accessibility option #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Arch.System.SourceGenerator.SnapshotTests/SnapshotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ private static void VerifyCompilation(string compilationFolder, string? testSyst
});

// Compare the generated files with the expected files
// Trim them because editors like newlines at ends of files
var expectedFiles = csFiles
.Where(file => file.FullName.Contains("ExpectedGeneration"))
.Select(file => (Name: file.Name, Text: File.ReadAllText(file.FullName)))
.Select(file => (Name: file.Name, Text: File.ReadAllText(file.FullName).Trim()))
.OrderBy(x => x.Name).ToArray();

var generatedFiles = driver.GetRunResult().GeneratedTrees
.Select(tree => (Name: Path.GetFileName(tree.FilePath), Text: tree.GetText().ToString()))
.Select(tree => (Name: Path.GetFileName(tree.FilePath), Text: tree.GetText().ToString().Trim()))
.Where(x => x.Name != "Attributes.g.cs") // Skip the attributes file
.OrderBy(x => x.Name).ToArray();

Expand Down Expand Up @@ -195,4 +196,10 @@ public void GeneratedUpdateCompilation()
{
VerifyCompilation(nameof(GeneratedUpdateCompilation), "GeneratedUpdateSystem");
}

[Test]
public void AccessibilityCompilation()
{
VerifyCompilation(nameof(AccessibilityCompilation), "AccessibilitySystem");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Arch.Core;
using NUnit.Framework;

namespace Arch.System.SourceGenerator.Tests;

/// <summary>
/// Tests basic query functionality.
/// </summary>
internal partial class AccessibilitySystem : BaseTestSystem
{
public AccessibilitySystem(World world) : base(world) { }

private int _number = 0;

[Query]
public void Default(IntComponentA _)
{
_number++;
}

[Query(Accessibility = QueryAccessibility.Public)]
public void Public(IntComponentA _)
{
_number++;
}

[Query(Accessibility = QueryAccessibility.Internal)]
public void Internal(IntComponentA _)
{
_number++;
}

[Query(Accessibility = QueryAccessibility.Private)]
public void Private(IntComponentA _)
{
_number++;
}

[Query(Accessibility = QueryAccessibility.Protected)]
public void Protected(IntComponentA _)
{
_number++;
}

[Query(Accessibility = QueryAccessibility.ProtectedInternal)]
public void ProtectedInternal(IntComponentA _)
{
_number++;
}

public override void Setup()
{
World.Create(new IntComponentA());
}

public override void Update(in int t)
{
Assert.That(_number, Is.Zero);
PublicQuery(World);
Assert.That(_number, Is.EqualTo(1));
InternalQuery(World);
Assert.That(_number, Is.EqualTo(2));
PrivateQuery(World);
Assert.That(_number, Is.EqualTo(3));
ProtectedQuery(World);
Assert.That(_number, Is.EqualTo(4));
ProtectedInternalQuery(World);
Assert.That(_number, Is.EqualTo(5));
DefaultQuery(World);
Assert.That(_number, Is.EqualTo(6));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Utils;
using ArrayExtensions = CommunityToolkit.HighPerformance.ArrayExtensions;
using Component = Arch.Core.Component;

namespace Arch.System.SourceGenerator.Tests
{
partial class AccessibilitySystem
{
private QueryDescription Default_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Arch.System.SourceGenerator.Tests.IntComponentA)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
private World? _Default_Initialized;
private Query? _Default_Query;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void DefaultQuery(World world)
{
if (!ReferenceEquals(_Default_Initialized, world))
{
_Default_Query = world.Query(in Default_QueryDescription);
_Default_Initialized = world;
}

foreach (ref var chunk in _Default_Query!)
{
ref var @intcomponentaFirstElement = ref chunk.GetFirst<global::Arch.System.SourceGenerator.Tests.IntComponentA>();
foreach (var entityIndex in chunk)
{
ref var @_ = ref Unsafe.Add(ref intcomponentaFirstElement, entityIndex);
Default(@_);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Utils;
using ArrayExtensions = CommunityToolkit.HighPerformance.ArrayExtensions;
using Component = Arch.Core.Component;

namespace Arch.System.SourceGenerator.Tests
{
partial class AccessibilitySystem
{
private QueryDescription Internal_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Arch.System.SourceGenerator.Tests.IntComponentA)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
private World? _Internal_Initialized;
private Query? _Internal_Query;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InternalQuery(World world)
{
if (!ReferenceEquals(_Internal_Initialized, world))
{
_Internal_Query = world.Query(in Internal_QueryDescription);
_Internal_Initialized = world;
}

foreach (ref var chunk in _Internal_Query!)
{
ref var @intcomponentaFirstElement = ref chunk.GetFirst<global::Arch.System.SourceGenerator.Tests.IntComponentA>();
foreach (var entityIndex in chunk)
{
ref var @_ = ref Unsafe.Add(ref intcomponentaFirstElement, entityIndex);
Internal(@_);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Utils;
using ArrayExtensions = CommunityToolkit.HighPerformance.ArrayExtensions;
using Component = Arch.Core.Component;

namespace Arch.System.SourceGenerator.Tests
{
partial class AccessibilitySystem
{
private QueryDescription Private_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Arch.System.SourceGenerator.Tests.IntComponentA)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
private World? _Private_Initialized;
private Query? _Private_Query;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void PrivateQuery(World world)
{
if (!ReferenceEquals(_Private_Initialized, world))
{
_Private_Query = world.Query(in Private_QueryDescription);
_Private_Initialized = world;
}

foreach (ref var chunk in _Private_Query!)
{
ref var @intcomponentaFirstElement = ref chunk.GetFirst<global::Arch.System.SourceGenerator.Tests.IntComponentA>();
foreach (var entityIndex in chunk)
{
ref var @_ = ref Unsafe.Add(ref intcomponentaFirstElement, entityIndex);
Private(@_);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Utils;
using ArrayExtensions = CommunityToolkit.HighPerformance.ArrayExtensions;
using Component = Arch.Core.Component;

namespace Arch.System.SourceGenerator.Tests
{
partial class AccessibilitySystem
{
private QueryDescription Protected_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Arch.System.SourceGenerator.Tests.IntComponentA)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
private World? _Protected_Initialized;
private Query? _Protected_Query;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void ProtectedQuery(World world)
{
if (!ReferenceEquals(_Protected_Initialized, world))
{
_Protected_Query = world.Query(in Protected_QueryDescription);
_Protected_Initialized = world;
}

foreach (ref var chunk in _Protected_Query!)
{
ref var @intcomponentaFirstElement = ref chunk.GetFirst<global::Arch.System.SourceGenerator.Tests.IntComponentA>();
foreach (var entityIndex in chunk)
{
ref var @_ = ref Unsafe.Add(ref intcomponentaFirstElement, entityIndex);
Protected(@_);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Utils;
using ArrayExtensions = CommunityToolkit.HighPerformance.ArrayExtensions;
using Component = Arch.Core.Component;

namespace Arch.System.SourceGenerator.Tests
{
partial class AccessibilitySystem
{
private QueryDescription ProtectedInternal_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Arch.System.SourceGenerator.Tests.IntComponentA)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
private World? _ProtectedInternal_Initialized;
private Query? _ProtectedInternal_Query;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected internal void ProtectedInternalQuery(World world)
{
if (!ReferenceEquals(_ProtectedInternal_Initialized, world))
{
_ProtectedInternal_Query = world.Query(in ProtectedInternal_QueryDescription);
_ProtectedInternal_Initialized = world;
}

foreach (ref var chunk in _ProtectedInternal_Query!)
{
ref var @intcomponentaFirstElement = ref chunk.GetFirst<global::Arch.System.SourceGenerator.Tests.IntComponentA>();
foreach (var entityIndex in chunk)
{
ref var @_ = ref Unsafe.Add(ref intcomponentaFirstElement, entityIndex);
ProtectedInternal(@_);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Core.Utils;
using ArrayExtensions = CommunityToolkit.HighPerformance.ArrayExtensions;
using Component = Arch.Core.Component;

namespace Arch.System.SourceGenerator.Tests
{
partial class AccessibilitySystem
{
private QueryDescription Public_QueryDescription = new QueryDescription(all: new Signature(typeof(global::Arch.System.SourceGenerator.Tests.IntComponentA)), any: Signature.Null, none: Signature.Null, exclusive: Signature.Null);
private World? _Public_Initialized;
private Query? _Public_Query;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PublicQuery(World world)
{
if (!ReferenceEquals(_Public_Initialized, world))
{
_Public_Query = world.Query(in Public_QueryDescription);
_Public_Initialized = world;
}

foreach (ref var chunk in _Public_Query!)
{
ref var @intcomponentaFirstElement = ref chunk.GetFirst<global::Arch.System.SourceGenerator.Tests.IntComponentA>();
foreach (var entityIndex in chunk)
{
ref var @_ = ref Unsafe.Add(ref intcomponentaFirstElement, entityIndex);
Public(@_);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void IncrementAQuery(World world)
_IncrementA_Initialized = world;
}

foreach (ref var chunk in _IncrementA_Query)
foreach (ref var chunk in _IncrementA_Query!)
{
ref var entityFirstElement = ref chunk.Entity(0);
foreach (var entityIndex in chunk)
Expand All @@ -35,4 +35,4 @@ public void IncrementAQuery(World world)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void IncrementAAndBQuery(World world)
_IncrementAAndB_Initialized = world;
}

foreach (ref var chunk in _IncrementAAndB_Query)
foreach (ref var chunk in _IncrementAAndB_Query!)
{
ref var entityFirstElement = ref chunk.Entity(0);
foreach (var entityIndex in chunk)
Expand All @@ -35,4 +35,4 @@ public void IncrementAAndBQuery(World world)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void IncrementAAndBExclusiveQuery(World world)
_IncrementAAndBExclusive_Initialized = world;
}

foreach (ref var chunk in _IncrementAAndBExclusive_Query)
foreach (ref var chunk in _IncrementAAndBExclusive_Query!)
{
ref var entityFirstElement = ref chunk.Entity(0);
foreach (var entityIndex in chunk)
Expand All @@ -35,4 +35,4 @@ public void IncrementAAndBExclusiveQuery(World world)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void IncrementANotBQuery(World world)
_IncrementANotB_Initialized = world;
}

foreach (ref var chunk in _IncrementANotB_Query)
foreach (ref var chunk in _IncrementANotB_Query!)
{
ref var entityFirstElement = ref chunk.Entity(0);
foreach (var entityIndex in chunk)
Expand All @@ -35,4 +35,4 @@ public void IncrementANotBQuery(World world)
}
}
}
}
}
Loading
Loading