Skip to content

Commit e4b7b0d

Browse files
committed
refactoring Source Generator unit-test
1 parent 471a40b commit e4b7b0d

19 files changed

+61
-86
lines changed

tests/ConsoleAppFramework.GeneratorTests/ArgumentParserTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
namespace ConsoleAppFramework.GeneratorTests;
55

6-
public class ArgumentParserTest
6+
[ClassDataSource<VerifyHelper>]
7+
public class ArgumentParserTest(VerifyHelper verifier)
78
{
8-
readonly VerifyHelper verifier = new("CAF");
9-
109
[Test]
1110
public async Task Lamda()
1211
{

tests/ConsoleAppFramework.GeneratorTests/ArrayParseTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class ArrayParseTest
3+
[ClassDataSource<VerifyHelper>]
4+
public class ArrayParseTest(VerifyHelper verifier)
45
{
5-
VerifyHelper verifier = new VerifyHelper("CAF");
6-
76
[Test]
87
public async Task Params()
98
{

tests/ConsoleAppFramework.GeneratorTests/BuildCustomDelegateTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
namespace ConsoleAppFramework.GeneratorTests;
88

9-
public class BuildCustomDelegateTest
9+
[ClassDataSource<VerifyHelper>]
10+
public class BuildCustomDelegateTest(VerifyHelper verifier)
1011
{
11-
VerifyHelper verifier = new VerifyHelper("CAF");
12-
1312
[Test]
1413
public async Task Run()
1514
{

tests/ConsoleAppFramework.GeneratorTests/CSharpGeneratorRunner.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ConsoleAppFramework;
1+
using Basic.Reference.Assemblies;
2+
using ConsoleAppFramework;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.Diagnostics;
@@ -8,12 +9,11 @@
89
using System.Runtime.CompilerServices;
910
using System.Runtime.Loader;
1011

11-
public static class CSharpGeneratorRunner
12+
public class CSharpGeneratorRunner
1213
{
13-
static Compilation baseCompilation = default!;
14+
Compilation baseCompilation = default!;
1415

15-
[ModuleInitializer]
16-
public static void InitializeCompilation()
16+
public CSharpGeneratorRunner()
1717
{
1818
var globalUsings = """
1919
global using System;
@@ -23,25 +23,15 @@ public static void InitializeCompilation()
2323
global using ConsoleAppFramework;
2424
""";
2525

26-
var references = AppDomain.CurrentDomain.GetAssemblies()
27-
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location))
28-
.Select(x => MetadataReference.CreateFromFile(x.Location))
29-
.Concat([
30-
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location), // System.Console.dll
31-
MetadataReference.CreateFromFile(typeof(IServiceProvider).Assembly.Location), // System.ComponentModel.dll
32-
MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.RequiredAttribute).Assembly.Location), // System.ComponentModel.DataAnnotations
33-
MetadataReference.CreateFromFile(typeof(System.Text.Json.JsonDocument).Assembly.Location), // System.Text.Json.dll
34-
]);
35-
3626
var compilation = CSharpCompilation.Create("generatortest",
37-
references: references,
27+
references: ReferenceAssemblies.Net80, // .NET 8
3828
syntaxTrees: [CSharpSyntaxTree.ParseText(globalUsings, path: "GlobalUsings.cs")],
3929
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication, allowUnsafe: true)); // .exe
4030

4131
baseCompilation = compilation;
4232
}
4333

44-
public static (Compilation, ImmutableArray<Diagnostic>) RunGenerator([StringSyntax("C#-test")] string source, string[]? preprocessorSymbols = null, AnalyzerConfigOptionsProvider? options = null)
34+
public (Compilation, ImmutableArray<Diagnostic>) RunGenerator([StringSyntax("C#-test")] string source, string[]? preprocessorSymbols = null, AnalyzerConfigOptionsProvider? options = null)
4535
{
4636
if (preprocessorSymbols == null)
4737
{
@@ -91,7 +81,7 @@ public static class Environment
9181
return (newCompilation, diagnostics);
9282
}
9383

94-
public static (Compilation Compilation, ImmutableArray<Diagnostic> Diagnostics, string Stdout, int ExitCode) CompileAndExecute(string source, string[] args, string[]? preprocessorSymbols = null, AnalyzerConfigOptionsProvider? options = null)
84+
public (Compilation Compilation, ImmutableArray<Diagnostic> Diagnostics, string Stdout, int ExitCode) CompileAndExecute(string source, string[] args, string[]? preprocessorSymbols = null, AnalyzerConfigOptionsProvider? options = null)
9585
{
9686
var (compilation, diagnostics) = RunGenerator(source, preprocessorSymbols, options);
9787

@@ -121,7 +111,7 @@ public static (Compilation Compilation, ImmutableArray<Diagnostic> Diagnostics,
121111
return (compilation, diagnostics, stringWriter.ToString(), exitCode);
122112
}
123113

124-
public static (string Key, string Reasons)[][] GetIncrementalGeneratorTrackedStepsReasons(string keyPrefixFilter, params string[] sources)
114+
public (string Key, string Reasons)[][] GetIncrementalGeneratorTrackedStepsReasons(string keyPrefixFilter, params string[] sources)
125115
{
126116
var parseOptions = new CSharpParseOptions(LanguageVersion.CSharp13); // 13
127117
var driver = CSharpGeneratorDriver.Create(
@@ -167,8 +157,13 @@ [new ConsoleAppGenerator().AsSourceGenerator()],
167157
}
168158
}
169159

170-
public class VerifyHelper(string idPrefix)
160+
public class VerifyHelper
171161
{
162+
readonly string idPrefix = "CAF";
163+
164+
[ClassDataSource<CSharpGeneratorRunner>(Shared = SharedType.PerTestSession)]
165+
public required CSharpGeneratorRunner CSharpGeneratorRunner { get; init; }
166+
172167
public async Task Ok([StringSyntax("C#-test")] string code, [CallerArgumentExpression("code")] string? codeExpr = null)
173168
{
174169
Console.WriteLine(codeExpr!);

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class ConsoleAppBuilderTest
3+
[ClassDataSource<VerifyHelper>]
4+
public class ConsoleAppBuilderTest(VerifyHelper verifier)
45
{
5-
VerifyHelper verifier = new VerifyHelper("CAF");
6-
76
[Test]
87
public async Task BuilderRun()
98
{

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppContextTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class ConsoleAppContextTest
3+
[ClassDataSource<VerifyHelper>]
4+
public class ConsoleAppContextTest(VerifyHelper verifier)
45
{
5-
VerifyHelper verifier = new VerifyHelper("CAF");
6-
76
[Test]
87
public async Task ForLambda()
98
{

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppFramework.GeneratorTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="Basic.Reference.Assemblies" Version="1.8.4" />
1314
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
1415
<PackageReference Include="TUnit" Version="1.2.11" />
1516
</ItemGroup>

tests/ConsoleAppFramework.GeneratorTests/DITest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class DITest
3+
[ClassDataSource<VerifyHelper>]
4+
public class DITest(VerifyHelper verifier)
45
{
5-
VerifyHelper verifier = new VerifyHelper("CAF");
6-
76
[Test]
87
public async Task ServiceProvider()
98
{

tests/ConsoleAppFramework.GeneratorTests/DiagnosticsTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class DiagnosticsTest
3+
[ClassDataSource<VerifyHelper>]
4+
public class DiagnosticsTest(VerifyHelper verifier)
45
{
5-
VerifyHelper verifier = new VerifyHelper("CAF");
6-
76
[Test]
87
public async Task ArgumentCount()
98
{

tests/ConsoleAppFramework.GeneratorTests/FilterTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace ConsoleAppFramework.GeneratorTests;
22

3-
public class FilterTest
3+
[ClassDataSource<VerifyHelper>]
4+
public class FilterTest(VerifyHelper verifier)
45
{
5-
VerifyHelper verifier = new VerifyHelper("CAF");
6-
76
[Test]
87
public async Task ForLambda()
98
{

0 commit comments

Comments
 (0)