From 36c404367b37624f2b8e6fa09ab6616cf2c995f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Dec 2024 19:36:25 +0100 Subject: [PATCH] Add unit tests for new analyzer --- ...ffectPropertyOnSemiAutoPropertyAnalyzer.cs | 2 +- ...CanvasEffectPropertyGenerator_Analyzers.cs | 227 ++++++++++++++++++ 2 files changed, 228 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs b/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs index d2b360b80..990925f1f 100644 --- a/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs +++ b/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs @@ -229,7 +229,7 @@ public override void Initialize(AnalysisContext context) } // The invalidation mode can either be the default value... - if (invalidationModeArgument is not { IsImplicit: true, ArgumentKind: ArgumentKind.DefaultValue }) + if (invalidationModeArgument is { IsImplicit: true, ArgumentKind: ArgumentKind.DefaultValue }) { validFlags[1] = 1; } diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_CanvasEffectPropertyGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_CanvasEffectPropertyGenerator_Analyzers.cs index 72ff08143..0082c1dc2 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_CanvasEffectPropertyGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_CanvasEffectPropertyGenerator_Analyzers.cs @@ -227,4 +227,231 @@ public abstract partial class MyEffect : CanvasEffect await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_NormalProperty_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string Name { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_SimilarProperty_NotObservableObject_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : MyBaseCanvasEffect + { + public string Name + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + + public abstract class MyBaseCanvasEffect + { + protected void SetPropertyAndInvalidateEffectGraph(ref T location, T value, CanvasEffectInvalidationType invalidationType = CanvasEffectInvalidationType.Update) + { + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_NoGetter_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string Name + { + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_NoSetter_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string Name + { + get => field; + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_OtherLocation_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string Name + { + get => field; + set + { + string test = field; + + SetPropertyAndInvalidateEffectGraph(ref test, value); + } + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_OtherValue_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string Name + { + get => field; + set + { + string test = "Bob"; + + SetPropertyAndInvalidateEffectGraph(ref field, test); + } + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithGeneratedCanvasEffectProperty_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public string Name + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_GetAccessorWithExpressionBody_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string Name + { + get => "Hello world"; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_ValidProperty_Warns() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public string {|CMPSD2DWINUI0008:Name|} + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithModifiers_Warns() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public new string {|CMPSD2DWINUI0008:Name|} + { + get => field; + private set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer_ValidProperty_WithBlocks_Warns() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + public abstract partial class SampleCanvasEffect : CanvasEffect + { + public new string {|CMPSD2DWINUI0008:Name|} + { + get + { + return field; + } + private set + { + SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } } \ No newline at end of file