Skip to content

Commit 759c6b3

Browse files
committed
Add unit test for diagnostics on inherited members
1 parent 69707bf commit 759c6b3

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,75 @@ public void Test_ObservableProperty_NullabilityAnnotations_Complex()
515515
}
516516
#endif
517517

518+
// See https://github.com/CommunityToolkit/dotnet/issues/201
519+
[TestMethod]
520+
public void Test_ObservableProperty_InheritedMembersAsAttributeTargets()
521+
{
522+
ConcreteViewModel model = new();
523+
524+
List<string?> propertyNames = new();
525+
List<object?> canExecuteChangedArgs = new();
526+
527+
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
528+
model.DoSomethingCommand.CanExecuteChanged += (s, _) => canExecuteChangedArgs.Add(s);
529+
model.ManualCommand.CanExecuteChanged += (s, _) => canExecuteChangedArgs.Add(s);
530+
531+
model.A = nameof(model.A);
532+
model.B = nameof(model.B);
533+
model.C = nameof(model.C);
534+
model.D = nameof(model.D);
535+
536+
CollectionAssert.AreEqual(new[]
537+
{
538+
nameof(model.A),
539+
nameof(model.Content),
540+
nameof(model.B),
541+
nameof(model.SomeGeneratedProperty),
542+
nameof(model.C),
543+
nameof(model.D)
544+
}, propertyNames);
545+
546+
CollectionAssert.AreEqual(new[] { model.DoSomethingCommand, model.ManualCommand }, canExecuteChangedArgs);
547+
}
548+
549+
public abstract partial class BaseViewModel : ObservableObject
550+
{
551+
public string? Content { get; set; }
552+
553+
[ObservableProperty]
554+
private string? someGeneratedProperty;
555+
556+
[ICommand]
557+
private void DoSomething()
558+
{
559+
}
560+
561+
public IRelayCommand ManualCommand { get; } = new RelayCommand(() => { });
562+
}
563+
564+
public partial class ConcreteViewModel : BaseViewModel
565+
{
566+
// Inherited property
567+
[ObservableProperty]
568+
[AlsoNotifyChangeFor(nameof(Content))]
569+
private string? _a;
570+
571+
// Inherited generated property
572+
[ObservableProperty]
573+
[AlsoNotifyChangeFor(nameof(SomeGeneratedProperty))]
574+
private string? _b;
575+
576+
// Inherited generated command
577+
[ObservableProperty]
578+
[AlsoNotifyCanExecuteFor(nameof(DoSomethingCommand))]
579+
private string? _c;
580+
581+
// Inherited manual command
582+
[ObservableProperty]
583+
[AlsoNotifyCanExecuteFor(nameof(ManualCommand))]
584+
private string? _d;
585+
}
586+
518587
public partial class SampleModel : ObservableObject
519588
{
520589
/// <summary>

0 commit comments

Comments
 (0)