Skip to content

Commit bc6b5b6

Browse files
Add Support For Delegate
1 parent f815aa7 commit bc6b5b6

3 files changed

Lines changed: 232 additions & 44 deletions

File tree

src/CommunityToolkit.Maui.Analyzers.UnitTests/AttachedBindablePropertyDefaultValueCreatorAnalyzerTests.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ public static partial class TestContainer
272272

273273
await VerifyAnalyzerAsync(source,
274274
Diagnostic()
275+
.WithSpan(12, 3, 12, 109)
275276
.WithSeverity(DiagnosticSeverity.Warning)
276-
.WithArguments("CreateDefaultStateViews"));
277+
.WithArguments("CreateStateViewsDelegate"));
277278
}
278279

279280
[Fact]
@@ -421,52 +422,57 @@ await VerifyAnalyzerAsync(
421422
}
422423

423424
[Fact]
424-
public async Task VerifyNoErrorWhenAttributeIsNotAttachedBindableProperty()
425+
public async Task VerifyErrorWhenDefaultValueCreatorMethodReturnsStaticNonReadonlyProperty()
425426
{
426427
const string source =
427428
/* language=C#-test */
428429
//lang=csharp
429430
"""
430431
#nullable enable
431432
#pragma warning disable MCTEXP001
432-
using System;
433433
using System.Collections.Generic;
434+
using CommunityToolkit.Maui;
434435
using Microsoft.Maui.Controls;
435436
436437
namespace CommunityToolkit.Maui.UnitTests
437438
{
438-
[Obsolete("Test")]
439+
[AttachedBindableProperty<IList<View>>("StateViews", DefaultValueCreatorMethodName = nameof(CreateDefaultStateViews))]
439440
public static partial class TestContainer
440441
{
441-
static readonly IList<View> DefaultStateViews = new List<View>();
442+
static IList<View> DefaultStateViews { get; set; } = new List<View>();
442443
443444
static IList<View> CreateDefaultStateViews(BindableObject bindable) => DefaultStateViews;
444445
}
445446
}
446447
""";
447448

448-
await VerifyAnalyzerAsync(source);
449+
await VerifyAnalyzerAsync(
450+
source,
451+
Diagnostic()
452+
.WithSpan(14, 3, 14, 92)
453+
.WithSeverity(DiagnosticSeverity.Warning)
454+
.WithArguments("CreateDefaultStateViews"));
449455
}
450456

451457
[Fact]
452-
public async Task VerifyNoErrorWhenDefaultValueCreatorMethodReturnsStaticNonReadonlyProperty()
458+
public async Task VerifyNoErrorWhenAttributeIsNotAttachedBindableProperty()
453459
{
454460
const string source =
455461
/* language=C#-test */
456462
//lang=csharp
457463
"""
458464
#nullable enable
459465
#pragma warning disable MCTEXP001
466+
using System;
460467
using System.Collections.Generic;
461-
using CommunityToolkit.Maui;
462468
using Microsoft.Maui.Controls;
463469
464470
namespace CommunityToolkit.Maui.UnitTests
465471
{
466-
[AttachedBindableProperty<IList<View>>("StateViews", DefaultValueCreatorMethodName = nameof(CreateDefaultStateViews))]
472+
[Obsolete("Test")]
467473
public static partial class TestContainer
468474
{
469-
static IList<View> DefaultStateViews { get; set; } = new List<View>();
475+
static readonly IList<View> DefaultStateViews = new List<View>();
470476
471477
static IList<View> CreateDefaultStateViews(BindableObject bindable) => DefaultStateViews;
472478
}

src/CommunityToolkit.Maui.Analyzers.UnitTests/BindablePropertyDefaultValueCreatorAnalyzerTests.cs

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ await VerifyAnalyzerAsync(
458458
}
459459

460460
[Fact]
461-
public async Task VerifyNoErrorWhenAttributeIsNotBindableProperty()
461+
public async Task VerifyNoErrorWhenDefaultValueCreatorMethodReturnsStaticNonReadonlyProperty()
462462
{
463463
const string source =
464464
/* language=C#-test */
@@ -467,27 +467,40 @@ public async Task VerifyNoErrorWhenAttributeIsNotBindableProperty()
467467
#nullable enable
468468
#pragma warning disable MCTEXP001
469469
#pragma warning disable CS9248
470-
using System;
471470
using System.Collections.Generic;
471+
using CommunityToolkit.Maui;
472472
using Microsoft.Maui.Controls;
473473
474474
namespace CommunityToolkit.Maui.UnitTests
475475
{
476-
[Obsolete("Test")]
477476
public partial class TestControl : View
478477
{
479-
static readonly IList<View> DefaultStateViews = new List<View>();
478+
[BindableProperty(DefaultValueCreatorMethodName = nameof(CreateDefaultStateViews))]
479+
public partial IList<View> StateViews { get; set; }
480+
481+
static IList<View> DefaultStateViews { get; set; } = new List<View>();
480482
481483
static IList<View> CreateDefaultStateViews(BindableObject bindable) => DefaultStateViews;
482484
}
485+
486+
public partial class TestControl
487+
{
488+
public static readonly global::Microsoft.Maui.Controls.BindableProperty? StateViewsProperty;
489+
public partial IList<View> StateViews { get => false ? field : (IList<View>)GetValue(StateViewsProperty); set => SetValue(StateViewsProperty, value); }
490+
}
483491
}
484492
""";
485493

486-
await VerifyAnalyzerAsync(source);
494+
await VerifyAnalyzerAsync(
495+
source,
496+
Diagnostic()
497+
.WithSpan(17, 3, 17, 92)
498+
.WithSeverity(DiagnosticSeverity.Warning)
499+
.WithArguments("CreateDefaultStateViews"));
487500
}
488501

