Skip to content

Commit

Permalink
Added IEnumerable<string> overloads to the Run methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jscarle committed Feb 21, 2024
1 parent 12e889e commit b73d839
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/SourceGeneratorTestHelpers/GeneratedSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ public sealed class GeneratedSource : IEquatable<GeneratedSource>
/// <summary>Gets the path to the generated source file.</summary>
public string FilePath { get; }

/// <summary>Gets the source code of the generated file.</summary>
/// <summary>Gets the source of the generated file.</summary>
public string Source { get; }

/// <summary>Initializes a new instance of the <see cref="GeneratedSource" /> class.</summary>
/// <param name="filePath">The path to the generated source file.</param>
/// <param name="source">The source code of the generated file.</param>
/// <param name="source">The source of the generated file.</param>
public GeneratedSource(string filePath, string source)
{
FilePath = filePath;
Expand Down
26 changes: 23 additions & 3 deletions src/SourceGeneratorTestHelpers/IncrementalGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace SourceGeneratorTestHelpers;
/// <summary>Provides helper methods for testing <see cref="IIncrementalGenerator" /> implementations.</summary>
public static class IncrementalGenerator
{
/// <summary>Executes a specified <see cref="IIncrementalGenerator" /> against the provided source code within a testing environment.</summary>
/// <summary>Executes a specified <see cref="IIncrementalGenerator" /> against the provided source within a testing environment.</summary>
/// <typeparam name="T">The type of <see cref="IIncrementalGenerator" /> to execute.</typeparam>
/// <param name="source">The source code to be analyzed and processed by the <see cref="IIncrementalGenerator" />.</param>
/// <param name="source">The source to be analyzed and processed by the <see cref="IIncrementalGenerator" />.</param>
/// <returns>The results of the <see cref="IIncrementalGenerator" /> execution.</returns>
public static GeneratorDriverRunResult Run<T>(string source)
where T : IIncrementalGenerator, new()
Expand All @@ -23,4 +23,24 @@ public static GeneratorDriverRunResult Run<T>(string source)

return runResult;
}
}

/// <summary>Executes a specified <see cref="IIncrementalGenerator" /> against the provided sources within a testing environment.</summary>
/// <typeparam name="T">The type of <see cref="IIncrementalGenerator" /> to execute.</typeparam>
/// <param name="sources">The sources to be analyzed and processed by the <see cref="IIncrementalGenerator" />.</param>
/// <returns>The results of the <see cref="IIncrementalGenerator" /> execution.</returns>
public static GeneratorDriverRunResult Run<T>(IEnumerable<string> sources)
where T : IIncrementalGenerator, new()
{
var generator = new T();

var driver = CSharpGeneratorDriver.Create(generator);

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

var compilation = CSharpCompilation.Create(nameof(SourceGeneratorTestHelpers), syntaxTrees, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) });

var runResult = driver.RunGenerators(compilation).GetRunResult();

return runResult;
}
}
36 changes: 23 additions & 13 deletions src/SourceGeneratorTestHelpers/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace SourceGeneratorTestHelpers;
/// <summary>Provides helper methods for testing <see cref="ISourceGenerator" /> implementations.</summary>
public static class SourceGenerator
{
/// <summary>Executes a specified <see cref="ISourceGenerator" /> against the provided source code within a testing environment.</summary>
/// <summary>Executes a specified <see cref="ISourceGenerator" /> against the provided source within a testing environment.</summary>
/// <typeparam name="T">The type of <see cref="ISourceGenerator" /> to execute.</typeparam>
/// <param name="source">The source code to be analyzed and processed by the <see cref="ISourceGenerator" />.</param>
/// <param name="source">The source to be analyzed and processed by the <see cref="ISourceGenerator" />.</param>
/// <returns>The results of the <see cref="ISourceGenerator" /> execution.</returns>
public static GeneratorDriverRunResult Run<T>(string source)
where T : ISourceGenerator, new()
Expand All @@ -17,17 +17,27 @@ public static GeneratorDriverRunResult Run<T>(string source)

var driver = CSharpGeneratorDriver.Create(generator);

var compilation = CSharpCompilation.Create(
nameof(SourceGeneratorTestHelpers),
new[]
{
CSharpSyntaxTree.ParseText(source)
},
new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
}
);
var compilation = CSharpCompilation.Create(nameof(SourceGeneratorTestHelpers), new[] { CSharpSyntaxTree.ParseText(source) }, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) });

var runResult = driver.RunGenerators(compilation).GetRunResult();

return runResult;
}

/// <summary>Executes a specified <see cref="ISourceGenerator" /> against the provided source within a testing environment.</summary>
/// <typeparam name="T">The type of <see cref="ISourceGenerator" /> to execute.</typeparam>
/// <param name="sources">The sources to be analyzed and processed by the <see cref="ISourceGenerator" />.</param>
/// <returns>The results of the <see cref="ISourceGenerator" /> execution.</returns>
public static GeneratorDriverRunResult Run<T>(IEnumerable<string> sources)
where T : ISourceGenerator, new()
{
var generator = new T();

var driver = CSharpGeneratorDriver.Create(generator);

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

var compilation = CSharpCompilation.Create(nameof(SourceGeneratorTestHelpers), syntaxTrees, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) });

var runResult = driver.RunGenerators(compilation).GetRunResult();

Expand Down
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.0</Version>
<Version>8.0.1</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.0.0</AssemblyVersion>
<FileVersion>8.0.0.0</FileVersion>
<AssemblyVersion>8.0.1.0</AssemblyVersion>
<FileVersion>8.0.1.0</FileVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down

0 comments on commit b73d839

Please sign in to comment.