Skip to content

Commit f92fef7

Browse files
committed
Add more unit tests for generics with [ObservableProperty]
This covers all cases from #224 as well.
1 parent a0f0b03 commit f92fef7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,29 @@ public void Test_ObservableProperty_InheritedMembersAsAttributeTargets()
546546
CollectionAssert.AreEqual(new[] { model.DoSomethingCommand, model.ManualCommand }, canExecuteChangedArgs);
547547
}
548548

549+
// See https://github.com/CommunityToolkit/dotnet/issues/224
550+
[TestMethod]
551+
public void Test_ObservableProperty_WithinGenericTypeWithMultipleTypeParameters()
552+
{
553+
ModelWithMultipleGenericParameters<int, string> model = new();
554+
555+
List<string?> propertyNames = new();
556+
557+
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
558+
559+
model.Value = true;
560+
model.TValue = 42;
561+
model.UValue = "Hello";
562+
model.List = new List<int>() { 420 };
563+
564+
Assert.AreEqual(model.Value, true);
565+
Assert.AreEqual(model.TValue, 42);
566+
Assert.AreEqual(model.UValue, "Hello");
567+
CollectionAssert.AreEqual(new[] { 420 }, model.List);
568+
569+
CollectionAssert.AreEqual(new[] { nameof(model.Value), nameof(model.TValue), nameof(model.UValue), nameof(model.List) }, propertyNames);
570+
}
571+
549572
public abstract partial class BaseViewModel : ObservableObject
550573
{
551574
public string? Content { get; set; }
@@ -889,4 +912,24 @@ public class Bar<T>
889912
}
890913
}
891914
#endif
915+
916+
interface IValueHolder
917+
{
918+
public bool Value { get; }
919+
}
920+
921+
partial class ModelWithMultipleGenericParameters<T, U> : ObservableObject, IValueHolder
922+
{
923+
[ObservableProperty]
924+
private bool value;
925+
926+
[ObservableProperty]
927+
private T tValue;
928+
929+
[ObservableProperty]
930+
private U uValue;
931+
932+
[ObservableProperty]
933+
private List<T> list;
934+
}
892935
}

0 commit comments

Comments
 (0)