SourceGen started

This commit is contained in:
ThomasCharester committed 2026-01-20 16:59:57 +03:00
1 parent a3cd3e86a1
commit a409054750
274 files changed
+14862 -3

No files matched your search

@@ -0,0 +1,67 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace InfraGens.Tests;
public class SampleIncrementalSourceGeneratorTests
{
private const string VectorClassText = @"
namespace TestNamespace;
[Generators.Report]
public partial class Vector3
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
}";
private const string ExpectedGeneratedClassText = @"// <auto-generated/>
using System;
using System.Collections.Generic;
namespace TestNamespace;
partial class Vector3
{
public IEnumerable<string> Report()
{
yield return $""X:{this.X}"";
yield return $""Y:{this.Y}"";
yield return $""Z:{this.Z}"";
}
}
";
[Fact]
public void GenerateReportMethod()
{
// Create an instance of the source generator.
var generator = new SampleIncrementalSourceGenerator();
// Source generators should be tested using 'GeneratorDriver'.
var driver = CSharpGeneratorDriver.Create(generator);
// We need to create a compilation with the required source code.
var compilation = CSharpCompilation.Create(nameof(SampleSourceGeneratorTests),
new[] { CSharpSyntaxTree.ParseText(VectorClassText) },
new[]
{
// To support 'System.Attribute' inheritance, add reference to 'System.Private.CoreLib'.
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
});
// Run generators and retrieve all results.
var runResult = driver.RunGenerators(compilation).GetRunResult();
// All generated files can be found in 'RunResults.GeneratedTrees'.
var generatedFileSyntax = runResult.GeneratedTrees.Single(t => t.FilePath.EndsWith("Vector3.g.cs"));
// Complex generators should be tested using text comparison.
Assert.Equal(ExpectedGeneratedClassText, generatedFileSyntax.GetText().ToString(),
ignoreLineEndingDifferences: true);
}
}
@@ -0,0 +1,48 @@
using System.IO;
using System.Linq;
using InfraGens.Tests.Utils;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
namespace InfraGens.Tests;
public class SampleSourceGeneratorTests
{
private const string DddRegistryText = @"User
Document
Customer";
[Fact]
public void GenerateClassesBasedOnDDDRegistry()
{
// Create an instance of the source generator.
var generator = new SampleSourceGenerator();
// Source generators should be tested using 'GeneratorDriver'.
var driver = CSharpGeneratorDriver.Create(new[] { generator },
new[]
{
// Add the additional file separately from the compilation.
new TestAdditionalFile("./DDD.UbiquitousLanguageRegistry.txt", DddRegistryText)
});
// To run generators, we can use an empty compilation.
var compilation = CSharpCompilation.Create(nameof(SampleSourceGeneratorTests));
// 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);
}
}
@@ -0,0 +1,20 @@
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
namespace InfraGens.Tests.Utils;
public class TestAdditionalFile : AdditionalText
{
private readonly SourceText _text;
public TestAdditionalFile(string path, string text)
{
Path = path;
_text = SourceText.From(text);
}
public override SourceText GetText(CancellationToken cancellationToken = new()) => _text;
public override string Path { get; }
}
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
Loaded 100 of 274 files, more files were not shown because too many files have changed in this diff. Show more