Skip to content

Commit 4edcd88

Browse files
committed
Add unit tests for [AlsoBroadcastChange]
1 parent 668de52 commit 4edcd88

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,47 @@ public partial class MyViewModel
998998
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0020");
999999
}
10001000

1001+
[TestMethod]
1002+
public void FieldWithOrphanedDependentObservablePropertyAttributesError_AlsoBroadcastChange()
1003+
{
1004+
string source = @"
1005+
using CommunityToolkit.Mvvm.ComponentModel;
1006+
1007+
namespace MyApp
1008+
{
1009+
public partial class MyViewModel
1010+
{
1011+
[AlsoBroadcastChange]
1012+
public int number;
1013+
}
1014+
}";
1015+
1016+
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0020");
1017+
}
1018+
1019+
[TestMethod]
1020+
public void FieldWithOrphanedDependentObservablePropertyAttributesError_MultipleUsesStillGenerateOnlyASingleDiagnostic()
1021+
{
1022+
string source = @"
1023+
using CommunityToolkit.Mvvm.ComponentModel;
1024+
1025+
namespace MyApp
1026+
{
1027+
public partial class MyViewModel
1028+
{
1029+
[AlsoNotifyChangeFor("")]
1030+
[AlsoNotifyChangeFor("")]
1031+
[AlsoNotifyChangeFor("")]
1032+
[AlsoNotifyCanExecuteFor("")]
1033+
[AlsoNotifyCanExecuteFor("")]
1034+
[AlsoBroadcastChange]
1035+
public int number;
1036+
}
1037+
}";
1038+
1039+
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0020");
1040+
}
1041+
10011042
[TestMethod]
10021043
public void InvalidAttributeCombinationForObservableRecipientAttributeError()
10031044
{
@@ -1020,6 +1061,25 @@ public partial class B : A
10201061
VerifyGeneratedDiagnostics<ObservableRecipientGenerator>(source, "MVVMTK0021");
10211062
}
10221063

1064+
[TestMethod]
1065+
public void InvalidContainingTypeForAlsoBroadcastChangeFieldError_ObservableObject()
1066+
{
1067+
string source = @"
1068+
using CommunityToolkit.Mvvm.ComponentModel;
1069+
1070+
namespace MyApp
1071+
{
1072+
public partial class MyViewModel : ObservableObject
1073+
{
1074+
[ObservableProperty]
1075+
[AlsoBroadcastChange]
1076+
public int number;
1077+
}
1078+
}";
1079+
1080+
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0022");
1081+
}
1082+
10231083
/// <summary>
10241084
/// Verifies the output of a source generator.
10251085
/// </summary>

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Threading.Tasks;
1212
using CommunityToolkit.Mvvm.ComponentModel;
1313
using CommunityToolkit.Mvvm.Input;
14+
using CommunityToolkit.Mvvm.Messaging;
15+
using CommunityToolkit.Mvvm.Messaging.Messages;
1416
using Microsoft.VisualStudio.TestTools.UnitTesting;
1517

1618
namespace CommunityToolkit.Mvvm.UnitTests;
@@ -360,6 +362,61 @@ public void Test_OnPropertyChangingAndChangedPartialMethodWithAdditionalValidati
360362
Assert.AreEqual("B", model.Name);
361363
}
362364

365+
[TestMethod]
366+
public void Test_AlsoBroadcastChange_WithObservableObject()
367+
{
368+
Test_AlsoBroadcastChange_Test(
369+
factory: static messenger => new BroadcastingViewModel(messenger),
370+
setter: static (model, value) => model.Name = value,
371+
propertyName: nameof(BroadcastingViewModel.Name));
372+
}
373+
374+
[TestMethod]
375+
public void Test_AlsoBroadcastChange_WithObservableRecipientAttribute()
376+
{
377+
Test_AlsoBroadcastChange_Test(
378+
factory: static messenger => new BroadcastingViewModelWithAttribute(messenger),
379+
setter: static (model, value) => model.Name = value,
380+
propertyName: nameof(BroadcastingViewModelWithAttribute.Name));
381+
}
382+
383+
[TestMethod]
384+
public void Test_AlsoBroadcastChange_WithInheritedObservableRecipientAttribute()
385+
{
386+
Test_AlsoBroadcastChange_Test(
387+
factory: static messenger => new BroadcastingViewModelWithInheritedAttribute(messenger),
388+
setter: static (model, value) => model.Name2 = value,
389+
propertyName: nameof(BroadcastingViewModelWithInheritedAttribute.Name2));
390+
}
391+
392+
private void Test_AlsoBroadcastChange_Test<T>(Func<IMessenger, T> factory, Action<T, string?> setter, string propertyName)
393+
where T : notnull
394+
{
395+
IMessenger messenger = new StrongReferenceMessenger();
396+
397+
T model = factory(messenger);
398+
399+
List<(object Sender, PropertyChangedMessage<string?> Message)> messages = new();
400+
401+
messenger.Register<PropertyChangedMessage<string?>>(model, (r, m) => messages.Add((r, m)));
402+
403+
setter(model, "Bob");
404+
405+
Assert.AreEqual(1, messages.Count);
406+
Assert.AreSame(model, messages[0].Sender);
407+
Assert.AreEqual(null, messages[0].Message.OldValue);
408+
Assert.AreEqual("Bob", messages[0].Message.NewValue);
409+
Assert.AreEqual(propertyName, messages[0].Message.PropertyName);
410+
411+
setter(model, "Ross");
412+
413+
Assert.AreEqual(2, messages.Count);
414+
Assert.AreSame(model, messages[1].Sender);
415+
Assert.AreEqual("Bob", messages[1].Message.OldValue);
416+
Assert.AreEqual("Ross", messages[1].Message.NewValue);
417+
Assert.AreEqual(propertyName, messages[0].Message.PropertyName);
418+
}
419+
363420
public partial class SampleModel : ObservableObject
364421
{
365422
/// <summary>
@@ -615,4 +672,36 @@ protected override void OnPropertyChanged(PropertyChangedEventArgs e)
615672
Assert.AreEqual(nameof(Name), e.PropertyName);
616673
}
617674
}
675+
676+
partial class BroadcastingViewModel : ObservableRecipient
677+
{
678+
public BroadcastingViewModel(IMessenger messenger)
679+
: base(messenger)
680+
{
681+
}
682+
683+
[ObservableProperty]
684+
[AlsoBroadcastChange]
685+
private string? name;
686+
}
687+
688+
[ObservableRecipient]
689+
partial class BroadcastingViewModelWithAttribute : ObservableObject
690+
{
691+
[ObservableProperty]
692+
[AlsoBroadcastChange]
693+
private string? name;
694+
}
695+
696+
partial class BroadcastingViewModelWithInheritedAttribute : BroadcastingViewModelWithAttribute
697+
{
698+
public BroadcastingViewModelWithInheritedAttribute(IMessenger messenger)
699+
: base(messenger)
700+
{
701+
}
702+
703+
[ObservableProperty]
704+
[AlsoBroadcastChange]
705+
private string? name2;
706+
}
618707
}

0 commit comments

Comments
 (0)