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

39 lines
1.4 KiB
C#
Raw Normal View History

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