diff --git a/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs b/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs index 54b72837..da15eade 100644 --- a/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs +++ b/src/ComputeSharp.D2D1.UI.CodeFixers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs @@ -58,7 +58,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer } // Retrieve the properties passed by the analyzer - if (!int.TryParse(diagnostic.Properties[UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.InvalidationModePropertyName], out int invalidationMode)) + if (!int.TryParse(diagnostic.Properties[UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.InvalidationTypePropertyName], out int invalidationType)) { return; } @@ -80,7 +80,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer semanticModel, root, propertyDeclaration, - invalidationMode), + invalidationType), equivalenceKey: "Use [GeneratedCanvasEffectProperty]"), diagnostic); } @@ -122,18 +122,18 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer /// The original document being fixed. /// The instance for the current compilation. /// The with the attribute to add. - /// The invalidation mode to use. + /// The invalidation type to use. /// The updated attribute syntax. private static AttributeListSyntax UpdateGeneratedCanvasEffectPropertyAttributeList( Document document, SemanticModel semanticModel, AttributeListSyntax generatedCanvasEffectPropertyAttributeList, - int invalidationMode) + int invalidationType) { - // If the invalidation mode is not the default, set it in the attribute. + // If the invalidation type is not the default, set it in the attribute. // We extract the generated attribute so we can add the new argument. // It's important to reuse it, as it has the "add usings" annotation. - if (invalidationMode == 1) + if (invalidationType == 1) { // Try to resolve the attribute type, if present (this should always be the case) if (semanticModel.Compilation.GetTypeByMetadataName(WellKnownTypeNames.CanvasEffectInvalidationType) is INamedTypeSymbol enumTypeSymbol) @@ -144,11 +144,11 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer SyntaxNode enumTypeSyntax = syntaxGenerator.TypeExpression(enumTypeSymbol).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation); // Create the member access expression for the target enum type. - // We only ever take this path for the 'Creation' invalidation mode. + // We only ever take this path for the 'Creation' invalidation type. SyntaxNode enumMemberAccessExpressionSyntax = syntaxGenerator.MemberAccessExpression(enumTypeSyntax, "Creation"); // Create the attribute argument to insert - SyntaxNode attributeArgumentSyntax = syntaxGenerator.AttributeArgument("InvalidationMode", enumMemberAccessExpressionSyntax); + SyntaxNode attributeArgumentSyntax = syntaxGenerator.AttributeArgument(enumMemberAccessExpressionSyntax); // Actually add the argument to the existing attribute syntax return (AttributeListSyntax)syntaxGenerator.AddAttributeArguments(generatedCanvasEffectPropertyAttributeList, [attributeArgumentSyntax]); @@ -159,11 +159,11 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer AttributeList(SingletonSeparatedList( generatedCanvasEffectPropertyAttributeList.Attributes[0] .AddArgumentListArguments( - AttributeArgument(ParseExpression($"global::{WellKnownTypeNames.CanvasEffectInvalidationType}.Creation")) - .WithNameEquals(NameEquals(IdentifierName("InvalidationMode")))))); + AttributeArgument(ParseExpression( + $"global::{WellKnownTypeNames.CanvasEffectInvalidationType}.Creation"))))); } - // If we have no custom invalidation mode, we can just reuse the attribute with no changes + // If we have no custom invalidation type, we can just reuse the attribute with no changes return generatedCanvasEffectPropertyAttributeList; } @@ -174,14 +174,14 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer /// The instance for the current compilation. /// The original tree root belonging to the current document. /// The for the property being updated. - /// The invalidation mode to use. + /// The invalidation type to use. /// An updated document with the applied code fix, and being replaced with a partial property. private static async Task ConvertToPartialProperty( Document document, SemanticModel semanticModel, SyntaxNode root, PropertyDeclarationSyntax propertyDeclaration, - int invalidationMode) + int invalidationType) { await Task.CompletedTask; @@ -200,7 +200,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer propertyDeclaration, generatedCanvasEffectPropertyAttributeList, syntaxEditor, - invalidationMode); + invalidationType); // Create the new document with the single change return document.WithSyntaxRoot(syntaxEditor.GetChangedRoot()); @@ -214,7 +214,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer /// The for the property being updated. /// The with the attribute to add. /// The instance to use. - /// The invalidation mode to use. + /// The invalidation type to use. /// An updated document with the applied code fix, and being replaced with a partial property. private static void ConvertToPartialProperty( Document document, @@ -222,14 +222,14 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer PropertyDeclarationSyntax propertyDeclaration, AttributeListSyntax generatedCanvasEffectPropertyAttributeList, SyntaxEditor syntaxEditor, - int invalidationMode) + int invalidationType) { - // Update the attribute to insert with the invalidation mode, if needed + // Update the attribute to insert with the invalidation type, if needed generatedCanvasEffectPropertyAttributeList = UpdateGeneratedCanvasEffectPropertyAttributeList( document, semanticModel, generatedCanvasEffectPropertyAttributeList, - invalidationMode); + invalidationType); // Start setting up the updated attribute lists SyntaxList attributeLists = propertyDeclaration.AttributeLists; @@ -330,7 +330,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer } // Retrieve the properties passed by the analyzer - if (!int.TryParse(diagnostic.Properties[UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.InvalidationModePropertyName], out int invalidationMode)) + if (!int.TryParse(diagnostic.Properties[UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.InvalidationTypePropertyName], out int invalidationType)) { continue; } @@ -341,7 +341,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer propertyDeclaration, generatedCanvasEffectPropertyAttributeList, syntaxEditor, - invalidationMode); + invalidationType); } return document.WithSyntaxRoot(syntaxEditor.GetChangedRoot()); diff --git a/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs b/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs index 990925f1..b34fdb6f 100644 --- a/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs +++ b/src/ComputeSharp.D2D1.UI.SourceGenerators/Diagnostics/Analyzers/UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer.cs @@ -50,9 +50,9 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer : private static readonly ObjectPool> PropertyFlagsStackPool = new(CreatePropertyFlagsStack); /// - /// The property name for the serialized invalidation mode. + /// The property name for the serialized invalidation type. /// - public const string InvalidationModePropertyName = "InvalidationMode"; + public const string InvalidationTypePropertyName = "InvalidationType"; /// public override ImmutableArray SupportedDiagnostics { get; } = [UseGeneratedCanvasEffectPropertyOnSemiAutoProperty]; @@ -205,7 +205,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer : } // We matched the method, now let's validate the arguments - if (invocationOperation.Arguments is not [{ } locationArgument, { } valueArgument, { } invalidationModeArgument]) + if (invocationOperation.Arguments is not [{ } locationArgument, { } valueArgument, { } invalidationTypeArgument]) { return; } @@ -229,11 +229,11 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer : } // The invalidation mode can either be the default value... - if (invalidationModeArgument is { IsImplicit: true, ArgumentKind: ArgumentKind.DefaultValue }) + if (invalidationTypeArgument is { IsImplicit: true, ArgumentKind: ArgumentKind.DefaultValue }) { validFlags[1] = 1; } - else if (invalidationModeArgument is { ConstantValue: { HasValue: true, Value: byte mode } } && mode is 0 or 1) + else if (invalidationTypeArgument is { Value.ConstantValue: { HasValue: true, Value: byte mode } } && mode is 0 or 1) { // ...Or is has to be set explicitly to one of the two supported values validFlags[1] = (byte)(mode + 1); @@ -289,7 +289,7 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer : context.ReportDiagnostic(Diagnostic.Create( UseGeneratedCanvasEffectPropertyOnSemiAutoProperty, pair.Key.Locations.FirstOrDefault(), - ImmutableDictionary.Create().Add(InvalidationModePropertyName, invalidationType.ToString()), + ImmutableDictionary.Create().Add(InvalidationTypePropertyName, invalidationType.ToString()), pair.Key)); } } diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Helpers/CSharpCodeFixerTest{TAnalyzer,TCodeFixer}.cs b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Helpers/CSharpCodeFixerTest{TAnalyzer,TCodeFixer}.cs index 7a2dfd36..54e83242 100644 --- a/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Helpers/CSharpCodeFixerTest{TAnalyzer,TCodeFixer}.cs +++ b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Helpers/CSharpCodeFixerTest{TAnalyzer,TCodeFixer}.cs @@ -30,6 +30,8 @@ internal sealed class CSharpCodeFixTest : CSharpCodeFixTe public CSharpCodeFixTest(LanguageVersion languageVersion) { this.languageVersion = languageVersion; + + TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf")); } /// diff --git a/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs new file mode 100644 index 00000000..33ecd76c --- /dev/null +++ b/tests/ComputeSharp.D2D1.WinUI.Tests.SourceGenerators/Test_UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer.cs @@ -0,0 +1,767 @@ +extern alias Core; +extern alias D2D1_WinUI; +extern alias D2D1; + +using System.Threading.Tasks; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.Testing; +using Microsoft.UI.Xaml.Controls; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Windows.Foundation; +using Windows.UI.ViewManagement; +using CSharpCodeFixTest = ComputeSharp.Tests.SourceGenerators.Helpers.CSharpCodeFixTest< + ComputeSharp.D2D1.WinUI.SourceGenerators.UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer, + ComputeSharp.D2D1.WinUI.SourceGenerators.UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer>; + +namespace ComputeSharp.D2D1.WinUI.Tests.SourceGenerators; + +[TestClass] +public class Test_UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyCodeFixer +{ + [TestMethod] + [DataRow("get;")] + [DataRow("get => field;")] + public async Task SimpleProperty_ImplicitInvalidationType(string getAccessor) + { + string original = $$""" + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + public string? [|Name|] + { + {{getAccessor}} + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:Name|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + [DataRow("get;")] + [DataRow("get => field;")] + public async Task SimpleProperty_InvalidationTypeUpdate(string getAccessor) + { + string original = $$""" + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + public string? [|Name|] + { + {{getAccessor}} + set => SetPropertyAndInvalidateEffectGraph(ref field, value, CanvasEffectInvalidationType.Update); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:Name|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + [DataRow("get;")] + [DataRow("get => field;")] + public async Task SimpleProperty_InvalidationTypeCreation(string getAccessor) + { + string original = $$""" + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + public string? [|Name|] + { + {{getAccessor}} + set => SetPropertyAndInvalidateEffectGraph(ref field, value, CanvasEffectInvalidationType.Creation); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty(CanvasEffectInvalidationType.Creation)] + public partial string? {|CS9248:Name|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_WithMissingUsingDirective() + { + const string original = """ + namespace MyApp; + + public abstract class MyEffect : ComputeSharp.D2D1.WinUI.CanvasEffect + { + public string? [|Name|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : ComputeSharp.D2D1.WinUI.CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:Name|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_WithLeadingTrivia() + { + const string original = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + /// + /// This is a property. + /// + public string? [|Name|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + /// + /// This is a property. + /// + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:Name|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_WithLeadingTrivia_AndAttribute() + { + const string original = """ + using System; + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + /// + /// This is a property. + /// + [Test("Targeting property")] + [field: Test("Targeting field")] + public string? [|Name|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + + public class TestAttribute(string text) : Attribute; + """; + + const string @fixed = """ + using System; + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + /// + /// This is a property. + /// + [GeneratedCanvasEffectProperty] + [Test("Targeting property")] + [field: Test("Targeting field")] + public partial string? {|CS9248:Name|} { get; set; } + } + + public class TestAttribute(string text) : Attribute; + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_Multiple() + { + const string original = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + public string? [|FirstName|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + public string? [|LastName|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:FirstName|} { get; set; } + + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:LastName|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_Multiple_OnlyTriggersOnFirstOne() + { + const string original = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + public string? [|FirstName|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + private string? lastName; + + public string? LastName + { + get => this.lastName; + set => SetPropertyAndInvalidateEffectGraph(ref this.lastName, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:FirstName|} { get; set; } + + private string? lastName; + + public string? LastName + { + get => this.lastName; + set => SetPropertyAndInvalidateEffectGraph(ref this.lastName, value); + } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_Multiple_OnlyTriggersOnSecondOne() + { + const string original = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + private string? firstName; + + public string? FirstName + { + get => this.firstName; + set => SetPropertyAndInvalidateEffectGraph(ref this.firstName, value); + } + + public string? [|LastName|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + private string? firstName; + + public string? FirstName + { + get => this.firstName; + set => SetPropertyAndInvalidateEffectGraph(ref this.firstName, value); + } + + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:LastName|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_WithinPartialType() + { + const string original = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + public string? [|Name|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:Name|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_WithMissingUsingDirective_Multiple() + { + const string original = """ + namespace MyApp; + + public abstract class MyEffect : ComputeSharp.D2D1.WinUI.CanvasEffect + { + public string? [|FirstName|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + public string? [|LastName|] + { + get; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + """; + + const string @fixed = """ + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : ComputeSharp.D2D1.WinUI.CanvasEffect + { + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:FirstName|} { get; set; } + + [GeneratedCanvasEffectProperty] + public partial string? {|CS9248:LastName|} { get; set; } + } + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } + + [TestMethod] + public async Task SimpleProperty_Multiple_MixedScenario() + { + const string original = """ + using System; + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract class MyEffect : CanvasEffect + { + [Test("This is an attribute")] + public string [|Prop1|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + // Single comment + public string [|Prop2|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + /// + /// This is a property. + /// + public string [|Prop3|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + /// + /// This is another property. + /// + [Test("Another attribute")] + public string [|Prop4|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + // Some other single comment + [Test("Yet another attribute")] + public string [|Prop5|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + [Test("Attribute without trivia")] + public string [|Prop6|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + + public string [|Prop7|] + { + get => field; + set => SetPropertyAndInvalidateEffectGraph(ref field, value); + } + } + + public class TestAttribute(string text) : Attribute; + """; + + const string @fixed = """ + using System; + using ComputeSharp.D2D1.WinUI; + + namespace MyApp; + + public abstract partial class MyEffect : CanvasEffect + { + [GeneratedCanvasEffectProperty] + [Test("This is an attribute")] + public partial string {|CS9248:Prop1|} { get; set; } + + // Single comment + [GeneratedCanvasEffectProperty] + public partial string {|CS9248:Prop2|} { get; set; } + + /// + /// This is a property. + /// + [GeneratedCanvasEffectProperty] + public partial string {|CS9248:Prop3|} { get; set; } + + /// + /// This is another property. + /// + [GeneratedCanvasEffectProperty] + [Test("Another attribute")] + public partial string {|CS9248:Prop4|} { get; set; } + + // Some other single comment + [GeneratedCanvasEffectProperty] + [Test("Yet another attribute")] + public partial string {|CS9248:Prop5|} { get; set; } + + [GeneratedCanvasEffectProperty] + [Test("Attribute without trivia")] + public partial string {|CS9248:Prop6|} { get; set; } + + [GeneratedCanvasEffectProperty] + public partial string {|CS9248:Prop7|} { get; set; } + } + + public class TestAttribute(string text) : Attribute; + """; + + CSharpCodeFixTest test = new(LanguageVersion.Preview) + { + TestCode = original, + FixedCode = @fixed, + ReferenceAssemblies = ReferenceAssemblies.Net.Net80, + TestState = { AdditionalReferences = + { + MetadataReference.CreateFromFile(typeof(Point).Assembly.Location), + MetadataReference.CreateFromFile(typeof(ApplicationView).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Button).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1_WinUI::ComputeSharp.D2D1.WinUI.CanvasEffect).Assembly.Location), + MetadataReference.CreateFromFile(typeof(D2D1::ComputeSharp.D2D1.ID2D1PixelShader).Assembly.Location), + MetadataReference.CreateFromFile(typeof(Core::ComputeSharp.Float4).Assembly.Location) + } } + }; + + await test.RunAsync(); + } +}