489502
[Fact]
490-
public async Task VerifyNoErrorWhenDefaultValueCreatorMethodReturnsStaticNonReadonlyProperty()
503+
public async Task VerifyNoErrorWhenAttributeIsNotBindableProperty()
491504
{
492505
const string source =
493506
/* language=C#-test */
@@ -496,27 +509,19 @@ public async Task VerifyNoErrorWhenDefaultValueCreatorMethodReturnsStaticNonRead
496509
#nullable enable
497510
#pragma warning disable MCTEXP001
498511
#pragma warning disable CS9248
512+
using System;
499513
using System.Collections.Generic;
500-
using CommunityToolkit.Maui;
501514
using Microsoft.Maui.Controls;
502515
503516
namespace CommunityToolkit.Maui.UnitTests
504517
{
518+
[Obsolete("Test")]
505519
public partial class TestControl : View
506520
{
507-
[BindableProperty(DefaultValueCreatorMethodName = nameof(CreateDefaultStateViews))]
508-
public partial IList<View> StateViews { get; set; }
509-
510-
static IList<View> DefaultStateViews { get; set; } = new List<View>();
521+
static readonly IList<View> DefaultStateViews = new List<View>();
511522
512523
static IList<View> CreateDefaultStateViews(BindableObject bindable) => DefaultStateViews;
513524
}
514-
515-
public partial class TestControl
516-
{
517-
public static readonly global::Microsoft.Maui.Controls.BindableProperty? StateViewsProperty;
518-
public partial IList<View> StateViews { get => false ? field : (IList<View>)GetValue(StateViewsProperty); set => SetValue(StateViewsProperty, value); }
519-
}
520525
}
521526
""";
522527

@@ -609,6 +614,47 @@ await VerifyAnalyzerAsync(
609614
.WithArguments("CreateDefaultStateViews"));
610615
}
611616

617+
[Fact]
618+
public async Task VerifyErrorWhenDefaultValueCreatorMethodReturnsCreateDefaultValueDelegateThatReturnsAStaticMember()
619+
{
620+
const string source =
621+
/* language=C#-test */
622+
//lang=csharp
623+
"""
624+
#nullable enable
625+
#pragma warning disable MCTEXP001
626+
using System.Collections.Generic;
627+
using CommunityToolkit.Maui;
628+
using Microsoft.Maui.Controls;
629+
630+
namespace CommunityToolkit.Maui.UnitTests
631+
{
632+
public partial class TestControl : View
633+
{
634+
[BindableProperty(DefaultValueCreatorMethodName = nameof(CreateStateViewsDelegate))]
635+
public partial IList<View>? StateViews { get; set; }
636+
637+
static readonly BindableProperty.CreateDefaultValueDelegate CreateStateViewsDelegate = _ => StateViewsList;
638+
639+
static List<View> StateViewsList { get; } = [];
640+
}
641+
642+
public partial class TestControl
643+
{
644+
public static readonly global::Microsoft.Maui.Controls.BindableProperty? StateViewsProperty;
645+
public partial IList<View> StateViews { get => false ? field : (IList<View>)GetValue(StateViewsProperty); set => SetValue(StateViewsProperty, value); }
646+
}
647+
}
648+
""";
649+
650+
await VerifyAnalyzerAsync(
651+
source,
652+
Diagnostic()
653+
.WithSpan(14, 3, 14, 110)
654+
.WithSeverity(DiagnosticSeverity.Warning)
655+
.WithArguments("CreateStateViewsDelegate"));
656+
}
657+
612658
[Fact]
613659
public async Task VerifyNoErrorWhenDefaultValueCreatorMethodReturnsLiteral()
614660
{
@@ -713,6 +759,40 @@ public partial class TestControl
713759
await VerifyAnalyzerAsync(source);
714760
}
715761

762+
[Fact]
763+
public async Task VerifyNoErrorWhenDefaultValueCreatorMethodReturnsCreateDefaultValueDelegateThatReturnsANewInstance()
764+
{
765+
const string source =
766+
/* language=C#-test */
767+
//lang=csharp
768+
"""
769+
#nullable enable
770+
#pragma warning disable MCTEXP001
771+
using System.Collections.Generic;
772+
using CommunityToolkit.Maui;
773+
using Microsoft.Maui.Controls;
774+
775+
namespace CommunityToolkit.Maui.UnitTests
776+
{
777+
public partial class TestControl : View
778+
{
779+
[BindableProperty(DefaultValueCreatorMethodName = nameof(CreateStateViewsDelegate))]
780+
public partial IList<View>? StateViews { get; set; }
781+
782+
static readonly BindableProperty.CreateDefaultValueDelegate CreateStateViewsDelegate = (x) => new List<View>();
783+
}
784+
785+
public partial class TestControl
786+
{
787+
public static readonly global::Microsoft.Maui.Controls.BindableProperty? StateViewsProperty;
788+
public partial IList<View> StateViews { get => false ? field : (IList<View>)GetValue(StateViewsProperty); set => SetValue(StateViewsProperty, value); }
789+
}
790+
}
791+
""";
792+
793+
await VerifyAnalyzerAsync(source);
794+
}
795+
716796
static Task VerifyAnalyzerAsync(string source, params IReadOnlyList<DiagnosticResult> expected)
717797
{
718798
return CSharpAnalyzerVerifier<BindablePropertyDefaultValueCreatorAnalyzer>

0 commit comments

Comments
 (0)