Skip to content

Commit f40eb62

Browse files
committed
Add more tests for generated partial methods
1 parent d4a58e8 commit f40eb62

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,23 @@ public void Test_OnPropertyChangingAndChangedPartialMethods()
288288
Assert.AreEqual(99, model.NumberChangedValue);
289289
}
290290

291+
[TestMethod]
292+
public void Test_OnPropertyChangingAndChangedPartialMethodWithAdditionalValidation()
293+
{
294+
ViewModelWithImplementedUpdateMethodAndAdditionalValidation model = new();
295+
296+
// The actual validation is performed inside the model itself.
297+
// This test validates that the order with which methods/events are generated is:
298+
// - On<PROPERTY_NAME>Changing(value);
299+
// - OnProperyChanging();
300+
// - field = value;
301+
// - On<PROPERTY_NAME>Changed(value);
302+
// - OnProperyChanged();
303+
model.Name = "B";
304+
305+
Assert.AreEqual("B", model.Name);
306+
}
307+
291308
public partial class SampleModel : ObservableObject
292309
{
293310
/// <summary>
@@ -443,4 +460,54 @@ partial void OnNumberChanged(int value)
443460
NumberChangedValue = value;
444461
}
445462
}
463+
464+
public partial class ViewModelWithImplementedUpdateMethodAndAdditionalValidation : ObservableObject
465+
{
466+
private int step;
467+
468+
[ObservableProperty]
469+
public string? name = "A";
470+
471+
partial void OnNameChanging(string? value)
472+
{
473+
Assert.AreEqual(0, this.step);
474+
475+
this.step = 1;
476+
477+
Assert.AreEqual("A", this.name);
478+
Assert.AreEqual("B", value);
479+
}
480+
481+
partial void OnNameChanged(string? value)
482+
{
483+
Assert.AreEqual(2, this.step);
484+
485+
this.step = 3;
486+
487+
Assert.AreEqual("B", this.name);
488+
Assert.AreEqual("B", value);
489+
}
490+
491+
protected override void OnPropertyChanging(PropertyChangingEventArgs e)
492+
{
493+
base.OnPropertyChanging(e);
494+
495+
Assert.AreEqual(1, this.step);
496+
497+
this.step = 2;
498+
499+
Assert.AreEqual("A", this.name);
500+
Assert.AreEqual(nameof(Name), e.PropertyName);
501+
}
502+
503+
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
504+
{
505+
base.OnPropertyChanged(e);
506+
507+
Assert.AreEqual(3, this.step);
508+
509+
Assert.AreEqual("B", this.name);
510+
Assert.AreEqual(nameof(Name), e.PropertyName);
511+
}
512+
}
446513
}

0 commit comments

Comments
 (0)