Skip to content

Commit

Permalink
Add more generator unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 13, 2024
1 parent bebdcac commit be37f5f
Showing 1 changed file with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2699,4 +2699,180 @@ public partial string? Name

CSharpGeneratorTest<DependencyPropertyGenerator>.VerifySources(source, ("MyNamespace.MyControl.g.cs", result), languageVersion: LanguageVersion.CSharp13);
}

[TestMethod]

// The 'string' and 'object' types are special
[DataRow("string", "string", "null")]
[DataRow("string", "string?", "null")]
[DataRow("object", "object", "null")]
[DataRow("object", "object?", "null")]

// Well known WinRT primitive types
[DataRow("int", "int", "null")]
[DataRow("byte", "byte", "null")]
[DataRow("sbyte", "sbyte", "null")]
[DataRow("short", "short", "null")]
[DataRow("ushort", "ushort", "null")]
[DataRow("uint", "uint", "null")]
[DataRow("long", "long", "null")]
[DataRow("ulong", "ulong", "null")]
[DataRow("char", "char", "null")]
[DataRow("float", "float", "null")]
[DataRow("double", "double", "null")]

// Well known WinRT struct types
[DataRow("Matrix3x2", "Matrix3x2", "null")]
[DataRow("Matrix4x4", "Matrix4x4", "null")]
[DataRow("Plane", "Plane", "null")]
[DataRow("Quaternion", "Quaternion", "null")]
[DataRow("Vector2", "Vector2", "null")]
[DataRow("Vector3", "Vector3", "null")]
[DataRow("Vector4", "Vector4", "null")]
[DataRow("Point", "Point", "null")]
[DataRow("Rect", "Rect", "null")]

// Well known WinRT enum types
[DataRow("Visibility", "Visibility", "null")]

// Nullable types, they're always just 'null'
[DataRow("int?", "int?", "null")]
[DataRow("byte?", "byte?", "null")]
[DataRow("char?", "char?", "null")]
[DataRow("long?", "long?", "null")]
[DataRow("float?", "float?", "null")]
[DataRow("double?", "double?", "null")]
[DataRow("DateTimeOffset?", "DateTimeOffset?", "null")]
[DataRow("TimeSpan?", "TimeSpan?", "null")]
[DataRow("Guid?", "Guid?", "null")]
[DataRow("KeyValuePair<int, float>?", "KeyValuePair<int, float>?", "null")]

// Custom struct types
[DataRow("MyStruct", "MyStruct", "new PropertyMetadata(default(MyStruct))", "public struct MyStruct { public int X; }")]
[DataRow("MyStruct", "MyStruct", "new PropertyMetadata(default(MyStruct))", "public struct MyStruct { public string X { get; set; }")]

