Skip to content

Commit

Permalink
Added source parsing to the generator driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Mar 25, 2024
1 parent af94253 commit a33c262
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/SourceGeneratorTestHelpers/IncrementalGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public static GeneratorDriverRunResult Run<T>(
)
where T : IIncrementalGenerator, new()
{
var generator = new T();

var driver = CSharpGeneratorDriver.Create(generator);
var generators = GetGenerators<T>().Select(x => x.AsSourceGenerator());
var driver = CSharpGeneratorDriver.Create(generators, null, cSharpParseOptions);

var compilation = CSharpCompilation.Create(
nameof(SourceGeneratorTestHelpers),
Expand Down Expand Up @@ -52,9 +51,8 @@ public static GeneratorDriverRunResult Run<T>(
)
where T : IIncrementalGenerator, new()
{
var generator = new T();

var driver = CSharpGeneratorDriver.Create(generator);
var generators = GetGenerators<T>().Select(x => x.AsSourceGenerator());
var driver = CSharpGeneratorDriver.Create(generators, null, cSharpParseOptions);

var syntaxTrees = sources.Select(source => CSharpSyntaxTree.ParseText(source, cSharpParseOptions)).ToArray();

Expand All @@ -69,4 +67,10 @@ public static GeneratorDriverRunResult Run<T>(

return runResult;
}

private static IEnumerable<T> GetGenerators<T>()
where T : IIncrementalGenerator, new()
{
yield return new T();
}
}
16 changes: 10 additions & 6 deletions src/SourceGeneratorTestHelpers/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public static GeneratorDriverRunResult Run<T>(
)
where T : ISourceGenerator, new()
{
var generator = new T();

var driver = CSharpGeneratorDriver.Create(generator);
var generators = GetGenerators<T>().Select(x => (ISourceGenerator)x);
var driver = CSharpGeneratorDriver.Create(generators, null, cSharpParseOptions);

var compilation = CSharpCompilation.Create(
nameof(SourceGeneratorTestHelpers),
Expand Down Expand Up @@ -52,9 +51,8 @@ public static GeneratorDriverRunResult Run<T>(
)
where T : ISourceGenerator, new()
{
var generator = new T();

var driver = CSharpGeneratorDriver.Create(generator);
var generators = GetGenerators<T>().Select(x => (ISourceGenerator)x);
var driver = CSharpGeneratorDriver.Create(generators, null, cSharpParseOptions);

var syntaxTrees = sources.Select(source => CSharpSyntaxTree.ParseText(source, cSharpParseOptions)).ToArray();

Expand All @@ -69,4 +67,10 @@ public static GeneratorDriverRunResult Run<T>(

return runResult;
}

private static IEnumerable<T> GetGenerators<T>()
where T : ISourceGenerator, new()
{
yield return new T();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<RootNamespace>SourceGeneratorTestHelpers</RootNamespace>
<LangVersion>latest</LangVersion>
<Version>8.0.11</Version>
<Version>8.0.12</Version>
<Title>SourceGeneratorTestHelpers</Title>
<Authors>Jean-Sebastien Carle</Authors>
<Description>Test helpers and extension methods to simplify testing of .NET source generators.</Description>
Expand All @@ -18,8 +18,8 @@
<RepositoryUrl>https://github.com/jscarle/SourceGeneratorTestHelpers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>testing source-generators</PackageTags>
<AssemblyVersion>8.0.11.0</AssemblyVersion>
<FileVersion>8.0.11.0</FileVersion>
<AssemblyVersion>8.0.12.0</AssemblyVersion>
<FileVersion>8.0.12.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down

0 comments on commit a33c262

Please sign in to comment.