mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-21 15:27:43 +01:00
Add "enum cloning" source generator
This commit is contained in:
parent
55eb7e41d8
commit
8bb6cdd8d6
14 changed files with 395 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
|
||||
<RootNamespace>Dalamud.EnumGenerator.Tests</RootNamespace>
|
||||
|
||||
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.XUnit" Version="1.1.1"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2"/>
|
||||
<PackageReference Include="xunit" Version="2.4.2"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Dalamud.EnumGenerator\Dalamud.EnumGenerator.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Xunit;
|
||||
|
||||
namespace Dalamud.EnumGenerator.Tests;
|
||||
|
||||
public class EnumCloneMapTests
|
||||
{
|
||||
[Fact]
|
||||
public void ParseMappings_SimpleLines_ParsesCorrectly()
|
||||
{
|
||||
var text = @"# Comment line
|
||||
My.Namespace.Target = Other.Namespace.Source
|
||||
|
||||
Another.Target = Some.Source";
|
||||
|
||||
var results = Dalamud.EnumGenerator.EnumCloneGenerator.ParseMappings(text);
|
||||
|
||||
Assert.Equal(2, results.Length);
|
||||
Assert.Equal("My.Namespace.Target", results[0].TargetFullName);
|
||||
Assert.Equal("Other.Namespace.Source", results[0].SourceFullName);
|
||||
Assert.Equal("Another.Target", results[1].TargetFullName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Generator_ProducesFile_WhenSourceResolved()
|
||||
{
|
||||
// We'll create a compilation that contains a source enum type and add an AdditionalText mapping
|
||||
var sourceEnum = @"namespace Foo.Bar { public enum SourceEnum { A = 1, B = 2 } }";
|
||||
|
||||
var mapText = "GeneratedNs.TargetEnum = Foo.Bar.SourceEnum";
|
||||
|
||||
var generator = new EnumCloneGenerator();
|
||||
var driver = CSharpGeneratorDriver.Create(generator)
|
||||
.AddAdditionalTexts(ImmutableArray.Create<AdditionalText>(new Utils.TestAdditionalFile("EnumCloneMap.txt", mapText)));
|
||||
|
||||
var compilation = CSharpCompilation.Create("TestGen", [CSharpSyntaxTree.ParseText(sourceEnum)],
|
||||
[MetadataReference.CreateFromFile(typeof(object).Assembly.Location)]);
|
||||
|
||||
driver.RunGeneratorsAndUpdateCompilation(compilation, out var newCompilation, out var diagnostics);
|
||||
|
||||
var generated = newCompilation.SyntaxTrees.Select(t => t.FilePath).Where(p => p.EndsWith("TargetEnum.CloneEnum.g.cs")).ToArray();
|
||||
Assert.Single(generated);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
using System.Threading;
|
||||
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace Dalamud.EnumGenerator.Tests.Utils;
|
||||
|
||||
public class TestAdditionalFile : AdditionalText
|
||||
{
|
||||
private readonly SourceText text;
|
||||
|
||||
public TestAdditionalFile(string path, string text)
|
||||
{
|
||||
Path = path;
|
||||
this.text = SourceText.From(text);
|
||||
}
|
||||
|
||||
public override SourceText GetText(CancellationToken cancellationToken = new()) => this.text;
|
||||
|
||||
public override string Path { get; }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue