Skip to content

Commit 9b7d871

Browse files
committed
Fix unit tests for generators
These two generators should never cause a build error
1 parent 7723c1e commit 9b7d871

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.CodeAnalysis;
1010
using Microsoft.CodeAnalysis.CSharp;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
12-
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1312

1413
namespace CommunityToolkit.Mvvm.SourceGenerators;
1514

@@ -40,8 +39,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4039
.Select(static (item, _) => Execute.GetInfo(item))
4140
.WithComparer(ValidationInfo.Comparer.Default);
4241

43-
context.FilterWithLanguageVersion(ref validationInfo, LanguageVersion.CSharp8, UnsupportedCSharpLanguageVersionError);
44-
4542
// Check whether the header file is needed
4643
IncrementalValueProvider<bool> isHeaderFileNeeded =
4744
validationInfo

CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
13-
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1413

1514
namespace CommunityToolkit.Mvvm.SourceGenerators;
1615

@@ -48,8 +47,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4847
.Select(static (item, _) => Execute.GetInfo(item.Type, item.Interfaces))
4948
.WithComparer(RecipientInfo.Comparer.Default);
5049

51-
context.FilterWithLanguageVersion(ref recipientInfo, LanguageVersion.CSharp8, UnsupportedCSharpLanguageVersionError);
52-
5350
// Check whether the header file is needed
5451
IncrementalValueProvider<bool> isHeaderFileNeeded =
5552
recipientInfo

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ public partial class SampleViewModel
296296
}
297297

298298
[TestMethod]
299+
[Ignore("The generator should just not trigger at all in this scenario, update this after migrating diagnostics")]
299300
public void UnsupportedCSharpLanguageVersion_FromObservableValidatorValidateAllPropertiesGenerator()
300301
{
301302
string source = @"
@@ -311,9 +312,9 @@ public partial class SampleViewModel : ObservableValidator
311312
}
312313
}";
313314

315+
// Compilation should be fine on C# 7.3 as well (the generator just doesn't trigger)
314316
VerifyGeneratedDiagnostics<ObservableValidatorValidateAllPropertiesGenerator>(
315-
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
316-
"MVVMTK0008");
317+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)));
317318

318319
VerifySuccessfulGeneration(source);
319320
}
@@ -362,9 +363,10 @@ public void Receive(MyMessage message)
362363
}
363364
}";
364365

366+
// This should run fine on C# 8.0 too, as it doesn't use any newer features. Additionally, when not supported this
367+
// generator should just not run, not cause issues. The MVVM Toolkit has a reflection-based fallback path for this.
365368
VerifyGeneratedDiagnostics<IMessengerRegisterAllGenerator>(
366-
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
367-
"MVVMTK0008");
369+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)));
368370

369371
VerifySuccessfulGeneration(source);
370372
}
@@ -1496,11 +1498,11 @@ private static void VerifyGeneratedDiagnostics<TGenerator>(SyntaxTree syntaxTree
14961498
}
14971499

14981500
/// <summary>
1499-
/// Verifies the output of a source generator.
1501+
/// Verifies the output of one or more source generators.
15001502
/// </summary>
1501-
/// <typeparam name="TGenerator">The generator type to use.</typeparam>
15021503
/// <param name="syntaxTree">The input source tree to process.</param>
1503-
/// <param name="diagnosticsIds">The diagnostic ids to expect for the input source code.</param>
1504+
/// <param name="generators">The generators to apply to the input syntax tree.</param>
1505+
/// <param name="generatorDiagnosticsIds">The diagnostic ids to expect for the input source code.</param>
15041506
private static void VerifyGeneratedDiagnostics(SyntaxTree syntaxTree, IIncrementalGenerator[] generators, string[] generatorDiagnosticsIds)
15051507
{
15061508
Type observableObjectType = typeof(ObservableObject);

0 commit comments

Comments
 (0)