Skip to content

Commit

Permalink
Add unit test for [AlsoValidateProperty]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed May 31, 2022
1 parent 3191845 commit 0b15992
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,46 @@ public void Test_ObservablePropertyWithValueNamedField_WithValidationAttributes(

model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);

bool errorsChanged = false;

model.ErrorsChanged += (s, e) => errorsChanged = true;

model.Value = "Hello world";

Assert.AreEqual(model.Value, "Hello world");

// The [AlsoValidateProperty] attribute wasn't used, so the property shouldn't be validated
Assert.IsFalse(errorsChanged);

CollectionAssert.AreEqual(new[] { nameof(model.Value) }, propertyNames);
}

[TestMethod]
public void Test_ObservablePropertyWithValueNamedField_WithValidationAttributesAndValidation()
{
ModelWithValuePropertyWithAutomaticValidation model = new();

List<string?> propertyNames = new();

model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);

List<DataErrorsChangedEventArgs> errors = new();

model.ErrorsChanged += (s, e) => errors.Add(e);

model.Value = "Bo";

Assert.IsTrue(model.HasErrors);
Assert.AreEqual(errors.Count, 1);
Assert.AreEqual(errors[0].PropertyName, nameof(ModelWithValuePropertyWithAutomaticValidation.Value));

model.Value = "Hello world";

Assert.IsFalse(model.HasErrors);
Assert.AreEqual(errors.Count, 2);
Assert.AreEqual(errors[1].PropertyName, nameof(ModelWithValuePropertyWithAutomaticValidation.Value));
}

// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4184
[TestMethod]
public void Test_GeneratedPropertiesWithValidationAttributesOverFields()
Expand Down Expand Up @@ -893,6 +926,15 @@ public partial class ModelWithValuePropertyWithValidation : ObservableValidator
private string? value;
}

public partial class ModelWithValuePropertyWithAutomaticValidation : ObservableValidator
{
[ObservableProperty]
[Required]
[MinLength(5)]
[AlsoValidateProperty]
private string? value;
}

public partial class ViewModelWithValidatableGeneratedProperties : ObservableValidator
{
[Required]
Expand Down

0 comments on commit 0b15992

Please sign in to comment.