using ComputeSharp.D2D1.WinUI.SourceGenerators;
using ComputeSharp.Tests.SourceGenerators.Helpers;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComputeSharp.D2D1.WinUI.Tests.SourceGenerators;
[TestClass]
public class Test_CanvasEffectPropertyGenerator
{
[TestMethod]
public void SingleProperty()
{
const string source = """
using ComputeSharp.D2D1.WinUI;
namespace MyNamespace;
public abstract partial class MyEffect : CanvasEffect
{
[GeneratedCanvasEffectProperty]
public partial int Number { get; set; }
}
""";
const string result = """"
//
#pragma warning disable
namespace MyNamespace
{
///
partial class MyEffect
{
///
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", )]
[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", )]
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", )]
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", )]
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", )]
partial void OnNumberChanged(int oldValue, int newValue);
}
}
"""";
VerifyGeneratedDiagnostics(source, ("MyNamespace.MyEffect.g.cs", result));
}
[TestMethod]
public void SingleProperty_WithNullabilityAnnotations()
{
const string source = """
using ComputeSharp.D2D1.WinUI;
namespace MyNamespace;
public abstract partial class MyEffect : CanvasEffect
{
[GeneratedCanvasEffectProperty]
public partial object? Instance { get; set; }
}
""";
const string result = """"
//
#pragma warning disable
namespace MyNamespace
{
///
partial class MyEffect
{
///
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", )]
[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", )]
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", )]
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", )]
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", )]
partial void OnInstanceChanged(object? oldValue, object? newValue);
}
}
"""";
VerifyGeneratedDiagnostics(source, ("MyNamespace.MyEffect.g.cs", result));
}
[TestMethod]
public void SingleProperty_WithNotNullableReferenceType()
{
const string source = """
using ComputeSharp.D2D1.WinUI;
namespace MyNamespace;
public abstract partial class MyEffect : CanvasEffect
{
[GeneratedCanvasEffectProperty]
public partial object Instance { get; set; }
}
""";
const string result = """"
//
#pragma warning disable
namespace MyNamespace
{
///
partial class MyEffect
{
///
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", )]
[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", )]
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", )]
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", )]
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", )]
partial void OnInstanceChanged(object? oldValue, object newValue);
}
}
"""";
VerifyGeneratedDiagnostics(source, ("MyNamespace.MyEffect.g.cs", result));
}
[TestMethod]
public void SingleProperty_WithCustomAccessorVisibility()
{
const string source = """
using ComputeSharp.D2D1.WinUI;
namespace MyNamespace;
public abstract partial class MyEffect : CanvasEffect
{
[GeneratedCanvasEffectProperty]
public partial 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", )]
[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", )]
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", )]
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", )]
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", )]
partial void OnNumberChanged(int oldValue, int newValue);
}
}
"""";
VerifyGeneratedDiagnostics(source, ("MyNamespace.MyEffect.g.cs", result));
}
[TestMethod]
public void MultipleProperties()
{
const string source = """
using ComputeSharp.D2D1.WinUI;
namespace MyNamespace;
public abstract partial class MyEffect : CanvasEffect
{
[GeneratedCanvasEffectProperty]
public partial int X { get; set; }
[GeneratedCanvasEffectProperty(CanvasEffectInvalidationType.Creation)]
public partial int Y { get; set; }
}
""";
const string result = """"
//
#pragma warning disable
namespace MyNamespace
{
///
partial class MyEffect
{
///
[global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.WinUI.CanvasEffectPropertyGenerator", )]
[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", )]
[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", )]
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", )]
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", )]
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", )]
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", )]
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", )]
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", )]
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", )]
partial void OnYChanged(int oldValue, int newValue);
}
}
"""";
VerifyGeneratedDiagnostics(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 void VerifyGeneratedDiagnostics(string source, (string Filename, string Source) result)
{
// We cannot run the analyzers on the input source, because they'd report unwanted dsiagnostics due to the
// partial properties missing an implementation part. For this generator, we just test analyzers individually.
CSharpGeneratorTest.VerifySources(source, result, languageVersion: LanguageVersion.Preview);
}
}