PacketMediator/PacketMediator.Generator/PacketMediator.Generator.Tests/SourceGeneratorWithAdditionalFilesTests.cs
2025-01-17 14:29:20 +01:00

38 lines
1.4 KiB
C#

using System.Collections.Immutable;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using PacketMediator.Generator.Tests.Utils;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace PacketMediator.Generator.Tests;
public class SourceGeneratorWithAdditionalFilesTests
{
private static readonly string[] _expected = ["PacketMediatorStatic.g.cs", "StructDictionary.g.cs"];
[Fact]
public void GenerateClassesBasedOnDDDRegistry()
{
// Create an instance of the source generator.
var generator = new PacketMediatorGenerator();
// Source generators should be tested using 'GeneratorDriver'.
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);
// To run generators, we can use an empty compilation.
var compilation = CSharpCompilation.Create(nameof(SourceGeneratorWithAdditionalFilesTests));
// 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(_expected, generatedFiles);
}
}