Add source generator tests for extension targets

This commit is contained in:
Sergio Pedri 2023-06-04 14:40:07 +02:00
Родитель 9bdf430546
Коммит d48772d02d
1 изменённых файлов: 345 добавлений и 0 удалений

Просмотреть файл

@ -355,6 +355,351 @@ public class Test_SourceGenerators_Codegen
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.WindowsRuntimeApplication, ("IMyAppService.g.cs", result));
}
[TestMethod]
public void Extension_ParameterlessMethod()
{
string source = """
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
[AppService("MyAppService")]
interface IMyAppService
{
Task FooAsync();
}
partial class MyAppService : IMyAppService
{
public async Task FooAsync()
{
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(this.FooAsync, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
[TestMethod]
public void Extension_MethodWithPrimitiveParameters()
{
string source = """
using System;
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
[AppService("MyAppService")]
interface IMyAppService
{
Task FooAsync(string name, int number, Guid guid, string[] ids, float[] scalars);
}
partial class MyAppService : IMyAppService
{
public async Task FooAsync(string name, int number, Guid guid, string[] ids, float[] scalars)
{
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(async parameters =>
{
parameters.GetParameter(out string name, "name");
parameters.GetParameter(out int number, "number");
parameters.GetParameter(out global::System.Guid guid, "guid");
parameters.GetParameter(out string[] ids, "ids");
parameters.GetParameter(out float[] scalars, "scalars");
await this.FooAsync(name, number, guid, ids, scalars);
}, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
[TestMethod]
public void Extension_MethodWithPrimitiveReturnType()
{
string source = """
using System;
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
[AppService("MyAppService")]
interface IMyAppService
{
Task<string> FooAsync();
}
partial class MyAppService : IMyAppService
{
public async Task<string> FooAsync()
{
return "";
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(this.FooAsync, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
[TestMethod]
public void Extension_MethodWithCustomSerializerParameter()
{
string source = """
using System;
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
public class MyClass
{
public string Text { get; set; }
}
public class MyClassSerializer : IValueSetSerializer<MyClass>
{
MyClass? IValueSetSerializer<MyClass>.Deserialize(ValueSet? valueSet)
{
return null;
}
ValueSet? IValueSetSerializer<MyClass>.Serialize(MyClass? value)
{
return null;
}
}
[AppService("MyAppService")]
interface IMyAppService
{
Task FooAsync([ValueSetSerializer(typeof(MyClassSerializer))] MyClass myClass);
}
partial class MyAppService : IMyAppService
{
public async Task FooAsync(MyClass myClass)
{
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(async parameters =>
{
parameters.GetParameter(new global::MyClassSerializer(), out global::MyClass myClass, "myClass");
await this.FooAsync(myClass);
}, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
[TestMethod]
public void Extension_MethodWithCustomSerializerReturnType()
{
string source = """
using System;
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
public class MyClass
{
public string Text { get; set; }
}
public class MyClassSerializer : IValueSetSerializer<MyClass>
{
MyClass? IValueSetSerializer<MyClass>.Deserialize(ValueSet? valueSet)
{
return null;
}
ValueSet? IValueSetSerializer<MyClass>.Serialize(MyClass? value)
{
return null;
}
}
[AppService("MyAppService")]
interface IMyAppService
{
[return: ValueSetSerializer(typeof(MyClassSerializer))]
Task<MyClass> FooAsync();
}
partial class MyAppService : IMyAppService
{
public async Task<MyClass> FooAsync(MyClass myClass)
{
return null;
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(new global::MyClassSerializer(), this.FooAsync, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
[TestMethod]
public void Extension_MethodWithCancellationToken()
{
string source = """
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
[AppService("MyAppService")]
interface IMyAppService
{
Task FooAsync(CancellationToken token);
}
partial class MyAppService : IMyAppService
{
public async Task FooAsync(CancellationToken token)
{
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(async parameters =>
{
parameters.GetCancellationToken(out global::System.Threading.CancellationToken token);
await this.FooAsync(token);
}, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
[TestMethod]
public void Extension_MethodWithProgress()
{
string source = """
using System;
using System.Threading.Tasks;
using CommunityToolkit.AppServices;
[AppService("MyAppService")]
interface IMyAppService
{
Task FooAsync(IProgress<string> progress);
}
partial class MyAppService : IMyAppService
{
public async Task FooAsync(IProgress<string> progress)
{
}
}
""";
string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
partial class MyAppService : global::CommunityToolkit.AppServices.AppServiceComponent
{
/// <summary>Creates a new <see cref="MyAppService"/> instance.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.AppServices.SourceGenerators.AppServiceGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public MyAppService() : base("MyAppService")
{
base.RegisterEndpoint(async parameters =>
{
parameters.GetProgress(out global::System.IProgress<string> progress);
await this.FooAsync(progress);
}, "FooAsync");
}
}
""";
VerifyGenerateSources<AppServiceGenerator>(source, OutputKind.ConsoleApplication, ("MyAppService.g.cs", result));
}
/// <summary>
/// Generates the requested sources
/// </summary>