From 8aecb7084e63b5775cf8e2b30615847cb695ae24 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 30 Nov 2024 19:10:03 -0800 Subject: [PATCH] Add unit tests for new analyzer --- .../Constants/WellKnownTypeNames.cs | 2 +- .../AnalyzerReleases.Shipped.md | 2 +- ...CanvasEffectPropertyGenerator_Analyzers.cs | 111 +++++++++++++++++- 3 files changed, 112 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1.UI.SourceGenerators/Constants/WellKnownTypeNames.cs b/src/ComputeSharp.D2D1.UI.SourceGenerators/Constants/WellKnownTypeNames.cs index 4abbee9d6..d79fbb566 100644 --- a/src/ComputeSharp.D2D1.UI.SourceGenerators/Constants/WellKnownTypeNames.cs +++ b/src/ComputeSharp.D2D1.UI.SourceGenerators/Constants/WellKnownTypeNames.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.D2D1.WinUI.SourceGenerators.Constants; internal static class WellKnownTypeNames { /// - /// The fully qualified type name for the [GeneratedCanvasEffectPropertyAttribute] type. + /// The fully qualified type name for the [GeneratedCanvasEffectProperty] type. /// public const string GeneratedCanvasEffectPropertyAttribute = #if WINDOWS_UWP diff --git a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md index da94478c8..38317ea6f 100644 --- a/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.D2D1.WinUI.SourceGenerators/AnalyzerReleases.Shipped.md @@ -14,4 +14,4 @@ CMPSD2DWINUI0004 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](htt CMPSD2DWINUI0005 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2DWINUI0006 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPSD2DWINUI0007 | ComputeSharp.D2D1.WinUI.Effects | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp) -CMPSD2DWINUI0008 | ComputeSharp.D2D1.Uwp.Effects | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPSD2DWINUI0008 | ComputeSharp.D2D1.WinUI.Effects | Warning | [Documentation](https://github.com/Sergio0694/ComputeSharp) 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..75c941b58 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_CanvasEffectPropertyGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_CanvasEffectPropertyGenerator_Analyzers.cs @@ -198,7 +198,6 @@ public abstract partial class MyEffect : CanvasEffect public async Task RequireCSharpLanguageVersionPreviewAnalyzer_LanguageVersionIsNotPreview_Warns() { const string source = """ - using System; using ComputeSharp.D2D1.WinUI; public abstract partial class MyEffect : CanvasEffect @@ -227,4 +226,114 @@ public abstract partial class MyEffect : CanvasEffect await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); } + + [TestMethod] + public async Task InvalidPropertyNonNullableDeclarationAnalyzer_NoAttribute_DoesNotWarn() + { + const string source = """ + using ComputeSharp.D2D1.WinUI; + + #nullable enable + + public abstract partial class MyEffect : CanvasEffect + { + public string Name { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + [DataRow("int")] + [DataRow("int?")] + [DataRow("string?")] + public async Task InvalidPropertyNonNullableDeclarationAnalyzer_NullableOrNotApplicableType_DoesNotWarn(string propertyType) + { + string source = $$""" + using ComputeSharp.D2D1.WinUI; + + #nullable enable + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial {{propertyType}} {|CS9248:Name|} { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task InvalidPropertyNonNullableDeclarationAnalyzer_NotNullableType_WithMaybeNullAttribute_DoesNotWarn() + { + string source = $$""" + using System.Diagnostics.CodeAnalysis; + using ComputeSharp.D2D1.WinUI; + + #nullable enable + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + [MaybeNull] + public partial string {|CS9248:Name|} { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task InvalidPropertyNonNullableDeclarationAnalyzer_NotNullableType_Required_DoesNotWarn() + { + string source = $$""" + using ComputeSharp.D2D1.WinUI; + + #nullable enable + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public required partial string {|CS9248:Name|} { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task InvalidPropertyNonNullableDeclarationAnalyzer_NotNullableType_NullableDisabled_DoesNotWarn() + { + string source = $$""" + using ComputeSharp.D2D1.WinUI; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public required partial string {|CS9248:Name|} { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } + + [TestMethod] + public async Task InvalidPropertyNonNullableDeclarationAnalyzer_NotNullableType_Warns() + { + string source = $$""" + using ComputeSharp.D2D1.WinUI; + + #nullable enable + + public abstract partial class MyEffect : CanvasEffect + { + [{|CMPSD2DWINUI0008:GeneratedCanvasEffectProperty|}] + public partial string {|CS9248:Name|} { get; set; } + } + """; + + await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview); + } } \ No newline at end of file