using System.Threading.Tasks; using ComputeSharp.D2D1.WinUI.SourceGenerators; using ComputeSharp.Tests.SourceGenerators.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.D2D1.WinUI.Tests.SourceGenerators; [TestClass] public class Test_CanvasEffectPropertyGenerator { [TestMethod] public async Task SingleProperty() { const string source = """ using ComputeSharp.D2D1.WinUI; namespace MyNamespace; public abstract partial class MyEffect : CanvasEffect { [GeneratedCanvasEffectProperty] public int Number { get; set; } } """; const string result = """" // #pragma warning disable namespace MyNamespace { /// partial class MyEffect { /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public partial int Number { get => field; set { if (global::System.Collections.Generic.EqualityComparer.Default.Equals(field, value)) { return; } int oldValue = field; OnNumberChanging(value); OnNumberChanging(oldValue, value); field = value; OnNumberChanged(value); OnNumberChanged(oldValue, value); InvalidateEffectGraph(global::ComputeSharp.D2D1.WinUI.CanvasEffectInvalidationType.Update); } } /// Executes the logic for when is changing. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanging(int newValue); /// Executes the logic for when is changing. /// The previous property value that is being replaced. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanging(int oldValue, int newValue); /// Executes the logic for when has just changed. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanged(int newValue); /// Executes the logic for when has just changed. /// The previous property value that has been replaced. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanged(int oldValue, int newValue); } } """"; await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyEffect.g.cs", result)); } [TestMethod] public async Task SingleProperty_WithNullabilityAnnotations() { const string source = """ using ComputeSharp.D2D1.WinUI; namespace MyNamespace; public abstract partial class MyEffect : CanvasEffect { [GeneratedCanvasEffectProperty] public object? Instance { get; set; } } """; const string result = """" // #pragma warning disable namespace MyNamespace { /// partial class MyEffect { /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public partial object? Instance { get => field; set { if (global::System.Collections.Generic.EqualityComparer.Default.Equals(field, value)) { return; } object? oldValue = field; OnInstanceChanging(value); OnInstanceChanging(oldValue, value); field = value; OnInstanceChanged(value); OnInstanceChanged(oldValue, value); InvalidateEffectGraph(global::ComputeSharp.D2D1.WinUI.CanvasEffectInvalidationType.Update); } } /// Executes the logic for when is changing. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanging(object? newValue); /// Executes the logic for when is changing. /// The previous property value that is being replaced. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanging(object? oldValue, object? newValue); /// Executes the logic for when has just changed. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanged(object? newValue); /// Executes the logic for when has just changed. /// The previous property value that has been replaced. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanged(object? oldValue, object? newValue); } } """"; await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyEffect.g.cs", result)); } [TestMethod] public async Task SingleProperty_WithNotNullableReferenceType() { const string source = """ using ComputeSharp.D2D1.WinUI; namespace MyNamespace; public abstract partial class MyEffect : CanvasEffect { [GeneratedCanvasEffectProperty] public object Instance { get; set; } } """; const string result = """" // #pragma warning disable namespace MyNamespace { /// partial class MyEffect { /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public partial object Instance { get => field; set { if (global::System.Collections.Generic.EqualityComparer.Default.Equals(field, value)) { return; } object? oldValue = field; OnInstanceChanging(value); OnInstanceChanging(oldValue, value); field = value; OnInstanceChanged(value); OnInstanceChanged(oldValue, value); InvalidateEffectGraph(global::ComputeSharp.D2D1.WinUI.CanvasEffectInvalidationType.Update); } } /// Executes the logic for when is changing. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanging(object newValue); /// Executes the logic for when is changing. /// The previous property value that is being replaced. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanging(object? oldValue, object newValue); /// Executes the logic for when has just changed. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanged(object newValue); /// Executes the logic for when has just changed. /// The previous property value that has been replaced. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnInstanceChanged(object? oldValue, object newValue); } } """"; await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyEffect.g.cs", result)); } [TestMethod] public async Task SingleProperty_WithCustomAccessorVisibility() { const string source = """ using ComputeSharp.D2D1.WinUI; namespace MyNamespace; public abstract partial class MyEffect : CanvasEffect { [GeneratedCanvasEffectProperty] public int Number { get; private set; } } """; const string result = """" // #pragma warning disable namespace MyNamespace { /// partial class MyEffect { /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public partial int Number { get => field; private set { if (global::System.Collections.Generic.EqualityComparer.Default.Equals(field, value)) { return; } int oldValue = field; OnNumberChanging(value); OnNumberChanging(oldValue, value); field = value; OnNumberChanged(value); OnNumberChanged(oldValue, value); InvalidateEffectGraph(global::ComputeSharp.D2D1.WinUI.CanvasEffectInvalidationType.Update); } } /// Executes the logic for when is changing. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanging(int newValue); /// Executes the logic for when is changing. /// The previous property value that is being replaced. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanging(int oldValue, int newValue); /// Executes the logic for when has just changed. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanged(int newValue); /// Executes the logic for when has just changed. /// The previous property value that has been replaced. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnNumberChanged(int oldValue, int newValue); } } """"; await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyEffect.g.cs", result)); } [TestMethod] public async Task MultipleProperties() { const string source = """ using ComputeSharp.D2D1.WinUI; namespace MyNamespace; public abstract partial class MyEffect : CanvasEffect { [GeneratedCanvasEffectProperty] public int X { get; set; } [GeneratedCanvasEffectProperty(CanvasEffectInvalidationType.Creation)] public int Y { get; set; } } """; const string result = """" // #pragma warning disable namespace MyNamespace { /// partial class MyEffect { /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public partial int X { get => field; set { if (global::System.Collections.Generic.EqualityComparer.Default.Equals(field, value)) { return; } int oldValue = field; OnXChanging(value); OnXChanging(oldValue, value); field = value; OnXChanged(value); OnXChanged(oldValue, value); InvalidateEffectGraph(global::ComputeSharp.D2D1.WinUI.CanvasEffectInvalidationType.Update); } } /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCode] [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] public partial int Y { get => field; set { if (global::System.Collections.Generic.EqualityComparer.Default.Equals(field, value)) { return; } int oldValue = field; OnYChanging(value); OnYChanging(oldValue, value); field = value; OnYChanged(value); OnYChanged(oldValue, value); InvalidateEffectGraph(global::ComputeSharp.D2D1.WinUI.CanvasEffectInvalidationType.Creation); } } /// Executes the logic for when is changing. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnXChanging(int newValue); /// Executes the logic for when is changing. /// The previous property value that is being replaced. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnXChanging(int oldValue, int newValue); /// Executes the logic for when has just changed. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnXChanged(int newValue); /// Executes the logic for when has just changed. /// The previous property value that has been replaced. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnXChanged(int oldValue, int newValue); /// Executes the logic for when is changing. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnYChanging(int newValue); /// Executes the logic for when is changing. /// The previous property value that is being replaced. /// The new property value being set. /// This method is invoked right before the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnYChanging(int oldValue, int newValue); /// Executes the logic for when has just changed. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnYChanged(int newValue); /// Executes the logic for when has just changed. /// The previous property value that has been replaced. /// The new property value that has been set. /// This method is invoked right after the value of is changed. [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", "3.0.0.0")] partial void OnYChanged(int oldValue, int newValue); } } """"; await VerifyGeneratedDiagnosticsAsync(source, ("MyNamespace.MyEffect.g.cs", result)); } /// /// Verifies the output of a source generator. /// /// The input source to process. /// The expected source to be generated. /// The task for the operation. private static async Task VerifyGeneratedDiagnosticsAsync(string source, (string Filename, string Source) result) { await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); await CSharpAnalyzerWithLanguageVersionTest.VerifyAnalyzerAsync(source); CSharpGeneratorTest.VerifySources(source, result); } }