Skip to content

Commit 0bf8edd

Browse files
committed
Add more unit tests for generic types
1 parent e3a8de1 commit 0bf8edd

File tree

1 file changed

+169
-0
lines changed
  • components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests

1 file changed

+169
-0
lines changed

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,78 @@ public partial class MyControl : Control
12171217
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
12181218
}
12191219

1220+
[TestMethod]
1221+
public async Task InvalidPropertyNullableAnnotationAnalyzer_InsideGeneric_NullableType_NotRequired_WithNotNull_NullResilientGetter_DoesNotWarn()
1222+
{
1223+
const string source = """
1224+
using System.Diagnostics.CodeAnalysis;
1225+
using CommunityToolkit.WinUI;
1226+
using Windows.UI.Xaml;
1227+
1228+
#nullable enable
1229+
1230+
namespace MyApp;
1231+
1232+
public abstract partial class Animation<TValue, TKeyFrame> : DependencyObject
1233+
where TKeyFrame : unmanaged
1234+
{
1235+
[GeneratedDependencyProperty]
1236+
[NotNull]
1237+
public partial KeyFrameCollection<TValue, TKeyFrame>? {|CS9248:KeyFrames|} { get; set; }
1238+
1239+
partial void {|CS0759:OnKeyFramesGet|}([NotNull] ref KeyFrameCollection<TValue, TKeyFrame>? propertyValue)
1240+
{
1241+
propertyValue = new();
1242+
}
1243+
1244+
partial void {|CS0759:OnKeyFramesPropertyChanged|}(DependencyPropertyChangedEventArgs e)
1245+
{
1246+
}
1247+
}
1248+
1249+
public sealed partial class KeyFrameCollection<TValue, TKeyFrame> : DependencyObjectCollection
1250+
where TKeyFrame : unmanaged;
1251+
""";
1252+
1253+
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
1254+
}
1255+
1256+
[TestMethod]
1257+
public async Task InvalidPropertyNullableAnnotationAnalyzer_InsideGeneric_NotNullableType_NotRequired_WithAllowNull_NullResilientGetter_DoesNotWarn()
1258+
{
1259+
const string source = """
1260+
using System.Diagnostics.CodeAnalysis;
1261+
using CommunityToolkit.WinUI;
1262+
using Windows.UI.Xaml;
1263+
1264+
#nullable enable
1265+
1266+
namespace MyApp;
1267+
1268+
public abstract partial class Animation<TValue, TKeyFrame> : DependencyObject
1269+
where TKeyFrame : unmanaged
1270+
{
1271+
[GeneratedDependencyProperty]
1272+
[AllowNull]
1273+
public partial KeyFrameCollection<TValue, TKeyFrame> {|CS9248:KeyFrames|} { get; set; }
1274+
1275+
partial void {|CS0759:OnKeyFramesGet|}([NotNull] ref KeyFrameCollection<TValue, TKeyFrame>? propertyValue)
1276+
{
1277+
propertyValue = new();
1278+
}
1279+
1280+
partial void {|CS0759:OnKeyFramesPropertyChanged|}(DependencyPropertyChangedEventArgs e)
1281+
{
1282+
}
1283+
}
1284+
1285+
public sealed partial class KeyFrameCollection<TValue, TKeyFrame> : DependencyObjectCollection
1286+
where TKeyFrame : unmanaged;
1287+
""";
1288+
1289+
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
1290+
}
1291+
12201292
[TestMethod]
12211293
public async Task InvalidPropertyNullableAnnotationAnalyzer_NullableType_Warns()
12221294
{
@@ -1320,6 +1392,103 @@ public partial class MyControl : Control
13201392
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
13211393
}
13221394

