PacketMediator/PacketMediator.Generator/PacketMediator.Generator.Tests/SourceGeneratorWithAdditionalFilesTests.cs

48 lines
1.7 KiB
C#
Raw Normal View History

2025-01-17 09:53:44 +01:00
using System.Collections.Immutable;
2024-08-07 19:06:55 +02:00
using System.IO;
using System.Linq;
2025-01-17 09:53:44 +01:00
using Microsoft.CodeAnalysis;
using PacketMediator.Generator.Tests.Utils;
2024-08-07 19:06:55 +02:00
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
2025-01-17 09:53:44 +01:00
namespace PacketMediator.Generator.Tests;
2024-08-07 19:06:55 +02:00
2025-01-17 09:53:44 +01:00
public class SourceGeneratorWithAdditionalFilesTests
2024-08-07 19:06:55 +02:00
{
private const string DddRegistryText = @"User
Document
Customer";
[Fact]
public void GenerateClassesBasedOnDDDRegistry()
{
// Create an instance of the source generator.
2025-01-17 09:53:44 +01:00
var generator = new SourceGeneratorWithAdditionalFiles();
2024-08-07 19:06:55 +02:00
// Source generators should be tested using 'GeneratorDriver'.
2025-01-17 09:53:44 +01:00
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);
// Add the additional file separately from the compilation.
driver = driver.AddAdditionalTexts(
ImmutableArray.Create<AdditionalText>(
2024-08-07 19:06:55 +02:00
new TestAdditionalFile("./DDD.UbiquitousLanguageRegistry.txt", DddRegistryText)
2025-01-17 09:53:44 +01:00
)
2024-08-07 19:06:55 +02:00
);
// To run generators, we can use an empty compilation.
2025-01-17 09:53:44 +01:00
var compilation = CSharpCompilation.Create(nameof(SourceGeneratorWithAdditionalFilesTests));
2024-08-07 19:06:55 +02:00
// Run generators. Don't forget to use the new compilation rather than the previous one.
driver.RunGeneratorsAndUpdateCompilation(compilation, out var newCompilation, out _);
// Retrieve all files in the compilation.
var generatedFiles = newCompilation.SyntaxTrees
.Select(t => Path.GetFileName(t.FilePath))
.ToArray();
// In this case, it is enough to check the file name.
Assert.Equivalent(new[] { "User.g.cs", "Document.g.cs", "Customer.g.cs" }, generatedFiles);
}
}