Skip to content

Commit 7f4c880

Browse files
committed
Add functional tests for new generated methods
1 parent c684646 commit 7f4c880

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,60 @@ public void Test_OnPropertyChangingAndChangedPartialMethods()
438438
Assert.AreEqual(99, model.NumberChangedValue);
439439
}
440440

441+
[TestMethod]
442+
public void Test_OnPropertyChangingAndChangedPartialMethods_WithPreviousValues()
443+
{
444+
ViewModelWithImplementedUpdateMethods2 model = new();
445+
446+
Assert.AreEqual(null, model.Name);
447+
Assert.AreEqual(0, model.Number);
448+
449+
CollectionAssert.AreEqual(Array.Empty<(string, string)>(), model.OnNameChangingValues);
450+
CollectionAssert.AreEqual(Array.Empty<(string, string)>(), model.OnNameChangedValues);
451+
CollectionAssert.AreEqual(Array.Empty<(int, int)>(), model.OnNumberChangingValues);
452+
CollectionAssert.AreEqual(Array.Empty<(int, int)>(), model.OnNumberChangedValues);
453+
454+
model.Name = "Bob";
455+
456+
CollectionAssert.AreEqual(new[] { ((string?)null, "Bob") }, model.OnNameChangingValues);
457+
CollectionAssert.AreEqual(new[] { ((string?)null, "Bob") }, model.OnNameChangedValues);
458+
459+
Assert.AreEqual("Bob", model.Name);
460+
461+
CollectionAssert.AreEqual(new[] { ((string?)null, "Bob") }, model.OnNameChangingValues);
462+
CollectionAssert.AreEqual(new[] { ((string?)null, "Bob") }, model.OnNameChangedValues);
463+
464+
model.Name = "Alice";
465+
466+
CollectionAssert.AreEqual(new[] { (null, "Bob"), ("Bob", "Alice") }, model.OnNameChangingValues);
467+
CollectionAssert.AreEqual(new[] { (null, "Bob"), ("Bob", "Alice") }, model.OnNameChangedValues);
468+
469+
Assert.AreEqual("Alice", model.Name);
470+
471+
CollectionAssert.AreEqual(new[] { (null, "Bob"), ("Bob", "Alice") }, model.OnNameChangingValues);
472+
CollectionAssert.AreEqual(new[] { (null, "Bob"), ("Bob", "Alice") }, model.OnNameChangedValues);
473+
474+
model.Number = 42;
475+
476+
CollectionAssert.AreEqual(new[] { (0, 42) }, model.OnNumberChangingValues);
477+
CollectionAssert.AreEqual(new[] { (0, 42) }, model.OnNumberChangedValues);
478+
479+
Assert.AreEqual(42, model.Number);
480+
481+
CollectionAssert.AreEqual(new[] { (0, 42) }, model.OnNumberChangingValues);
482+
CollectionAssert.AreEqual(new[] { (0, 42) }, model.OnNumberChangedValues);
483+
484+
model.Number = 77;
485+
486+
CollectionAssert.AreEqual(new[] { (0, 42), (42, 77) }, model.OnNumberChangingValues);
487+
CollectionAssert.AreEqual(new[] { (0, 42), (42, 77) }, model.OnNumberChangedValues);
488+
489+
Assert.AreEqual(77, model.Number);
490+
491+
CollectionAssert.AreEqual(new[] { (0, 42), (42, 77) }, model.OnNumberChangingValues);
492+
CollectionAssert.AreEqual(new[] { (0, 42), (42, 77) }, model.OnNumberChangedValues);
493+
}
494+
441495
[TestMethod]
442496
public void Test_OnPropertyChangingAndChangedPartialMethodWithAdditionalValidation()
443497
{
@@ -1253,6 +1307,43 @@ partial void OnNumberChanged(int value)
12531307
}
12541308
}
12551309

1310+
public partial class ViewModelWithImplementedUpdateMethods2 : ObservableObject
1311+
{
1312+
[ObservableProperty]
1313+
public string? name;
1314+
1315+
[ObservableProperty]
1316+
public int number;
1317+
1318+
public List<(string? Old, string? New)> OnNameChangingValues { get; } = new();
1319+
1320+
public List<(string? Old, string? New)> OnNameChangedValues { get; } = new();
1321+
1322+
public List<(int Old, int New)> OnNumberChangingValues { get; } = new();
1323+
1324+
public List<(int Old, int New)> OnNumberChangedValues { get; } = new();
1325+
1326+
partial void OnNameChanging(string? oldValue, string? newValue)
1327+
{
1328+
OnNameChangingValues.Add((oldValue, newValue));
1329+
}
1330+
1331+
partial void OnNameChanged(string? oldValue, string? newValue)
1332+
{
1333+
OnNameChangedValues.Add((oldValue, newValue));
1334+
}
1335+
1336+
partial void OnNumberChanging(int oldValue, int newValue)
1337+
{
1338+
OnNumberChangingValues.Add((oldValue, newValue));
1339+
}
1340+
1341+
partial void OnNumberChanged(int oldValue, int newValue)
1342+
{
1343+
OnNumberChangedValues.Add((oldValue, newValue));
1344+
}
1345+
}
1346+
12561347
public partial class ViewModelWithImplementedUpdateMethodAndAdditionalValidation : ObservableObject
12571348
{
12581349
private int step;

0 commit comments

Comments
 (0)