Skip to content

Commit

Permalink
Added unit test for validation with an injected service
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Feb 10, 2021
1 parent e12f4f9 commit fc39f6f
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions UnitTests/UnitTests.Shared/Mvvm/Test_ObservableValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -355,6 +356,42 @@ public void Test_ObservableValidator_CustomValidation()
Assert.AreEqual(events.Count, 0);
}

[TestCategory("Mvvm")]
[TestMethod]
public void Test_ObservableValidator_CustomValidationWithInjectedService()
{
var model = new ValidationWithServiceModel(new FancyService());
var events = new List<DataErrorsChangedEventArgs>();

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

model.Name = "This is a valid name";

Assert.IsFalse(model.HasErrors);
Assert.AreEqual(events.Count, 0);

model.Name = "This is invalid238!!";

Assert.IsTrue(model.HasErrors);
Assert.AreEqual(events.Count, 1);
Assert.AreEqual(events[0].PropertyName, nameof(ValidationWithServiceModel.Name));
Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 1);

model.Name = "This is valid but it is too long so the validation will fail anyway";

Assert.IsTrue(model.HasErrors);
Assert.AreEqual(events.Count, 2);
Assert.AreEqual(events[1].PropertyName, nameof(ValidationWithServiceModel.Name));
Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 1);

model.Name = "This is both too long and it also contains invalid characters, a real disaster!";

Assert.IsTrue(model.HasErrors);
Assert.AreEqual(events.Count, 3);
Assert.AreEqual(events[2].PropertyName, nameof(ValidationWithServiceModel.Name));
Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 2);
}

public class Person : ObservableValidator
{
private string name;
Expand Down Expand Up @@ -493,5 +530,54 @@ public static ValidationResult ValidateA(int x, ValidationContext context)
return new ValidationResult("Missing the magic number");
}
}

public interface IFancyService
{
bool Validate(string name);
}

public class FancyService : IFancyService
{
public bool Validate(string name)
{
return Regex.IsMatch(name, @"^[A-Za-z ]+$");
}
}

/// <summary>
/// Test model for custom validation with an injected service.
/// See https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/3750 for the original request for this feature.
/// </summary>
public class ValidationWithServiceModel : ObservableValidator
{
private readonly IFancyService service;

public ValidationWithServiceModel(IFancyService service)
{
this.service = service;
}

private string name;

[MaxLength(25, ErrorMessage = "The name is too long")]
[CustomValidation(typeof(ValidationWithServiceModel), nameof(ValidateName))]
public string Name
{
get => this.name;
set => SetProperty(ref this.name, value, true);
}

public static ValidationResult ValidateName(string name, ValidationContext context)
{
bool isValid = ((ValidationWithServiceModel)context.ObjectInstance).service.Validate(name);

if (isValid)
{
return ValidationResult.Success;
}

return new ValidationResult("The name contains invalid characters");
}
}
}
}

0 comments on commit fc39f6f

Please sign in to comment.