-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from ByronMayne/generator-host-unit-testing
Added support for unit testing the SGF Source Generators
- Loading branch information
Showing
7 changed files
with
159 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Sandbox/ConsoleApp.SourceGenerator.Tests/ConsoleApp.SourceGenerator.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>12</LangVersion> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ConsoleApp.SourceGenerator\ConsoleApp.SourceGenerator.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace ConsoleApp.SourceGenerator.Tests | ||
{ | ||
public class TestCase | ||
{ | ||
[Fact] | ||
public void Compiles() | ||
{ | ||
Compose(""" | ||
namespace MyNamespace | ||
{ | ||
public class MyClass | ||
{ | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
|
||
private void Compose(string source) | ||
{ | ||
// Create the generator, you can pass any parameters you want | ||
ConsoleAppSourceGenerator generator = new ConsoleAppSourceGenerator() | ||
{ | ||
WarningMessage = "I am running from a unit test!" // Change any settings you want | ||
}; | ||
// Create the 'host' which is the wrapper that is auto generated by SGF. | ||
ConsoleAppSourceGeneratorHoist host = new ConsoleAppSourceGeneratorHoist(generator); | ||
// Parse the source into syntax trees | ||
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); | ||
// Setup the compilation settings | ||
CSharpCompilation compilation = CSharpCompilation.Create( | ||
assemblyName: "UniTests", | ||
syntaxTrees: new[] { syntaxTree }); | ||
// Create the driver the executes the generator | ||
GeneratorDriver driver = CSharpGeneratorDriver.Create(host); | ||
// Run it | ||
driver = driver.RunGenerators(compilation); | ||
// Get the results | ||
GeneratorDriverRunResult results = driver.GetRunResult(); | ||
// Test the results | ||
Assert.NotEmpty(results.GeneratedTrees); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("ConsoleApp.SourceGenerator.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters