Skip to content

Instantly share code, notes, and snippets.

@JKamsker
Created July 26, 2024 12:48
Show Gist options
  • Save JKamsker/d3dff82803e54b89da7b9dfe469e7ead to your computer and use it in GitHub Desktop.
Save JKamsker/d3dff82803e54b89da7b9dfe469e7ead to your computer and use it in GitHub Desktop.
Generatorunittest.cs
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using SourceGeneratorTestHelpers;
namespace Generator.Equals.DynamicGenerationTests;
public class UnitTest1
{
[Fact]
public void Test1()
{
string input = """
using Generator.Equals;
namespace Tests;
[EquatableAttribute]
partial class MyRecord(
[property: OrderedEquality] string[] Fruits
);
""";
var references = new List<MetadataReference>()
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(EquatableAttribute).Assembly.Location),
};
// add netstandard
references.Add(MetadataReference.CreateFromFile(typeof(System.Runtime.GCSettings).Assembly.Location));
var result = IncrementalGenerator.Run<EqualsGenerator>
(
input,
new Microsoft.CodeAnalysis.CSharp.CSharpParseOptions()
{
},
references
);
var gensource = result.Results
.SelectMany(x => x.GeneratedSources)
.Select(x => x.SourceText)
.ToList()
;
Assert.NotNull(gensource);
//var genSourceString = gensource.ToString();
//// Assert in the same namespace
//Assert.Contains("namespace Tests", genSourceString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment