Skip to content

Commit

Permalink
Add unit tests for new analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 1, 2024
1 parent 2e92535 commit 8aecb70
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ComputeSharp.D2D1.WinUI.SourceGenerators.Constants;
internal static class WellKnownTypeNames
{
/// <summary>
/// The fully qualified type name for the <c>[GeneratedCanvasEffectPropertyAttribute]</c> type.
/// The fully qualified type name for the <c>[GeneratedCanvasEffectProperty]</c> type.
/// </summary>
public const string GeneratedCanvasEffectPropertyAttribute =
#if WINDOWS_UWP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -227,4 +226,114 @@ public abstract partial class MyEffect : CanvasEffect

await CSharpAnalyzerWithLanguageVersionTest<RequireCSharpLanguageVersionPreviewAnalyzer>.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<InvalidGeneratedCanvasEffectPropertyNonNullableDeclarationAnalyzer>.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<InvalidGeneratedCanvasEffectPropertyNonNullableDeclarationAnalyzer>.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<InvalidGeneratedCanvasEffectPropertyNonNullableDeclarationAnalyzer>.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<InvalidGeneratedCanvasEffectPropertyNonNullableDeclarationAnalyzer>.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<InvalidGeneratedCanvasEffectPropertyNonNullableDeclarationAnalyzer>.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<InvalidGeneratedCanvasEffectPropertyNonNullableDeclarationAnalyzer>.VerifyAnalyzerAsync(source, languageVersion: LanguageVersion.Preview);
}
}

0 comments on commit 8aecb70

Please sign in to comment.