-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
340 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,3 +396,6 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
|
||
# Verify | ||
*.received.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
test/Backgrounder.Generators.Tests/BackgroundGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using System.Collections.Immutable; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace Backgrounder.Generators.Tests; | ||
|
||
[UsesVerify] | ||
public class BackgroundGeneratorTests | ||
{ | ||
[Fact] | ||
public Task GenerateSingleParameter() | ||
{ | ||
var source = @" | ||
using Backgrounder; | ||
namespace Backgrounder.Sample; | ||
public interface ISampleJob | ||
{ | ||
Task DoWork(int? jobId); | ||
} | ||
public class SampleJob : ISampleJob | ||
{ | ||
[BackgroundOperation] | ||
public Task DoWork(int? jobId) => Task.FromResult(jobId); | ||
} | ||
"; | ||
|
||
var (diagnostics, output) = GetGeneratedOutput<BackgroundGenerator>(source); | ||
|
||
diagnostics.Should().BeEmpty(); | ||
|
||
return Verifier | ||
.Verify(output) | ||
.UseDirectory("Snapshots") | ||
.ScrubLinesContaining("GeneratedCodeAttribute"); | ||
} | ||
|
||
[Fact] | ||
public Task GenerateMultipleParameter() | ||
{ | ||
var source = @" | ||
using Backgrounder; | ||
namespace Backgrounder.Sample; | ||
public interface ISampleJob | ||
{ | ||
Task DoWork(int jobId, string? name); | ||
} | ||
public class SampleJob : ISampleJob | ||
{ | ||
[BackgroundOperation<ISampleJob>] | ||
public Task DoWork(int jobId, string? name) => Task.FromResult(jobId); | ||
} | ||
"; | ||
|
||
var (diagnostics, output) = GetGeneratedOutput<BackgroundGenerator>(source); | ||
|
||
diagnostics.Should().BeEmpty(); | ||
|
||
return Verifier | ||
.Verify(output) | ||
.UseDirectory("Snapshots") | ||
.ScrubLinesContaining("GeneratedCodeAttribute"); | ||
} | ||
|
||
[Fact] | ||
public Task GenerateStaticMethod() | ||
{ | ||
var source = @" | ||
using Backgrounder; | ||
namespace Backgrounder.Sample; | ||
public class SampleJob | ||
{ | ||
[BackgroundOperation] | ||
public static Task StaticWork(int jobId) | ||
{ | ||
return Task.FromResult(jobId); | ||
} | ||
} | ||
"; | ||
|
||
var (diagnostics, output) = GetGeneratedOutput<BackgroundGenerator>(source); | ||
|
||
diagnostics.Should().BeEmpty(); | ||
|
||
return Verifier | ||
.Verify(output) | ||
.UseDirectory("Snapshots") | ||
.ScrubLinesContaining("GeneratedCodeAttribute"); | ||
} | ||
|
||
[Fact] | ||
public Task GenerateComplexParameter() | ||
{ | ||
var source = @" | ||
using Backgrounder; | ||
namespace Backgrounder.Sample; | ||
public interface ISampleJob | ||
{ | ||
void CheckPerson(Person person); | ||
} | ||
public class SampleJob : ISampleJob | ||
{ | ||
[BackgroundOperation] | ||
public void CheckPerson(Person person) { } | ||
} | ||
public class Person | ||
{ | ||
public string? Name { get; set; } | ||
public string? Email { get; set; } | ||
} | ||
"; | ||
|
||
var (diagnostics, output) = GetGeneratedOutput<BackgroundGenerator>(source); | ||
|
||
diagnostics.Should().BeEmpty(); | ||
|
||
return Verifier | ||
.Verify(output) | ||
.UseDirectory("Snapshots") | ||
.ScrubLinesContaining("GeneratedCodeAttribute"); | ||
} | ||
|
||
public static (ImmutableArray<Diagnostic> Diagnostics, string Output) GetGeneratedOutput<T>(string source) | ||
where T : IIncrementalGenerator, new() | ||
{ | ||
var syntaxTree = CSharpSyntaxTree.ParseText(source); | ||
var references = AppDomain.CurrentDomain.GetAssemblies() | ||
.Where(_ => !_.IsDynamic && !string.IsNullOrWhiteSpace(_.Location)) | ||
.Select(_ => MetadataReference.CreateFromFile(_.Location)) | ||
.Concat(new[] | ||
{ | ||
MetadataReference.CreateFromFile(typeof(T).Assembly.Location), | ||
MetadataReference.CreateFromFile(typeof(BackgroundOperationAttribute).Assembly.Location), | ||
}); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
"generator", | ||
new[] { syntaxTree }, | ||
references, | ||
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
||
var originalTreeCount = compilation.SyntaxTrees.Length; | ||
var generator = new T(); | ||
|
||
var driver = CSharpGeneratorDriver.Create(generator); | ||
driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out var diagnostics); | ||
|
||
var trees = outputCompilation.SyntaxTrees.ToList(); | ||
|
||
return (diagnostics, trees.Count != originalTreeCount ? trees[trees.Count - 1].ToString() : string.Empty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Backgrounder.Generators.Tests; | ||
|
||
public static class ModuleInitialization | ||
{ | ||
[ModuleInitializer] | ||
public static void Initialize() => VerifySourceGenerators.Initialize(); | ||
} |
38 changes: 38 additions & 0 deletions
38
...nder.Generators.Tests/Snapshots/BackgroundGeneratorTests.RunComplexParameter.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// <auto-generated /> | ||
#nullable enable | ||
|
||
namespace Backgrounder.Sample | ||
{ | ||
/// <summary> | ||
/// Extension methods for Backgrounder | ||
/// </summary> | ||
public static partial class SampleJobCheckPersonExtensions | ||
{ | ||
public record CheckPersonMessageDAADBCDB(Backgrounder.Sample.Person person); | ||
|
||
/// <summary> | ||
/// Enqueue the method Backgrounder.Sample.SampleJob.CheckPerson(Backgrounder.Sample.Person) for execution in the background | ||
/// </summary> | ||
public static async global::System.Threading.Tasks.Task CheckPerson(this global::Backgrounder.IBackgrounder backgrounder, Backgrounder.Sample.Person person) | ||
{ | ||
var methodParameters = new CheckPersonMessageDAADBCDB(person); | ||
await backgrounder.EnqueueAsync("Backgrounder.Sample.SampleJob.CheckPerson(Backgrounder.Sample.Person)", methodParameters); | ||
} | ||
|
||
internal static global::System.Threading.Tasks.Task ExecuteCheckPersonDAADBCDB(global::System.IServiceProvider serviceProvider, byte[] messageBody) | ||
{ | ||
var messageSerializer = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::Backgrounder.IMessageSerializer>(serviceProvider); | ||
var methodParameters = messageSerializer.Deserialize<CheckPersonMessageDAADBCDB>(messageBody); | ||
var worker = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<SampleJob>(serviceProvider); | ||
worker.CheckPerson(methodParameters.person); | ||
return global::System.Threading.Tasks.Task.CompletedTask; | ||
} | ||
|
||
[global::System.Runtime.CompilerServices.ModuleInitializerAttribute] | ||
internal static void RegisterCheckPersonDAADBCDB() | ||
{ | ||
BackgroundRouter.Register("Backgrounder.Sample.SampleJob.CheckPerson(Backgrounder.Sample.Person)", Backgrounder.Sample.SampleJobCheckPersonExtensions.ExecuteCheckPersonDAADBCDB); | ||
} | ||
|
||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...der.Generators.Tests/Snapshots/BackgroundGeneratorTests.RunMultipleParameter.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// <auto-generated /> | ||
#nullable enable | ||
|
||
namespace Backgrounder.Sample | ||
{ | ||
/// <summary> | ||
/// Extension methods for Backgrounder | ||
/// </summary> | ||
public static partial class SampleJobDoWorkExtensions | ||
{ | ||
public record DoWorkMessage5A94B72F(int jobId, string? name); | ||
|
||
/// <summary> | ||
/// Enqueue the method Backgrounder.Sample.SampleJob.DoWork(int, string?) for execution in the background | ||
/// </summary> | ||
public static async global::System.Threading.Tasks.Task DoWork(this global::Backgrounder.IBackgrounder backgrounder, int jobId, string? name) | ||
{ | ||
var methodParameters = new DoWorkMessage5A94B72F(jobId, name); | ||
await backgrounder.EnqueueAsync("Backgrounder.Sample.SampleJob.DoWork(int, string?)", methodParameters); | ||
} | ||
|
||
internal static global::System.Threading.Tasks.Task ExecuteDoWork5A94B72F(global::System.IServiceProvider serviceProvider, byte[] messageBody) | ||
{ | ||
var messageSerializer = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::Backgrounder.IMessageSerializer>(serviceProvider); | ||
var methodParameters = messageSerializer.Deserialize<DoWorkMessage5A94B72F>(messageBody); | ||
var worker = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::Backgrounder.Sample.ISampleJob>(serviceProvider); | ||
worker.DoWork(methodParameters.jobId, methodParameters.name); | ||
return global::System.Threading.Tasks.Task.CompletedTask; | ||
} | ||
|
||
[global::System.Runtime.CompilerServices.ModuleInitializerAttribute] | ||
internal static void RegisterDoWork5A94B72F() | ||
{ | ||
BackgroundRouter.Register("Backgrounder.Sample.SampleJob.DoWork(int, string?)", Backgrounder.Sample.SampleJobDoWorkExtensions.ExecuteDoWork5A94B72F); | ||
} | ||
|
||
} | ||
} |
1 change: 1 addition & 0 deletions
1
.../Backgrounder.Generators.Tests/Snapshots/BackgroundGeneratorTests.RunResults.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
38 changes: 38 additions & 0 deletions
38
...under.Generators.Tests/Snapshots/BackgroundGeneratorTests.RunSingleParameter.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// <auto-generated /> | ||
#nullable enable | ||
|
||
namespace Backgrounder.Sample | ||
{ | ||
/// <summary> | ||
/// Extension methods for Backgrounder | ||
/// </summary> | ||
public static partial class SampleJobDoWorkExtensions | ||
{ | ||
public record DoWorkMessage3299E5EA(int? jobId); | ||
|
||
/// <summary> | ||
/// Enqueue the method Backgrounder.Sample.SampleJob.DoWork(int?) for execution in the background | ||
/// </summary> | ||
public static async global::System.Threading.Tasks.Task DoWork(this global::Backgrounder.IBackgrounder backgrounder, int? jobId) | ||
{ | ||
var methodParameters = new DoWorkMessage3299E5EA(jobId); | ||
await backgrounder.EnqueueAsync("Backgrounder.Sample.SampleJob.DoWork(int?)", methodParameters); | ||
} | ||
|
||
internal static global::System.Threading.Tasks.Task ExecuteDoWork3299E5EA(global::System.IServiceProvider serviceProvider, byte[] messageBody) | ||
{ | ||
var messageSerializer = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::Backgrounder.IMessageSerializer>(serviceProvider); | ||
var methodParameters = messageSerializer.Deserialize<DoWorkMessage3299E5EA>(messageBody); | ||
var worker = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<SampleJob>(serviceProvider); | ||
worker.DoWork(methodParameters.jobId); | ||
return global::System.Threading.Tasks.Task.CompletedTask; | ||
} | ||
|
||
[global::System.Runtime.CompilerServices.ModuleInitializerAttribute] | ||
internal static void RegisterDoWork3299E5EA() | ||
{ | ||
BackgroundRouter.Register("Backgrounder.Sample.SampleJob.DoWork(int?)", Backgrounder.Sample.SampleJobDoWorkExtensions.ExecuteDoWork3299E5EA); | ||
} | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...grounder.Generators.Tests/Snapshots/BackgroundGeneratorTests.RunStaticMethod.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// <auto-generated /> | ||
#nullable enable | ||
|
||
namespace Backgrounder.Sample | ||
{ | ||
/// <summary> | ||
/// Extension methods for Backgrounder | ||
/// </summary> | ||
public static partial class SampleJobStaticWorkExtensions | ||
{ | ||
public record StaticWorkMessage34AA9206(int jobId); | ||
|
||
/// <summary> | ||
/// Enqueue the method Backgrounder.Sample.SampleJob.StaticWork(int) for execution in the background | ||
/// </summary> | ||
public static async global::System.Threading.Tasks.Task StaticWork(this global::Backgrounder.IBackgrounder backgrounder, int jobId) | ||
{ | ||
var methodParameters = new StaticWorkMessage34AA9206(jobId); | ||
await backgrounder.EnqueueAsync("Backgrounder.Sample.SampleJob.StaticWork(int)", methodParameters); | ||
} | ||
|
||
internal static global::System.Threading.Tasks.Task ExecuteStaticWork34AA9206(global::System.IServiceProvider serviceProvider, byte[] messageBody) | ||
{ | ||
var messageSerializer = global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::Backgrounder.IMessageSerializer>(serviceProvider); | ||
var methodParameters = messageSerializer.Deserialize<StaticWorkMessage34AA9206>(messageBody); | ||
Backgrounder.Sample.SampleJob.StaticWork(methodParameters.jobId); | ||
return global::System.Threading.Tasks.Task.CompletedTask; | ||
} | ||
|
||
[global::System.Runtime.CompilerServices.ModuleInitializerAttribute] | ||
internal static void RegisterStaticWork34AA9206() | ||
{ | ||
BackgroundRouter.Register("Backgrounder.Sample.SampleJob.StaticWork(int)", Backgrounder.Sample.SampleJobStaticWorkExtensions.ExecuteStaticWork34AA9206); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.