Skip to content

Commit

Permalink
Add test options
Browse files Browse the repository at this point in the history
  • Loading branch information
sschmid committed Jul 7, 2023
1 parent e71e2fd commit fb7f45f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ end_of_line=lf
trim_trailing_whitespace=true
insert_final_newline=true
indent_style=space
entitas_test_option=true
13 changes: 9 additions & 4 deletions gen/Entitas.Generators/Component/ComponentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ public sealed class ComponentGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext initContext)
{
var options = initContext.AnalyzerConfigOptionsProvider
.Select((provider, _) => new EntitasAnalyzerConfigOptions(provider.GlobalOptions));

var components = initContext.SyntaxProvider
.CreateSyntaxProvider(IsComponentCandidate, CreateComponentDeclarations)
.Where(static components => components is not null)
.SelectMany(static (components, _) => components!.Value);

var fullNameOrContextChanged = components.WithComparer(new FullNameAndContextComparer());
initContext.RegisterSourceOutput(fullNameOrContextChanged, OnFullNameOrContextChanged);
initContext.RegisterSourceOutput(fullNameOrContextChanged.Combine(options), OnFullNameOrContextChanged);

var fullNameOrMembersOrContextChanged = components.WithComparer(new FullNameAndMembersAndContextComparer());
initContext.RegisterSourceOutput(fullNameOrMembersOrContextChanged, OnFullNameOrMembersOrContextChanged);
Expand Down Expand Up @@ -163,20 +166,22 @@ static bool IsContextInitializationMethodCandidate(SyntaxNode node, Cancellation
.ToImmutableArray();
}

static void OnFullNameOrContextChanged(SourceProductionContext spc, ComponentDeclaration component)
static void OnFullNameOrContextChanged(SourceProductionContext spc, (ComponentDeclaration Component, EntitasAnalyzerConfigOptions Options) pair)
{
ComponentIndex(spc, component);
var (component, options) = pair;
ComponentIndex(spc, component, options);
Matcher(spc, component);
}

static void ComponentIndex(SourceProductionContext spc, ComponentDeclaration component)
static void ComponentIndex(SourceProductionContext spc, ComponentDeclaration component, EntitasAnalyzerConfigOptions options)
{
spc.AddSource(
GeneratedPath($"{component.FullName}.{component.ContextPrefix}.ComponentIndex"),
GeneratedFileHeader(GeneratorSource(nameof(ComponentIndex))) +
$"using global::{component.ContextPrefix};\n\n" +
NamespaceDeclaration(component.Namespace,
$$"""
// {{options.TestValue}}
public static class {{component.ContextAwareComponentPrefix}}ComponentIndex
{
public static ComponentIndex Index;
Expand Down
25 changes: 25 additions & 0 deletions gen/Entitas.Generators/EntitasAnalyzerConfigOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Entitas.Generators;

public readonly struct EntitasAnalyzerConfigOptions : IEquatable<EntitasAnalyzerConfigOptions>
{
public const string TestValueKey = "entitas_test_option";

public readonly bool TestValue;

public EntitasAnalyzerConfigOptions(AnalyzerConfigOptions options)
{
TestValue = IsTrue(options, TestValueKey);
}

static bool IsTrue(AnalyzerConfigOptions options, string key)
{
return options.TryGetValue(key, out var value) && value.Equals("true", StringComparison.OrdinalIgnoreCase);
}

public bool Equals(EntitasAnalyzerConfigOptions other) => TestValue == other.TestValue;
public override bool Equals(object? obj) => obj is EntitasAnalyzerConfigOptions other && Equals(other);
public override int GetHashCode() => TestValue.GetHashCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void GetsComponent()
}

[Fact]
public void GettingNonExistingComponentReturnNull()
public void GettingNonExistingComponentReturnsNull()
{
var entity = _context.CreateEntity();
entity.GetPosition().Should().BeNull();
Expand Down
39 changes: 38 additions & 1 deletion tests/Entitas.Generators.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using MyFeature;
using VerifyXunit;

Expand Down Expand Up @@ -32,7 +36,14 @@ public static Task Verify(string source, IIncrementalGenerator generator)
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

var driver = CSharpGeneratorDriver.Create(generator).RunGenerators(compilation);
var driver = CSharpGeneratorDriver
.Create(generator)
.WithUpdatedAnalyzerConfigOptions(new TestAnalyzerConfigOptionsProvider(new Dictionary<string, string>
{
{ EntitasAnalyzerConfigOptions.TestValueKey, "true" }
}))
.RunGenerators(compilation);

return Verifier.Verify(driver).UseDirectory("snapshots");
}

Expand All @@ -50,4 +61,30 @@ public static void AssertUsesGlobalNamespaces(string code)
}
}
}

sealed class TestAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
{
public override AnalyzerConfigOptions GlobalOptions { get; }

public TestAnalyzerConfigOptionsProvider(Dictionary<string, string> options)
{
GlobalOptions = new DictionaryAnalyzerConfigOptions(options.ToImmutableDictionary());
}

public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => DictionaryAnalyzerConfigOptions.Empty;
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) => DictionaryAnalyzerConfigOptions.Empty;
}

sealed class DictionaryAnalyzerConfigOptions : AnalyzerConfigOptions
{
static readonly ImmutableDictionary<string, string> EmptyDictionary = ImmutableDictionary.Create<string, string>(KeyComparer);

public static DictionaryAnalyzerConfigOptions Empty { get; } = new DictionaryAnalyzerConfigOptions(EmptyDictionary);

readonly ImmutableDictionary<string, string> _options;

public DictionaryAnalyzerConfigOptions(ImmutableDictionary<string, string> options) => _options = options;

public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) => _options.TryGetValue(key, out value);
}
}

0 comments on commit fb7f45f

Please sign in to comment.