Add unit tests for code fixer, fix bugs

このコミットが含まれているのは:
Sergio Pedri committed 2024-12-21 01:30:35 +01:00
1 親 3b72d1d2b8
コミット 0f8019b70c
4ファイル変更
+795 -26

検索条件に一致するファイルはありません

@@ -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
/// <param name="document">The original document being fixed.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the current compilation.</param>
/// <param name="generatedCanvasEffectPropertyAttributeList">The <see cref="AttributeListSyntax"/> with the attribute to add.</param>
/// <param name="invalidationMode">The invalidation mode to use.</param>
/// <param name="invalidationType">The invalidation type to use.</param>
/// <returns>The updated attribute syntax.</returns>
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
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the current compilation.</param>
/// <param name="root">The original tree root belonging to the current document.</param>
/// <param name="propertyDeclaration">The <see cref="PropertyDeclarationSyntax"/> for the property being updated.</param>
/// <param name="invalidationMode">The invalidation mode to use.</param>
/// <param name="invalidationType">The invalidation type to use.</param>
/// <returns>An updated document with the applied code fix, and <paramref name="propertyDeclaration"/> being replaced with a partial property.</returns>
private static async Task<Document> 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
/// <param name="propertyDeclaration">The <see cref="PropertyDeclarationSyntax"/> for the property being updated.</param>
/// <param name="generatedCanvasEffectPropertyAttributeList">The <see cref="AttributeListSyntax"/> with the attribute to add.</param>
/// <param name="syntaxEditor">The <see cref="SyntaxEditor"/> instance to use.</param>
/// <param name="invalidationMode">The invalidation mode to use.</param>
/// <param name="invalidationType">The invalidation type to use.</param>
/// <returns>An updated document with the applied code fix, and <paramref name="propertyDeclaration"/> being replaced with a partial property.</returns>
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<AttributeListSyntax> 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());
@@ -50,9 +50,9 @@ public sealed class UseGeneratedCanvasEffectPropertyOnSemiAutoPropertyAnalyzer :
private static readonly ObjectPool<Stack<byte[]>> PropertyFlagsStackPool = new(CreatePropertyFlagsStack);
/// <summary>
/// The property name for the serialized invalidation mode.
/// The property name for the serialized invalidation type.
/// </summary>
public const string InvalidationModePropertyName = "InvalidationMode";
public const string InvalidationTypePropertyName = "InvalidationType";
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> 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<string, string?>().Add(InvalidationModePropertyName, invalidationType.ToString()),
ImmutableDictionary.Create<string, string?>().Add(InvalidationTypePropertyName, invalidationType.ToString()),
pair.Key));
}
}
@@ -30,6 +30,8 @@ internal sealed class CSharpCodeFixTest<TAnalyzer, TCodeFixer> : CSharpCodeFixTe
public CSharpCodeFixTest(LanguageVersion languageVersion)
{
this.languageVersion = languageVersion;
TestState.AnalyzerConfigFiles.Add(("/.editorconfig", "[*]\nend_of_line = lf"));
}
/// <inheritdoc/>
@@ -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
{
/// <summary>
/// This is a property.
/// </summary>
public string? [|Name|]
{
get;
set => SetPropertyAndInvalidateEffectGraph(ref field, value);
}
}
""";
const string @fixed = """
using ComputeSharp.D2D1.WinUI;
namespace MyApp;
public abstract partial class MyEffect : CanvasEffect
{
/// <summary>
/// This is a property.
/// </summary>
[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
{
/// <summary>
/// This is a property.
/// </summary>
[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
{
/// <summary>
/// This is a property.
/// </summary>
[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);
}
/// <summary>
/// This is a property.
/// </summary>
public string [|Prop3|]
{
get => field;
set => SetPropertyAndInvalidateEffectGraph(ref field, value);
}
/// <summary>
/// This is another property.
/// </summary>
[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; }
/// <summary>
/// This is a property.
/// </summary>
[GeneratedCanvasEffectProperty]
public partial string {|CS9248:Prop3|} { get; set; }
/// <summary>
/// This is another property.
/// </summary>
[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();
}
}