From 5fd2c8ad9a30587b91d42654043a5f0e8cc03af1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 12 Dec 2022 00:02:57 +0100 Subject: [PATCH] Add unit tests for new analyzer --- .../Test_SourceGeneratorsDiagnostics.cs | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs b/tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs index 68fbf2920..5a2055fbd 100644 --- a/tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs +++ b/tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs @@ -1495,6 +1495,42 @@ private void GreetUser(User user) VerifyGeneratedDiagnostics(source, "MVVMTK0031"); } + [TestMethod] + public async Task UsingINotifyPropertyChangedAttributeOnClassWithNoBaseType() + { + string source = """ + using CommunityToolkit.Mvvm.ComponentModel; + + namespace MyApp + { + [INotifyPropertyChanged] + public partial class {|MVVMTK0032:SampleViewModel|} + { + } + } + """; + + await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration(source, LanguageVersion.CSharp8); + } + + [TestMethod] + public async Task UsingObservableObjectAttributeOnClassWithNoBaseType() + { + string source = """ + using CommunityToolkit.Mvvm.ComponentModel; + + namespace MyApp + { + [ObservableObject] + public partial class {|MVVMTK0033:SampleViewModel|} + { + } + } + """; + + await VerifyAnalyzerDiagnosticsAndSuccessfulGeneration(source, LanguageVersion.CSharp8); + } + /// /// Verifies the diagnostic errors for a given analyzer, and that all available source generators can run successfully with the input source (including subsequent compilation). /// @@ -1538,20 +1574,6 @@ private static void VerifyGeneratedDiagnostics(string source, params VerifyGeneratedDiagnostics(CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)), new[] { generator }, diagnosticsIds); } - /// - /// Verifies the output of a source generator. - /// - /// The generator type to use. - /// The input source to process. - /// The diagnostic ids to expect for the input source code. - private static void VerifyGeneratedDiagnostics(SyntaxTree syntaxTree, params string[] diagnosticsIds) - where TGenerator : class, IIncrementalGenerator, new() - { - IIncrementalGenerator generator = new TGenerator(); - - VerifyGeneratedDiagnostics(syntaxTree, new[] { generator }, diagnosticsIds); - } - /// /// Verifies the output of one or more source generators. ///