Skip to content

Commit 909923d

Browse files
committed
Add more analyzer unit tests
1 parent 1cad5f3 commit 909923d

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Diagnostics/Analyzers/UseGeneratedDependencyPropertyOnManualPropertyAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
416416
{
417417
// Here we need to special case non nullable value types that are not well known WinRT projected types.
418418
// In this case, we cannot rely on XAML calling their default constructor. Rather, we need to preserve
419-
// the explicit 'null' value that users had in their code. The analyzer will then warn on these cases
419+
// the explicit 'null' value that users had in their code. The analyzer will then warn on these cases.
420420
if (!propertyTypeSymbol.IsDefaultValueNull() &&
421421
!propertyTypeSymbol.IsWellKnownWinRTProjectedValueType(useWindowsUIXaml))
422422
{

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_Analyzers.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,16 @@ public string? Name
22302230
}
22312231

22322232
[TestMethod]
2233+
[DataRow("global::System.Numerics.Matrix3x2?", "global::System.Numerics.Matrix3x2?", "default(global::System.Numerics.Matrix3x2)")]
2234+
[DataRow("global::System.Numerics.Matrix4x4?", "global::System.Numerics.Matrix4x4?", "default(global::System.Numerics.Matrix4x4)")]
2235+
[DataRow("global::System.Numerics.Plane?", "global::System.Numerics.Plane?", "default(global::System.Numerics.Plane)")]
2236+
[DataRow("global::System.Numerics.Quaternion?", "global::System.Numerics.Quaternion?", "default(global::System.Numerics.Quaternion)")]
2237+
[DataRow("global::System.Numerics.Vector2?", "global::System.Numerics.Vector2?", "default(global::System.Numerics.Vector2)")]
2238+
[DataRow("global::System.Numerics.Vector3?", "global::System.Numerics.Vector3?", "default(global::System.Numerics.Vector3)")]
2239+
[DataRow("global::System.Numerics.Vector4?", "global::System.Numerics.Vector4?", "default(global::System.Numerics.Vector4)")]
2240+
[DataRow("global::Windows.Foundation.Point?", "global::Windows.Foundation.Point?", "default(global::Windows.Foundation.Point)")]
2241+
[DataRow("global::Windows.Foundation.Rect?", "global::Windows.Foundation.Rect?", "default(global::Windows.Foundation.Rect)")]
2242+
[DataRow("global::Windows.Foundation.Size?", "global::Windows.Foundation.Size?", "default(global::Windows.Foundation.Size)")]
22332243
[DataRow("global::System.TimeSpan", "global::System.TimeSpan", "global::System.TimeSpan.FromSeconds(1)")]
22342244
[DataRow("global::System.TimeSpan?", "global::System.TimeSpan?", "global::System.TimeSpan.FromSeconds(1)")]
22352245
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_ValidProperty_ExplicitDefaultValue_DoesNotWarn(
@@ -2267,6 +2277,52 @@ public enum MyEnum { A, B, C }
22672277
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
22682278
}
22692279

2280+
// Using 'default(T)' is not a constant (therefore it's not allowed as attribute argument), even if constrained to an enum type.
2281+
// Some of these combinations (eg. 'object' property with 'T1?' backing type) are also just flat out invalid and would error out.
2282+
[TestMethod]
2283+
[DataRow("T1?", "T1?", "new PropertyMetadata(default(T1))")]
2284+
[DataRow("T1", "object", "new PropertyMetadata(default(T1))")]
2285+
[DataRow("T1?", "object", "new PropertyMetadata(default(T1))")]
2286+
[DataRow("object", "T1?", "new PropertyMetadata(default(T1))")]
2287+
[DataRow("T2?", "T2?", "new PropertyMetadata(default(T2))")]
2288+
[DataRow("T2", "object", "new PropertyMetadata(default(T2))")]
2289+
[DataRow("T2?", "object", "new PropertyMetadata(default(T2))")]
2290+
[DataRow("object", "T2?", "new PropertyMetadata(default(T2))")]
2291+
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_ValidProperty_ExplicitDefaultValue_ConstrainedGeneric_DoesNotWarn(
2292+
string dependencyPropertyType,
2293+
string propertyType,
2294+
string propertyMetadataExpression)
2295+
{
2296+
string source = $$"""
2297+
using System;
2298+
using Windows.UI.Xaml;
2299+
using Windows.UI.Xaml.Controls;
2300+
2301+
#nullable enable
2302+
2303+
namespace MyApp;
2304+
2305+
public partial class MyControl<T1, T2> : Control
2306+
where T1 : struct, Enum
2307+
where T2 : unmanaged, Enum
2308+
{
2309+
public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
2310+
name: "Name",
2311+
propertyType: typeof({{dependencyPropertyType}}),
2312+
ownerType: typeof(MyControl<T1, T2>),
2313+
typeMetadata: {{propertyMetadataExpression}});
2314+
2315+
public {{propertyType}} Name
2316+
{
2317+
get => ({{propertyType}})GetValue(NameProperty);
2318+
set => SetValue(NameProperty, value);
2319+
}
2320+
}
2321+
""";
2322+
2323+
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
2324+
}
2325+
22702326
[TestMethod]
22712327
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_ValidProperty_WithInvalidAttribute_DoesNotWarn()
22722328
{
@@ -2409,6 +2465,7 @@ public class TestAttribute : Attribute;
24092465
[DataRow("int?", "int?", "null")]
24102466
[DataRow("int?", "int?", "0")]
24112467
[DataRow("int?", "int?", "42")]
2468+
[DataRow("int?", "int?", "default(int)")]
24122469
[DataRow("int?", "int?", "default(int?)")]
24132470
[DataRow("int?", "int?", "null")]
24142471
[DataRow("global::System.Numerics.Matrix3x2", "global::System.Numerics.Matrix3x2", "default(global::System.Numerics.Matrix3x2)")]
@@ -2424,6 +2481,14 @@ public class TestAttribute : Attribute;
24242481
[DataRow("global::Windows.UI.Xaml.Visibility", "global::Windows.UI.Xaml.Visibility", "default(global::Windows.UI.Xaml.Visibility)")]
24252482
[DataRow("global::Windows.UI.Xaml.Visibility", "global::Windows.UI.Xaml.Visibility", "global::Windows.UI.Xaml.Visibility.Visible")]
24262483
[DataRow("global::Windows.UI.Xaml.Visibility", "global::Windows.UI.Xaml.Visibility", "global::Windows.UI.Xaml.Visibility.Collapsed")]
2484+
[DataRow("global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility?", "default(global::Windows.UI.Xaml.Visibility)")]
2485+
[DataRow("global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility?", "default(global::Windows.UI.Xaml.Visibility?)")]
2486+
[DataRow("global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility.Visible")]
2487+
[DataRow("global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility.Collapsed")]
2488+
[DataRow("object", "global::Windows.UI.Xaml.Visibility?", "default(global::Windows.UI.Xaml.Visibility)")]
2489+
[DataRow("object", "global::Windows.UI.Xaml.Visibility?", "default(global::Windows.UI.Xaml.Visibility?)")]
2490+
[DataRow("object", "global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility.Visible")]
2491+
[DataRow("object", "global::Windows.UI.Xaml.Visibility?", "global::Windows.UI.Xaml.Visibility.Collapsed")]
24272492
[DataRow("global::System.TimeSpan", "global::System.TimeSpan", "default(System.TimeSpan)")]
24282493
[DataRow("global::System.DateTimeOffset", "global::System.DateTimeOffset", "default(global::System.DateTimeOffset)")]
24292494
[DataRow("global::System.DateTimeOffset?", "global::System.DateTimeOffset?", "null")]
@@ -2437,7 +2502,14 @@ public class TestAttribute : Attribute;
24372502
[DataRow("global::MyApp.MyStruct?", "global::MyApp.MyStruct?", "default(global::MyApp.MyStruct?)")]
24382503
[DataRow("global::MyApp.MyEnum", "global::MyApp.MyEnum", "default(global::MyApp.MyEnum)")]
24392504
[DataRow("global::MyApp.MyEnum?", "global::MyApp.MyEnum?", "null")]
2505+
[DataRow("global::MyApp.MyEnum?", "global::MyApp.MyEnum?", "global::MyApp.MyEnum.A")]
2506+
[DataRow("global::MyApp.MyEnum?", "global::MyApp.MyEnum?", "global::MyApp.MyEnum.B")]
2507+
[DataRow("global::MyApp.MyEnum?", "global::MyApp.MyEnum?", "default(global::MyApp.MyEnum)")]
24402508
[DataRow("global::MyApp.MyEnum?", "global::MyApp.MyEnum?", "default(global::MyApp.MyEnum?)")]
2509+
[DataRow("object", "global::MyApp.MyEnum?", "global::MyApp.MyEnum.A")]
2510+
[DataRow("object", "global::MyApp.MyEnum?", "global::MyApp.MyEnum.B")]
2511+
[DataRow("object", "global::MyApp.MyEnum?", "default(global::MyApp.MyEnum)")]
2512+
[DataRow("object", "global::MyApp.MyEnum?", "default(global::MyApp.MyEnum?)")]
24412513
[DataRow("global::MyApp.MyClass", "global::MyApp.MyClass", "null")]
24422514
[DataRow("global::MyApp.MyClass", "global::MyApp.MyClass", "default(global::MyApp.MyClass)")]
24432515
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_ValidProperty_ExplicitDefaultValue_Warns(
@@ -2476,6 +2548,55 @@ public class MyClass { }
24762548
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
24772549
}
24782550

2551+
// Using the declared property type as first argument here for clarity when reading all combinations
2552+
[TestMethod]
2553+
[DataRow("T1?", "T1?", "new PropertyMetadata(default(T1?))")]
2554+
[DataRow("T1?", "object", "new PropertyMetadata(default(T1?))")]
2555+
[DataRow("T1?", "T1?", "new PropertyMetadata(null)")]
2556+
[DataRow("T1?", "object", "new PropertyMetadata(null)")]
2557+
[DataRow("T1?", "T1?", "null")]
2558+
[DataRow("T1?", "object", "null")]
2559+
[DataRow("T2?", "T2?", "new PropertyMetadata(default(T2?))")]
2560+
[DataRow("T2?", "object", "new PropertyMetadata(default(T2?))")]
2561+
[DataRow("T2?", "T2?", "new PropertyMetadata(null)")]
2562+
[DataRow("T2?", "object", "new PropertyMetadata(null)")]
2563+
[DataRow("T2?", "T2?", "null")]
2564+
[DataRow("T2?", "object", "null")]
2565+
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_ValidProperty_ExplicitDefaultValue_ConstrainedGeneric_Warns(
2566+
string propertyType,
2567+
string dependencyPropertyType,
2568+
string propertyMetadataExpression)
2569+
{
2570+
string source = $$"""
2571+
using System;
2572+
using Windows.UI.Xaml;
2573+
using Windows.UI.Xaml.Controls;
2574+
2575+
#nullable enable
2576+
2577+
namespace MyApp;
2578+
2579+
public partial class MyControl<T1, T2> : Control
2580+
where T1 : struct, Enum
2581+
where T2 : unmanaged, Enum
2582+
{
2583+
public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
2584+
name: "Name",
2585+
propertyType: typeof({{dependencyPropertyType}}),
2586+
ownerType: typeof(MyControl<T1, T2>),
2587+
typeMetadata: {{propertyMetadataExpression}});
2588+
2589+
public {{propertyType}} {|WCTDP0017:Name|}
2590+
{
2591+
get => ({{propertyType}})GetValue(NameProperty);
2592+
set => SetValue(NameProperty, value);
2593+
}
2594+
}
2595+
""";
2596+
2597+
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
2598+
}
2599+
24792600
[TestMethod]
24802601
[DataRow("string?", "object")]
24812602
[DataRow("MyControl", "DependencyObject")]

0 commit comments

Comments
 (0)