@@ -8,9 +8,15 @@ namespace Immediate.Validations.Tests.GeneratorTests;
88
99public static class GeneratorTestHelper
1010{
11- public static GeneratorDriverRunResult RunGenerator ( [ StringSyntax ( "c#-test" ) ] string source )
11+ public static GeneratorDriverRunResult RunGenerator (
12+ [ StringSyntax ( "c#-test" ) ] string source ,
13+ params ReadOnlySpan < string > skippedSteps
14+ )
1215 {
13- var syntaxTree = CSharpSyntaxTree . ParseText ( source , cancellationToken : TestContext . Current . CancellationToken ) ;
16+ var syntaxTree = CSharpSyntaxTree . ParseText (
17+ source ,
18+ cancellationToken : TestContext . Current . CancellationToken
19+ ) ;
1420
1521 var compilation = CSharpCompilation . Create (
1622 assemblyName : "Tests" ,
@@ -29,24 +35,21 @@ public static GeneratorDriverRunResult RunGenerator([StringSyntax("c#-test")] st
2935 )
3036 ) ;
3137
32- var clone = compilation . Clone ( ) . AddSyntaxTrees ( CSharpSyntaxTree . ParseText ( "// dummy" , cancellationToken : TestContext . Current . CancellationToken ) ) ;
33-
3438 GeneratorDriver driver = CSharpGeneratorDriver . Create (
3539 generators : [ new ImmediateValidationsGenerator ( ) . AsSourceGenerator ( ) ] ,
3640 driverOptions : new GeneratorDriverOptions ( default , trackIncrementalGeneratorSteps : true )
3741 ) ;
3842
39- var result1 = RunGenerator ( ref driver , compilation ) ;
40- var result2 = RunGenerator ( ref driver , clone ) ;
43+ driver = RunGenerator ( driver , compilation ) ;
44+ var result = driver . GetRunResult ( ) ;
4145
42- foreach ( var ( _, step ) in result2 . Results [ 0 ] . TrackedOutputSteps )
43- AssertSteps ( step ) ;
46+ VerifyIncrementality ( driver , compilation , skippedSteps ) ;
4447
45- return result1 ;
48+ return result ;
4649 }
4750
48- private static GeneratorDriverRunResult RunGenerator (
49- ref GeneratorDriver driver ,
51+ private static GeneratorDriver RunGenerator (
52+ GeneratorDriver driver ,
5053 Compilation compilation
5154 )
5255 {
@@ -65,9 +68,67 @@ Compilation compilation
6568 ) ;
6669
6770 Assert . Empty ( diagnostics ) ;
68- return driver . GetRunResult ( ) ;
71+ return driver ;
72+ }
73+
74+ private static void VerifyIncrementality (
75+ GeneratorDriver driver ,
76+ Compilation compilation ,
77+ ReadOnlySpan < string > skippedSteps
78+ )
79+ {
80+ var clone = compilation . Clone ( ) . AddSyntaxTrees (
81+ CSharpSyntaxTree . ParseText (
82+ "// dummy" ,
83+ cancellationToken : TestContext . Current . CancellationToken
84+ )
85+ ) ;
86+
87+ driver = RunGenerator ( driver , clone ) ;
88+
89+ if (
90+ driver . GetRunResult ( ) is not
91+ {
92+ Results :
93+ [
94+ {
95+ TrackedOutputSteps : { } outputSteps ,
96+ TrackedSteps : { } trackedSteps ,
97+ }
98+ ] ,
99+ }
100+ )
101+ {
102+ Assert . Fail ( "Unable to verify incrementality." ) ;
103+ return ;
104+ }
105+
106+ foreach ( var ( _, step ) in outputSteps )
107+ AssertSteps ( step ) ;
108+
109+ foreach ( var step in TrackedSteps )
110+ {
111+ if ( skippedSteps . Contains ( step ) )
112+ {
113+ if ( trackedSteps . ContainsKey ( step ) )
114+ Assert . Fail ( $ "Step `{ step } ` should have been skipped, but is present.") ;
115+ }
116+ else
117+ {
118+ if ( ! trackedSteps . TryGetValue ( step , out var outputs ) )
119+ Assert . Fail ( $ "Step `{ step } ` expected, but is missing.") ;
120+
121+ AssertSteps ( outputs ) ;
122+ }
123+ }
69124 }
70125
126+ private static ReadOnlySpan < string > TrackedSteps =>
127+ new string [ ]
128+ {
129+ "ValidationClasses" ,
130+ } ;
131+
71132 private static void AssertSteps (
72133 ImmutableArray < IncrementalGeneratorRunStep > steps
73134 )
0 commit comments