// Custom enum types
[DataRow("MyEnum", "MyEnum", "new PropertyMetadata(default(MyEnum))", "public enum MyEnum { A, B, C }")]
public void SingleProperty_MultipleTypes_WithNoCaching_DefaultValueIsOptimized(
string dependencyPropertyType,
string propertyType,
string propertyMetadataExpression,
string? typeDefinition = "")
{
string source = $$"""
using Windows.UI.Xaml;
using CommunityToolkit.WinUI;
#nullable enable
namespace MyNamespace;
{{typeDefinition}}
public partial class MyControl : DependencyObject
{
[GeneratedDependencyProperty]
public partial {{propertyType}} Name { get; set; }
}
""";

string result = $$"""
// <auto-generated/>
#pragma warning disable
#nullable enable
namespace MyNamespace
{
/// <inheritdoc cref="MyControl"/>
partial class MyControl
{
/// <summary>
/// The backing <see cref="global::Windows.UI.Xaml.DependencyProperty"/> instance for <see cref="Name"/>.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
public static readonly global::Windows.UI.Xaml.DependencyProperty NameProperty = global::Windows.UI.Xaml.DependencyProperty.Register(
name: "Name",
propertyType: typeof({{dependencyPropertyType}}),
ownerType: typeof(MyControl),
typeMetadata: {{propertyMetadataExpression}});
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial {{propertyType}} Name
{
get
{
object? __boxedValue = GetValue(NameProperty);
OnNameGet(ref __boxedValue);
{{propertyType}} __unboxedValue = ({{propertyType}})__boxedValue;
OnNameGet(ref __unboxedValue);
return __unboxedValue;
}
set
{
OnNameSet(ref value);
object? __boxedValue = value;
OnNameSet(ref __boxedValue);
SetValue(NameProperty, __boxedValue);
OnNameChanged(value);
}
}
/// <summary>Executes the logic for when the <see langword="get"/> accessor <see cref="Name"/> is invoked</summary>
/// <param name="propertyValue">The raw property value that has been retrieved from <see cref="NameProperty"/>.</param>
/// <remarks>This method is invoked on the boxed value retrieved via <see cref="GetValue"/> on <see cref="NameProperty"/>.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnNameGet(ref object? propertyValue);
/// <summary>Executes the logic for when the <see langword="get"/> accessor <see cref="Name"/> is invoked</summary>
/// <param name="propertyValue">The unboxed property value that has been retrieved from <see cref="NameProperty"/>.</param>
/// <remarks>This method is invoked on the unboxed value retrieved via <see cref="GetValue"/> on <see cref="NameProperty"/>.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnNameGet(ref {{propertyType}} propertyValue);
/// <summary>Executes the logic for when the <see langword="set"/> accessor <see cref="Name"/> is invoked</summary>
/// <param name="propertyValue">The boxed property value that has been produced before assigning to <see cref="NameProperty"/>.</param>
/// <remarks>This method is invoked on the boxed value that is about to be passed to <see cref="SetValue"/> on <see cref="NameProperty"/>.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnNameSet(ref object? propertyValue);
/// <summary>Executes the logic for when the <see langword="set"/> accessor <see cref="Name"/> is invoked</summary>
/// <param name="propertyValue">The property value that is being assigned to <see cref="Name"/>.</param>
/// <remarks>This method is invoked on the raw value being assigned to <see cref="Name"/>, before <see cref="SetValue"/> is used.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnNameSet(ref {{propertyType}} propertyValue);
/// <summary>Executes the logic for when <see cref="Name"/> has just changed.</summary>
/// <param name="value">The new property value that has been set.</param>
/// <remarks>This method is invoked right after the value of <see cref="Name"/> is changed.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnNameChanged({{propertyType}} newValue);
/// <summary>Executes the logic for when <see cref="Name"/> has just changed.</summary>
/// <param name="e">Event data that is issued by any event that tracks changes to the effective value of this property.</param>
/// <remarks>This method is invoked by the <see cref="global::Windows.UI.Xaml.DependencyProperty"/> infrastructure, after the value of <see cref="Name"/> is changed.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnNamePropertyChanged(global::Windows.UI.Xaml.DependencyPropertyChangedEventArgs e);
/// <summary>Executes the logic for when any dependency property has just changed.</summary>
/// <param name="e">Event data that is issued by any event that tracks changes to the effective value of this property.</param>
/// <remarks>This method is invoked by the <see cref="global::Windows.UI.Xaml.DependencyProperty"/> infrastructure, after the value of any dependency property has just changed.</remarks>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.WinUI.DependencyPropertyGenerator", <ASSEMBLY_VERSION>)]
partial void OnPropertyChanged(global::Windows.UI.Xaml.DependencyPropertyChangedEventArgs e);
}
}
""";

CSharpGeneratorTest<DependencyPropertyGenerator>.VerifySources(source, ("MyNamespace.MyControl.g.cs", result), languageVersion: LanguageVersion.CSharp13);
}
}

0 comments on commit be37f5f

Please sign in to comment.