Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,84 @@ await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
}

[Test]
public async Task Generates_Batch_Registration_Method_When_Register_Method_Comes_From_Inherited_Interface()
{
const string source = """
#nullable enable
using System;
using System.Collections.Generic;
using GFramework.Godot.SourceGenerators.Abstractions;

namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class RegisterExportedCollectionAttribute : Attribute
{
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
}
}

namespace TestApp
{
public interface IKeyValue<TKey, TValue>
{
}

public interface IRegistry<TKey, TValue>
{
void Registry(IKeyValue<TKey, TValue> mapping);
}

public interface IAssetRegistry<TValue> : IRegistry<string, TValue>
{
}

public sealed class IntConfig : IKeyValue<string, int>
{
}

[AutoRegisterExportedCollections]
public partial class Bootstrapper
{
private readonly IAssetRegistry<int>? _registry = null;

[RegisterExportedCollection(nameof(_registry), "Registry")]
public List<IntConfig>? Values { get; } = new();
}
}
""";

const string expected = """
// <auto-generated />
#nullable enable

namespace TestApp;

partial class Bootstrapper
{
private void __RegisterExportedCollections_Generated()
{
if (this.Values is not null && this._registry is not null)
{
foreach (var __generatedItem in this.Values)
{
this._registry.Registry(__generatedItem);
}
}
}
}

""";

await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
source,
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
}
Comment thread
GeWuYou marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.

[Test]
public async Task Reports_Diagnostic_When_Collection_Member_Is_Not_Instance_Readable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ private static bool TryCreateRegistration(
return false;
}

var hasCompatibleMethod = registryType.GetMembers(registerMethodName)
.OfType<IMethodSymbol>()
var hasCompatibleMethod = EnumerateCandidateMethods(registryType, registerMethodName)
.Any(method =>
!method.IsStatic &&
method.Parameters.Length == 1 &&
Expand Down Expand Up @@ -319,6 +318,26 @@ private static bool CanAcceptElementType(
return compilation.ClassifyConversion(elementType, parameterType).IsImplicit;
}

private static IEnumerable<IMethodSymbol> EnumerateCandidateMethods(
INamedTypeSymbol registryType,
string registerMethodName)
{
foreach (var method in registryType.GetMembers(registerMethodName).OfType<IMethodSymbol>())
yield return method;

for (var baseType = registryType.BaseType; baseType is not null; baseType = baseType.BaseType)
{
foreach (var method in baseType.GetMembers(registerMethodName).OfType<IMethodSymbol>())
yield return method;
}

foreach (var interfaceType in registryType.AllInterfaces)
{
foreach (var method in interfaceType.GetMembers(registerMethodName).OfType<IMethodSymbol>())
yield return method;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Comment thread
GeWuYou marked this conversation as resolved.

private static bool TryGetRegistrationAttributeArguments(
SourceProductionContext context,
ISymbol collectionMember,
Expand Down
Loading