mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
- 实现了 AutoRegisterExportedCollectionsGenerator 源生成器 - 支持扫描标记了 AutoRegisterExportedCollectionsAttribute 的 partial 类型 - 为使用 RegisterExportedCollectionAttribute 声明的集合成员生成集中注册方法 - 提供详细的诊断支持,包括 GF_AutoExport_001 到 GF_AutoExport_008 错误码 - 支持从基类和接口继承链查找注册方法 - 实现了完整的单元测试覆盖各种使用场景 - 验证集合可枚举性、元素类型推导和注册表成员可访问性 - 生成安全的空值检查代码防止运行时异常 - 支持泛型类型约束和复杂继承关系的处理
782 lines
37 KiB
C#
782 lines
37 KiB
C#
using GFramework.Godot.SourceGenerators.Registration;
|
|
using GFramework.Godot.SourceGenerators.Tests.Core;
|
|
|
|
namespace GFramework.Godot.SourceGenerators.Tests.Registration;
|
|
|
|
[TestFixture]
|
|
public class AutoRegisterExportedCollectionsGeneratorTests
|
|
{
|
|
[Test]
|
|
public async Task Generates_Batch_Registration_Method_For_Annotated_Collections()
|
|
{
|
|
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 sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper<TReference, TNotNull, TValue, TUnmanaged>
|
|
where TReference : class?
|
|
where TNotNull : notnull
|
|
where TValue : struct
|
|
where TUnmanaged : unmanaged
|
|
{
|
|
private readonly IntRegistry? _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public List<int>? Values { get; } = new();
|
|
}
|
|
}
|
|
""";
|
|
|
|
const string expected = """
|
|
// <auto-generated />
|
|
#nullable enable
|
|
|
|
namespace TestApp;
|
|
|
|
partial class Bootstrapper<TReference, TNotNull, TValue, TUnmanaged>
|
|
where TReference : class?
|
|
where TNotNull : notnull
|
|
where TValue : struct
|
|
where TUnmanaged : unmanaged
|
|
{
|
|
private void __RegisterExportedCollections_Generated()
|
|
{
|
|
if (this.Values is not null && this._registry is not null)
|
|
{
|
|
foreach (var __generatedItem in this.Values)
|
|
{
|
|
this._registry.Register(__generatedItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Collection_Element_Type_Cannot_Be_Inferred()
|
|
{
|
|
const string source = """
|
|
using System;
|
|
using System.Collections;
|
|
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 sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly IntRegistry _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public IEnumerable {|#0:Values|} { get; } = new ArrayList();
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" }
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_005", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("Values"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Generates_Batch_Registration_Method_When_Register_Method_Uses_Array_Parameter()
|
|
{
|
|
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 sealed class ArrayRegistry
|
|
{
|
|
public void Register(int[] value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly ArrayRegistry _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(ArrayRegistry.Register))]
|
|
public List<int[]> 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.Register(__generatedItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
|
|
source,
|
|
("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));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Register_Method_Is_Only_Explicitly_Implemented_Interface_Member()
|
|
{
|
|
const string source = """
|
|
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 IRegistry
|
|
{
|
|
void Register(int value);
|
|
}
|
|
|
|
public sealed class ExplicitRegistry : IRegistry
|
|
{
|
|
void IRegistry.Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly ExplicitRegistry _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), "Register")]
|
|
public List<int> {|#0:Values|} { get; } = new();
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_003", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("Register", "_registry", "Values"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Generates_Batch_Registration_Method_When_Register_Method_Comes_From_Base_Class()
|
|
{
|
|
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 class BaseRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
public sealed class DerivedRegistry : BaseRegistry
|
|
{
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly DerivedRegistry? _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(BaseRegistry.Register))]
|
|
public List<int>? 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.Register(__generatedItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Generates_Batch_Registration_Method_When_Registry_Member_Comes_From_Base_Class()
|
|
{
|
|
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 sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
public abstract class BootstrapperBase
|
|
{
|
|
protected readonly IntRegistry? _registry = new();
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper : BootstrapperBase
|
|
{
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public List<int>? 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.Register(__generatedItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Collection_Member_Is_Not_Instance_Readable()
|
|
{
|
|
const string source = """
|
|
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 sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly IntRegistry _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public static List<int> {|#0:StaticValues|} = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public static List<int> {|#1:StaticPropertyValues|} { get; } = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public List<int> {|#2:WriteOnlyValues|} { set { } }
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_006", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("StaticValues"));
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_006", DiagnosticSeverity.Error)
|
|
.WithLocation(1)
|
|
.WithArguments("StaticPropertyValues"));
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_006", DiagnosticSeverity.Error)
|
|
.WithLocation(2)
|
|
.WithArguments("WriteOnlyValues"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Registry_Member_Is_Not_Instance_Readable()
|
|
{
|
|
const string source = """
|
|
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 sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private static readonly IntRegistry {|#0:_registry|} = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public List<int> Values { get; } = new();
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_007", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("_registry", "Values"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_Register_Method_Is_Not_Accessible_From_Owner_Type()
|
|
{
|
|
const string source = """
|
|
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 sealed class IntRegistry
|
|
{
|
|
private void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly IntRegistry _registry = new();
|
|
|
|
[RegisterExportedCollection(nameof(_registry), "Register")]
|
|
public List<int> {|#0:Values|} { get; } = new();
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_003", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("Register", "_registry", "Values"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Reports_Diagnostic_When_RegisterExportedCollection_Attribute_Arguments_Are_Invalid()
|
|
{
|
|
const string source = """
|
|
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) { }
|
|
}
|
|
}
|
|
|
|
namespace TestApp
|
|
{
|
|
public sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly IntRegistry _registry = new();
|
|
|
|
[{|#0:RegisterExportedCollection(nameof(_registry))|}]
|
|
public List<int> Values { get; } = new();
|
|
}
|
|
}
|
|
""";
|
|
|
|
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
|
|
{
|
|
TestState =
|
|
{
|
|
Sources = { source }
|
|
},
|
|
DisabledDiagnostics = { "GF_Common_Trace_001" },
|
|
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
|
|
};
|
|
|
|
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_008", DiagnosticSeverity.Error)
|
|
.WithLocation(0)
|
|
.WithArguments("Values"));
|
|
|
|
await test.RunAsync();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Generates_Only_One_Source_When_Multiple_Partial_Declarations_Are_Annotated()
|
|
{
|
|
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 = true)]
|
|
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 sealed class IntRegistry
|
|
{
|
|
public void Register(int value) { }
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
private readonly IntRegistry? _registry = new();
|
|
}
|
|
|
|
[AutoRegisterExportedCollections]
|
|
public partial class Bootstrapper
|
|
{
|
|
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
|
|
public List<int>? 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.Register(__generatedItem);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
""";
|
|
|
|
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
|
|
source,
|
|
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
|
|
}
|
|
}
|