Skip to content

Commit 126a15c

Browse files
committed
Added unit test for validation with an injected service
1 parent 145bc8f commit 126a15c

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

UnitTests/UnitTests.Shared/Mvvm/Test_ObservableValidator.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel;
88
using System.ComponentModel.DataAnnotations;
99
using System.Linq;
10+
using System.Text.RegularExpressions;
1011
using Microsoft.Toolkit.Mvvm.ComponentModel;
1112
using Microsoft.VisualStudio.TestTools.UnitTesting;
1213

@@ -355,6 +356,42 @@ public void Test_ObservableValidator_CustomValidation()
355356
Assert.AreEqual(events.Count, 0);
356357
}
357358

359+
[TestCategory("Mvvm")]
360+
[TestMethod]
361+
public void Test_ObservableValidator_CustomValidationWithInjectedService()
362+
{
363+
var model = new ValidationWithServiceModel(new FancyService());
364+
var events = new List<DataErrorsChangedEventArgs>();
365+
366+
model.ErrorsChanged += (s, e) => events.Add(e);
367+
368+
model.Name = "This is a valid name";
369+
370+
Assert.IsFalse(model.HasErrors);
371+
Assert.AreEqual(events.Count, 0);
372+
373+
model.Name = "This is invalid238!!";
374+
375+
Assert.IsTrue(model.HasErrors);
376+
Assert.AreEqual(events.Count, 1);
377+
Assert.AreEqual(events[0].PropertyName, nameof(ValidationWithServiceModel.Name));
378+
Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 1);
379+
380+
model.Name = "This is valid but it is too long so the validation will fail anyway";
381+
382+
Assert.IsTrue(model.HasErrors);
383+
Assert.AreEqual(events.Count, 2);
384+
Assert.AreEqual(events[1].PropertyName, nameof(ValidationWithServiceModel.Name));
385+
Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 1);
386+
387+
model.Name = "This is both too long and it also contains invalid characters, a real disaster!";
388+
389+
Assert.IsTrue(model.HasErrors);
390+
Assert.AreEqual(events.Count, 3);
391+
Assert.AreEqual(events[2].PropertyName, nameof(ValidationWithServiceModel.Name));
392+
Assert.AreEqual(model.GetErrors(nameof(ValidationWithServiceModel.Name)).ToArray().Length, 2);
393+
}
394+
358395
public class Person : ObservableValidator
359396
{
360397
private string name;
@@ -493,5 +530,54 @@ public static ValidationResult ValidateA(int x, ValidationContext context)
493530
return new ValidationResult("Missing the magic number");
494531
}
495532
}
533+
534+
public interface IFancyService
535+
{
536+
bool Validate(string name);
537+
}
538+
539+
public class FancyService : IFancyService
540+
{
541+
public bool Validate(string name)
542+
{
543+
return Regex.IsMatch(name, @"^[A-Za-z ]+$");
544+
}
545+
}
546+
547+
/// <summary>
548+
/// Test model for custom validation with an injected service.
549+
/// See https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/3750 for the original request for this feature.
550+
/// </summary>
551+
public class ValidationWithServiceModel : ObservableValidator
552+
{
553+
private readonly IFancyService service;
554+
555+
public ValidationWithServiceModel(IFancyService service)
556+
{
557+
this.service = service;
558+
}
559+
560+
private string name;
561+
562+
[MaxLength(25, ErrorMessage = "The name is too long")]
563+
[CustomValidation(typeof(ValidationWithServiceModel), nameof(ValidateName))]
564+
public string Name
565+
{
566+
get => this.name;
567+
set => SetProperty(ref this.name, value, true);
568+
}
569+
570+
public static ValidationResult ValidateName(string name, ValidationContext context)
571+
{
572+
bool isValid = ((ValidationWithServiceModel)context.ObjectInstance).service.Validate(name);
573+
574+
if (isValid)
575+
{
576+
return ValidationResult.Success;
577+
}
578+
579+
return new ValidationResult("The name contains invalid characters");
580+
}
581+
}
496582
}
497583
}

0 commit comments

Comments
 (0)