1395+
[TestMethod]
1396+
public async Task InvalidPropertyNullableAnnotationAnalyzer_InsideGeneric_NullableType_NotRequired_WithNotNull_Warns()
1397+
{
1398+
const string source = """
1399+
using System.Diagnostics.CodeAnalysis;
1400+
using CommunityToolkit.WinUI;
1401+
using Windows.UI.Xaml;
1402+
1403+
#nullable enable
1404+
1405+
namespace MyApp;
1406+
1407+
public abstract partial class Animation<TValue, TKeyFrame> : DependencyObject
1408+
where TKeyFrame : unmanaged
1409+
{
1410+
[{|WCTDP0025:GeneratedDependencyProperty|}]
1411+
[NotNull]
1412+
public partial KeyFrameCollection<TValue, TKeyFrame>? {|CS9248:KeyFrames|} { get; set; }
1413+
1414+
partial void {|CS0759:OnKeyFramesGet|}(ref KeyFrameCollection<TValue, TKeyFrame>? propertyValue)
1415+
{
1416+
}
1417+
1418+
partial void {|CS0759:OnKeyFramesPropertyChanged|}(DependencyPropertyChangedEventArgs e)
1419+
{
1420+
}
1421+
}
1422+
1423+
public sealed partial class KeyFrameCollection<TValue, TKeyFrame> : DependencyObjectCollection
1424+
where TKeyFrame : unmanaged;
1425+
""";
1426+
1427+
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
1428+
}
1429+
1430+
[TestMethod]
1431+
public async Task InvalidPropertyNullableAnnotationAnalyzer_InsideGeneric_NotNullableType_NotRequired_WithAllowNull_Warns()
1432+
{
1433+
const string source = """
1434+
using System.Diagnostics.CodeAnalysis;
1435+
using CommunityToolkit.WinUI;
1436+
using Windows.UI.Xaml;
1437+
1438+
#nullable enable
1439+
1440+
namespace MyApp;
1441+
1442+
public abstract partial class Animation<TValue, TKeyFrame> : DependencyObject
1443+
where TKeyFrame : unmanaged
1444+
{
1445+
[{|WCTDP0009:{|WCTDP0024:GeneratedDependencyProperty|}|}]
1446+
[AllowNull]
1447+
public partial KeyFrameCollection<TValue, TKeyFrame> {|CS9248:KeyFrames|} { get; set; }
1448+
1449+
partial void {|CS0759:OnKeyFramesGet|}(ref KeyFrameCollection<TValue, TKeyFrame>? propertyValue)
1450+
{
1451+
}
1452+
1453+
partial void {|CS0759:OnKeyFramesPropertyChanged|}(DependencyPropertyChangedEventArgs e)
1454+
{
1455+
}
1456+
}
1457+
1458+
public sealed partial class KeyFrameCollection<TValue, TKeyFrame> : DependencyObjectCollection
1459+
where TKeyFrame : unmanaged;
1460+
""";
1461+
1462+
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
1463+
}
1464+
1465+
[TestMethod]
1466+
public async Task InvalidPropertyNullableAnnotationAnalyzer_InsideGeneric_NotNullableType_rEQUIRED_WithAllowNull_Warns()
1467+
{
1468+
const string source = """
1469+
using System.Diagnostics.CodeAnalysis;
1470+
using CommunityToolkit.WinUI;
1471+
using Windows.UI.Xaml;
1472+
1473+
#nullable enable
1474+
1475+
namespace MyApp;
1476+
1477+
public abstract partial class Animation<TValue, TKeyFrame> : DependencyObject
1478+
where TKeyFrame : unmanaged
1479+
{
1480+
[{|WCTDP0024:GeneratedDependencyProperty|}]
1481+
[AllowNull]
1482+
public required partial KeyFrameCollection<TValue, TKeyFrame> {|CS9248:KeyFrames|} { get; set; }
1483+
}
1484+
1485+
public sealed partial class KeyFrameCollection<TValue, TKeyFrame> : DependencyObjectCollection
1486+
where TKeyFrame : unmanaged;
1487+
""";
1488+
1489+
await CSharpAnalyzerTest<InvalidPropertyNullableAnnotationAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
1490+
}
1491+
13231492
[TestMethod]
13241493
public async Task InvalidPropertyDefaultValueTypeAnalyzer_NoAttribute_DoesNotWarn()
13251494
{

0 commit comments

Comments
 (0)